Example usage for io.netty.channel ChannelHandlerContext name

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

Introduction

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

Prototype

String name();

Source Link

Document

The unique name of the ChannelHandlerContext .The name was used when then ChannelHandler was added to the ChannelPipeline .

Usage

From source file:org.jfxvnc.net.rfb.codec.ProtocolHandler.java

License:Apache License

@Override
public void handlerAdded(ChannelHandlerContext ctx) {
    ChannelPipeline cp = ctx.pipeline();
    if (cp.get(ProtocolHandshakeHandler.class) == null) {
        cp.addBefore(ctx.name(), "rfb-handshake-handler", new ProtocolHandshakeHandler(config));
    }// ww w  . j  a v  a 2 s.c  om
}

From source file:org.jfxvnc.net.rfb.codec.ProtocolHandshakeHandler.java

License:Apache License

@Override
public void channelActive(final ChannelHandlerContext ctx) throws Exception {
    ctx.pipeline().addBefore(ctx.name(), "rfb-version-decoder", new ProtocolVersionDecoder());
}

From source file:org.jfxvnc.net.rfb.codec.security.RfbSecurityHandshaker.java

License:Apache License

public final ChannelFuture handshake(Channel channel, boolean sendResponse, ChannelPromise promise) {
    ChannelPipeline p = channel.pipeline();
    ChannelHandlerContext ctx = p.context(RfbClientDecoder.class);
    p.addBefore(ctx.name(), "rfb-security-decoder", newSecurityDecoder());

    ChannelHandlerContext ctx2 = p.context(RfbClientEncoder.class);
    p.addBefore(ctx2.name(), "rfb-security-encoder", newSecurityEncoder());
    if (!sendResponse) {
        return promise.setSuccess();
    }//from   ww w. ja  v  a  2 s.c o m
    channel.writeAndFlush(Unpooled.buffer(1).writeByte(securityType.getType()), promise);
    return promise;
}

From source file:org.radarlab.client.ws.WebSocketClientHandler.java

License:Apache License

@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    Channel ch = ctx.channel();//from w  ww.j  a v a2 s .  c  om
    if (!handshaker.isHandshakeComplete()) {
        handshaker.finishHandshake(ch, (FullHttpResponse) msg);
        logger.info("WebSocket Client:" + ctx.name() + " connected!");
        handshakeFuture.setSuccess();
        return;
    }

    if (msg instanceof FullHttpResponse) {
        FullHttpResponse response = (FullHttpResponse) msg;
        throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.getStatus()
                + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
    }

    WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
        TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
        String result = textFrame.text();
        JSONObject json = new JSONObject(result);
        if (json.has("id")) {
            RadarWebSocketClient.queues.get(json.getLong("id")).put(result);
        } else {
            RadarWebSocketClient.subscribeQueue.offer(result);
        }
    } else if (frame instanceof PongWebSocketFrame) {
        System.out.println("WebSocket Client received pong");
    } else if (frame instanceof CloseWebSocketFrame) {
        System.out.println("WebSocket Client received closing");
        ch.close();
    }
}

From source file:org.springframework.web.reactive.socket.adapter.RxNettyWebSocketSession.java

License:Apache License

/**
 * Insert an {@link WebSocketFrameAggregator} after the
 * {@code WebSocketFrameDecoder} for receiving full messages.
 * @param channel the channel for the session
 * @param frameDecoderName the name of the WebSocketFrame decoder
 *//*from w w w.  j a  v a2s.c om*/
