use of io.servicetalk.encoding.api.BufferDecoder in project servicetalk by apple.
the class ContentEncodingHttpServiceFilter method create.
@Override
public StreamingHttpServiceFilter create(final StreamingHttpService service) {
return new StreamingHttpServiceFilter(service) {
@Override
public Single<StreamingHttpResponse> handle(final HttpServiceContext ctx, final StreamingHttpRequest request, final StreamingHttpResponseFactory responseFactory) {
return Single.defer(() -> {
final StreamingHttpRequest requestDecompressed;
Iterator<? extends CharSequence> contentEncodingItr = request.headers().valuesIterator(CONTENT_ENCODING);
final boolean hasContentEncoding = contentEncodingItr.hasNext();
if (hasContentEncoding) {
BufferDecoder decoder = matchAndRemoveEncoding(decompressors.decoders(), BufferDecoder::encodingName, contentEncodingItr, request.headers());
if (decoder == null) {
return succeeded(responseFactory.unsupportedMediaType()).shareContextOnSubscribe();
}
requestDecompressed = request.transformPayloadBody(pub -> decoder.streamingDecoder().deserialize(pub, ctx.executionContext().bufferAllocator()));
} else {
requestDecompressed = request;
}
return super.handle(ctx, requestDecompressed, responseFactory).map(response -> {
final CharSequence reqAcceptEncoding;
if (isPassThrough(request.method(), response) || (reqAcceptEncoding = request.headers().get(ACCEPT_ENCODING)) == null) {
return response;
}
BufferEncoder encoder = negotiateAcceptedEncodingRaw(reqAcceptEncoding, compressors, BufferEncoder::encodingName);
if (encoder == null || identityEncoder().equals(encoder)) {
return response;
}
addContentEncoding(response.headers(), encoder.encodingName());
return response.transformPayloadBody(bufPub -> encoder.streamingEncoder().serialize(bufPub, ctx.executionContext().bufferAllocator()));
}).shareContextOnSubscribe();
});
}
};
}
use of io.servicetalk.encoding.api.BufferDecoder in project servicetalk by apple.
the class ContentEncodingHttpRequesterFilter method applyEncodingAndDecoding.
private Single<StreamingHttpResponse> applyEncodingAndDecoding(final StreamingHttpRequester delegate, final StreamingHttpRequest request) {
return Single.defer(() -> {
boolean decompressResponse = false;
CharSequence encodings = decompressors.advertisedMessageEncoding();
if (encodings != null && !request.headers().contains(ACCEPT_ENCODING)) {
request.headers().set(ACCEPT_ENCODING, encodings);
decompressResponse = true;
}
BufferEncoder encoder = request.contentEncoding();
final StreamingHttpRequest encodedRequest;
if (encoder != null && !identityEncoder().equals(encoder)) {
addContentEncoding(request.headers(), encoder.encodingName());
encodedRequest = request.transformPayloadBody(pub -> encoder.streamingEncoder().serialize(pub, delegate.executionContext().bufferAllocator()));
} else {
encodedRequest = request;
}
Single<StreamingHttpResponse> respSingle = delegate.request(encodedRequest);
return (decompressResponse ? respSingle.map(response -> {
Iterator<? extends CharSequence> contentEncodingItr = response.headers().valuesIterator(CONTENT_ENCODING);
final boolean hasContentEncoding = contentEncodingItr.hasNext();
if (!hasContentEncoding) {
return response;
}
BufferDecoder decoder = matchAndRemoveEncoding(decompressors.decoders(), BufferDecoder::encodingName, contentEncodingItr, response.headers());
if (decoder == null) {
throw new UnsupportedContentEncodingException(response.headers().get(CONTENT_ENCODING, "<null>").toString());
}
return response.transformPayloadBody(pub -> decoder.streamingDecoder().deserialize(pub, delegate.executionContext().bufferAllocator()));
}) : respSingle).shareContextOnSubscribe();
});
}
Aggregations