Search in sources :

Example 1 with HttpServerResponse

use of io.reactivex.netty.protocol.http.server.HttpServerResponse in project spring-framework by spring-projects.

the class RxNettyHttpHandlerAdapter method handle.

@Override
public Observable<Void> handle(HttpServerRequest<ByteBuf> nativeRequest, HttpServerResponse<ByteBuf> nativeResponse) {
    Channel channel = nativeResponse.unsafeNettyChannel();
    NettyDataBufferFactory bufferFactory = new NettyDataBufferFactory(channel.alloc());
    InetSocketAddress remoteAddress = (InetSocketAddress) channel.remoteAddress();
    RxNettyServerHttpRequest request = new RxNettyServerHttpRequest(nativeRequest, bufferFactory, remoteAddress);
    RxNettyServerHttpResponse response = new RxNettyServerHttpResponse(nativeResponse, bufferFactory);
    Publisher<Void> result = this.httpHandler.handle(request, response).otherwise(ex -> {
        logger.error("Could not complete request", ex);
        nativeResponse.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
        return Mono.empty();
    }).doOnSuccess(aVoid -> logger.debug("Successfully completed request"));
    return RxReactiveStreams.toObservable(result);
}
Also used : HttpServerResponse(io.reactivex.netty.protocol.http.server.HttpServerResponse) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory) Publisher(org.reactivestreams.Publisher) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Mono(reactor.core.publisher.Mono) InetSocketAddress(java.net.InetSocketAddress) Observable(rx.Observable) Channel(io.netty.channel.Channel) RxReactiveStreams(rx.RxReactiveStreams) ByteBuf(io.netty.buffer.ByteBuf) HttpServerRequest(io.reactivex.netty.protocol.http.server.HttpServerRequest) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) RequestHandler(io.reactivex.netty.protocol.http.server.RequestHandler) Assert(org.springframework.util.Assert) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) NettyDataBufferFactory(org.springframework.core.io.buffer.NettyDataBufferFactory)

Example 2 with HttpServerResponse

use of io.reactivex.netty.protocol.http.server.HttpServerResponse in project WSPerfLab by Netflix-Skunkworks.

the class TestRouteBasic method handle.

public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    long startTime = System.currentTimeMillis();
    List<String> _id = request.getQueryParameters().get("id");
    if (_id == null || _id.size() != 1) {
        return writeError(request, response, "Please provide a numerical 'id' value. It can be a random number (uuid).");
    }
    long id = Long.parseLong(String.valueOf(_id.get(0)));
    Observable<List<BackendResponse>> acd = getDataFromBackend("/mock.json?numItems=2&itemSize=50&delay=50&id=" + id).doOnError(Throwable::printStackTrace).<// Eclipse 20140224-0627 can't infer without this type hint even though the Java 8 compiler can
    List<BackendResponse>>flatMap(responseA -> {
        Observable<BackendResponse> responseC = getDataFromBackend("/mock.json?numItems=1&itemSize=5000&delay=80&id=" + responseA.getResponseKey());
        Observable<BackendResponse> responseD = getDataFromBackend("/mock.json?numItems=1&itemSize=1000&delay=1&id=" + responseA.getResponseKey());
        return Observable.zip(Observable.just(responseA), responseC, responseD, Arrays::asList);
    }).doOnError(Throwable::printStackTrace);
    Observable<List<BackendResponse>> be = getDataFromBackend("/mock.json?numItems=25&itemSize=30&delay=150&id=" + id).<// Eclipse 20140224-0627 can't infer without this type hint even though the Java 8 compiler can
    List<BackendResponse>>flatMap(responseB -> {
        Observable<BackendResponse> responseE = getDataFromBackend("/mock.json?numItems=100&itemSize=30&delay=4&id=" + responseB.getResponseKey());
        return Observable.zip(Observable.just(responseB), responseE, Arrays::asList);
    }).doOnError(Throwable::printStackTrace);
    return Observable.zip(acd, be, (_acd, _be) -> {
        BackendResponse responseA = _acd.get(0);
        BackendResponse responseB = _be.get(0);
        BackendResponse responseC = _acd.get(1);
        BackendResponse responseD = _acd.get(2);
        BackendResponse responseE = _be.get(1);
        return new BackendResponse[] { responseA, responseB, responseC, responseD, responseE };
    }).flatMap(backendResponses -> {
        try {
            ByteArrayOutputStream responseStream = ServiceResponseBuilder.buildTestAResponse(jsonFactory, backendResponses);
            // set response header
            response.getHeaders().addHeader("Content-Type", "application/json");
            // performance headers
            addResponseHeaders(response, startTime);
            int contentLength = responseStream.size();
            response.getHeaders().addHeader("Content-Length", contentLength);
            return response.writeBytesAndFlush(responseStream.toByteArray());
        } catch (Exception e) {
            return writeError(request, response, "Failed: " + e.getMessage());
        }
    }).doOnError(Throwable::printStackTrace);
}
Also used : HttpServerResponse(io.reactivex.netty.protocol.http.server.HttpServerResponse) Arrays(java.util.Arrays) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RxNetty(io.reactivex.netty.RxNetty) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) BackendResponse(perf.test.utils.BackendResponse) ServiceResponseBuilder(perf.test.utils.ServiceResponseBuilder) JsonFactory(org.codehaus.jackson.JsonFactory) Observable(rx.Observable) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) HttpClientResponse(io.reactivex.netty.protocol.http.client.HttpClientResponse) ByteBuf(io.netty.buffer.ByteBuf) LogLevel(io.netty.handler.logging.LogLevel) JsonParseException(perf.test.utils.JsonParseException) HttpServerRequest(io.reactivex.netty.protocol.http.server.HttpServerRequest) Map(java.util.Map) HttpClient(io.reactivex.netty.protocol.http.client.HttpClient) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) HttpClientRequest(io.reactivex.netty.protocol.http.client.HttpClientRequest) HttpClientBuilder(io.reactivex.netty.protocol.http.client.HttpClientBuilder) BackendResponse(perf.test.utils.BackendResponse) List(java.util.List) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Observable(rx.Observable) JsonParseException(perf.test.utils.JsonParseException)

