001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.http;
014
015import java.util.*;
016
017/**
018 * Represents valid HTTP 1.1 method names per the RFC 2616 spec.
019 *
020 * <ul class='seealso'>
021 *    <li class='extlink'>{@doc RFC2616}
022 * </ul>
023 */
024public enum HttpMethod {
025
026   /** {@doc RFC2616.section9#sec9.2 OPTIONS} */
027   OPTIONS,
028
029   /** {@doc RFC2616.section9#sec9.3 GET} */
030   GET,
031
032   /** {@doc RFC2616.section9#sec9.4 HEAD} */
033   HEAD,
034
035   /** {@doc RFC2616.section9#sec9.5 POST} */
036   POST,
037
038   /** {@doc RFC2616.section9#sec9.6 PUT} */
039   PUT,
040
041   /** {@doc RFC2616.section9#sec9.7 DELETE} */
042   DELETE,
043
044   /** {@doc RFC2616.section9#sec9.8 TRACE} */
045   TRACE,
046
047   /** {@doc RFC2616.section9#sec9.9 CONNECT} */
048   CONNECT,
049
050   /** A non-standard value. */
051   OTHER;
052
053   private static final Map<String,HttpMethod> cache = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
054   static {
055      cache.put("OPTIONS", OPTIONS);
056      cache.put("GET", GET);
057      cache.put("HEAD", HEAD);
058      cache.put("POST", POST);
059      cache.put("PUT", PUT);
060      cache.put("DELETE", DELETE);
061      cache.put("TRACE", TRACE);
062      cache.put("CONNECT", CONNECT);
063   }
064
065   /**
066    * Returns the enum for the specified key.
067    *
068    * <p>
069    * Case is ignored.
070    *
071    * @param key The HTTP method name.
072    * @return The HttpMethod enum, or {@link #OTHER} if it's not a standard method name.
073    */
074   public static HttpMethod forString(String key) {
075      HttpMethod m = cache.get(key);
076      return m == null ? OTHER : m;
077   }
078}