Search in sources :

Example 1 with SerializationException

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

the class VarIntLengthStreamingSerializer method getVarInt.

static int getVarInt(Buffer buffer) {
    final int maxBytesToInspect = min(MAX_LENGTH_BYTES, buffer.readableBytes());
    final int readerIndex = buffer.readerIndex();
    int i = 0;
    for (; i < maxBytesToInspect; ++i) {
        final byte b = buffer.getByte(i + readerIndex);
        if ((b & 0x80) == 0) {
            if (i == 0) {
                return buffer.readByte();
            } else if (i == 1) {
                final short varInt = buffer.readShort();
                return ((varInt & 0x7F) << 7) | ((varInt & 0x7F00) >> 8);
            } else if (i == 2) {
                final int varInt = buffer.readMedium();
                return ((varInt & 0x7F) << 14) | ((varInt & 0x7F00) >> 1) | ((varInt & 0x7F0000) >> 16);
            } else if (i == 3) {
                final int varInt = buffer.readInt();
                return ((varInt & 0x7F) << 21) | ((varInt & 0x7F00) << 6) | ((varInt & 0x7F0000) >> 9) | ((varInt & 0x7F000000) >> 24);
            } else {
                assert i == 4;
                final byte b0 = buffer.readByte();
                final int varInt = buffer.readInt();
                if ((varInt & 0xF8) != 0) {
                    throw new SerializationException("java int cannot support larger than " + Integer.MAX_VALUE);
                }
                return ((varInt & 0x7) << 28) | ((varInt & 0x7F00) << 13) | ((varInt & 0x7F0000) >> 2) | ((varInt & 0x7F000000) >> 17) | (b0 & 0x7F);
            }
        }
    }
    if (i == MAX_LENGTH_BYTES) {
        throw new SerializationException("java int cannot support more than " + MAX_LENGTH_BYTES + " bytes of VarInt");
    }
    return -1;
}
Also used : SerializationException(io.servicetalk.serializer.api.SerializationException)

Example 2 with SerializationException

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

the class HttpSerializerErrorTest method blockingStreamingDeserializationHeaderMismatch.

@ParameterizedTest
@MethodSource("executors")
void blockingStreamingDeserializationHeaderMismatch(HttpTestExecutionStrategy serverStrategy) throws Exception {
    serverExecutionStrategy = serverStrategy.executorSupplier.get();
    HttpStreamingSerializerDeserializer<String> streamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(String.class));
    try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenBlockingStreamingAndAwait((ctx, request, responseFactory) -> {
        try {
            BlockingIterable<String> reqIterable = request.payloadBody(streamingSerializer);
            try (HttpPayloadWriter<String> stream = responseFactory.sendMetaData(streamingSerializer)) {
                for (String reqChunk : reqIterable) {
                    stream.write(reqChunk);
                }
            }
        } catch (SerializationException e) {
            responseFactory.status(BAD_REQUEST);
            responseFactory.sendMetaData().close();
        }
    });
        BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
        HttpResponse resp = clt.request(clt.post("/foo").payloadBody(clt.executionContext().bufferAllocator().fromAscii("hello")));
        assertEquals(BAD_REQUEST, resp.status());
    }
}
Also used : SerializationException(io.servicetalk.serializer.api.SerializationException) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with SerializationException

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

the class HttpSerializerErrorTest method streamingDeserializationHeaderMismatch.

