use of com.linkedin.r2.filter.compression.CompressionException in project rest.li by linkedin.
the class TestClientStreamCompressionFilter method testRequestCompressionRules.
@Test(dataProvider = "requestData")
public void testRequestCompressionRules(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;
HeaderCaptureFilter captureFilter = new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, headerShouldBePresent, entityLength);
clientCompressionFilter.onStreamRequest(streamRequest, context, Collections.<String, String>emptyMap(), captureFilter);
FutureCallback<ByteString> callback = new FutureCallback<ByteString>();
FullEntityReader reader = new FullEntityReader(callback);
captureFilter.getEntityStream().setReader(reader);
ByteString entityRead = callback.get(10, TimeUnit.SECONDS);
Assert.assertEquals(entityRead.length(), entityLength);
}
use of com.linkedin.r2.filter.compression.CompressionException in project rest.li by linkedin.
the class TestServerCompressionFilter method testResponseCompressionRules.
// Test response compression rules where the server has a default threshold of Integer.MAX_VALUE.
@Test(dataProvider = "headersData")
public void testResponseCompressionRules(String acceptEncoding, int compressionThreshold, EncodingType expectedContentEncoding) throws CompressionException, URISyntaxException {
ServerCompressionFilter serverCompressionFilter = new ServerCompressionFilter(ACCEPT_COMPRESSIONS);
RequestContext context = new RequestContext();
context.putLocalAttr(HttpConstants.ACCEPT_ENCODING, acceptEncoding);
context.putLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, compressionThreshold);
int originalLength = 100;
byte[] entity = new byte[originalLength];
Arrays.fill(entity, (byte) 'A');
int compressedLength = (expectedContentEncoding == null) ? originalLength : expectedContentEncoding.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
String expectedContentEncodingName = (expectedContentEncoding == null) ? null : expectedContentEncoding.getHttpName();
RestResponse restResponse = new RestResponseBuilder().setEntity(entity).build();
serverCompressionFilter.onRestResponse(restResponse, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, expectedContentEncodingName, compressedLength));
}
use of com.linkedin.r2.filter.compression.CompressionException in project rest.li by linkedin.
the class TestStreamingCompression method testSnappyCompressor.
@Test
public void testSnappyCompressor() throws IOException, InterruptedException, CompressionException, ExecutionException {
StreamingCompressor compressor = new SnappyCompressor(_executor);
final byte[] origin = new byte[BUF_SIZE];
Arrays.fill(origin, (byte) 'a');
ByteArrayOutputStream out = new ByteArrayOutputStream();
SnappyFramedOutputStream snappy = new SnappyFramedOutputStream(out);
IOUtils.write(origin, snappy);
snappy.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 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);
}
Aggregations