1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
59
60
61
62 public final class HttpAsyncMethods {
63
64
65
66
67
68
69
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
79
80
81
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
91
92
93
94
95 public static HttpAsyncRequestProducer createGet(final URI requestURI) {
96 return create(new HttpGet(requestURI));
97 }
98
99
100
101
102
103
104
105 public static HttpAsyncRequestProducer createGet(final String requestURI) {
106 return create(new HttpGet(URI.create(requestURI)));
107 }
108
109
110
111
112
113
114
115 public static HttpAsyncRequestProducer createHead(final URI requestURI) {
116 return create(new HttpHead(requestURI));
117 }
118
119
120
121
122
123
124
125 public static HttpAsyncRequestProducer createHead(final String requestURI) {
126 return create(new HttpHead(URI.create(requestURI)));
127 }
128
129
130
131
132
133
134
135 public static HttpAsyncRequestProducer createDelete(final URI requestURI) {
136 return create(new HttpDelete(requestURI));
137 }
138
139
140
141
142
143
144
145 public static HttpAsyncRequestProducer createDelete(final String requestURI) {
146 return create(new HttpDelete(URI.create(requestURI)));
147 }
148
149
150
151
152
153
154
155 public static HttpAsyncRequestProducer createOptions(final URI requestURI) {
156 return create(new HttpOptions(requestURI));
157 }
158
159
160
161
162
163
164
165 public static HttpAsyncRequestProducer createOptions(final String requestURI) {
166 return create(new HttpOptions(URI.create(requestURI)));
167 }
168
169
170
171
172
173
174
175 public static HttpAsyncRequestProducer createTrace(final URI requestURI) {
176 return create(new HttpTrace(requestURI));
177 }
178
179
180
181
182
183
184
185 public static HttpAsyncRequestProducer createTrace(final String requestURI) {
186 return create(new HttpTrace(URI.create(requestURI)));
187 }
188
189
190
191
192
193
194
195
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
210
211
212
213
214
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
225
226
227
228
229
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
244
245
246
247
248
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
259
260
261
262
263
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
278
279
280
281
282
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
293
294
295
296
297
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
312
313
314
315
316
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
327
328
329
330
331
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
342
343
344
345
346
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
357
358
359
360
361
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
372
373
374
375
376
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
387
388
389 public static HttpAsyncResponseConsumer<HttpResponse> createConsumer() {
390 return new BasicAsyncResponseConsumer();
391 }
392
393
394
395
396
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 }