Search in sources :

Example 1 with CompressionException

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);
}
Also used : ByteString(com.linkedin.data.ByteString) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) Executor(java.util.concurrent.Executor) ByteArrayInputStream(java.io.ByteArrayInputStream) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) FutureCallback(com.linkedin.common.callback.FutureCallback) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 2 with CompressionException

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));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 3 with CompressionException

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);
}
Also used : SnappyCompressor(com.linkedin.r2.filter.compression.streaming.SnappyCompressor) SnappyFramedOutputStream(org.iq80.snappy.SnappyFramedOutputStream) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 4 with CompressionException

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);
}
Also used : BZip2CompressorOutputStream(org.apache.commons.compress.compressors.bzip2.BZip2CompressorOutputStream) Bzip2Compressor(com.linkedin.r2.filter.compression.streaming.Bzip2Compressor) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 5 with CompressionException

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);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) FutureCallback(com.linkedin.common.callback.FutureCallback)

Aggregations

Test (org.testng.annotations.Test)13 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)10 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 FutureCallback (com.linkedin.common.callback.FutureCallback)7 RequestContext (com.linkedin.r2.message.RequestContext)5 CompressionConfig (com.linkedin.r2.filter.CompressionConfig)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 URI (java.net.URI)4 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)3 Bzip2Compressor (com.linkedin.r2.filter.compression.streaming.Bzip2Compressor)2 DeflateCompressor (com.linkedin.r2.filter.compression.streaming.DeflateCompressor)2 GzipCompressor (com.linkedin.r2.filter.compression.streaming.GzipCompressor)2 SnappyCompressor (com.linkedin.r2.filter.compression.streaming.SnappyCompressor)2 StreamEncodingType (com.linkedin.r2.filter.compression.streaming.StreamEncodingType)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)2