Example 3 with HttpServerResponse

use of io.reactivex.netty.protocol.http.server.HttpServerResponse in project Hystrix by Netflix.

the class HystrixMetricsStreamHandlerTest method createServer.

// We try a few times in case we hit into used port.
private HttpServer<ByteBuf, ByteBuf> createServer() {
    Random random = new Random();
    Exception error = null;
    for (int i = 0; i < 3 && server == null; i++) {
        port = 10000 + random.nextInt(50000);
        try {
            return RxNetty.newHttpServerBuilder(port, new HystrixMetricsStreamHandler<ByteBuf, ByteBuf>(DEFAULT_HYSTRIX_PREFIX, DEFAULT_INTERVAL, new // Application handler
            RequestHandler<ByteBuf, ByteBuf>() {

                @Override
                public Observable<Void> handle(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
                    return Observable.empty();
                }
            })).build().start();
        } catch (Exception e) {
            error = e;
        }
    }
    throw new RuntimeException("Cannot initialize RxNetty server", error);
}
Also used : Random(java.util.Random) RequestHandler(io.reactivex.netty.protocol.http.server.RequestHandler) HttpServerRequest(io.reactivex.netty.protocol.http.server.HttpServerRequest) HttpServerResponse(io.reactivex.netty.protocol.http.server.HttpServerResponse) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)3 HttpServerRequest (io.reactivex.netty.protocol.http.server.HttpServerRequest)3 HttpServerResponse (io.reactivex.netty.protocol.http.server.HttpServerResponse)3 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)2 RequestHandler (io.reactivex.netty.protocol.http.server.RequestHandler)2 Observable (rx.Observable)2 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)1 Channel (io.netty.channel.Channel)1 LogLevel (io.netty.handler.logging.LogLevel)1 RxNetty (io.reactivex.netty.RxNetty)1 HttpClient (io.reactivex.netty.protocol.http.client.HttpClient)1 HttpClientBuilder (io.reactivex.netty.protocol.http.client.HttpClientBuilder)1 HttpClientRequest (io.reactivex.netty.protocol.http.client.HttpClientRequest)1 HttpClientResponse (io.reactivex.netty.protocol.http.client.HttpClientResponse)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Random (java.util.Random)1