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 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.CompressionException in project rest.li by linkedin.
the class TestClientStreamCompressionFilter method testAcceptEncodingHeader.
@Test(dataProvider = "requestData")
public void testAcceptEncodingHeader(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent, String operation) throws CompressionException, URISyntaxException, InterruptedException, ExecutionException, TimeoutException {
Executor executor = Executors.newCachedThreadPool();
ClientStreamCompressionFilter clientCompressionFilter = new ClientStreamCompressionFilter(StreamEncodingType.GZIP.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR), executor);
// The entity should be compressible for this test.
int original = 100;
byte[] entity = new byte[original];
Arrays.fill(entity, (byte) 'A');
StreamRequest streamRequest = new StreamRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(entity))));
int compressed = EncodingType.GZIP.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
RequestContext context = new RequestContext();
if (operation != null) {
context.putLocalAttr(R2Constants.OPERATION, operation);
}
context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
int entityLength = headerShouldBePresent ? compressed : original;
clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.ACCEPT_ENCODING, true));
}
use of com.linkedin.r2.filter.compression.CompressionException 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.CompressionException in project rest.li by linkedin.
the class TestStreamingCompression method testCompressThenDecompress.
private void testCompressThenDecompress(StreamingCompressor compressor, byte[] origin) throws CompressionException, ExecutionException, InterruptedException {
ByteWriter writer = new ByteWriter(origin);
EntityStream uncompressedStream = EntityStreams.newEntityStream(writer);
EntityStream compressedStream = compressor.deflate(uncompressedStream);
EntityStream decompressedStream = compressor.inflate(compressedStream);
FutureCallback<byte[]> callback = new FutureCallback<byte[]>();
decompressedStream.setReader(new ByteReader(callback));
byte[] result = callback.get();
Assert.assertEquals(result, origin);
}
Aggregations