Example usage for io.netty.channel ChannelHandlerContext fireChannelRead

List of usage examples for io.netty.channel ChannelHandlerContext fireChannelRead

Introduction

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

Prototype

@Override
    ChannelHandlerContext fireChannelRead(Object msg);

Source Link

Usage

From source file:org.wso2.carbon.transport.http.netty.listener.HTTPTraceLoggingHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (logger.isEnabled(internalLevel)) {
        logger.log(internalLevel, format(ctx, EVENT_INBOUND, msg));
    }/*from  w  ww.j a v a  2  s .  c o m*/
    ctx.fireChannelRead(msg);
}

From source file:org.wso2.carbon.transport.http.netty.listener.WebSocketServerHandshakeHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

    if (msg instanceof HttpRequest) {
        HttpRequest httpRequest = (HttpRequest) msg;
        HttpHeaders headers = httpRequest.headers();
        String httpMethod = httpRequest.method().name();
        if (httpMethod.equalsIgnoreCase("GET") && isConnectionUpgrade(headers)
                && Constants.WEBSOCKET_UPGRADE.equalsIgnoreCase(headers.get(Constants.UPGRADE))) {
            log.debug("Upgrading the connection from Http to WebSocket for " + "channel : " + ctx.channel());
            handleWebSocketHandshake(httpRequest, ctx);
            return;
        }/*  w w  w  .  j a v  a 2 s.  co  m*/
    }
    ctx.fireChannelRead(msg);
}

From source file:org.wso2.custom.inbound.InboundHttp2ServerInitializer.java

License:Apache License

/**
 * Configure the pipeline for a clear text upgrade from HTTP to HTTP/2.0
 *//*from   w  w  w.  j  av  a 2  s  .  co  m*/
private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            // If this handler is hit then no upgrade has been attempted and the client is just talking HTTP.
            log.info("No upgrade done: continue with " + msg.protocolVersion());
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new InboundHttpSourceHandler(config));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(msg);
        }
    });
    p.addLast(new UserEventLogger());
}

From source file:org.wso2.esb.integration.common.utils.servers.http2.Http2ServerInitializer.java

License:Open Source License

private void configureClearText(SocketChannel ch) {
    final ChannelPipeline p = ch.pipeline();
    final HttpServerCodec sourceCodec = new HttpServerCodec();

    p.addLast(sourceCodec);/* w ww.  j a v a 2  s.c om*/
    p.addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory));
    p.addLast(new SimpleChannelInboundHandler<HttpMessage>() {
        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpMessage msg) throws Exception {
            ChannelPipeline pipeline = ctx.pipeline();
            ChannelHandlerContext thisCtx = pipeline.context(this);
            pipeline.addAfter(thisCtx.name(), null, new Http1Handler("Direct. No Upgrade Attempted."));
            pipeline.replace(this, null, new HttpObjectAggregator(maxHttpContentLength));
            ctx.fireChannelRead(msg);
        }
    });

    p.addLast(new UserEventLogger());
}

From source file:org.wyb.sows.client.SocksServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception {
    switch (socksRequest.requestType()) {
    case INIT: {/*from  ww  w  .  j a va 2  s . c om*/
        // auth support example
        //ctx.pipeline().addFirst(new SocksAuthRequestDecoder());
        //ctx.write(new SocksInitResponse(SocksAuthScheme.AUTH_PASSWORD));
        ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
        ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH));
        break;
    }
    case AUTH:
        ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
        ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS));
        break;
    case CMD:
        SocksCmdRequest req = (SocksCmdRequest) socksRequest;
        if (req.cmdType() == SocksCmdType.CONNECT) {
            ctx.pipeline().addLast(new SocksServerConnectHandler(bridgeServiceUri, userName, passcode));
            ctx.pipeline().remove(this);
            ctx.fireChannelRead(socksRequest);
        } else {
            ctx.close();
        }
        break;
    case UNKNOWN:
        ctx.close();
        break;
    }
}

From source file:pers.zlf.sslocal.handler.socks.SocksServerHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, SocksRequest socksRequest) throws Exception {
    switch (socksRequest.requestType()) {
    case INIT: {/*from w w  w. j av a 2  s  .  co  m*/
        ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
        ctx.write(new SocksInitResponse(SocksAuthScheme.NO_AUTH));
        break;
    }
    case AUTH:
        ctx.pipeline().addFirst(new SocksCmdRequestDecoder());
        ctx.write(new SocksAuthResponse(SocksAuthStatus.SUCCESS));
        break;
    case CMD:
        SocksCmdRequest req = (SocksCmdRequest) socksRequest;
        if (req.cmdType() == SocksCmdType.CONNECT) {
            ctx.pipeline().addLast(new ShadowsocksServerConnectHandler());
            ctx.pipeline().remove(this);
            ctx.fireChannelRead(socksRequest);
        } else {
            ctx.close();
        }
        break;
    case UNKNOWN:
        ctx.close();
        break;
    }
}

From source file:preformance_test.handler.WebSocketServerHandler.java

License:Apache License

private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
    // Handle a bad request. //http? http ??Upgrade(???)
    if (!req.decoderResult().isSuccess() || req.method() != GET
            || (!"websocket".equals(req.headers().get("Upgrade")))) {
        sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
        return;//from w  w  w  .j  av a 2  s  .  co m
    }

    // ??
    WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(getWebSocketLocation(req),
            null, true, 5 * 1024 * 1024);
    handshaker = wsFactory.newHandshaker(req);
    if (handshaker == null) {
        // ??
        WebSocketServerHandshakerFactory.sendUnsupportedVersionResponse(ctx.channel());
    } else {
        handshaker.handshake(ctx.channel(), req);
        ctx.fireChannelRead(req.retain()); // 
    }
}

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/*w ww.  j ava 2 s  .c om*/
                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());
        }
    });
}

From source file:reactor.io.net.impl.netty.http.NettyHttpClientHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Class<?> messageClass = msg.getClass();
    if (HttpResponse.class.isAssignableFrom(messageClass)) {
        HttpResponse response = (HttpResponse) msg;
        if (request != null) {
            request.setNettyResponse(response);
        }/*w ww  . j a  va  2 s  .  c om*/

        checkResponseCode(ctx, response);

        if (FullHttpResponse.class.isAssignableFrom(messageClass)) {
            postRead(ctx, msg);
        }
        ctx.fireChannelRead(msg);
    } else if (HttpContent.class.isAssignableFrom(messageClass)) {
        super.channelRead(ctx, ((ByteBufHolder) msg).content());
        postRead(ctx, msg);
    } else if (!discardBody) {
        super.channelRead(ctx, msg);
    }
}

From source file:reactor.ipc.netty.channel.ByteBufHolderHandler.java

License:Open Source License

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    if (msg instanceof ByteBufHolder) {
        ByteBuf bb = ((ByteBufHolder) msg).content();
        if (bb == Unpooled.EMPTY_BUFFER || bb instanceof EmptyByteBuf) {
            ctx.fireChannelRead(msg);
        } else {//from   www .ja va 2 s  .co m
            ctx.fireChannelRead(bb.retain());
        }
    } else {
        ctx.fireChannelRead(msg);
    }
}