public RxNettyWebSocketSession aggregateFrames(Channel channel, String frameDecoderName) {
    ChannelPipeline pipeline = channel.pipeline();
    if (pipeline.context(FRAME_AGGREGATOR_NAME) != null) {
        return this;
    }
    ChannelHandlerContext frameDecoder = pipeline.context(frameDecoderName);
    if (frameDecoder == null) {
        throw new IllegalArgumentException("WebSocketFrameDecoder not found: " + frameDecoderName);
    }
    ChannelHandler frameAggregator = new WebSocketFrameAggregator(DEFAULT_FRAME_MAX_SIZE);
    pipeline.addAfter(frameDecoder.name(), FRAME_AGGREGATOR_NAME, frameAggregator);
    return this;
}

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

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, HttpObject msg, List<Object> out) throws Exception {
    try {// w w  w  .j  a v a2  s  .  c o  m
        super.decode(ctx, msg, out);
    } catch (Exception e) {
        log.warn("Message length validation failed");

        Iterator<Map.Entry<String, ChannelHandler>> iterator = ctx.pipeline().iterator();

        boolean canRemove = false;
        while (iterator.hasNext()) {
            Map.Entry<String, ChannelHandler> channelHandlerEntry = iterator.next();
            if (channelHandlerEntry.getKey().equalsIgnoreCase(ctx.name())) {
                canRemove = true;
            }
            if (canRemove && !channelHandlerEntry.getKey().equalsIgnoreCase(ctx.name())) {
                ctx.pipeline().remove(channelHandlerEntry.getKey());
            }
        }

        String rejectMessage = requestSizeValidationConfig.getRequestRejectMessage();
        byte[] errorMessageBytes = rejectMessage.getBytes(Charset.defaultCharset());
        ByteBuf content = Unpooled.wrappedBuffer(errorMessageBytes);
        DefaultFullHttpResponse rejectResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                HttpResponseStatus.valueOf(requestSizeValidationConfig.getRequestRejectStatusCode()), content);
        rejectResponse.headers().set(Constants.HTTP_CONTENT_LENGTH, errorMessageBytes.length);
        rejectResponse.headers().set(Constants.HTTP_CONTENT_TYPE,
                requestSizeValidationConfig.getRequestRejectMsgContentType());

        ctx.writeAndFlush(rejectResponse);
    }
}

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

License:Open Source License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, List<Object> out) throws Exception {
    super.decode(ctx, buffer, out);

    for (Object o : out) {
        if (o instanceof DefaultHttpRequest) {
            DefaultHttpRequest httpRequest = (DefaultHttpRequest) o;
            if (httpRequest.getDecoderResult().isFailure()
                    && httpRequest.getDecoderResult().cause() instanceof TooLongFrameException) {

                log.warn("Header size is larger than the valid limit");

                Iterator<Map.Entry<String, ChannelHandler>> iterator = ctx.pipeline().iterator();

                boolean canRemove = false;
                while (iterator.hasNext()) {
                    Map.Entry<String, ChannelHandler> channelHandlerEntry = iterator.next();
                    if (channelHandlerEntry.getKey().equalsIgnoreCase(ctx.name())) {
                        canRemove = true;
                    }//from w  w w  . j  av a  2s  .  co m
                    if (canRemove && !channelHandlerEntry.getKey().equalsIgnoreCase(ctx.name())) {
                        ctx.pipeline().remove(channelHandlerEntry.getKey());
                    }
                }

                String rejectMessage = requestSizeValidationConfig.getHeaderRejectMessage();
                byte[] errorMessageBytes = rejectMessage.getBytes(Charset.defaultCharset());
                ByteBuf content = Unpooled.wrappedBuffer(errorMessageBytes);
                DefaultFullHttpResponse rejectResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                        HttpResponseStatus.valueOf(requestSizeValidationConfig.getHeaderRejectStatusCode()),
                        content);
                rejectResponse.headers().set(Constants.HTTP_CONTENT_LENGTH, errorMessageBytes.length);
                rejectResponse.headers().set(Constants.HTTP_CONTENT_TYPE,
                        requestSizeValidationConfig.getHeaderRejectMsgContentType());

                ctx.writeAndFlush(rejectResponse);
                break;
            }
        }
    }
}

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
 */// w w  w .j  a v  a2 s  .c  o  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);//  www .j ava  2 s . c o  m
    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:pers.zlf.fhsp.socks.RelayHandler.java

License:Apache License

@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
    // Split plaintext transferred within HTTP only
    if (((InetSocketAddress) relayChannel.remoteAddress()).getPort() == 80) {
        ctx.pipeline().addBefore(ctx.name(), ByteBufSplitter.class.getName(), SPLITTER);
    }//from   w  ww  .j  a va2 s.  c o  m
}