|
27 | 27 | package org.apache.hc.client5.http.examples; |
28 | 28 |
|
29 | 29 | import java.io.ByteArrayOutputStream; |
| 30 | +import java.io.IOException; |
30 | 31 | import java.io.InputStream; |
| 32 | +import java.io.OutputStream; |
31 | 33 | import java.nio.charset.StandardCharsets; |
32 | 34 | import java.util.concurrent.Future; |
33 | 35 |
|
34 | | -import org.apache.commons.compress.compressors.CompressorInputStream; |
| 36 | +import org.apache.commons.compress.compressors.CompressorException; |
35 | 37 | import org.apache.commons.compress.compressors.CompressorStreamFactory; |
36 | 38 | import org.apache.hc.client5.http.async.methods.DeflatingZstdEntityProducer; |
37 | 39 | import org.apache.hc.client5.http.async.methods.SimpleHttpRequest; |
|
42 | 44 | import org.apache.hc.core5.http.ClassicHttpRequest; |
43 | 45 | import org.apache.hc.core5.http.ClassicHttpResponse; |
44 | 46 | import org.apache.hc.core5.http.ContentType; |
45 | | -import org.apache.hc.core5.http.HttpEntity; |
46 | 47 | import org.apache.hc.core5.http.HttpHeaders; |
47 | 48 | import org.apache.hc.core5.http.HttpResponse; |
48 | 49 | import org.apache.hc.core5.http.HttpStatus; |
49 | 50 | import org.apache.hc.core5.http.Message; |
50 | 51 | import org.apache.hc.core5.http.impl.bootstrap.HttpServer; |
51 | 52 | import org.apache.hc.core5.http.impl.bootstrap.ServerBootstrap; |
52 | 53 | import org.apache.hc.core5.http.io.HttpRequestHandler; |
| 54 | +import org.apache.hc.core5.http.io.entity.ByteArrayEntity; |
53 | 55 | import org.apache.hc.core5.http.io.entity.StringEntity; |
54 | 56 | import org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer; |
55 | 57 | import org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer; |
@@ -151,36 +153,35 @@ public static void main(final String[] args) throws Exception { |
151 | 153 | * Classic echo handler that decodes request Content-Encoding: zstd and returns plain text. |
152 | 154 | */ |
153 | 155 | private static final class EchoHandler implements HttpRequestHandler { |
| 156 | + |
154 | 157 | @Override |
155 | | - public void handle(final ClassicHttpRequest request, |
156 | | - final ClassicHttpResponse response, |
157 | | - final HttpContext context) { |
158 | | - try { |
159 | | - final HttpEntity ent = request.getEntity(); |
160 | | - final byte[] body; |
161 | | - if (ent != null && hasZstd(ent)) { |
162 | | - try (InputStream in = ent.getContent(); |
163 | | - CompressorInputStream zin = new CompressorStreamFactory() |
164 | | - .createCompressorInputStream("zstd", in)) { |
165 | | - body = readAll(zin); |
166 | | - } |
167 | | - } else { |
168 | | - body = ent != null ? readAll(ent.getContent()) : new byte[0]; |
169 | | - } |
170 | | - final String text = new String(body, StandardCharsets.UTF_8); |
| 158 | + public void handle( |
| 159 | + final ClassicHttpRequest request, |
| 160 | + final ClassicHttpResponse response, |
| 161 | + final HttpContext context) throws IOException { |
| 162 | + |
| 163 | + try (InputStream in = new CompressorStreamFactory() |
| 164 | + .createCompressorInputStream(ContentCoding.ZSTD.token(), request.getEntity().getContent())) { |
| 165 | + |
| 166 | + final byte[] data = readAll(in); |
| 167 | + final String text = new String(data, StandardCharsets.UTF_8); |
| 168 | + |
171 | 169 | response.setCode(HttpStatus.SC_OK); |
172 | | - response.setEntity(new StringEntity("echo: " + text, ContentType.TEXT_PLAIN)); |
173 | | - } catch (final Exception ex) { |
| 170 | + response.addHeader("Content-Encoding", ContentCoding.ZSTD.token()); |
| 171 | + |
| 172 | + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 173 | + try (final OutputStream zstdOut = new CompressorStreamFactory() |
| 174 | + .createCompressorOutputStream("zstd", baos)) { |
| 175 | + zstdOut.write(text.getBytes(StandardCharsets.UTF_8)); |
| 176 | + } |
| 177 | + response.setEntity(new ByteArrayEntity(baos.toByteArray(), ContentType.TEXT_PLAIN)); |
| 178 | + } catch (final CompressorException ex) { |
174 | 179 | response.setCode(HttpStatus.SC_BAD_REQUEST); |
175 | | - response.setEntity(new StringEntity("Bad request", ContentType.TEXT_PLAIN)); |
| 180 | + response.setEntity(new StringEntity("Unable to process compressed payload", StandardCharsets.UTF_8)); |
176 | 181 | } |
177 | 182 | } |
178 | 183 |
|
179 | | - private static boolean hasZstd(final HttpEntity e) { |
180 | | - return "zstd".equalsIgnoreCase(e.getContentEncoding()); |
181 | | - } |
182 | | - |
183 | | - private static byte[] readAll(final InputStream in) throws Exception { |
| 184 | + private static byte[] readAll(final InputStream in) throws IOException { |
184 | 185 | final ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
185 | 186 | final byte[] buf = new byte[8192]; |
186 | 187 | int n; |
|
0 commit comments