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 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);
}
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;
}
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));
}
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));
}
Aggregations