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.hc.core5.http.nio.support;
28
29 import java.io.IOException;
30 import java.net.URI;
31
32 import org.apache.hc.core5.http.HttpException;
33 import org.apache.hc.core5.http.HttpHost;
34 import org.apache.hc.core5.http.HttpRequest;
35 import org.apache.hc.core5.http.Method;
36 import org.apache.hc.core5.http.message.BasicHttpRequest;
37 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
38 import org.apache.hc.core5.http.nio.AsyncRequestProducer;
39 import org.apache.hc.core5.http.nio.DataStreamChannel;
40 import org.apache.hc.core5.http.nio.RequestChannel;
41 import org.apache.hc.core5.http.protocol.HttpContext;
42
43
44
45
46
47
48
49 public class BasicRequestProducer implements AsyncRequestProducer {
50
51 private final HttpRequest request;
52 private final AsyncEntityProducer dataProducer;
53
54 public BasicRequestProducer(final HttpRequest request, final AsyncEntityProducer dataProducer) {
55 this.request = request;
56 this.dataProducer = dataProducer;
57 }
58
59 public BasicRequestProducer(final String method, final HttpHost host, final String path, final AsyncEntityProducer dataProducer) {
60 this(new BasicHttpRequest(method, host, path), dataProducer);
61 }
62
63 public BasicRequestProducer(final String method, final HttpHost host, final String path) {
64 this(method, host, path, null);
65 }
66
67 public BasicRequestProducer(final String method, final URI requestUri, final AsyncEntityProducer dataProducer) {
68 this(new BasicHttpRequest(method, requestUri), dataProducer);
69 }
70
71 public BasicRequestProducer(final String method, final URI requestUri) {
72 this(method, requestUri, null);
73 }
74
75 public BasicRequestProducer(final Method method, final HttpHost host, final String path, final AsyncEntityProducer dataProducer) {
76 this(new BasicHttpRequest(method, host, path), dataProducer);
77 }
78
79 public BasicRequestProducer(final Method method, final HttpHost host, final String path) {
80 this(method, host, path, null);
81 }
82
83 public BasicRequestProducer(final Method method, final URI requestUri, final AsyncEntityProducer dataProducer) {
84 this(new BasicHttpRequest(method, requestUri), dataProducer);
85 }
86
87 public BasicRequestProducer(final Method method, final URI requestUri) {
88 this(method, requestUri, null);
89 }
90
91 @Override
92 public void sendRequest(final RequestChannel requestChannel, final HttpContext httpContext) throws HttpException, IOException {
93 requestChannel.sendRequest(request, dataProducer, httpContext);
94 }
95
96 @Override
97 public int available() {
98 return dataProducer != null ? dataProducer.available() : 0;
99 }
100
101 @Override
102 public void produce(final DataStreamChannel channel) throws IOException {
103 if (dataProducer != null) {
104 dataProducer.produce(channel);
105 }
106 }
107
108 @Override
109 public boolean isRepeatable() {
110 return dataProducer == null || dataProducer.isRepeatable();
111 }
112
113 @Override
114 public void failed(final Exception cause) {
115 try {
116 if (dataProducer != null) {
117 dataProducer.failed(cause);
118 }
119 } finally {
120 releaseResources();
121 }
122 }
123
124 @Override
125 public void releaseResources() {
126 if (dataProducer != null) {
127 dataProducer.releaseResources();
128 }
129 }
130 }