Search in sources :

Example 21 with CompressionException

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);
}
Also used : GzipCompressor(com.linkedin.r2.filter.compression.streaming.GzipCompressor) GZIPOutputStream(java.util.zip.GZIPOutputStream) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 22 with CompressionException

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);
}
Also used : DeflateCompressor(com.linkedin.r2.filter.compression.streaming.DeflateCompressor) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 23 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 24 with CompressionException

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);
}
Also used : DeflateCompressor(com.linkedin.r2.filter.compression.streaming.DeflateCompressor) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 25 with CompressionException

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);
}
Also used : CompressionOption(com.linkedin.r2.filter.CompressionOption)

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