Search in sources :

Example 6 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 7 with CompressionException

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

Example 8 with CompressionException

use of com.linkedin.r2.filter.compression.CompressionException in project rest.li by linkedin.

the class AcceptEncoding method parseAcceptEncodingHeader.

/**
   * Takes the value of Accept-Encoding HTTP header field and returns a list of supported types in
   * their order of appearance in the HTTP header value (unsupported types are filtered out).
   * @param headerValue Http header value of Accept-Encoding field
   * @return ArrayList of accepted-encoding entries
   * @throws com.linkedin.r2.filter.compression.CompressionException
   */
public static List<AcceptEncoding> parseAcceptEncodingHeader(String headerValue, Set<StreamEncodingType> supportedEncodings) throws CompressionException {
    headerValue = headerValue.toLowerCase();
    String[] entries = headerValue.split(CompressionConstants.ENCODING_DELIMITER);
    List<AcceptEncoding> parsedEncodings = new ArrayList<AcceptEncoding>();
    for (String entry : entries) {
        String[] content = entry.trim().split(CompressionConstants.QUALITY_DELIMITER);
        if (content.length < 1 || content.length > 2) {
            throw new IllegalArgumentException(CompressionConstants.ILLEGAL_FORMAT + entry);
        }
        StreamEncodingType type = StreamEncodingType.get(content[0].trim());
        Float quality = 1.0f;
        if (type != null && supportedEncodings.contains(type)) {
            if (content.length > 1) {
                String acceptEncodingPart = content[1].trim();
                if (acceptEncodingPart.startsWith(CompressionConstants.QUALITY_PREFIX)) {
                    try {
                        quality = Float.parseFloat(acceptEncodingPart.substring(CompressionConstants.QUALITY_PREFIX.length()));
                    } catch (NumberFormatException e) {
                        throw new CompressionException(CompressionConstants.ILLEGAL_FORMAT + entry, e);
                    }
                } else {
                    throw new CompressionException(CompressionConstants.ILLEGAL_FORMAT + entry);
                }
            }
            parsedEncodings.add(new AcceptEncoding(type, quality));
        }
    }
    return parsedEncodings;
}
Also used : ArrayList(java.util.ArrayList) CompressionException(com.linkedin.r2.filter.compression.CompressionException)

Example 9 with CompressionException

use of com.linkedin.r2.filter.compression.CompressionException in project rest.li by linkedin.

the class TestClientCompressionFilter method testResponseCompressionRules.

@Test(dataProvider = "responseCompressionData")
public void testResponseCompressionRules(CompressionConfig responseCompressionConfig, CompressionOption responseCompressionOverride, String expectedAcceptEncoding, String expectedCompressionThreshold, String operation) throws CompressionException, URISyntaxException {
    ClientCompressionFilter clientCompressionFilter = new ClientCompressionFilter(EncodingType.SNAPPY.getHttpName(), new CompressionConfig(Integer.MAX_VALUE), ACCEPT_COMPRESSIONS, responseCompressionConfig, Arrays.asList(ClientCompressionHelper.COMPRESS_ALL_RESPONSES_INDICATOR));
    RestRequest restRequest = new RestRequestBuilder(new URI(URI)).build();
    RequestContext context = new RequestContext();
    if (operation != null) {
        context.putLocalAttr(R2Constants.OPERATION, operation);
    }
    context.putLocalAttr(R2Constants.RESPONSE_COMPRESSION_OVERRIDE, responseCompressionOverride);
    clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.ACCEPT_ENCODING, expectedAcceptEncoding));
    clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD, expectedCompressionThreshold));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

Example 10 with CompressionException

use of com.linkedin.r2.filter.compression.CompressionException in project rest.li by linkedin.

the class TestClientCompressionFilter method testRequestCompressionRules.

@Test(dataProvider = "requestCompressionData")
public void testRequestCompressionRules(CompressionConfig requestCompressionConfig, CompressionOption requestCompressionOverride, boolean headerShouldBePresent) throws CompressionException, URISyntaxException {
    ClientCompressionFilter clientCompressionFilter = new ClientCompressionFilter(EncodingType.SNAPPY.getHttpName(), requestCompressionConfig, ACCEPT_COMPRESSIONS, new CompressionConfig(Integer.MAX_VALUE), Collections.<String>emptyList());
    // The entity should be compressible for this test.
    int original = 100;
    byte[] entity = new byte[original];
    Arrays.fill(entity, (byte) 'A');
    RestRequest restRequest = new RestRequestBuilder(new URI(URI)).setMethod(RestMethod.POST).setEntity(entity).build();
    int compressed = EncodingType.SNAPPY.getCompressor().deflate(new ByteArrayInputStream(entity)).length;
    RequestContext context = new RequestContext();
    context.putLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE, requestCompressionOverride);
    int entityLength = headerShouldBePresent ? compressed : original;
    String expectedContentEncoding = headerShouldBePresent ? EncodingType.SNAPPY.getHttpName() : null;
    clientCompressionFilter.onRestRequest(restRequest, context, Collections.<String, String>emptyMap(), new HeaderCaptureFilter(HttpConstants.CONTENT_ENCODING, expectedContentEncoding, entityLength));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) CompressionConfig(com.linkedin.r2.filter.CompressionConfig) Test(org.testng.annotations.Test)

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