View Javadoc
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.client5.http.classic.methods;
29  
30  import java.net.URI;
31  import java.util.Locale;
32  
33  import org.apache.hc.core5.http.Method;
34  import org.apache.hc.core5.util.Args;
35  
36  
37  /**
38   * Common HTTP methods using {@link HttpUriRequest} as a HTTP request message representation.
39   * <p>
40   * Each static method creates a request object of the exact subclass of {@link HttpUriRequest}
41   * with a non-null URI.
42   *
43   * @since 5.0
44   */
45  public final class ClassicHttpRequests {
46  
47      private static Method normalizedValueOf(final String method) {
48          // TODO Next version of HttpCore:
49          // Method.normalizedValueOf(method)
50          return Method.valueOf(Args.notNull(method, "method").toUpperCase(Locale.ROOT));
51      }
52  
53      /**
54       * Creates a new HttpUriRequest for the given {@code Method} and {@code String} URI.
55       *
56       * @param method A method.
57       * @param uri a URI.
58       * @return a new HttpUriRequest.
59       */
60      public static HttpUriRequest create(final Method method, final String uri) {
61          return create(method, URI.create(uri));
62      }
63  
64      /**
65       * Creates a new HttpUriRequest for the given {@code Method} and {@code URI}.
66       *
67       * @param method A method.
68       * @param uri a URI.
69       * @return a new HttpUriRequest.
70       */
71      public static HttpUriRequest create(final Method method, final URI uri) {
72          switch (Args.notNull(method, "method")) {
73          case DELETE:
74              return delete(uri);
75          case GET:
76              return get(uri);
77          case HEAD:
78              return head(uri);
79          case OPTIONS:
80              return options(uri);
81          case PATCH:
82              return patch(uri);
83          case POST:
84              return post(uri);
85          case PUT:
86              return put(uri);
87          case TRACE:
88              return trace(uri);
89          default:
90              throw new IllegalArgumentException(method.toString());
91          }
92      }
93  
94      /**
95       * Creates a new HttpUriRequest for the given {@code method} and {@code String} URI.
96       *
97       * @param method A method supported by this class.
98       * @param uri a non-null request string URI.
99       * @throws IllegalArgumentException if the method is not supported.
100      * @throws IllegalArgumentException if the string uri is null.
101      * @return A new HttpUriRequest.
102      */
103     public static HttpUriRequest create(final String method, final String uri) {
104         return create(normalizedValueOf(method), uri);
105     }
106 
107     /**
108      * Creates a new HttpUriRequest for the given {@code method} and {@code URI}.
109      *
110      * @param method A method supported by this class.
111      * @param uri a non-null request URI.
112      * @throws IllegalArgumentException if the method is not supported.
113      * @throws IllegalArgumentException if the uri is null.
114      * @return A new HttpUriRequest.
115      */
116     public static HttpUriRequest create(final String method, final URI uri) {
117         return create(normalizedValueOf(method), uri);
118     }
119 
120     public static HttpUriRequest delete(final String uri) {
121         return delete(URI.create(uri));
122     }
123 
124     public static HttpUriRequest delete(final URI uri) {
125         return new HttpDelete(uri);
126     }
127 
128     public static HttpUriRequest get(final String uri) {
129         return get(URI.create(uri));
130     }
131 
132     public static HttpUriRequest get(final URI uri) {
133         return new HttpGet(uri);
134     }
135 
136     public static HttpUriRequest head(final String uri) {
137         return head(URI.create(uri));
138     }
139 
140     public static HttpUriRequest head(final URI uri) {
141         return new HttpHead(uri);
142     }
143 
144     public static HttpUriRequest options(final String uri) {
145         return options(URI.create(uri));
146     }
147 
148     public static HttpUriRequest options(final URI uri) {
149         return new HttpOptions(uri);
150     }
151 
152     public static HttpUriRequest patch(final String uri) {
153         return patch(URI.create(uri));
154     }
155 
156     public static HttpUriRequest patch(final URI uri) {
157         return new HttpPatch(uri);
158     }
159 
160     public static HttpUriRequest post(final String uri) {
161         return post(URI.create(uri));
162     }
163 
164     public static HttpUriRequest post(final URI uri) {
165         return new HttpPost(uri);
166     }
167 
168     public static HttpUriRequest put(final String uri) {
169         return put(URI.create(uri));
170     }
171 
172     public static HttpUriRequest put(final URI uri) {
173         return new HttpPut(uri);
174     }
175 
176     public static HttpUriRequest trace(final String uri) {
177         return trace(URI.create(uri));
178     }
179 
180     public static HttpUriRequest trace(final URI uri) {
181         return new HttpTrace(uri);
182     }
183 
184 }