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