Search in sources :

Example 1 with ALLOW

use of io.servicetalk.http.api.HttpHeaderNames.ALLOW 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 2 with ALLOW

use of io.servicetalk.http.api.HttpHeaderNames.ALLOW 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)2 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 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 RESP_STREAMING_SERIALIZER (io.servicetalk.examples.http.serialization.protobuf.SerializerUtils.RESP_STREAMING_SERIALIZER)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1