Example usage for io.netty.channel SimpleChannelInboundHandler SimpleChannelInboundHandler

List of usage examples for io.netty.channel SimpleChannelInboundHandler SimpleChannelInboundHandler

Introduction

In this page you can find the example usage for io.netty.channel SimpleChannelInboundHandler SimpleChannelInboundHandler.

Prototype

protected SimpleChannelInboundHandler(Class<? extends I> inboundMessageType) 

Source Link

Document

see #SimpleChannelInboundHandler(Class,boolean) with true as boolean value.

Usage

From source file:ratpack.http.client.internal.ContentAggregatingRequestAction.java

License:Apache License

@Override
protected void addResponseHandlers(ChannelPipeline p, Fulfiller<? super ReceivedResponse> fulfiller) {
    p.addLast("aggregator", new HttpObjectAggregator(maxContentLengthBytes));
    p.addLast("httpResponseHandler", new SimpleChannelInboundHandler<FullHttpResponse>(false) {
        @Override//from  w w w .j a  v a  2  s . c  om
        public void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
            final Headers headers = new NettyHeadersBackedHeaders(msg.headers());
            String contentType = headers.get(HttpHeaderConstants.CONTENT_TYPE.toString());
            ByteBuf responseBuffer = initBufferReleaseOnExecutionClose(msg.content(), execution);
            final ByteBufBackedTypedData typedData = new ByteBufBackedTypedData(responseBuffer,
                    DefaultMediaType.get(contentType));
            final Status status = new DefaultStatus(msg.status());

            success(fulfiller, new DefaultReceivedResponse(status, headers, typedData));
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.close();
            error(fulfiller, cause);
        }
    });
}

From source file:ratpack.http.client.internal.ContentStreamingRequestAction.java

License:Apache License

@Override
protected void addResponseHandlers(ChannelPipeline p, Fulfiller<? super StreamedResponse> fulfiller) {
    p.addLast("httpResponseHandler", new SimpleChannelInboundHandler<HttpResponse>(false) {
        @Override//from  w ww  . j ava 2s. c  om
        public void channelRead0(ChannelHandlerContext ctx, HttpResponse msg) throws Exception {
            // Switch auto reading off so we can control the flow of response content
            p.channel().config().setAutoRead(false);
            execution.onCleanup(() -> {
                if (!subscribedTo.get() && ctx.channel().isOpen()) {
                    ctx.close();
                }
            });

            final Headers headers = new NettyHeadersBackedHeaders(msg.headers());
            final Status status = new DefaultStatus(msg.status());

            success(fulfiller, new DefaultStreamedResponse(p, status, headers));
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            ctx.close();
            error(fulfiller, cause);
        }
    });
}

From source file:ratpack.http.client.internal.RequestActionSupport.java

License:Apache License

public void execute(final Fulfiller<? super T> fulfiller) throws Exception {
    final AtomicBoolean redirecting = new AtomicBoolean();

    final Bootstrap b = new Bootstrap();
    b.group(this.execution.getEventLoop()).channel(ChannelImplDetector.getSocketChannelImpl())
            .handler(new ChannelInitializer<SocketChannel>() {
                @Override//from ww w . j  ava 2s  . co  m
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline p = ch.pipeline();

                    if (finalUseSsl) {
                        SSLEngine engine = SSLContext.getDefault().createSSLEngine();
                        engine.setUseClientMode(true);
                        p.addLast("ssl", new SslHandler(engine));
                    }

                    p.addLast("codec", new HttpClientCodec());
                    p.addLast("readTimeout",
                            new ReadTimeoutHandler(requestParams.readTimeoutNanos, TimeUnit.NANOSECONDS));

                    p.addLast("redirectHandler", new SimpleChannelInboundHandler<HttpObject>(false) {
                        @Override
                        protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg)
                                throws Exception {
                            if (msg instanceof HttpResponse) {
                                final HttpResponse response = (HttpResponse) msg;
                                final Headers headers = new NettyHeadersBackedHeaders(response.headers());
                                final Status status = new DefaultStatus(response.status());
                                int maxRedirects = requestSpecBacking.getMaxRedirects();
                                String locationValue = headers.get("Location");

                                //Check for redirect and location header if it is follow redirect if we have request forwarding left
                                if (shouldRedirect(status) && maxRedirects > 0 && locationValue != null) {
                                    redirecting.compareAndSet(false, true);

                                    Action<? super RequestSpec> redirectRequestConfig = Action
                                            .join(requestConfigurer, s -> {
                                                if (status.getCode() == 301 || status.getCode() == 302) {
                                                    s.method("GET");
                                                }

                                                s.redirects(maxRedirects - 1);
                                            });

                                    URI locationUrl;
                                    if (ABSOLUTE_PATTERN.matcher(locationValue).matches()) {
                                        locationUrl = new URI(locationValue);
                                    } else {
                                        locationUrl = new URI(uri.getScheme(), null, uri.getHost(),
                                                uri.getPort(), locationValue, null, null);
                                    }

                                    buildRedirectRequestAction(redirectRequestConfig, locationUrl)
                                            .execute(fulfiller);
                                } else {
                                    p.remove(this);
                                }
                            }

                            if (!redirecting.get()) {
                                ctx.fireChannelRead(msg);
                            }
                        }
                    });

                    addResponseHandlers(p, fulfiller);
                }

                @Override
                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                    ctx.close();
                    error(fulfiller, cause);
                }
            });

    ChannelFuture connectFuture = b.connect(host, port);
    connectFuture.addListener(f1 -> {
        if (connectFuture.isSuccess()) {
            String fullPath = getFullPath(uri);
            FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,
                    HttpMethod.valueOf(requestSpecBacking.getMethod()), fullPath, requestSpecBacking.getBody());
            if (headers.get(HttpHeaderConstants.HOST) == null) {
                headers.set(HttpHeaderConstants.HOST, host);
            }
            headers.set(HttpHeaderConstants.CONNECTION, HttpHeaderValues.CLOSE);
            int contentLength = request.content().readableBytes();
            if (contentLength > 0) {
                headers.set(HttpHeaderConstants.CONTENT_LENGTH, Integer.toString(contentLength, 10));
            }

            HttpHeaders requestHeaders = request.headers();

            for (String name : headers.getNames()) {
                requestHeaders.set(name, headers.getAll(name));
            }

            ChannelFuture writeFuture = connectFuture.channel().writeAndFlush(request);
            writeFuture.addListener(f2 -> {
                if (!writeFuture.isSuccess()) {
                    writeFuture.channel().close();
                    error(fulfiller, writeFuture.cause());
                }
            });
        } else {
            connectFuture.channel().close();
            error(fulfiller, connectFuture.cause());
        }
    });
}