use of com.linkedin.r2.filter.compression.CompressionException 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.CompressionException in project rest.li by linkedin.
the class TestStreamingCompression method testDeflateCompressor.
@Test
public void testDeflateCompressor() throws IOException, InterruptedException, CompressionException, ExecutionException {
StreamingCompressor compressor = new DeflateCompressor(_executor);
final byte[] origin = new byte[BUF_SIZE];
Arrays.fill(origin, (byte) 'c');
ByteArrayOutputStream out = new ByteArrayOutputStream();
DeflaterOutputStream zlib = new DeflaterOutputStream(out);
IOUtils.write(origin, zlib);
zlib.close();
byte[] compressed = out.toByteArray();
testCompress(compressor, origin, compressed);
testDecompress(compressor, origin, compressed);
testCompressThenDecompress(compressor, origin);
}
use of com.linkedin.r2.filter.compression.CompressionException in project rest.li by linkedin.
the class TestStreamingCompression method testBzip2Compressor.
@Test
public void testBzip2Compressor() throws IOException, InterruptedException, CompressionException, ExecutionException {
StreamingCompressor compressor = new Bzip2Compressor(_executor);
final byte[] origin = new byte[BUF_SIZE];
Arrays.fill(origin, (byte) 'c');
ByteArrayOutputStream out = new ByteArrayOutputStream();
BZip2CompressorOutputStream bzip = new BZip2CompressorOutputStream(out);
IOUtils.write(origin, bzip);
bzip.close();
byte[] compressed = out.toByteArray();
testCompress(compressor, origin, compressed);
testDecompress(compressor, origin, compressed);
testCompressThenDecompress(compressor, origin);
}
use of com.linkedin.r2.filter.compression.CompressionException in project rest.li by linkedin.
the class TestStreamingCompression method testDeflateCompressor.
@Test
public void testDeflateCompressor() throws IOException, InterruptedException, CompressionException, ExecutionException {
StreamingCompressor compressor = new DeflateCompressor(_executor);
final byte[] origin = new byte[BUF_SIZE];
Arrays.fill(origin, (byte) 'c');
ByteArrayOutputStream out = new ByteArrayOutputStream();
DeflaterOutputStream zlib = new DeflaterOutputStream(out);
IOUtils.write(origin, zlib);
zlib.close();
byte[] compressed = out.toByteArray();
testCompress(compressor, origin, compressed);
testDecompress(compressor, origin, compressed);
testCompressThenDecompress(compressor, origin);
}
use of com.linkedin.r2.filter.compression.CompressionException in project rest.li by linkedin.
the class ClientCompressionFilter method onRestRequest.
/**
* Optionally compresses outgoing REST requests
* */
@Override
public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
try {
if (_requestContentEncoding.hasCompressor()) {
if (_helper.shouldCompressRequest(req.getEntity().length(), (CompressionOption) requestContext.getLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE))) {
Compressor compressor = _requestContentEncoding.getCompressor();
byte[] compressed = compressor.deflate(req.getEntity().asInputStream());
if (compressed.length < req.getEntity().length()) {
req = req.builder().setEntity(compressed).setHeader(HttpConstants.CONTENT_ENCODING, compressor.getContentEncodingName()).build();
}
}
}
String operation = (String) requestContext.getLocalAttr(R2Constants.OPERATION);
if (!_acceptEncodingHeader.isEmpty() && _helper.shouldCompressResponseForOperation(operation)) {
CompressionOption responseCompressionOverride = (CompressionOption) requestContext.getLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE);
req = addResponseCompressionHeaders(responseCompressionOverride, req);
}
} catch (CompressionException e) {
LOG.error(e.getMessage(), e.getCause());
}
//Specify the actual compression algorithm used
nextFilter.onRequest(req, requestContext, wireAttrs);
}
Aggregations