@ParameterizedTest
@MethodSource("executors")
void streamingDeserializationHeaderMismatch(HttpTestExecutionStrategy serverStrategy) throws Exception {
    serverExecutionStrategy = serverStrategy.executorSupplier.get();
    HttpStreamingSerializerDeserializer<String> streamingSerializer = jsonStreamingSerializer(JACKSON.streamingSerializerDeserializer(String.class));
    try (ServerContext srv = HttpServers.forAddress(localAddress(0)).executionStrategy(serverExecutionStrategy).listenStreamingAndAwait((ctx, request, responseFactory) -> {
        try {
            return succeeded(responseFactory.ok().payloadBody(request.payloadBody(streamingSerializer), streamingSerializer));
        } catch (SerializationException e) {
            return succeeded(responseFactory.badRequest());
        }
    });
        BlockingHttpClient clt = HttpClients.forSingleAddress(serverHostAndPort(srv)).buildBlocking()) {
        HttpResponse resp = clt.request(clt.post("/foo").payloadBody(clt.executionContext().bufferAllocator().fromAscii("hello")));
        assertEquals(BAD_REQUEST, resp.status());
    }
}
Also used : SerializationException(io.servicetalk.serializer.api.SerializationException) ServerContext(io.servicetalk.transport.api.ServerContext) BlockingHttpClient(io.servicetalk.http.api.BlockingHttpClient) HttpResponse(io.servicetalk.http.api.HttpResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with SerializationException

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

the class ProtobufSerializer method serialize.

@Override
public void serialize(final T toSerialize, final BufferAllocator allocator, final Buffer buffer) {
    final int writerIdx = buffer.writerIndex();
    final int writableBytes = buffer.writableBytes();
    final CodedOutputStream out = buffer.hasArray() ? newInstance(buffer.array(), buffer.arrayOffset() + writerIdx, writableBytes) : newInstance(buffer.toNioBuffer(writerIdx, writableBytes));
    try {
        toSerialize.writeTo(out);
    } catch (IOException e) {
        throw new SerializationException(e);
    }
    // Forward write index of our buffer
    buffer.writerIndex(writerIdx + toSerialize.getSerializedSize());
}
Also used : SerializationException(io.servicetalk.serializer.api.SerializationException) CodedOutputStream(com.google.protobuf.CodedOutputStream) IOException(java.io.IOException)

Example 5 with SerializationException

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

the class ProtobufSerializer method deserialize.

@Override
public T deserialize(final Buffer serializedData, final BufferAllocator allocator) {
    try {
        final CodedInputStream in;
        if (serializedData.nioBufferCount() == 1) {
            in = CodedInputStream.newInstance(serializedData.toNioBuffer());
        } else {
            // Aggregated payload body may consist of multiple Buffers. In this case,
            // CompositeBuffer.toNioBuffer(idx, length) may return a single ByteBuffer (when requested
            // length < components[0].length) or create a new ByteBuffer and copy multiple components
            // into it. Later, proto parser will copy data from this temporary ByteBuffer again.
            // To avoid unnecessary copying, we use newCodedInputStream(buffers, lengthOfData).
            final ByteBuffer[] buffers = serializedData.toNioBuffers();
            in = buffers.length == 1 ? CodedInputStream.newInstance(buffers[0]) : newCodedInputStream(buffers, serializedData.readableBytes());
        }
        T result = parser.parseFrom(in);
        serializedData.skipBytes(result.getSerializedSize());
        return result;
    } catch (InvalidProtocolBufferException e) {
        throw new SerializationException(e);
    }
}
Also used : SerializationException(io.servicetalk.serializer.api.SerializationException) CodedInputStream(com.google.protobuf.CodedInputStream) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

SerializationException (io.servicetalk.serializer.api.SerializationException)7 BlockingHttpClient (io.servicetalk.http.api.BlockingHttpClient)2 HttpResponse (io.servicetalk.http.api.HttpResponse)2 ServerContext (io.servicetalk.transport.api.ServerContext)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 CodedInputStream (com.google.protobuf.CodedInputStream)1 CodedOutputStream (com.google.protobuf.CodedOutputStream)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 Buffer (io.servicetalk.buffer.api.Buffer)1 Http2Exception (io.servicetalk.http.api.Http2Exception)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 CancellationException (java.util.concurrent.CancellationException)1 TimeoutException (java.util.concurrent.TimeoutException)1