/** * Replace the HTTP decoder with a new Web Socket decoder. * Note that we do not use {@link ChannelPipeline#replace(String, String, ChannelHandler)}, because the server * might have sent the first frame immediately after the upgrade response. In such a case, the HTTP decoder might * have the first frame in its cumulation buffer and the HTTP decoder will forward it to the next handler. * The Web Socket decoder will not receive it if we simply replaced it. For more information, refer to * {@link HttpResponseDecoder} and its unit tests. */ static void replaceDecoder(Channel channel, ChannelHandler wsDecoder) { ChannelPipeline p = channel.getPipeline(); ChannelHandlerContext httpDecoderCtx = p.getContext(HttpResponseDecoder.class); if (httpDecoderCtx == null) { throw new IllegalStateException("can't find an HTTP decoder from the pipeline"); } p.addAfter(httpDecoderCtx.getName(), "ws-decoder", wsDecoder); p.remove(httpDecoderCtx.getName()); } }
/** * Upgrades the connection and send the handshake response. */ protected ChannelFuture writeHandshakeResponse( Channel channel, HttpResponse res, ChannelHandler encoder, ChannelHandler decoder) { final ChannelPipeline p = channel.getPipeline(); if (p.get(HttpChunkAggregator.class) != null) { p.remove(HttpChunkAggregator.class); } final String httpEncoderName = p.getContext(HttpResponseEncoder.class).getName(); p.addAfter(httpEncoderName, "wsencoder", encoder); p.get(HttpRequestDecoder.class).replace("wsdecoder", decoder); final ChannelFuture future = channel.write(res); future.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { p.remove(httpEncoderName); } }); return future; }
public static ConnectionContext getContext(Channel channel) { ConnectionContext context = (ConnectionContext) channel.getPipeline() .getContext(ConnectionContextHandler.class) .getAttachment(); Preconditions.checkState(context != null, "Context not yet set on channel %s", channel); return context; } }
@Override protected void abortOutputRequested(ChannelPipeline pipeline, final WriteAbortEvent evt) throws Exception { ChannelHandlerContext ctx = transport.getPipeline().getContext(SslHandler.class); ChannelFuture tlsFuture = evt.getFuture(); abortOutputOrClose(ctx, tlsFuture); }
@Override protected void abortInputRequested(ChannelPipeline pipeline, final ReadAbortEvent evt) throws Exception { ChannelHandlerContext ctx = transport.getPipeline().getContext(SslHandler.class); ChannelFuture tlsFuture = evt.getFuture(); abortInputOrSuccess(ctx, tlsFuture); }
@Override protected void abortInputRequested(ChannelPipeline pipeline, final ReadAbortEvent evt) throws Exception { ChannelHandlerContext ctx = transport.getPipeline().getContext(SslHandler.class); ChannelFuture tlsFuture = evt.getFuture(); abortInputOrSuccess(ctx, tlsFuture); }
@Override protected void abortOutputRequested(ChannelPipeline pipeline, final WriteAbortEvent evt) throws Exception { ChannelHandlerContext ctx = transport.getPipeline().getContext(SslHandler.class); ChannelFuture tlsFuture = evt.getFuture(); abortOutputOrClose(ctx, tlsFuture); };
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { SMTPSession session = (SMTPSession) pipeline.getContext(HandlerConstants.CORE_HANDLER).getAttachment(); if (session == null || session.needsCommandInjectionDetection()) { String trimedLowerCasedInput = readAll(buffer).trim().toLowerCase(Locale.US); if (hasCommandInjection(trimedLowerCasedInput)) { throw new CommandInjectionDetectedException(); } } return super.decode(ctx, channel, buffer); }
@Override public void channelConnected(ChannelHandlerContext ctx, final ChannelStateEvent e) throws Exception { Channel channel = e.getChannel(); ChannelPipeline childPipeline = channel.getPipeline(); IoSessionFactoryChannelHandler newHandler = new IoSessionFactoryChannelHandler(connector, connectFuture, sessionInitializer); childPipeline.replace(this, "factory", newHandler); ChannelHandlerContext childCtx = childPipeline.getContext(newHandler); newHandler.channelConnected(childCtx, e); }
@Override public void run(Channel channel) throws Exception { nettyResponseFuture.setState(NettyAsyncHttpState.SENDING_REQUEST); // add the response handler to the channel object, so we can notify caller when request is complete channel.getPipeline().getContext(NettyHttpResponseChannelHandler.class).setAttachment(nettyResponseFuture); HttpRequest nettyRequest = buildNettyHttpRequest(request); channel.write(nettyRequest).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { nettyResponseFuture.setState(NettyAsyncHttpState.WAITING_FOR_RESPONSE); } else if (future.isCancelled()) { nettyResponseFuture.setException(new CanceledRequestException()); } else { Throwable cause = future.getCause(); if (cause == null) { cause = new UnknownRequestException(); } nettyResponseFuture.setException(cause); } } }); }
private void stopStreamAligned(final ChannelPipeline pipeline) { LOGGER.debug("Stopping pipeline"); for (ChannelHandler handler : pipeline.toMap().values()) { if (LOGGER.isDebugEnabled()) { Channel channel = pipeline.getChannel(); int id = (channel != null) ? channel.getId() : 0; LOGGER.debug(format("[id: 0x%08x] %s", id, handler)); } // note: removing this handler can trigger script completion // which in turn can re-attempt to stop this pipeline pipeline.remove(handler); } // non-empty pipeline required to avoid warnings if (pipeline.getContext(closeOnExceptionHandler) == null) { pipeline.addLast("closeOnException", closeOnExceptionHandler); } }
@Override public void childChannelOpen(ChannelHandlerContext ctx, ChildChannelStateEvent e) throws Exception { LOGGER.debug("Control Channel Opened"); Channel childChannel = e.getChildChannel(); channelGroup.add(childChannel); final ControlServerHandler controller = (ControlServerHandler) childChannel.getPipeline().getContext("control.handler").getHandler(); // Add the controller to our list controlHandlers.add(controller); // And remove it when the channel is closed. controller.getChannelClosedFuture().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { controlHandlers.remove(controller); } }); }
final ChannelHandlerContext framerContext = pipeline.getContext(FRAMER);
/** * Starts this application * * @param pipelineFactory the {@link CoapChannelPipelineFactory} that creates the instances of * {@link AbstractCoapChannelHandler}s that deal with inbound and outbound messages * @param localSocket the socket address to be used for inbound and outbound messages */ protected void startApplication(CoapChannelPipelineFactory pipelineFactory, InetSocketAddress localSocket) { //ChannelFactory channelFactory = new NioDatagramChannelFactory(executor, executor.getCorePoolSize() / 2 ); ChannelFactory channelFactory = new NioDatagramChannelFactory(executor, 1 ); //System.out.println("Threads: " + (executor.getCorePoolSize() - 1)); //Create and configure bootstrap ConnectionlessBootstrap bootstrap = new ConnectionlessBootstrap(channelFactory); bootstrap.setPipelineFactory(pipelineFactory); bootstrap.setOption("receiveBufferSizePredictor", new FixedReceiveBufferSizePredictor(RECEIVE_BUFFER_SIZE)); //Create datagram channel this.channel = (DatagramChannel) bootstrap.bind(localSocket); // set the channel handler contexts for (ChannelHandler handler : pipelineFactory.getChannelHandlers()) { if (handler instanceof AbstractCoapChannelHandler) { ChannelHandlerContext context = this.channel.getPipeline().getContext(handler.getClass()); ((AbstractCoapChannelHandler) handler).setContext(context); } } }
@Override public void operationComplete(ChannelFuture connectFuture) throws Exception { if (connectFuture.isSuccess()) { transport = connectFuture.getChannel(); transport.getConfig().setBufferFactory(httpConnectConfig.getBufferFactory()); ChannelPipeline pipeline = transport.getPipeline(); ChannelHandlerContext ctx = pipeline.getContext(HttpClientChannelSource.class); HttpClientChannelSource channelSource = (HttpClientChannelSource) ctx.getHandler(); if (!httpConnectChannel.isBound()) { ChannelAddress httpLocalAddress = httpRemoteAddress; httpConnectChannel.setLocalAddress(httpLocalAddress); httpConnectChannel.setBound(); fireChannelBound(httpConnectChannel, httpLocalAddress); } channelSource.setHttpChannel(httpConnectChannel); httpConnectChannel.setRemoteAddress(httpRemoteAddress); httpConnectChannel.setConnected(); httpConnectFuture.setSuccess(); fireChannelConnected(httpConnectChannel, httpRemoteAddress); } else { httpConnectFuture.setFailure(connectFuture.getCause()); } } });
ChannelHandlerContext httpDecoderCtx = pipeline.getContext(HttpResponseDecoder.class); HttpResponseDecoder httpDecoder = (HttpResponseDecoder) httpDecoderCtx.getHandler(); httpDecoder.replace(format("%s.noop", httpDecoderCtx.getName()), NOOP_HANDLER);