Search in sources :

Example 1 with BufferDecoder

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();
            });
        }
    };
}
Also used : EmptyBufferDecoderGroup(io.servicetalk.encoding.api.EmptyBufferDecoderGroup) Function(java.util.function.Function) ArrayList(java.util.ArrayList) CharSequences.regionMatches(io.servicetalk.buffer.api.CharSequences.regionMatches) HEAD(io.servicetalk.http.api.HttpRequestMethod.HEAD) Single.succeeded(io.servicetalk.concurrent.api.Single.succeeded) INFORMATIONAL_1XX(io.servicetalk.http.api.HttpResponseStatus.StatusClass.INFORMATIONAL_1XX) Objects.requireNonNull(java.util.Objects.requireNonNull) BufferDecoderGroup(io.servicetalk.encoding.api.BufferDecoderGroup) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Nullable(javax.annotation.Nullable) Iterator(java.util.Iterator) HeaderUtils.addContentEncoding(io.servicetalk.http.api.HeaderUtils.addContentEncoding) Single(io.servicetalk.concurrent.api.Single) NO_CONTENT(io.servicetalk.http.api.HttpResponseStatus.NO_CONTENT) CONNECT(io.servicetalk.http.api.HttpRequestMethod.CONNECT) HeaderUtils.negotiateAcceptedEncodingRaw(io.servicetalk.encoding.api.internal.HeaderUtils.negotiateAcceptedEncodingRaw) Identity.identityEncoder(io.servicetalk.encoding.api.Identity.identityEncoder) CharSequences(io.servicetalk.buffer.api.CharSequences) SUCCESSFUL_2XX(io.servicetalk.http.api.HttpResponseStatus.StatusClass.SUCCESSFUL_2XX) ACCEPT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.ACCEPT_ENCODING) List(java.util.List) NOT_MODIFIED(io.servicetalk.http.api.HttpResponseStatus.NOT_MODIFIED) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder) CONTENT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.CONTENT_ENCODING) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder)

Example 2 with BufferDecoder

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();
    });
}
Also used : ACCEPT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.ACCEPT_ENCODING) Iterator(java.util.Iterator) HeaderUtils.addContentEncoding(io.servicetalk.http.api.HeaderUtils.addContentEncoding) Single(io.servicetalk.concurrent.api.Single) Objects.requireNonNull(java.util.Objects.requireNonNull) BufferDecoderGroup(io.servicetalk.encoding.api.BufferDecoderGroup) ContentEncodingHttpServiceFilter.matchAndRemoveEncoding(io.servicetalk.http.api.ContentEncodingHttpServiceFilter.matchAndRemoveEncoding) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Identity.identityEncoder(io.servicetalk.encoding.api.Identity.identityEncoder) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder) CONTENT_ENCODING(io.servicetalk.http.api.HttpHeaderNames.CONTENT_ENCODING) BufferEncoder(io.servicetalk.encoding.api.BufferEncoder) Iterator(java.util.Iterator) BufferDecoder(io.servicetalk.encoding.api.BufferDecoder)

Aggregations

Single (io.servicetalk.concurrent.api.Single)2 BufferDecoder (io.servicetalk.encoding.api.BufferDecoder)2 BufferDecoderGroup (io.servicetalk.encoding.api.BufferDecoderGroup)2 BufferEncoder (io.servicetalk.encoding.api.BufferEncoder)2 Identity.identityEncoder (io.servicetalk.encoding.api.Identity.identityEncoder)2 HeaderUtils.addContentEncoding (io.servicetalk.http.api.HeaderUtils.addContentEncoding)2 ACCEPT_ENCODING (io.servicetalk.http.api.HttpHeaderNames.ACCEPT_ENCODING)2 CONTENT_ENCODING (io.servicetalk.http.api.HttpHeaderNames.CONTENT_ENCODING)2 Iterator (java.util.Iterator)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2 CharSequences (io.servicetalk.buffer.api.CharSequences)1 CharSequences.regionMatches (io.servicetalk.buffer.api.CharSequences.regionMatches)1 Single.succeeded (io.servicetalk.concurrent.api.Single.succeeded)1 EmptyBufferDecoderGroup (io.servicetalk.encoding.api.EmptyBufferDecoderGroup)1 HeaderUtils.negotiateAcceptedEncodingRaw (io.servicetalk.encoding.api.internal.HeaderUtils.negotiateAcceptedEncodingRaw)1 ContentEncodingHttpServiceFilter.matchAndRemoveEncoding (io.servicetalk.http.api.ContentEncodingHttpServiceFilter.matchAndRemoveEncoding)1 CONNECT (io.servicetalk.http.api.HttpRequestMethod.CONNECT)1 HEAD (io.servicetalk.http.api.HttpRequestMethod.HEAD)1 NOT_MODIFIED (io.servicetalk.http.api.HttpResponseStatus.NOT_MODIFIED)1 NO_CONTENT (io.servicetalk.http.api.HttpResponseStatus.NO_CONTENT)1