View Javadoc

    1   /*
    2    * Copyright 2012 The Netty Project
    3    *
    4    * The Netty Project licenses this file to you under the Apache License,
    5    * version 2.0 (the "License"); you may not use this file except in compliance
    6    * with the License. You may obtain a copy of the License at:
    7    *
    8    *   http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    13   * License for the specific language governing permissions and limitations
    14   * under the License.
    15   */
    16  package org.jboss.netty.handler.codec.http;
    17
    18  import java.util.HashMap;
    19  import java.util.Map;
    20
    21  /**
    22   * The request method of HTTP or its derived protocols, such as
    23   * <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
    24   * <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
    25   * @apiviz.exclude
    26   */
    27  public class HttpMethod implements Comparable<HttpMethod> {
    28      /**
    29       * The OPTIONS method represents a request for information about the communication options
    30       * available on the request/response chain identified by the Request-URI. This method allows
    31       * the client to determine the options and/or requirements associated with a resource, or the
    32       * capabilities of a server, without implying a resource action or initiating a resource
    33       * retrieval.
    34       */
    35      public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS");
    36
    37      /**
    38       * The GET method means retrieve whatever information (in the form of an entity) is identified
    39       * by the Request-URI.  If the Request-URI refers to a data-producing process, it is the
    40       * produced data which shall be returned as the entity in the response and not the source text
    41       * of the process, unless that text happens to be the output of the process.
    42       */
    43      public static final HttpMethod GET = new HttpMethod("GET");
    44
    45      /**
    46       * The HEAD method is identical to GET except that the server MUST NOT return a message-body in
    47       * the response.
    48       */
    49      public static final HttpMethod HEAD = new HttpMethod("HEAD");
    50
    51      /**
    52       * The POST method is used to request that the origin server accept the entity enclosed in the
    53       * request as a new subordinate of the resource identified by the Request-URI in the
    54       * Request-Line.
    55       */
    56      public static final HttpMethod POST = new HttpMethod("POST");
    57
    58      /**
    59       * The PUT method requests that the enclosed entity be stored under the supplied Request-URI.
    60       */
    61      public static final HttpMethod PUT = new HttpMethod("PUT");
    62
    63      /**
    64       * The PATCH method requests that a set of changes described in the
    65       * request entity be applied to the resource identified by the Request-URI.
    66       */
    67      public static final HttpMethod PATCH = new HttpMethod("PATCH");
    68
    69      /**
    70       * The DELETE method requests that the origin server delete the resource identified by the
    71       * Request-URI.
    72       */
    73      public static final HttpMethod DELETE = new HttpMethod("DELETE");
    74
    75      /**
    76       * The TRACE method is used to invoke a remote, application-layer loop- back of the request
    77       * message.
    78       */
    79      public static final HttpMethod TRACE = new HttpMethod("TRACE");
    80
    81      /**
    82       * This specification reserves the method name CONNECT for use with a proxy that can
    83       * dynamically switch to being a tunnel
    84       */
    85      public static final HttpMethod CONNECT = new HttpMethod("CONNECT");
    86
    87      private static final Map<String, HttpMethod> methodMap =
    88              new HashMap<String, HttpMethod>();
    89
    90      static {
    91          methodMap.put(OPTIONS.toString(), OPTIONS);
    92          methodMap.put(GET.toString(), GET);
    93          methodMap.put(HEAD.toString(), HEAD);
    94          methodMap.put(POST.toString(), POST);
    95          methodMap.put(PUT.toString(), PUT);
    96          methodMap.put(PATCH.toString(), PATCH);
    97          methodMap.put(DELETE.toString(), DELETE);
    98          methodMap.put(TRACE.toString(), TRACE);
    99          methodMap.put(CONNECT.toString(), CONNECT);
    100     }
    101
    102     /**
    103      * Returns the {@link HttpMethod} represented by the specified name.
    104      * If the specified name is a standard HTTP method name, a cached instance
    105      * will be returned.  Otherwise, a new instance will be returned.
    106      */
    107     public static HttpMethod valueOf(String name) {
    108         if (name == null) {
    109             throw new NullPointerException("name");
    110         }
    111
    112         name = name.trim().toUpperCase();
    113         if (name.length() == 0) {
    114             throw new IllegalArgumentException("empty name");
    115         }
    116
    117         HttpMethod result = methodMap.get(name);
    118         if (result != null) {
    119             return result;
    120         } else {
    121             return new HttpMethod(name);
    122         }
    123     }
    124
    125     private final String name;
    126
    127     /**
    128      * Creates a new HTTP method with the specified name.  You will not need to
    129      * create a new method unless you are implementing a protocol derived from
    130      * HTTP, such as
    131      * <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
    132      * <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>
    133      */
    134     public HttpMethod(String name) {
    135         if (name == null) {
    136             throw new NullPointerException("name");
    137         }
    138
    139         name = name.trim().toUpperCase();
    140         if (name.length() == 0) {
    141             throw new IllegalArgumentException("empty name");
    142         }
    143
    144         for (int i = 0; i < name.length(); i ++) {
    145             if (Character.isISOControl(name.charAt(i)) ||
    146                 Character.isWhitespace(name.charAt(i))) {
    147                 throw new IllegalArgumentException("invalid character in name");
    148             }
    149         }
    150
    151         this.name = name;
    152     }
    153
    154     /**
    155      * Returns the name of this method.
    156      */
    157     public String getName() {
    158         return name;
    159     }
    160
    161     @Override
    162     public int hashCode() {
    163         return getName().hashCode();
    164     }
    165
    166     @Override
    167     public boolean equals(Object o) {
    168         if (!(o instanceof HttpMethod)) {
    169             return false;
    170         }
    171
    172         HttpMethod that = (HttpMethod) o;
    173         return getName().equals(that.getName());
    174     }
    175
    176     @Override
    177     public String toString() {
    178         return getName();
    179     }
    180
    181     public int compareTo(HttpMethod o) {
    182         return getName().compareTo(o.getName());
    183     }
    184 }