Search in sources :

Example 1 with SerializationException

use of io.servicetalk.serialization.api.SerializationException in project servicetalk by apple.

the class FormUrlEncodedHttpSerializer method serialize.

private Buffer serialize(@Nullable final Map<String, List<String>> parameters, final BufferAllocator allocator, final boolean isContinuation) {
    if (parameters == null) {
        return EMPTY_BUFFER;
    }
    final Buffer buffer = allocator.newBuffer();
    // Null values may be omitted
    // https://tools.ietf.org/html/rfc1866#section-8.2
    parameters.forEach((key, values) -> {
        if (key == null || key.isEmpty()) {
            throw new SerializationException("Null or empty keys are not supported " + "for x-www-form-urlencoded params");
        }
        if (values == null) {
            return;
        }
        values.forEach(value -> {
            if (value == null) {
                return;
            }
            if (buffer.writerIndex() != 0 || isContinuation) {
                buffer.writeBytes("&".getBytes(charset));
            }
            buffer.writeBytes(urlEncode(key).getBytes(charset));
            buffer.writeBytes("=".getBytes(charset));
            buffer.writeBytes(urlEncode(value).getBytes(charset));
        });
    });
    return buffer;
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) SerializationException(io.servicetalk.serialization.api.SerializationException)

Example 2 with SerializationException

use of io.servicetalk.serialization.api.SerializationException in project servicetalk by apple.

the class ProtobufSerializationProvider method reflectionParserFor.

@SuppressWarnings("unchecked")
static <T> Parser<T> reflectionParserFor(final Class<T> classToDeSerialize) {
    requireMessageLite(classToDeSerialize);
    try {
        // using reflection to get the ubiquitous static PARSER field that's been there since at least proto 2.5
        // probably before but that's the oldest version that we currently still need to support with this
        Field field = classToDeSerialize.getDeclaredField("PARSER");
        field.setAccessible(true);
        Object object = field.get(null);
        if (object instanceof Parser) {
            return (Parser<T>) object;
        }
        throw new SerializationException("'PARSER' field from " + classToDeSerialize.getName() + " was not an instance of " + Parser.class.getName());
    } catch (NoSuchFieldException e) {
        throw new SerializationException("Could not find static field 'PARSER' from " + classToDeSerialize.getName());
    } catch (IllegalAccessException e) {
        throw new SerializationException("'PARSER' field on " + classToDeSerialize.getName() + " was not publicly accessible");
    }
}
Also used : Field(java.lang.reflect.Field) SerializationException(io.servicetalk.serialization.api.SerializationException) Parser(com.google.protobuf.Parser)

Example 3 with SerializationException

use of io.servicetalk.serialization.api.SerializationException in project servicetalk by apple.

the class JacksonSerializationProviderTest method deserializeIncompleteBufferAsAggregated.

@Test
void deserializeIncompleteBufferAsAggregated() {
    TestPojo expected = new TestPojo(true, (byte) -2, (short) -3, 'a', 2, 5, 3.2f, -8.5, null, new String[] { "bar" }, null);
    final Buffer buffer = serializePojo(expected);
    final StreamingDeserializer<TestPojo> deSerializer = serializationProvider.getDeserializer(TestPojo.class);
    deSerializer.deserialize(buffer.readBytes(buffer.readableBytes() - 1));
    try {
        deSerializer.close();
        fail();
    } catch (SerializationException e) {
    // expected
    }
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) EmptyBuffer(io.servicetalk.buffer.api.EmptyBuffer) SerializationException(io.servicetalk.serialization.api.SerializationException) Test(org.junit.jupiter.api.Test)

Example 4 with SerializationException

use of io.servicetalk.serialization.api.SerializationException in project servicetalk by apple.

the class JacksonSerializationProviderTest method deserializeInvalidData.

@Test
void deserializeInvalidData() {
    TestPojo expected = new TestPojo(true, (byte) -2, (short) -3, 'a', 2, 5, 3.2f, -8.5, null, new String[] { "bar" }, null);
    final Buffer serialized = serializePojo(expected);
    serialized.setByte(serialized.writerIndex() - 1, serialized.getByte(serialized.writerIndex() - 1) + 1);
    final StreamingDeserializer<TestPojo> deserializer = serializationProvider.getDeserializer(TestPojo.class);
    try {
        deserializer.deserialize(serialized);
        fail();
    } catch (SerializationException e) {
        assertThat("Unexpected exception", e.getCause(), instanceOf(JsonParseException.class));
    }
    assertThat("Unexpected data remaining in deserializer", deserializer.hasData(), is(true));
}
Also used : Buffer(io.servicetalk.buffer.api.Buffer) EmptyBuffer(io.servicetalk.buffer.api.EmptyBuffer) SerializationException(io.servicetalk.serialization.api.SerializationException) Test(org.junit.jupiter.api.Test)

Example 5 with SerializationException

use of io.servicetalk.serialization.api.SerializationException in project servicetalk by apple.

