Search in sources :

Example 1 with BlockingIterable

use of io.servicetalk.concurrent.BlockingIterable 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 2 with BlockingIterable

use of io.servicetalk.concurrent.BlockingIterable in project servicetalk by apple.

the class BlockingProtobufStreamingServer method main.

public static void main(String[] args) throws Exception {
    HttpServers.forPort(8080).listenBlockingStreamingAndAwait((ctx, request, response) -> {
        if (!"/protobuf".equals(request.requestTarget())) {
            response.status(NOT_FOUND).sendMetaData().close();
        } else if (!POST.equals(request.method())) {
            response.status(METHOD_NOT_ALLOWED).addHeader(ALLOW, POST.name()).sendMetaData().close();
        } else {
            BlockingIterable<RequestMessage> values = request.payloadBody(REQ_STREAMING_SERIALIZER);
            response.status(CREATED);
            try (HttpPayloadWriter<ResponseMessage> writer = response.sendMetaData(RESP_STREAMING_SERIALIZER)) {
                for (RequestMessage req : values) {
                    writer.write(ResponseMessage.newBuilder().setLength(req.getMessage().length()).build());
                }
            }
        }
    }).awaitShutdown();
}
Also used : BlockingIterable(io.servicetalk.concurrent.BlockingIterable) CREATED(io.servicetalk.http.api.HttpResponseStatus.CREATED) HttpPayloadWriter(io.servicetalk.http.api.HttpPayloadWriter) NOT_FOUND(io.servicetalk.http.api.HttpResponseStatus.NOT_FOUND) RESP_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_STREAMING_SERIALIZER) POST(io.servicetalk.http.api.HttpRequestMethod.POST) METHOD_NOT_ALLOWED(io.servicetalk.http.api.HttpResponseStatus.METHOD_NOT_ALLOWED) REQ_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_STREAMING_SERIALIZER) ResponseMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage) ALLOW(io.servicetalk.http.api.HttpHeaderNames.ALLOW) RequestMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage) HttpServers(io.servicetalk.http.netty.HttpServers) RequestMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage) ResponseMessage(io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage)

Example 3 with BlockingIterable

use of io.servicetalk.concurrent.BlockingIterable in project servicetalk by apple.

the class BlockingPojoStreamingServer method main.

public static void main(String[] args) throws Exception {
    HttpServers.forPort(8080).listenBlockingStreamingAndAwait((ctx, request, response) -> {
        if (!"/pojos".equals(request.requestTarget())) {
            response.status(NOT_FOUND).sendMetaData().close();
        } else if (!POST.equals(request.method())) {
            response.status(METHOD_NOT_ALLOWED).addHeader(ALLOW, POST.name()).sendMetaData().close();
        } else {
            BlockingIterable<CreatePojoRequest> values = request.payloadBody(REQ_STREAMING_SERIALIZER);
            response.status(CREATED);
            try (HttpPayloadWriter<PojoResponse> writer = response.sendMetaData(RESP_STREAMING_SERIALIZER)) {
                for (CreatePojoRequest req : values) {
                    writer.write(new PojoResponse(ID_GENERATOR.getAndIncrement(), req.getValue()));
                }
            }
        }
    }).awaitShutdown();
}
Also used : CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) RESP_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.RESP_STREAMING_SERIALIZER) PojoResponse(io.servicetalk.examples.http.serialization.json.PojoResponse) POST(io.servicetalk.http.api.HttpRequestMethod.POST) METHOD_NOT_ALLOWED(io.servicetalk.http.api.HttpResponseStatus.METHOD_NOT_ALLOWED) BlockingIterable(io.servicetalk.concurrent.BlockingIterable) CREATED(io.servicetalk.http.api.HttpResponseStatus.CREATED) REQ_STREAMING_SERIALIZER(io.servicetalk.examples.http.serialization.json.SerializerUtils.REQ_STREAMING_SERIALIZER) HttpPayloadWriter(io.servicetalk.http.api.HttpPayloadWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) NOT_FOUND(io.servicetalk.http.api.HttpResponseStatus.NOT_FOUND) ALLOW(io.servicetalk.http.api.HttpHeaderNames.ALLOW) HttpServers(io.servicetalk.http.netty.HttpServers) CreatePojoRequest(io.servicetalk.examples.http.serialization.json.CreatePojoRequest) PojoResponse(io.servicetalk.examples.http.serialization.json.PojoResponse)

Aggregations

BlockingIterable (io.servicetalk.concurrent.BlockingIterable)3 ALLOW (io.servicetalk.http.api.HttpHeaderNames.ALLOW)2 HttpPayloadWriter (io.servicetalk.http.api.HttpPayloadWriter)2 POST (io.servicetalk.http.api.HttpRequestMethod.POST)2 CREATED (io.servicetalk.http.api.HttpResponseStatus.CREATED)2 METHOD_NOT_ALLOWED (io.servicetalk.http.api.HttpResponseStatus.METHOD_NOT_ALLOWED)2 NOT_FOUND (io.servicetalk.http.api.HttpResponseStatus.NOT_FOUND)2 HttpServers (io.servicetalk.http.netty.HttpServers)2 MessageLite (com.google.protobuf.MessageLite)1 Parser (com.google.protobuf.Parser)1 Buffer (io.servicetalk.buffer.api.Buffer)1 Publisher (io.servicetalk.concurrent.api.Publisher)1 ContentCodec (io.servicetalk.encoding.api.ContentCodec)1 CreatePojoRequest (io.servicetalk.examples.http.serialization.json.CreatePojoRequest)1 PojoResponse (io.servicetalk.examples.http.serialization.json.PojoResponse)1 REQ_STREAMING_SERIALIZER (io.servicetalk.examples.http.serialization.json.SerializerUtils.REQ_STREAMING_SERIALIZER)1 RESP_STREAMING_SERIALIZER (io.servicetalk.examples.http.serialization.json.SerializerUtils.RESP_STREAMING_SERIALIZER)1 RequestMessage (io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage)1 ResponseMessage (io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.ResponseMessage)1 REQ_STREAMING_SERIALIZER (io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.REQ_STREAMING_SERIALIZER)1