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
28 package org.apache.hc.core5.http.nio.entity;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.nio.ByteBuffer;
33 import java.nio.CharBuffer;
34 import java.nio.charset.Charset;
35 import java.nio.file.OpenOption;
36 import java.nio.file.Path;
37 import java.nio.file.StandardOpenOption;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.LinkedHashSet;
41 import java.util.List;
42 import java.util.Set;
43
44 import org.apache.hc.core5.function.Callback;
45 import org.apache.hc.core5.http.ContentType;
46 import org.apache.hc.core5.http.Header;
47 import org.apache.hc.core5.http.NameValuePair;
48 import org.apache.hc.core5.http.nio.AsyncEntityProducer;
49 import org.apache.hc.core5.http.nio.DataStreamChannel;
50 import org.apache.hc.core5.http.nio.StreamChannel;
51 import org.apache.hc.core5.net.WWWFormCodec;
52
53
54
55
56
57
58 public final class AsyncEntityProducers {
59
60 private AsyncEntityProducers() {
61 }
62
63 public static AsyncEntityProducer create(final String content, final ContentType contentType) {
64 return new BasicAsyncEntityProducer(content, contentType);
65 }
66
67 public static AsyncEntityProducer create(final String content, final Charset charset) {
68 return new BasicAsyncEntityProducer(content, ContentType.TEXT_PLAIN.withCharset(charset));
69 }
70
71 public static AsyncEntityProducer create(final String content) {
72 return new BasicAsyncEntityProducer(content, ContentType.TEXT_PLAIN);
73 }
74
75 public static AsyncEntityProducer create(final byte[] content, final ContentType contentType) {
76 return new BasicAsyncEntityProducer(content, contentType);
77 }
78
79 public static AsyncEntityProducer create(final File content, final ContentType contentType) {
80 return new FileEntityProducer(content, contentType);
81 }
82
83 public static AsyncEntityProducer createUrlEncoded(
84 final Iterable <? extends NameValuePair> parameters, final Charset charset) {
85 final ContentType contentType = charset != null ?
86 ContentType.APPLICATION_FORM_URLENCODED.withCharset(charset) :
87 ContentType.APPLICATION_FORM_URLENCODED;
88 return create(WWWFormCodec.format(parameters, contentType.getCharset()), contentType);
89 }
90
91 public static AsyncEntityProducer createBinary(
92 final Callback<StreamChannel<ByteBuffer>> callback,
93 final ContentType contentType) {
94 return new AbstractBinAsyncEntityProducer(0, contentType) {
95
96 @Override
97 protected int availableData() {
98 return Integer.MAX_VALUE;
99 }
100
101 @Override
102 protected void produceData(final StreamChannel<ByteBuffer> channel) throws IOException {
103 callback.execute(channel);
104 }
105
106 @Override
107 public boolean isRepeatable() {
108 return false;
109 }
110
111 @Override
112 public void failed(final Exception cause) {
113 }
114
115 };
116 }
117
118 public static AsyncEntityProducer createText(
119 final Callback<StreamChannel<CharBuffer>> callback,
120 final ContentType contentType) {
121 return new AbstractCharAsyncEntityProducer(4096, 2048, contentType) {
122
123 @Override
124 protected int availableData() {
125 return Integer.MAX_VALUE;
126 }
127
128 @Override
129 protected void produceData(final StreamChannel<CharBuffer> channel) throws IOException {
130 callback.execute(channel);
131 }
132
133 @Override
134 public boolean isRepeatable() {
135 return false;
136 }
137
138 @Override
139 public void failed(final Exception cause) {
140 }
141
142 };
143 }
144
145 public static AsyncEntityProducer withTrailers(final AsyncEntityProducer entity, final Header... trailers) {
146 return new AsyncEntityProducerWrapper(entity) {
147
148 @Override
149 public boolean isChunked() {
150
151 return true;
152 }
153
154 @Override
155 public long getContentLength() {
156 return -1;
157 }
158
159 @Override
160 public Set<String> getTrailerNames() {
161 final Set<String> names = new LinkedHashSet<>();
162 for (final Header trailer: trailers) {
163 names.add(trailer.getName());
164 }
165 return names;
166 }
167
168 @Override
169 public void produce(final DataStreamChannel channel) throws IOException {
170 super.produce(new DataStreamChannel() {
171
172 @Override
173 public void requestOutput() {
174 channel.requestOutput();
175 }
176
177 @Override
178 public int write(final ByteBuffer src) throws IOException {
179 return channel.write(src);
180 }
181
182 @Override
183 public void endStream(final List<? extends Header> p) throws IOException {
184 final List<Header> allTrailers;
185 if (p != null && !p.isEmpty()) {
186 allTrailers = new ArrayList<>(p);
187 allTrailers.addAll(Arrays.asList(trailers));
188 } else {
189 allTrailers = Arrays.asList(trailers);
190 }
191 channel.endStream(allTrailers);
192 }
193
194 @Override
195 public void endStream() throws IOException {
196 channel.endStream();
197 }
198
199 });
200 }
201 };
202 }
203
204 public static AsyncEntityProducer create(final String content, final ContentType contentType, final Header... trailers) {
205 return withTrailers(create(content, contentType), trailers);
206 }
207
208 public static AsyncEntityProducer create(final String content, final Charset charset, final Header... trailers) {
209 return withTrailers(create(content, charset), trailers);
210 }
211
212 public static AsyncEntityProducer create(final String content, final Header... trailers) {
213 return withTrailers(create(content), trailers);
214 }
215
216 public static AsyncEntityProducer create(final byte[] content, final ContentType contentType, final Header... trailers) {
217 return withTrailers(create(content, contentType), trailers);
218 }
219
220 public static AsyncEntityProducer create(final File content, final ContentType contentType, final Header... trailers) {
221 return withTrailers(create(content, contentType), trailers);
222 }
223
224
225
226
227 public static AsyncEntityProducer create(final Path content, final ContentType contentType, final Header... trailers) throws IOException {
228 return withTrailers(new PathEntityProducer(content, contentType, StandardOpenOption.READ), trailers);
229 }
230
231
232
233
234 public static AsyncEntityProducer create(final Path content, final ContentType contentType, final OpenOption... options) throws IOException {
235 return new PathEntityProducer(content, contentType, options);
236 }
237
238 public static AsyncEntityProducer createBinary(
239 final Callback<StreamChannel<ByteBuffer>> callback,
240 final ContentType contentType,
241 final Header... trailers) {
242 return withTrailers(createBinary(callback, contentType), trailers);
243 }
244
245 public static AsyncEntityProducer createText(
246 final Callback<StreamChannel<CharBuffer>> callback,
247 final ContentType contentType,
248 final Header... trailers) {
249 return withTrailers(createText(callback, contentType), trailers);
250 }
251
252 }