Java Code Examples for io.netty.channel.ChannelPipeline#context()

The following examples show how to use io.netty.channel.ChannelPipeline#context() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Socks5ProxyHandler.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Override
protected void removeDecoder(ChannelHandlerContext ctx) throws Exception {
    ChannelPipeline p = ctx.pipeline();
    if (p.context(decoderName) != null) {
        p.remove(decoderName);
    }
}
 
Example 2
Source File: WebSocketServerHandshaker13Extension.java    From spring-boot-protocol with Apache License 2.0 5 votes vote down vote up
private String getHttpDecoderContextName(ChannelPipeline pipeline){
    ChannelHandlerContext ctx = pipeline.context(HttpRequestDecoder.class);
    if (ctx == null) {
        ctx = pipeline.context(HttpServerCodec.class);
    }
    return ctx == null? null : ctx.name();
}
 
Example 3
Source File: PipelineUtil.java    From ViaVersion with MIT License 5 votes vote down vote up
/**
 * Get the context for a the channel handler before a certain name.
 *
 * @param name     The name of the channel handler
 * @param pipeline The pipeline to target
 * @return The ChannelHandler before the one requested.
 */
public static ChannelHandlerContext getContextBefore(String name, ChannelPipeline pipeline) {
    boolean mark = false;
    for (String s : pipeline.names()) {
        if (mark) {
            return pipeline.context(pipeline.get(s));
        }
        if (s.equalsIgnoreCase(name))
            mark = true;
    }
    return null;
}
 
Example 4
Source File: PipelineUtil.java    From ViaVersion with MIT License 5 votes vote down vote up
public static ChannelHandlerContext getPreviousContext(String name, ChannelPipeline pipeline) {
    String previous = null;
    for (String entry : pipeline.toMap().keySet()) {
        if (entry.equals(name)) {
            return pipeline.context(previous);
        }
        previous = entry;
    }
    return null;
}
 
Example 5
Source File: NegotiateChannelHandler.java    From sailfish with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private void remove(ChannelHandlerContext ctx) {
	try {
		ctx.channel().attr(OneTime.channelConfig).remove();
		ChannelPipeline pipeline = ctx.pipeline();
		if (pipeline.context(this) != null) {
			pipeline.remove(this);
		}
	} finally {
		negotiateMap.remove(ctx);
	}
}
 
Example 6
Source File: ChannelInitializer.java    From ProtocolSupportBungee with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {
	try {
		initChannel(channelHandlerContext.channel());
	} catch (Throwable t) {
		exceptionCaught(channelHandlerContext, t);
	} finally {
		ChannelPipeline pipeline = channelHandlerContext.pipeline();
		if (pipeline.context(this) != null) {
			pipeline.remove(this);
		}
	}
	channelHandlerContext.pipeline().fireChannelRegistered();
}
 
Example 7
Source File: HttpClientPipelineConfigurator.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Override
public void connect(ChannelHandlerContext ctx, SocketAddress remoteAddress, SocketAddress localAddress,
                    ChannelPromise promise) throws Exception {

    // Remember the requested remote address for later use.
    final InetSocketAddress inetRemoteAddr = (InetSocketAddress) remoteAddress;
    this.remoteAddress = inetRemoteAddr;

    // Configure the pipeline.
    final Channel ch = ctx.channel();
    ChannelUtil.disableWriterBufferWatermark(ch);

    final ChannelPipeline p = ch.pipeline();
    p.addLast(new FlushConsolidationHandler());
    p.addLast(ReadSuppressingAndChannelDeactivatingHandler.INSTANCE);

    try {
        if (sslCtx != null) {
            configureAsHttps(ch, inetRemoteAddr);
        } else {
            configureAsHttp(ch);
        }
    } catch (Throwable t) {
        promise.tryFailure(t);
        ctx.close();
    } finally {
        if (p.context(this) != null) {
            p.remove(this);
        }
    }

    ctx.connect(remoteAddress, localAddress, promise);
}
 
Example 8
Source File: RfbSecurityHandshaker.java    From jfxvnc with Apache License 2.0 5 votes vote down vote up
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();
  }
  channel.writeAndFlush(Unpooled.buffer(1).writeByte(securityType.getType()), promise);
  return promise;
}
 
Example 9
Source File: ConnectionImpl.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
public void initCodec(IPacketIdCodec packetIdCodec, AbstractPacketEncoder encoder, AbstractPacketDecoder decoder) {
	ChannelPipeline pipeline = networkmanager.getChannel().pipeline();
	this.logicCtx = pipeline.context(ChannelHandlers.LOGIC);
	this.rawSendCtx = pipeline.context(ChannelHandlers.RAW_CAPTURE_SEND);
	this.rawRecvCtx = pipeline.context(ChannelHandlers.RAW_CAPTURE_RECEIVE);
	this.codec = new PacketDataCodecImpl(this, packetIdCodec, pipeline.context(encoder), pipeline.context(decoder));
	encoder.init(codec);
	decoder.init(codec);
}
 
