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;
}
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);
}
}
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));
}
}
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;
}
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));
}
Aggregations