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.protocol;
28
29 import java.io.IOException;
30
31 import org.apache.http.HttpEntity;
32 import org.apache.http.HttpEntityEnclosingRequest;
33 import org.apache.http.HttpHost;
34 import org.apache.http.HttpRequest;
35 import org.apache.http.nio.ContentEncoder;
36 import org.apache.http.nio.IOControl;
37 import org.apache.http.nio.entity.EntityAsyncContentProducer;
38 import org.apache.http.nio.entity.HttpAsyncContentProducer;
39 import org.apache.http.protocol.HttpContext;
40 import org.apache.http.util.Args;
41
42
43
44
45
46
47
48
49
50
51
52 public class BasicAsyncRequestProducer implements HttpAsyncRequestProducer {
53
54 private final HttpHost target;
55 private final HttpRequest request;
56 private final HttpAsyncContentProducer producer;
57
58
59
60
61
62
63
64
65
66
67
68
69 protected BasicAsyncRequestProducer(
70 final HttpHost target,
71 final HttpEntityEnclosingRequest request,
72 final HttpAsyncContentProducer producer) {
73 super();
74 Args.notNull(target, "HTTP host");
75 Args.notNull(request, "HTTP request");
76 Args.notNull(producer, "HTTP content producer");
77 this.target = target;
78 this.request = request;
79 this.producer = producer;
80 }
81
82
83
84
85
86
87
88
89
90 public BasicAsyncRequestProducer(final HttpHost target, final HttpRequest request) {
91 Args.notNull(target, "HTTP host");
92 Args.notNull(request, "HTTP request");
93 this.target = target;
94 this.request = request;
95 if (request instanceof HttpEntityEnclosingRequest) {
96 final HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
97 if (entity != null) {
98 if (entity instanceof HttpAsyncContentProducer) {
99 this.producer = (HttpAsyncContentProducer) entity;
100 } else {
101 this.producer = new EntityAsyncContentProducer(entity);
102 }
103 } else {
104 this.producer = null;
105 }
106 } else {
107 this.producer = null;
108 }
109 }
110
111 @Override
112 public HttpRequest generateRequest() {
113 return this.request;
114 }
115
116 @Override
117 public HttpHost getTarget() {
118 return this.target;
119 }
120
121 @Override
122 public void produceContent(
123 final ContentEncoder encoder, final IOControl ioControl) throws IOException {
124 if (this.producer != null) {
125 this.producer.produceContent(encoder, ioControl);
126 if (encoder.isCompleted()) {
127 this.producer.close();
128 }
129 }
130 }
131
132 @Override
133 public void requestCompleted(final HttpContext context) {
134 }
135
136 @Override
137 public void failed(final Exception ex) {
138 }
139
140 @Override
141 public boolean isRepeatable() {
142 return this.producer == null || this.producer.isRepeatable();
143 }
144
145 @Override
146 public void resetRequest() throws IOException {
147 if (this.producer != null) {
148 this.producer.close();
149 }
150 }
151
152 @Override
153 public void close() throws IOException {
154 if (this.producer != null) {
155 this.producer.close();
156 }
157 }
158
159 @Override
160 public String toString() {
161 final StringBuilder sb = new StringBuilder();
162 sb.append(this.target);
163 sb.append(' ');
164 sb.append(this.request);
165 if (this.producer != null) {
166 sb.append(' ');
167 sb.append(this.producer);
168 }
169 return sb.toString();
170 }
171
172 }