use of com.linkedin.r2.filter.CompressionOption 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);
}
Aggregations