use of com.linkedin.r2.filter.compression.streaming.PartialReader in project rest.li by linkedin.
the class ClientStreamCompressionFilter method onStreamRequest.
/**
* Optionally compresses outgoing Stream requests
*/
public void onStreamRequest(StreamRequest req, final RequestContext requestContext, final Map<String, String> wireAttrs, final NextFilter<StreamRequest, StreamResponse> nextFilter) {
// Set accepted encoding for compressed response
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);
}
if (_requestContentEncoding != StreamEncodingType.IDENTITY) {
final StreamRequest request = req;
final StreamingCompressor compressor = _requestContentEncoding.getCompressor(_executor);
CompressionOption option = (CompressionOption) requestContext.getLocalAttr(R2Constants.REQUEST_COMPRESSION_OVERRIDE);
if (option == null || option != CompressionOption.FORCE_OFF) {
final int threshold = option == CompressionOption.FORCE_ON ? 0 : _requestCompressionConfig.getCompressionThreshold();
PartialReader reader = new PartialReader(threshold, new Callback<EntityStream[]>() {
@Override
public void onError(Throwable ex) {
nextFilter.onError(ex, requestContext, wireAttrs);
}
@Override
public void onSuccess(EntityStream[] result) {
if (result.length == 1) {
StreamRequest uncompressedRequest = request.builder().build(result[0]);
nextFilter.onRequest(uncompressedRequest, requestContext, wireAttrs);
} else {
StreamRequestBuilder builder = request.builder();
EntityStream compressedStream = compressor.deflate(EntityStreams.newEntityStream(new CompositeWriter(result)));
Map<String, String> headers = stripHeaders(builder.getHeaders(), HttpConstants.CONTENT_LENGTH);
StreamRequest compressedRequest = builder.setHeaders(headers).setHeader(HttpConstants.CONTENT_ENCODING, compressor.getContentEncodingName()).build(compressedStream);
nextFilter.onRequest(compressedRequest, requestContext, wireAttrs);
}
}
});
req.getEntityStream().setReader(reader);
return;
}
}
nextFilter.onRequest(req, requestContext, wireAttrs);
}
use of com.linkedin.r2.filter.compression.streaming.PartialReader 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