Search in sources :

Example 1 with HttpClientRequest

use of mantis.io.reactivex.netty.protocol.http.client.HttpClientRequest in project mantis by Netflix.

the class HttpUtility method getGetResponse.

static Observable<String> getGetResponse(String host, int port, String uri) {
    return new CompositeHttpClientBuilder<ByteBuf, ByteBuf>().appendPipelineConfigurator(new PipelineConfigurator<HttpClientResponse<ByteBuf>, HttpClientRequest<ByteBuf>>() {

        @Override
        public void configureNewPipeline(ChannelPipeline pipeline) {
            pipeline.addLast("introspecting-handler", new ChannelDuplexHandler() {

                private String uri = "<undefined>";

                @Override
                public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                    if (msg instanceof HttpRequest) {
                        HttpRequest request = (HttpRequest) msg;
                        uri = request.getUri();
                        logger.info("Sending request on channel id: " + ctx.channel().toString() + ", request URI: " + uri);
                    }
                    super.write(ctx, msg, promise);
                }

                @Override
                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                    if (msg instanceof HttpResponse) {
                        logger.info("Received response on channel id: " + ctx.channel().toString() + ", request URI: " + uri);
                    }
                    super.channelRead(ctx, msg);
                }
            });
            try {
                // Ten megabytes
                int maxContentLength = 10 * 1024 * 1024;
                pipeline.replace(HttpObjectAggregator.class, "http-object-aggregator", new HttpObjectAggregator(maxContentLength));
            } catch (NoSuchElementException ex) {
                logger.error("HttpObjectAggregator did not exist in this pipeline. Error: {}", ex.getMessage(), ex);
            } catch (IllegalArgumentException ex) {
                logger.error("ChannelHandler named http-object-aggregator already existed in this" + " pipeline. Error: {}", ex.getMessage(), ex);
            } catch (Throwable t) {
                logger.error("Unknown error adding HttpObjectAggregator to Master Client " + "Pipeline. Error: {}", t.getMessage(), t);
            }
        }
    }).build().submit(new RxClient.ServerInfo(host, port), HttpClientRequest.createGet(uri), new HttpClient.HttpClientConfig.Builder().setFollowRedirect(true).followRedirect(MAX_REDIRECTS).build()).flatMap(new Func1<HttpClientResponse<ByteBuf>, Observable<ByteBuf>>() {

        @Override
        public Observable<ByteBuf> call(HttpClientResponse<ByteBuf> response) {
            return response.getContent();
        }
    }).map(new Func1<ByteBuf, String>() {

        @Override
        public String call(ByteBuf o) {
            return o.toString(Charset.defaultCharset());
        }
    }).doOnError(new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            logger.warn("Error: " + throwable.getMessage(), throwable);
        }
    }).timeout(GET_TIMEOUT_SECS, TimeUnit.SECONDS);
}
Also used : CompositeHttpClientBuilder(mantis.io.reactivex.netty.protocol.http.client.CompositeHttpClientBuilder) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) RxClient(mantis.io.reactivex.netty.client.RxClient) HttpClientResponse(mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse) Func1(rx.functions.Func1) HttpRequest(io.netty.handler.codec.http.HttpRequest) Action1(rx.functions.Action1) HttpResponse(io.netty.handler.codec.http.HttpResponse) ChannelPipeline(io.netty.channel.ChannelPipeline) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpClientRequest(mantis.io.reactivex.netty.protocol.http.client.HttpClientRequest) HttpClient(mantis.io.reactivex.netty.protocol.http.client.HttpClient) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ByteBuf (io.netty.buffer.ByteBuf)1 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelPipeline (io.netty.channel.ChannelPipeline)1 ChannelPromise (io.netty.channel.ChannelPromise)1 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)1 HttpRequest (io.netty.handler.codec.http.HttpRequest)1 HttpResponse (io.netty.handler.codec.http.HttpResponse)1 NoSuchElementException (java.util.NoSuchElementException)1 RxClient (mantis.io.reactivex.netty.client.RxClient)1 CompositeHttpClientBuilder (mantis.io.reactivex.netty.protocol.http.client.CompositeHttpClientBuilder)1 HttpClient (mantis.io.reactivex.netty.protocol.http.client.HttpClient)1 HttpClientRequest (mantis.io.reactivex.netty.protocol.http.client.HttpClientRequest)1 HttpClientResponse (mantis.io.reactivex.netty.protocol.http.client.HttpClientResponse)1 Action1 (rx.functions.Action1)1 Func1 (rx.functions.Func1)1