1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package org.apache.hc.core5.http;
29
30 import java.util.Locale;
31
32 import org.apache.hc.core5.util.Args;
33
34 /**
35 * Common HTTP methods defined by the HTTP spec.
36 *
37 * @since 5.0
38 */
39 public enum Method {
40
41 GET(true, true),
42 HEAD(true, true),
43 POST(false, false),
44 PUT(false, true),
45 DELETE(false, true),
46 CONNECT(false, false),
47 TRACE(true, true),
48 OPTIONS(true, true),
49 PATCH(false, false);
50
51 private final boolean safe;
52 private final boolean idempotent;
53
54 Method(final boolean safe, final boolean idempotent) {
55 this.safe = safe;
56 this.idempotent = idempotent;
57 }
58
59 public boolean isSafe() {
60 return safe;
61 }
62
63 public boolean isIdempotent() {
64 return idempotent;
65 }
66
67 public static boolean isSafe(final String value) {
68 if (value == null) {
69 return false;
70 }
71 try {
72 return normalizedValueOf(value).safe;
73 } catch (final IllegalArgumentException ex) {
74 return false;
75 }
76 }
77
78 public static boolean isIdempotent(final String value) {
79 if (value == null) {
80 return false;
81 }
82 try {
83 return normalizedValueOf(value).idempotent;
84 } catch (final IllegalArgumentException ex) {
85 return false;
86 }
87 }
88
89 /**
90 * Returns the Method for a normalized {@code value} of a method name.
91 *
92 * @param method A method name like {@code "delete"}, {@code "DELETE"}, or any mixed-case variant.
93 * @return the Method for the given method name.
94 */
95 public static Method normalizedValueOf(final String method) {
96 return valueOf(Args.notNull(method, "method").toUpperCase(Locale.ROOT));
97 }
98
99 public boolean isSame(final String value) {
100 if (value == null) {
101 return false;
102 }
103 return name().equalsIgnoreCase(value);
104 }
105
106 }