Search in sources :

Example 1 with ContentCodec

use of io.servicetalk.encoding.api.ContentCodec in project servicetalk by apple.

the class HeaderUtils method identifyContentEncodingOrNullIfIdentity.

/**
 * Attempts to identify the {@link ContentCodec} from a name, as found in the {@code 'Content-Encoding'}
 * header of a request or a response.
 * If the name can not be matched to any of the supported encodings on this endpoint, then
 * a {@link UnsupportedContentEncodingException} is thrown.
 * If the matched encoding is {@link Identity#identity()} then this returns {@code null}.
 * @param headers The headers to read the encoding name from
 * @param allowedEncodings The supported encodings for this endpoint
 * @return The {@link ContentCodec} that matches the name or null if matches to identity
 * @deprecated Will be removed along with {@link ContentCodec}.
 */
@Nullable
@Deprecated
static ContentCodec identifyContentEncodingOrNullIfIdentity(final HttpHeaders headers, final List<ContentCodec> allowedEncodings) {
    final CharSequence encoding = headers.get(CONTENT_ENCODING);
    if (encoding == null) {
        return null;
    }
    ContentCodec enc = encodingFor(allowedEncodings, encoding);
    if (enc == null) {
        throw new UnsupportedContentEncodingException(encoding.toString());
    }
    return identity().equals(enc) ? null : enc;
}
Also used : ContentCodec(io.servicetalk.encoding.api.ContentCodec) Nullable(javax.annotation.Nullable)

Example 2 with ContentCodec

use of io.servicetalk.encoding.api.ContentCodec in project servicetalk by apple.

the class ProtoBufSerializationProviderBuilder method build0.

@SuppressWarnings("unchecked")
private void build0() {
    for (Map.Entry<Class<? extends MessageLite>, Parser<? extends MessageLite>> entry : types.entrySet()) {
        Class<MessageLite> messageType = (Class<MessageLite>) entry.getKey();
        Parser<MessageLite> parser = (Parser<MessageLite>) entry.getValue();
        Map<ContentCodec, HttpSerializer> serializersForType = new HashMap<>();
        Map<ContentCodec, HttpDeserializer> deserializersForType = new HashMap<>();
        for (ContentCodec codec : supportedCodings) {
            DefaultSerializer serializer = new DefaultSerializer(new ProtoBufSerializationProvider<>(messageType, codec, parser));
            HttpSerializer<MessageLite> httpSerializer = new ProtoHttpSerializer<>(serializer, codec, messageType);
            serializersForType.put(codec, httpSerializer);
            deserializersForType.put(codec, new HttpDeserializer<MessageLite>() {

                @Override
                public MessageLite deserialize(final HttpHeaders headers, final Buffer payload) {
                    return serializer.deserializeAggregatedSingle(payload, messageType);
                }

                @Override
                public BlockingIterable<MessageLite> deserialize(final HttpHeaders headers, final BlockingIterable<Buffer> payload) {
                    return serializer.deserialize(payload, messageType);
                }

                @Override
                public Publisher<MessageLite> deserialize(final HttpHeaders headers, final Publisher<Buffer> payload) {
                    return serializer.deserialize(payload, messageType);
                }
            });
        }
        serializers.put(messageType, serializersForType);
        deserializers.put(messageType, deserializersForType);
    }
}
Also used : HttpHeaders(io.servicetalk.http.api.HttpHeaders) HashMap(java.util.HashMap) DefaultSerializer(io.servicetalk.serialization.api.DefaultSerializer) HttpDeserializer(io.servicetalk.http.api.HttpDeserializer) BlockingIterable(io.servicetalk.concurrent.BlockingIterable) HttpSerializer(io.servicetalk.http.api.HttpSerializer) Buffer(io.servicetalk.buffer.api.Buffer) Publisher(io.servicetalk.concurrent.api.Publisher) MessageLite(com.google.protobuf.MessageLite) ContentCodec(io.servicetalk.encoding.api.ContentCodec) Parser(com.google.protobuf.Parser) HashMap(java.util.HashMap) Map(java.util.Map) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap)

Example 3 with ContentCodec

use of io.servicetalk.encoding.api.ContentCodec in project servicetalk by apple.