Example 10
Source File: ChannelInitializer.java    From ProtocolSupport with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void channelRegistered(ChannelHandlerContext channelHandlerContext) throws Exception {
	try {
		initChannel(channelHandlerContext.channel());
	} catch (Throwable t) {
		exceptionCaught(channelHandlerContext, t);
	} finally {
		ChannelPipeline pipeline = channelHandlerContext.pipeline();
		if (pipeline.context(this) != null) {
			pipeline.remove(this);
		}
	}
	channelHandlerContext.pipeline().fireChannelRegistered();
}
 
Example 11
Source File: DefaultClientChannelManager.java    From zuul with Apache License 2.0 5 votes vote down vote up
protected void releaseHandlers(PooledConnection conn) {
    final ChannelPipeline pipeline = conn.getChannel().pipeline();
    removeHandlerFromPipeline(OriginResponseReceiver.CHANNEL_HANDLER_NAME, pipeline);
    // The Outbound handler is always after the inbound handler, so look for it.
    ChannelHandlerContext passportStateHttpClientHandlerCtx =
            pipeline.context(PassportStateHttpClientHandler.OutboundHandler.class);
    pipeline.addAfter(passportStateHttpClientHandlerCtx.name(), IDLE_STATE_HANDLER_NAME,
        new IdleStateHandler(0, 0, connPoolConfig.getIdleTimeout(), TimeUnit.MILLISECONDS));
}
 
Example 12
Source File: WebSocketClientHandshaker.java    From netty4.0.27Learn with Apache License 2.0 4 votes vote down vote up
/**
 * Validates and finishes the opening handshake initiated by {@link #handshake}}.
 *
 * @param channel
 *            Channel
 * @param response
 *            HTTP response containing the closing handshake details
 */
public final void finishHandshake(Channel channel, FullHttpResponse response) {
    verify(response);

    // Verify the subprotocol that we received from the server.
    // This must be one of our expected subprotocols - or null/empty if we didn't want to speak a subprotocol
    String receivedProtocol = response.headers().get(HttpHeaders.Names.SEC_WEBSOCKET_PROTOCOL);
    receivedProtocol = receivedProtocol != null ? receivedProtocol.trim() : null;
    String expectedProtocol = expectedSubprotocol != null ? expectedSubprotocol : "";
    boolean protocolValid = false;

    if (expectedProtocol.isEmpty() && receivedProtocol == null) {
        // No subprotocol required and none received
        protocolValid = true;
        setActualSubprotocol(expectedSubprotocol); // null or "" - we echo what the user requested
    } else if (!expectedProtocol.isEmpty() && receivedProtocol != null && !receivedProtocol.isEmpty()) {
        // We require a subprotocol and received one -> verify it
        for (String protocol : StringUtil.split(expectedSubprotocol, ',')) {
            if (protocol.trim().equals(receivedProtocol)) {
                protocolValid = true;
                setActualSubprotocol(receivedProtocol);
                break;
            }
        }
    } // else mixed cases - which are all errors

    if (!protocolValid) {
        throw new WebSocketHandshakeException(String.format(
                "Invalid subprotocol. Actual: %s. Expected one of: %s",
                receivedProtocol, expectedSubprotocol));
    }

    setHandshakeComplete();

    ChannelPipeline p = channel.pipeline();
    // Remove decompressor from pipeline if its in use
    HttpContentDecompressor decompressor = p.get(HttpContentDecompressor.class);
    if (decompressor != null) {
        p.remove(decompressor);
    }

    // Remove aggregator if present before
    HttpObjectAggregator aggregator = p.get(HttpObjectAggregator.class);
    if (aggregator != null) {
        p.remove(aggregator);
    }

    ChannelHandlerContext ctx = p.context(HttpResponseDecoder.class);
    if (ctx == null) {
        ctx = p.context(HttpClientCodec.class);
        if (ctx == null) {
            throw new IllegalStateException("ChannelPipeline does not contain " +
                    "a HttpRequestEncoder or HttpClientCodec");
        }
        p.replace(ctx.name(), "ws-decoder", newWebsocketDecoder());
    } else {
        if (p.get(HttpRequestEncoder.class) != null) {
            p.remove(HttpRequestEncoder.class);
        }
        p.replace(ctx.name(),
                "ws-decoder", newWebsocketDecoder());
    }
}