use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.
the class ServerStreamCompressionFilter method onStreamRequest.
/**
* Handles compression tasks for incoming requests
*/
@Override
public void onStreamRequest(StreamRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
try {
//Check if the request is compressed, if so, decompress
String requestContentEncoding = req.getHeader(HttpConstants.CONTENT_ENCODING);
if (requestContentEncoding != null) {
//This must be a specific compression type other than *
StreamEncodingType encoding = StreamEncodingType.get(requestContentEncoding.trim().toLowerCase());
if (encoding == null || encoding == StreamEncodingType.ANY) {
throw new CompressionException(CompressionConstants.UNSUPPORTED_ENCODING + requestContentEncoding);
}
//Process the correct content-encoding types only
StreamingCompressor compressor = encoding.getCompressor(_executor);
if (compressor == null) {
throw new CompressionException(CompressionConstants.UNKNOWN_ENCODING + encoding);
}
EntityStream uncompressedStream = compressor.inflate(req.getEntityStream());
Map<String, String> headers = stripHeaders(req.getHeaders(), HttpConstants.CONTENT_ENCODING, HttpConstants.CONTENT_LENGTH);
req = req.builder().setHeaders(headers).build(uncompressedStream);
}
//Get client support for compression and flag compress if need be
String responseCompression = req.getHeader(HttpConstants.ACCEPT_ENCODING);
if (responseCompression == null) {
// per RFC 2616, section 14.3, if no Accept-Encoding field is present in a request,
// server SHOULD use "identity" content-encoding if it is available.
responseCompression = StreamEncodingType.IDENTITY.getHttpName();
}
if (!responseCompression.equalsIgnoreCase(StreamEncodingType.IDENTITY.getHttpName())) {
requestContext.putLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, _serverCompressionHelper.getResponseCompressionThreshold(req));
}
requestContext.putLocalAttr(HttpConstants.ACCEPT_ENCODING, responseCompression);
nextFilter.onRequest(req, requestContext, wireAttrs);
} catch (CompressionException ex) {
LOG.error(ex.getMessage(), ex.getCause());
StreamResponse streamResponse = new StreamResponseBuilder().setStatus(HttpConstants.UNSUPPORTED_MEDIA_TYPE).build(EntityStreams.emptyStream());
nextFilter.onError(new StreamException(streamResponse, ex), requestContext, wireAttrs);
}
}
use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.
the class TestStreamingCompression method testCompress.
private void testCompress(StreamingCompressor compressor, byte[] uncompressed, byte[] compressed) throws CompressionException, ExecutionException, InterruptedException {
ByteWriter writer = new ByteWriter(uncompressed);
EntityStream uncompressedStream = EntityStreams.newEntityStream(writer);
EntityStream compressedStream = compressor.deflate(uncompressedStream);
FutureCallback<byte[]> callback = new FutureCallback<byte[]>();
compressedStream.setReader(new ByteReader(callback));
byte[] result = callback.get();
Assert.assertEquals(result, compressed);
}
use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.
the class TestStreamingCompression method testGzipCompressor.
@Test
public void testGzipCompressor() throws IOException, InterruptedException, CompressionException, ExecutionException {
StreamingCompressor compressor = new GzipCompressor(_executor);
final byte[] origin = new byte[BUF_SIZE];
Arrays.fill(origin, (byte) 'b');
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
IOUtils.write(origin, gzip);
gzip.close();
byte[] compressed = out.toByteArray();
testCompress(compressor, origin, compressed);
testDecompress(compressor, origin, compressed);
testCompressThenDecompress(compressor, origin);
}
use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.
the class TestStreamingCompression method testDecompress.
private void testDecompress(StreamingCompressor compressor, byte[] uncompressed, byte[] compressed) throws CompressionException, ExecutionException, InterruptedException {
ByteWriter writer = new ByteWriter(compressed);
EntityStream compressedStream = EntityStreams.newEntityStream(writer);
EntityStream uncompressedStream = compressor.inflate(compressedStream);
FutureCallback<byte[]> callback = new FutureCallback<byte[]>();
uncompressedStream.setReader(new ByteReader(callback));
byte[] result = callback.get();
Assert.assertEquals(result, uncompressed);
}
use of com.linkedin.r2.filter.compression.streaming.StreamingCompressor in project rest.li by linkedin.
the class TestStreamingCompression method testCompress.
private void testCompress(StreamingCompressor compressor, byte[] uncompressed, byte[] compressed) throws CompressionException, ExecutionException, InterruptedException {
ByteWriter writer = new ByteWriter(uncompressed);
EntityStream uncompressedStream = EntityStreams.newEntityStream(writer);
EntityStream compressedStream = compressor.deflate(uncompressedStream);
FutureCallback<byte[]> callback = new FutureCallback<byte[]>();
compressedStream.setReader(new ByteReader(callback));
byte[] result = callback.get();
Assert.assertEquals(result, compressed);
}
Aggregations