Search in sources :

Example 11 with AcceptEncoding

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);
}
Also used : ByteString(com.linkedin.data.ByteString) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) ByteString(com.linkedin.data.ByteString)

Example 12 with AcceptEncoding

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);
}
Also used : StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CompositeWriter(com.linkedin.r2.message.stream.entitystream.CompositeWriter) PartialReader(com.linkedin.r2.filter.compression.streaming.PartialReader) StreamEncodingType(com.linkedin.r2.filter.compression.streaming.StreamEncodingType) EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) Callback(com.linkedin.common.callback.Callback) StreamingCompressor(com.linkedin.r2.filter.compression.streaming.StreamingCompressor) AcceptEncoding(com.linkedin.r2.filter.compression.streaming.AcceptEncoding)

Aggregations

Client (com.linkedin.r2.transport.common.Client)6 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)5 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)5 FutureCallback (com.linkedin.common.callback.FutureCallback)4 CompressionConfig (com.linkedin.r2.filter.CompressionConfig)4 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 DataProvider (org.testng.annotations.DataProvider)4 StreamEncodingType (com.linkedin.r2.filter.compression.streaming.StreamEncodingType)3 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)3 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)3 None (com.linkedin.common.util.None)2 ClientCompressionFilter (com.linkedin.r2.filter.compression.ClientCompressionFilter)2 ClientStreamCompressionFilter (com.linkedin.r2.filter.compression.ClientStreamCompressionFilter)2 EncodingType (com.linkedin.r2.filter.compression.EncodingType)2 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)2 StreamFilter (com.linkedin.r2.filter.message.stream.StreamFilter)2 StreamException (com.linkedin.r2.message.stream.StreamException)2 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)2 TransportClientFactory (com.linkedin.r2.transport.common.TransportClientFactory)2 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)2