View Javadoc

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