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