the class ContentCodingHttpRequesterFilter method encodePayloadContentIfAvailable.

private static void encodePayloadContentIfAvailable(final StreamingHttpRequest request, final BufferAllocator allocator) {
    ContentCodec coding = request.encoding();
    if (coding != null && !identity().equals(coding)) {
        addContentEncoding(request.headers(), coding.name());
        request.transformPayloadBody(pub -> coding.encode(pub, allocator));
    }
}
Also used : ContentCodec(io.servicetalk.encoding.api.ContentCodec)

Example 4 with ContentCodec

use of io.servicetalk.encoding.api.ContentCodec in project servicetalk by apple.

the class HeaderUtils method parseAcceptEncoding.

@Deprecated
private static List<ContentCodec> parseAcceptEncoding(@Nullable final CharSequence acceptEncodingHeaderValue, final List<ContentCodec> allowedEncodings) {
    if (acceptEncodingHeaderValue == null || acceptEncodingHeaderValue.length() == 0) {
        return NONE_CONTENT_ENCODING_SINGLETON;
    }
    List<ContentCodec> knownEncodings = new ArrayList<>();
    List<CharSequence> acceptEncodingValues = split(acceptEncodingHeaderValue, ',', true);
    for (CharSequence val : acceptEncodingValues) {
        ContentCodec enc = encodingFor(allowedEncodings, val);
        if (enc != null) {
            knownEncodings.add(enc);
        }
    }
    return knownEncodings;
}
Also used : ArrayList(java.util.ArrayList) ContentCodec(io.servicetalk.encoding.api.ContentCodec)

Example 5 with ContentCodec

use of io.servicetalk.encoding.api.ContentCodec in project servicetalk by apple.

the class NettyChannelContentCodecTest method testGzipIntegrationWithJDK.

@Test
void testGzipIntegrationWithJDK() throws Exception {
    ContentCodec codec = ContentCodings.gzipDefault();
    // Deflate with JDK and inflate with ST
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos);
    gzipOutputStream.write(INPUT.getBytes(US_ASCII));
    gzipOutputStream.close();
    Buffer decoded = codec.decode(DEFAULT_ALLOCATOR.wrap(baos.toByteArray()), DEFAULT_ALLOCATOR);
    assertThat(decoded.toString(US_ASCII), equalTo(INPUT));
    // Deflate with ST and inflate with JDK
    Buffer source = DEFAULT_ALLOCATOR.fromAscii(INPUT);
    Buffer encoded = codec.encode(source.duplicate(), DEFAULT_ALLOCATOR);
    byte[] inflated = new byte[1024 * 2];
    ByteArrayInputStream bais = new ByteArrayInputStream(encoded.array(), encoded.arrayOffset() + encoded.readerIndex(), encoded.readableBytes());
    GZIPInputStream gzipInputStream = new GZIPInputStream(bais);
    int read = gzipInputStream.read(inflated);
    gzipInputStream.close();
    assertThat(new String(inflated, 0, read, US_ASCII), equalTo(INPUT));
}
Also used : CompositeBuffer(io.servicetalk.buffer.api.CompositeBuffer) Buffer(io.servicetalk.buffer.api.Buffer) GZIPInputStream(java.util.zip.GZIPInputStream) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ContentCodec(io.servicetalk.encoding.api.ContentCodec) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ContentCodec (io.servicetalk.encoding.api.ContentCodec)6 Buffer (io.servicetalk.buffer.api.Buffer)2 MessageLite (com.google.protobuf.MessageLite)1 Parser (com.google.protobuf.Parser)1 CompositeBuffer (io.servicetalk.buffer.api.CompositeBuffer)1 BlockingIterable (io.servicetalk.concurrent.BlockingIterable)1 Publisher (io.servicetalk.concurrent.api.Publisher)1 HttpDeserializer (io.servicetalk.http.api.HttpDeserializer)1 HttpHeaders (io.servicetalk.http.api.HttpHeaders)1 HttpSerializer (io.servicetalk.http.api.HttpSerializer)1 DefaultSerializer (io.servicetalk.serialization.api.DefaultSerializer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 ArrayList (java.util.ArrayList)1 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 GZIPOutputStream (java.util.zip.GZIPOutputStream)1 Nullable (javax.annotation.Nullable)1