the class HeaderUtilsTest method checkContentTypeCases.

@Test
void checkContentTypeCases() {
    final String invalidContentType = "invalid";
    final Predicate<HttpHeaders> jsonContentTypeValidator = headers -> headers.contains(CONTENT_TYPE, APPLICATION_JSON);
    checkContentType(headersWithContentType(APPLICATION_JSON), jsonContentTypeValidator);
    SerializationException e = assertThrows(SerializationException.class, () -> checkContentType(headersWithContentType(of(invalidContentType)), jsonContentTypeValidator));
    assertThat(e.getMessage(), containsString(invalidContentType));
}
Also used : APPLICATION_JSON(io.servicetalk.http.api.HttpHeaderValues.APPLICATION_JSON) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) SerializationException(io.servicetalk.serialization.api.SerializationException) CONTENT_TYPE(io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE) HeaderUtils.checkContentType(io.servicetalk.http.api.HeaderUtils.checkContentType) HeaderUtils.isTransferEncodingChunked(io.servicetalk.http.api.HeaderUtils.isTransferEncodingChunked) HeaderUtils.pathMatches(io.servicetalk.http.api.HeaderUtils.pathMatches) Charset(java.nio.charset.Charset) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) ORIGIN(io.servicetalk.http.api.HttpHeaderNames.ORIGIN) APPLICATION_X_WWW_FORM_URLENCODED(io.servicetalk.http.api.HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) HeaderUtils.isTchar(io.servicetalk.http.api.HeaderUtils.isTchar) CHUNKED(io.servicetalk.http.api.HttpHeaderValues.CHUNKED) Predicate(java.util.function.Predicate) UTF_8(java.nio.charset.StandardCharsets.UTF_8) UTF_16(java.nio.charset.StandardCharsets.UTF_16) TRANSFER_ENCODING(io.servicetalk.http.api.HttpHeaderNames.TRANSFER_ENCODING) AsciiString.of(io.netty.util.AsciiString.of) CharsetDecoder(java.nio.charset.CharsetDecoder) CONTENT_LENGTH(io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH) Test(org.junit.jupiter.api.Test) US_ASCII(java.nio.charset.StandardCharsets.US_ASCII) CharsetEncoder(java.nio.charset.CharsetEncoder) TEXT_PLAIN_UTF_8(io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN_UTF_8) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) INSTANCE(io.servicetalk.http.api.EmptyHttpHeaders.INSTANCE) APPLICATION_X_WWW_FORM_URLENCODED_UTF_8(io.servicetalk.http.api.HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED_UTF_8) TEXT_PLAIN(io.servicetalk.http.api.HttpHeaderValues.TEXT_PLAIN) Matchers.containsString(org.hamcrest.Matchers.containsString) DEFAULT_DEBUG_HEADER_FILTER(io.servicetalk.http.api.HeaderUtils.DEFAULT_DEBUG_HEADER_FILTER) SerializationException(io.servicetalk.serialization.api.SerializationException) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Aggregations

SerializationException (io.servicetalk.serialization.api.SerializationException)7 Test (org.junit.jupiter.api.Test)5 Buffer (io.servicetalk.buffer.api.Buffer)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 EmptyBuffer (io.servicetalk.buffer.api.EmptyBuffer)2 Parser (com.google.protobuf.Parser)1 AsciiString.of (io.netty.util.AsciiString.of)1 INSTANCE (io.servicetalk.http.api.EmptyHttpHeaders.INSTANCE)1 DEFAULT_DEBUG_HEADER_FILTER (io.servicetalk.http.api.HeaderUtils.DEFAULT_DEBUG_HEADER_FILTER)1 HeaderUtils.checkContentType (io.servicetalk.http.api.HeaderUtils.checkContentType)1 HeaderUtils.isTchar (io.servicetalk.http.api.HeaderUtils.isTchar)1 HeaderUtils.isTransferEncodingChunked (io.servicetalk.http.api.HeaderUtils.isTransferEncodingChunked)1 HeaderUtils.pathMatches (io.servicetalk.http.api.HeaderUtils.pathMatches)1 CONTENT_LENGTH (io.servicetalk.http.api.HttpHeaderNames.CONTENT_LENGTH)1 CONTENT_TYPE (io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE)1 ORIGIN (io.servicetalk.http.api.HttpHeaderNames.ORIGIN)1 TRANSFER_ENCODING (io.servicetalk.http.api.HttpHeaderNames.TRANSFER_ENCODING)1 APPLICATION_JSON (io.servicetalk.http.api.HttpHeaderValues.APPLICATION_JSON)1 APPLICATION_X_WWW_FORM_URLENCODED (io.servicetalk.http.api.HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED)1 APPLICATION_X_WWW_FORM_URLENCODED_UTF_8 (io.servicetalk.http.api.HttpHeaderValues.APPLICATION_X_WWW_FORM_URLENCODED_UTF_8)1