use of com.linkedin.r2.filter.compression.streaming.AcceptEncoding in project rest.li by linkedin.
the class ServerCompressionFilter method onRestResponse.
/**
* Optionally compresses outgoing response
*/
@Override
public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
try {
if (res.getEntity().length() > 0) {
String responseAcceptedEncodings = (String) requestContext.getLocalAttr(HttpConstants.ACCEPT_ENCODING);
if (responseAcceptedEncodings == null) {
throw new CompressionException(HttpConstants.ACCEPT_ENCODING + " not in local attribute.");
}
List<AcceptEncoding> parsedEncodings = AcceptEncoding.parseAcceptEncodingHeader(responseAcceptedEncodings, _supportedEncoding);
EncodingType selectedEncoding = AcceptEncoding.chooseBest(parsedEncodings);
// Check if there exists an acceptable encoding
if (selectedEncoding != null) {
if (selectedEncoding.hasCompressor() && res.getEntity().length() > (Integer) requestContext.getLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD)) {
Compressor compressor = selectedEncoding.getCompressor();
ByteString compressed = compressor.deflate(res.getEntity());
if (compressed.length() < res.getEntity().length()) {
RestResponseBuilder resCompress = res.builder();
resCompress.removeHeader(HttpConstants.CONTENT_LENGTH);
resCompress.addHeaderValue(HttpConstants.CONTENT_ENCODING, compressor.getContentEncodingName());
resCompress.setEntity(compressed);
res = resCompress.build();
}
}
} else {
// Not acceptable encoding status
res = res.builder().setStatus(HttpConstants.NOT_ACCEPTABLE).setEntity(new byte[0]).build();
}
}
} catch (CompressionException e) {
LOG.error(e.getMessage(), e.getCause());
}
nextFilter.onResponse(res, requestContext, wireAttrs);
}
use of com.linkedin.r2.filter.compression.streaming.AcceptEncoding in project rest.li by linkedin.
the class ServerStreamCompressionFilter method onStreamResponse.
/**
* Optionally compresses outgoing response
*/
@Override
public void onStreamResponse(final StreamResponse res, final RequestContext requestContext, final Map<String, String> wireAttrs, final NextFilter<StreamRequest, StreamResponse> nextFilter) {
StreamResponse response = res;
try {
String responseCompression = (String) requestContext.getLocalAttr(HttpConstants.ACCEPT_ENCODING);
if (responseCompression == null) {
throw new CompressionException(HttpConstants.ACCEPT_ENCODING + " not in local attribute.");
}
List<AcceptEncoding> parsedEncodings = AcceptEncoding.parseAcceptEncodingHeader(responseCompression, _supportedEncoding);
StreamEncodingType selectedEncoding = AcceptEncoding.chooseBest(parsedEncodings);
// Check if there exists an acceptable encoding
if (selectedEncoding == null) {
// Not acceptable encoding status
response = new StreamResponseBuilder().setStatus(HttpConstants.NOT_ACCEPTABLE).build(EntityStreams.emptyStream());
} else if (selectedEncoding != StreamEncodingType.IDENTITY) {
final int threshold = (Integer) requestContext.getLocalAttr(HttpConstants.HEADER_RESPONSE_COMPRESSION_THRESHOLD);
final StreamingCompressor compressor = selectedEncoding.getCompressor(_executor);
PartialReader reader = new PartialReader(threshold, new Callback<EntityStream[]>() {
@Override
public void onError(Throwable ex) {
nextFilter.onError(ex, requestContext, wireAttrs);
}
@Override
public void onSuccess(EntityStream[] results) {
if (// entity stream is less than threshold
results.length == 1) {
StreamResponse response = res.builder().build(results[0]);
nextFilter.onResponse(response, requestContext, wireAttrs);
} else {
EntityStream compressedStream = compressor.deflate(EntityStreams.newEntityStream(new CompositeWriter(results)));
StreamResponseBuilder builder = res.builder();
// remove original content-length header if presents.
if (builder.getHeader(HttpConstants.CONTENT_LENGTH) != null) {
Map<String, String> headers = stripHeaders(builder.getHeaders(), HttpConstants.CONTENT_LENGTH);
builder.setHeaders(headers);
}
StreamResponse response = builder.addHeaderValue(HttpConstants.CONTENT_ENCODING, compressor.getContentEncodingName()).build(compressedStream);
nextFilter.onResponse(response, requestContext, wireAttrs);
}
}
});
res.getEntityStream().setReader(reader);
return;
}
} catch (CompressionException e) {
LOG.error(e.getMessage(), e.getCause());
}
nextFilter.onResponse(response, requestContext, wireAttrs);
}
Aggregations