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  package org.apache.http.nio.client.methods;
28  
29  import java.io.File;
30  import java.io.FileNotFoundException;
31  import java.io.UnsupportedEncodingException;
32  import java.net.URI;
33  
34  import org.apache.http.HttpEntityEnclosingRequest;
35  import org.apache.http.HttpHost;
36  import org.apache.http.HttpRequest;
37  import org.apache.http.HttpResponse;
38  import org.apache.http.client.methods.HttpDelete;
39  import org.apache.http.client.methods.HttpGet;
40  import org.apache.http.client.methods.HttpOptions;
41  import org.apache.http.client.methods.HttpPost;
42  import org.apache.http.client.methods.HttpPut;
43  import org.apache.http.client.methods.HttpTrace;
44  import org.apache.http.client.methods.HttpUriRequest;
45  import org.apache.http.client.utils.URIUtils;
46  import org.apache.http.entity.ContentType;
47  import org.apache.http.nio.entity.HttpAsyncContentProducer;
48  import org.apache.http.nio.entity.NByteArrayEntity;
49  import org.apache.http.nio.entity.NStringEntity;
50  import org.apache.http.nio.protocol.BasicAsyncRequestProducer;
51  import org.apache.http.nio.protocol.BasicAsyncResponseConsumer;
52  import org.apache.http.nio.protocol.HttpAsyncRequestProducer;
53  import org.apache.http.nio.protocol.HttpAsyncResponseConsumer;
54  import org.apache.http.util.Args;
55  
56  /**
57   * Factory methods for asynchronous request producers and response consumers.
58   *
59   * @since 4.0
60   */
61  public final class HttpAsyncMethods {
62  
63      /**
64       * Creates asynchronous request generator for the given request message.
65       *
66       * @param target request target.
67       * @param request request message.
68       * @return asynchronous request generator
69       */
70      public static HttpAsyncRequestProducer create(final HttpHost target, final HttpRequest request) {
71          Args.notNull(target, "HTTP host");
72          Args.notNull(request, "HTTP request");
73          return new RequestProducerImpl(target, request);
74      }
75  
76      /**
77       * Creates asynchronous request generator for the given request message.
78       *
79       * @param request request message.
80       * @return asynchronous request generator
81       */
82      public static HttpAsyncRequestProducer create(final HttpUriRequest request) {
83          Args.notNull(request, "HTTP request");
84          final HttpHost target = URIUtils.extractHost(request.getURI());
85          return new RequestProducerImpl(target, request);
86      }
87  
88      /**
89       * Creates asynchronous {@code GET} request generator.
90       *
91       * @param requestURI request URI.
92       * @return asynchronous request generator
93       */
94      public static HttpAsyncRequestProducer createGet(final URI requestURI) {
95          return create(new HttpGet(requestURI));
96      }
97  
98      /**
99       * Creates asynchronous {@code GET} request generator.
100      *
101      * @param requestURI request URI.
102      * @return asynchronous request generator
103      */
104     public static HttpAsyncRequestProducer createGet(final String requestURI) {
105         return create(new HttpGet(URI.create(requestURI)));
106     }
107 
108     /**
109      * Creates asynchronous {@code HEAD} request generator.
110      *
111      * @param requestURI request URI.
112      * @return asynchronous request generator
113      */
114     public static HttpAsyncRequestProducer createHead(final URI requestURI) {
115         return create(new HttpGet(requestURI));
116     }
117 
118     /**
119      * Creates asynchronous {@code HEAD} request generator.
120      *
121      * @param requestURI request URI.
122      * @return asynchronous request generator
123      */
124     public static HttpAsyncRequestProducer createHead(final String requestURI) {
125         return create(new HttpGet(URI.create(requestURI)));
126     }
127 
128     /**
129      * Creates asynchronous {@code DELETE} request generator.
130      *
131      * @param requestURI request URI.
132      * @return asynchronous request generator
133      */
134     public static HttpAsyncRequestProducer createDelete(final URI requestURI) {
135         return create(new HttpDelete(requestURI));
136     }
137 
138     /**
139      * Creates asynchronous {@code DELETE} request generator.
140      *
141      * @param requestURI request URI.
142      * @return asynchronous request generator
143      */
144     public static HttpAsyncRequestProducer createDelete(final String requestURI) {
145         return create(new HttpDelete(URI.create(requestURI)));
146     }
147 
148     /**
149      * Creates asynchronous {@code OPTIONS} request generator.
150      *
151      * @param requestURI request URI.
152      * @return asynchronous request generator
153      */
154     public static HttpAsyncRequestProducer createOptions(final URI requestURI) {
155         return create(new HttpOptions(requestURI));
156     }
157 
158     /**
159      * Creates asynchronous {@code OPTIONS} request generator.
160      *
161      * @param requestURI request URI.
162      * @return asynchronous request generator
163      */
164     public static HttpAsyncRequestProducer createOptions(final String requestURI) {
165         return create(new HttpOptions(URI.create(requestURI)));
166     }
167 
168     /**
169      * Creates asynchronous {@code TRACE} request generator.
170      *
171      * @param requestURI request URI.
172      * @return asynchronous request generator
173      */
174     public static HttpAsyncRequestProducer createTrace(final URI requestURI) {
175         return create(new HttpTrace(requestURI));
176     }
177 
178     /**
179      * Creates asynchronous {@code TRACE} request generator.
180      *
181      * @param requestURI request URI.
182      * @return asynchronous request generator
183      */
184     public static HttpAsyncRequestProducer createTrace(final String requestURI) {
185         return create(new HttpTrace(URI.create(requestURI)));
186     }
187 
188     /**
189      * Creates asynchronous {@code POST} request generator.
190      *
191      * @param requestURI request URI.
192      * @param content request content.
193      * @param contentType request contentType.
194      * @return asynchronous request generator
195      */
196     public static HttpAsyncRequestProducer createPost(
197             final URI requestURI,
198             final String content,
199             final ContentType contentType) throws UnsupportedEncodingException {
200         final HttpPost httppost = new HttpPost(requestURI);
201         final NStringEntity entity = new NStringEntity(content, contentType);
202         httppost.setEntity(entity);
203         final HttpHost target = URIUtils.extractHost(requestURI);
204         return new RequestProducerImpl(target, httppost, entity);
205     }
206 
207     /**
208      * Creates asynchronous {@code POST} request generator.
209      *
210      * @param requestURI request URI.
211      * @param content request content.
212      * @param contentType request contentType.
213      * @return asynchronous request generator
214      */
215     public static HttpAsyncRequestProducer createPost(
216             final String requestURI,
217             final String content,
218             final ContentType contentType) throws UnsupportedEncodingException {
219         return createPost(URI.create(requestURI), content, contentType);
220     }
221 
222     /**
223      * Creates asynchronous {@code POST} request generator.
224      *
225      * @param requestURI request URI.
226      * @param content request content.
227      * @param contentType request contentType.
228      * @return asynchronous request generator
229      */
230     public static HttpAsyncRequestProducer createPost(
231             final URI requestURI,
232             final byte[] content,
233             final ContentType contentType) {
234         final HttpPost httppost = new HttpPost(requestURI);
235         final NByteArrayEntity entity = new NByteArrayEntity(content, contentType);
236         httppost.setEntity(entity);
237         final HttpHost target = URIUtils.extractHost(requestURI);
238         return new RequestProducerImpl(target, httppost, entity);
239     }
240 
241     /**
242      * Creates asynchronous {@code POST} request generator.
243      *
244      * @param requestURI request URI.
245      * @param content request content.
246      * @param contentType request contentType.
247      * @return asynchronous request generator
248      */
249     public static HttpAsyncRequestProducer createPost(
250             final String requestURI,
251             final byte[] content,
252             final ContentType contentType) {
253         return createPost(URI.create(requestURI), content, contentType);
254     }
255 
256     /**
257      * Creates asynchronous {@code PUT} request generator.
258      *
259      * @param requestURI request URI.
260      * @param content request content.
261      * @param contentType request contentType.
262      * @return asynchronous request generator
263      */
264     public static HttpAsyncRequestProducer createPut(
265             final URI requestURI,
266             final String content,
267             final ContentType contentType) throws UnsupportedEncodingException {
268         final HttpPut httpput = new HttpPut(requestURI);
269         final NStringEntity entity = new NStringEntity(content, contentType);
270         httpput.setEntity(entity);
271         final HttpHost target = URIUtils.extractHost(requestURI);
272         return new RequestProducerImpl(target, httpput, entity);
273     }
274 
275     /**
276      * Creates asynchronous {@code PUT} request generator.
277      *
278      * @param requestURI request URI.
279      * @param content request content.
280      * @param contentType request contentType.
281      * @return asynchronous request generator
282      */
283     public static HttpAsyncRequestProducer createPut(
284             final String requestURI,
285             final String content,
286             final ContentType contentType) throws UnsupportedEncodingException {
287         return createPut(URI.create(requestURI), content, contentType);
288     }
289 
290     /**
291      * Creates asynchronous {@code PUT} request generator.
292      *
293      * @param requestURI request URI.
294      * @param content request content.
295      * @param contentType request contentType.
296      * @return asynchronous request generator
297      */
298     public static HttpAsyncRequestProducer createPut(
299             final URI requestURI,
300             final byte[] content,
301             final ContentType contentType) {
302         final HttpPut httpput = new HttpPut(requestURI);
303         final NByteArrayEntity entity = new NByteArrayEntity(content, contentType);
304         httpput.setEntity(entity);
305         final HttpHost target = URIUtils.extractHost(requestURI);
306         return new RequestProducerImpl(target, httpput, entity);
307     }
308 
309     /**
310      * Creates asynchronous {@code PUT} request generator.
311      *
312      * @param requestURI request URI.
313      * @param content request content.
314      * @param contentType request contentType.
315      * @return asynchronous request generator
316      */
317     public static HttpAsyncRequestProducer createPut(
318             final String requestURI,
319             final byte[] content,
320             final ContentType contentType) {
321         return createPut(URI.create(requestURI), content, contentType);
322     }
323 
324     /**
325      * Creates asynchronous zero-copy {@code POST} request generator.
326      *
327      * @param requestURI request URI.
328      * @param content request content.
329      * @param contentType request contentType.
330      * @return asynchronous request generator
331      */
332     public static HttpAsyncRequestProducer createZeroCopyPost(
333             final URI requestURI,
334             final File content,
335             final ContentType contentType) throws FileNotFoundException {
336         return new ZeroCopyPost(requestURI, content, contentType);
337     }
338 
339     /**
340      * Creates asynchronous zero-copy {@code POST} request generator.
341      *
342      * @param requestURI request URI.
343      * @param content request content.
344      * @param contentType request contentType.
345      * @return asynchronous request generator
346      */
347     public static HttpAsyncRequestProducer createZeroCopyPost(
348             final String requestURI,
349             final File content,
350             final ContentType contentType) throws FileNotFoundException {
351         return new ZeroCopyPost(URI.create(requestURI), content, contentType);
352     }
353 
354     /**
355      * Creates asynchronous zero-copy {@code PUT} request generator.
356      *
357      * @param requestURI request URI.
358      * @param content request content.
359      * @param contentType request contentType.
360      * @return asynchronous request generator
361      */
362     public static HttpAsyncRequestProducer createZeroCopyPut(
363             final URI requestURI,
364             final File content,
365             final ContentType contentType) throws FileNotFoundException {
366         return new ZeroCopyPut(requestURI, content, contentType);
367     }
368 
369     /**
370      * Creates asynchronous zero-copy {@code PUT} request generator.
371      *
372      * @param requestURI request URI.
373      * @param content request content.
374      * @param contentType request contentType.
375      * @return asynchronous request generator
376      */
377     public static HttpAsyncRequestProducer createZeroCopyPut(
378             final String requestURI,
379             final File content,
380             final ContentType contentType) throws FileNotFoundException {
381         return new ZeroCopyPut(URI.create(requestURI), content, contentType);
382     }
383 
384     /**
385      * Creates basic response consumer that will buffer response content in memory.
386      * @return asynchronous response consumer.
387      */
388     public static HttpAsyncResponseConsumer<HttpResponse> createConsumer() {
389         return new BasicAsyncResponseConsumer();
390     }
391 
392     /**
393      * Creates zero-copy response consumer that will stream response content
394      * directly to the given file.
395      * @return asynchronous response consumer.
396      */
397     public static HttpAsyncResponseConsumer<HttpResponse> createZeroCopyConsumer(
398             final File file) throws FileNotFoundException {
399         return new ZeroCopyConsumer<HttpResponse>(file) {
400 
401             @Override
402             protected HttpResponse process(
403                     final HttpResponse response,
404                     final File file,
405                     final ContentType contentType) {
406                 return response;
407             }
408 
409         };
410     }
411 
412     static class RequestProducerImpl extends BasicAsyncRequestProducer {
413 
414         protected RequestProducerImpl(
415                 final HttpHost target,
416                 final HttpEntityEnclosingRequest request,
417                 final HttpAsyncContentProducer producer) {
418             super(target, request, producer);
419         }
420 
421         public RequestProducerImpl(final HttpHost target, final HttpRequest request) {
422             super(target, request);
423         }
424 
425     }
426 
427 }