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