/** * 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()); } }
/** * Replace this {@link FrameDecoder} in the {@link ChannelPipeline} with the given {@link ChannelHandler}. All * remaining bytes in the {@link ChannelBuffer} will get send to the new {@link ChannelHandler} that was used * as replacement * */ public void replace(String handlerName, ChannelHandler handler) { if (ctx == null) { throw new IllegalStateException( "Replace cann only be called once the FrameDecoder is added to the ChannelPipeline"); } ChannelPipeline pipeline = ctx.getPipeline(); pipeline.addAfter(ctx.getName(), handlerName, handler); try { if (cumulation != null) { Channels.fireMessageReceived(ctx, cumulation.readBytes(actualReadableBytes())); } } finally { pipeline.remove(this); } }
/** * 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 void upgradePipelineForWebSockets(ChannelPipeline pipeline) { pipeline.addAfter(HTTP_HANDLER, WS_ENCODER_HANDLER, new WebSocket08FrameEncoder(true)); pipeline.remove(HTTP_HANDLER); pipeline.addBefore(WS_PROCESSOR, WS_DECODER_HANDLER, new WebSocket08FrameDecoder(false, false, nettyConfig.getWebSocketMaxFrameSize())); pipeline.addAfter(WS_DECODER_HANDLER, WS_FRAME_AGGREGATOR, new WebSocketFrameAggregator(nettyConfig.getWebSocketMaxBufferSize())); }
public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port) throws IOException, GeneralSecurityException { if (pipeline.get(HTTP_HANDLER) != null) pipeline.remove(HTTP_HANDLER); if (isSecure(scheme)) if (isSslHandlerConfigured(pipeline)) { pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec()); } else { pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); pipeline.addFirst(SSL_HANDLER, createSslHandler(host, port)); } else pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); if (isWebSocket(scheme)) { pipeline.addAfter(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor); pipeline.remove(HTTP_PROCESSOR); } }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { // Will use the first 23 bytes to detect the policy file request. if (buffer.readableBytes() >= 23) { ChannelPipeline p = ctx.getPipeline(); ChannelBuffer firstMessage = buffer.readBytes(23); if (FLASH_POLICY_REQUEST.equals(firstMessage)) { p.addAfter("flashpolicydecoder", "flashpolicyhandler", new FlashPolicyFileHandler(executor, exceptionHandler, ioExceptionHandler, this.publicPort)); } p.remove(this); if (buffer.readable()) { return new Object[]{firstMessage, buffer.readBytes(buffer.readableBytes())}; } else { return firstMessage; } } // Forward the current buffer as is to handlers. return buffer.readBytes(buffer.readableBytes()); }
@Nullable @Override public ChannelPipeline apply(@Nullable ChannelPipeline input) { if (input == null) { input = Channels.pipeline(); } input.addAfter(DECODER_CHANNEL_HANDLER_NAME, AUTHENTICATION_CHANNEL_HANDLER_NAME, new SecurityAuthenticationHttpHandler(realm, tokenValidator, cConf, accessTokenTransformer, discoveryServiceClient)); return input; } });
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { // add bind handler to pipeline String baseName = ctx.getName(); String name = format("%s:socket", baseName); ctx.getPipeline().addAfter(baseName, name, bindHandler); // propagate channel open event super.channelOpen(ctx, e); } }
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { // add the bind handler to the pipeline String baseName = ctx.getName(); String name = format("%s:bind", baseName); ctx.getPipeline().addAfter(baseName, name, bindHandler); // propagate the open event to bind handler super.channelOpen(ctx, e); }
/** * @see org.apache.james.protocols.smtp.SMTPSession#pushLineHandler(org.apache.james.smtpserver.protocol.LineHandler) */ public void pushLineHandler(LineHandler<SMTPSession> overrideCommandHandler) { lineHandlerCount++; getChannelHandlerContext().getPipeline().addAfter("timeoutHandler", "lineHandler" + lineHandlerCount, new LineHandlerUpstreamHandler<SMTPSession>(overrideCommandHandler)); }
public void upgradePipelineForWebSockets(ChannelPipeline pipeline) { pipeline.addAfter(HTTP_HANDLER, WS_ENCODER_HANDLER, new WebSocket08FrameEncoder(true)); pipeline.remove(HTTP_HANDLER); pipeline.addBefore(WS_PROCESSOR, WS_DECODER_HANDLER, new WebSocket08FrameDecoder(false, false, config.getWebSocketMaxFrameSize())); pipeline.addAfter(WS_DECODER_HANDLER, WS_FRAME_AGGREGATOR, new WebSocketFrameAggregator(config.getWebSocketMaxBufferSize())); }
public void upgradePipelineForWebSockets(ChannelPipeline pipeline) { pipeline.addAfter(HTTP_HANDLER, WS_ENCODER_HANDLER, new WebSocket08FrameEncoder(true)); pipeline.remove(HTTP_HANDLER); pipeline.addBefore(WS_PROCESSOR, WS_DECODER_HANDLER, new WebSocket08FrameDecoder(false, false, config.getWebSocketMaxFrameSize())); pipeline.addAfter(WS_DECODER_HANDLER, WS_FRAME_AGGREGATOR, new WebSocketFrameAggregator(config.getWebSocketMaxBufferSize())); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { final ChannelBuffer message = (ChannelBuffer) e.getMessage(); if (! message.readable()) { return; } if (! routed) { if (usesOctetCountFraming(message)) { ctx.getPipeline().addAfter(ctx.getName(), "framer-octet", new SyslogOctetCountFrameDecoder()); } else { ctx.getPipeline().addAfter(ctx.getName(), "framer-delimiter", new DelimiterBasedFrameDecoder(maxFrameLength, delimiter)); } routed = true; } ctx.sendUpstream(e); }
@Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { final ChannelBuffer message = (ChannelBuffer) e.getMessage(); if (! message.readable()) { return; } if (! routed) { if (usesOctetCountFraming(message)) { ctx.getPipeline().addAfter(ctx.getName(), "framer-octet", new SyslogOctetCountFrameDecoder()); } else { ctx.getPipeline().addAfter(ctx.getName(), "framer-delimiter", new DelimiterBasedFrameDecoder(maxFrameLength, delimiter)); } routed = true; } ctx.sendUpstream(e); }
public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port) throws IOException, GeneralSecurityException { if (pipeline.get(HTTP_HANDLER) != null) pipeline.remove(HTTP_HANDLER); if (isSecure(scheme)) if (isSslHandlerConfigured(pipeline)) { pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec()); } else { pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); pipeline.addFirst(SSL_HANDLER, createSslHandler(host, port)); } else pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); if (isWebSocket(scheme)) { pipeline.addAfter(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor); pipeline.remove(HTTP_PROCESSOR); } }
@Override public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { Channel channel = e.getChannel(); if (OPTIMIZE_PERFORMANCE_CLIENT) { channel.getConfig().setBufferFactory(CHANNEL_BUFFER_FACTORY); } ChannelIoSession<?> session = service.createSession(channel); String baseName = ctx.getName(); String name = format("%s#session", baseName); ChannelHandler handler = new IoSessionChannelHandler(session, future, initializer, service.getSessionIdleTracker()); ChannelPipeline pipeline = ctx.getPipeline(); pipeline.addAfter(baseName, name, handler); ctx.sendUpstream(e); pipeline.remove(this); }
public void upgradeProtocol(ChannelPipeline pipeline, Uri requestUri) throws GeneralSecurityException { if (pipeline.get(HTTP_HANDLER) != null) pipeline.remove(HTTP_HANDLER); if (requestUri.isSecured()) if (isSslHandlerConfigured(pipeline)) { pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec()); } else { pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); pipeline.addFirst(SSL_HANDLER, createSslHandler(requestUri.getHost(), requestUri.getExplicitPort())); } else pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); if (requestUri.isWebSocket()) { pipeline.addAfter(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor); pipeline.remove(HTTP_PROCESSOR); } }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { // Will use the first 23 bytes to detect the policy file request. if (buffer.readableBytes() >= 23) { ChannelPipeline p = ctx.getPipeline(); ChannelBuffer firstMessage = buffer.readBytes(23); if (FLASH_POLICY_REQUEST.equals(firstMessage)) { p.addAfter("flashpolicydecoder", "flashpolicyhandler", new FlashPolicyFileHandler(executor, exceptionHandler, ioExceptionHandler, this.publicPort)); } p.remove(this); if (buffer.readable()) { return new Object[]{firstMessage, buffer.readBytes(buffer.readableBytes())}; } else { return firstMessage; } } // Forward the current buffer as is to handlers. return buffer.readBytes(buffer.readableBytes()); }
@Override protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer) throws Exception { if (buffer.readableBytes() < 9) { return null; } if (looksLikeTLS(buffer)) { ctx.getPipeline().addAfter(ctx.getName(), sslHandlerName, serverConfiguration.createHandler()); // Tell the SessionAwareSslHandler to handle the TLS handshake. Channels.fireMessageReceived(ctx, TLSConnectedEvent.SINGLETON); } ctx.getPipeline().remove(this); return buffer.readBytes(buffer.readableBytes()); }
public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port) throws IOException, GeneralSecurityException { if (pipeline.get(HTTP_HANDLER) != null) pipeline.remove(HTTP_HANDLER); if (isSecure(scheme)) if (isSslHandlerConfigured(pipeline)){ pipeline.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec()); } else { pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); pipeline.addFirst(SSL_HANDLER, createSslHandler(host, port)); } else pipeline.addFirst(HTTP_HANDLER, newHttpClientCodec()); if (isWebSocket(scheme)) pipeline.replace(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor); }