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