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.io.entity;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 import java.io.Serializable;
35 import java.nio.charset.Charset;
36 import java.nio.file.Path;
37 import java.util.Arrays;
38 import java.util.LinkedHashSet;
39 import java.util.List;
40 import java.util.Set;
41 import java.util.zip.GZIPOutputStream;
42
43 import org.apache.hc.core5.function.Supplier;
44 import org.apache.hc.core5.http.ContentType;
45 import org.apache.hc.core5.http.Header;
46 import org.apache.hc.core5.http.HttpEntity;
47 import org.apache.hc.core5.http.NameValuePair;
48 import org.apache.hc.core5.io.IOCallback;
49 import org.apache.hc.core5.net.WWWFormCodec;
50 import org.apache.hc.core5.util.Args;
51
52
53
54
55
56
57 public final class HttpEntities {
58
59 private HttpEntities() {
60 }
61
62 public static HttpEntity create(final String content, final ContentType contentType) {
63 return new StringEntity(content, contentType);
64 }
65
66 public static HttpEntity create(final String content, final Charset charset) {
67 return new StringEntity(content, ContentType.TEXT_PLAIN.withCharset(charset));
68 }
69
70 public static HttpEntity create(final String content) {
71 return new StringEntity(content, ContentType.TEXT_PLAIN);
72 }
73
74 public static HttpEntity create(final byte[] content, final ContentType contentType) {
75 return new ByteArrayEntity(content, contentType);
76 }
77
78 public static HttpEntity create(final File content, final ContentType contentType) {
79 return new FileEntity(content, contentType);
80 }
81
82 public static HttpEntity create(final Serializable serializable, final ContentType contentType) {
83 return new SerializableEntity(serializable, contentType);
84 }
85
86 public static HttpEntity createUrlEncoded(
87 final Iterable <? extends NameValuePair> parameters, final Charset charset) {
88 final ContentType contentType = charset != null ?
89 ContentType.APPLICATION_FORM_URLENCODED.withCharset(charset) :
90 ContentType.APPLICATION_FORM_URLENCODED;
91 return create(WWWFormCodec.format(parameters, contentType.getCharset()), contentType);
92 }
93
94 public static HttpEntity create(final IOCallback<OutputStream> callback, final ContentType contentType) {
95 return new EntityTemplate(-1, contentType, null, callback);
96 }
97
98 public static HttpEntityttp/HttpEntity.html#HttpEntity">HttpEntity gzip(final HttpEntity entity) {
99 return new HttpEntityWrapper(entity) {
100
101 @Override
102 public String getContentEncoding() {
103 return "gzip";
104 }
105
106 @Override
107 public long getContentLength() {
108 return -1;
109 }
110
111 @Override
112 public InputStream getContent() throws IOException {
113 throw new UnsupportedOperationException();
114 }
115
116 @Override
117 public void writeTo(final OutputStream outStream) throws IOException {
118 Args.notNull(outStream, "Output stream");
119 final GZIPOutputStream gzip = new GZIPOutputStream(outStream);
120 super.writeTo(gzip);
121
122
123 gzip.close();
124 }
125
126 };
127 }
128
129 public static HttpEntity createGzipped(final String content, final ContentType contentType) {
130 return gzip(create(content, contentType));
131 }
132
133 public static HttpEntity createGzipped(final String content, final Charset charset) {
134 return gzip(create(content, charset));
135 }
136
137 public static HttpEntity createGzipped(final String content) {
138 return gzip(create(content));
139 }
140
141 public static HttpEntity createGzipped(final byte[] content, final ContentType contentType) {
142 return gzip(create(content, contentType));
143 }
144
145 public static HttpEntity createGzipped(final File content, final ContentType contentType) {
146 return gzip(create(content, contentType));
147 }
148
149 public static HttpEntity createGzipped(final Serializable serializable, final ContentType contentType) {
150 return gzip(create(serializable, contentType));
151 }
152
153 public static HttpEntity createGzipped(final IOCallback<OutputStream> callback, final ContentType contentType) {
154 return gzip(create(callback, contentType));
155 }
156
157 public static HttpEntity createGzipped(final Path content, final ContentType contentType) {
158 return gzip(create(content, contentType));
159 }
160
161 public static HttpEntityEntity.html#HttpEntity">HttpEntity withTrailers(final HttpEntity entity, final Header... trailers) {
162 return new HttpEntityWrapper(entity) {
163
164 @Override
165 public boolean isChunked() {
166
167 return true;
168 }
169
170 @Override
171 public long getContentLength() {
172 return -1;
173 }
174
175 @Override
176 public Supplier<List<? extends Header>> getTrailers() {
177 return new Supplier<List<? extends Header>>() {
178
179 @Override
180 public List<? extends Header> get() {
181 return Arrays.asList(trailers);
182 }
183
184 };
185 }
186
187 @Override
188 public Set<String> getTrailerNames() {
189 final Set<String> names = new LinkedHashSet<>();
190 for (final Header trailer: trailers) {
191 names.add(trailer.getName());
192 }
193 return names;
194 }
195
196 };
197 }
198
199 public static HttpEntity create(final String content, final ContentType contentType, final Header... trailers) {
200 return withTrailers(create(content, contentType), trailers);
201 }
202
203 public static HttpEntity create(final String content, final Charset charset, final Header... trailers) {
204 return withTrailers(create(content, charset), trailers);
205 }
206
207 public static HttpEntity create(final String content, final Header... trailers) {
208 return withTrailers(create(content), trailers);
209 }
210
211 public static HttpEntity create(final byte[] content, final ContentType contentType, final Header... trailers) {
212 return withTrailers(create(content, contentType), trailers);
213 }
214
215 public static HttpEntity create(final File content, final ContentType contentType, final Header... trailers) {
216 return withTrailers(create(content, contentType), trailers);
217 }
218
219 public static HttpEntity create(
220 final Serializable serializable, final ContentType contentType, final Header... trailers) {
221 return withTrailers(create(serializable, contentType), trailers);
222 }
223
224 public static HttpEntity create(
225 final IOCallback<OutputStream> callback, final ContentType contentType, final Header... trailers) {
226 return withTrailers(create(callback, contentType), trailers);
227 }
228
229 public static HttpEntity create(final Path content, final ContentType contentType) {
230 return new PathEntity(content, contentType);
231 }
232
233 public static HttpEntity create(final Path content, final ContentType contentType, final Header... trailers) {
234 return withTrailers(create(content, contentType), trailers);
235 }
236
237 }