Search in sources :

Example 6 with CompressionOption

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

Aggregations

CompressionConfig (com.linkedin.r2.filter.CompressionConfig)4 RequestContext (com.linkedin.r2.message.RequestContext)4 URI (java.net.URI)4 Test (org.testng.annotations.Test)4 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)3 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 CompressionOption (com.linkedin.r2.filter.CompressionOption)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)2 Executor (java.util.concurrent.Executor)2 FutureCallback (com.linkedin.common.callback.FutureCallback)1 ByteString (com.linkedin.data.ByteString)1 PartialReader (com.linkedin.r2.filter.compression.streaming.PartialReader)1 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)1 CompositeWriter (com.linkedin.r2.message.stream.entitystream.CompositeWriter)1 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)1 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)1 Map (java.util.Map)1