Search in sources :

Example 1 with RequestMessage

use of io.servicetalk.examples.http.serialization.protobuf.ExampleProtos.RequestMessage 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)

Aggregations

BlockingIterable (io.servicetalk.concurrent.BlockingIterable)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 RESP_STREAMING_SERIALIZER (io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_STREAMING_SERIALIZER)1 ALLOW (io.servicetalk.http.api.HttpHeaderNames.ALLOW)1 HttpPayloadWriter (io.servicetalk.http.api.HttpPayloadWriter)1 POST (io.servicetalk.http.api.HttpRequestMethod.POST)1 CREATED (io.servicetalk.http.api.HttpResponseStatus.CREATED)1 METHOD_NOT_ALLOWED (io.servicetalk.http.api.HttpResponseStatus.METHOD_NOT_ALLOWED)1 NOT_FOUND (io.servicetalk.http.api.HttpResponseStatus.NOT_FOUND)1 HttpServers (io.servicetalk.http.netty.HttpServers)1