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