/** * The Http2ServerDowngrader currently is always incorrectly setting the "x-http2-stream-id" * header to "0", which is confusing. And as we don't actually need it and the other "x-http2-" headers, we * strip them out here to avoid the confusion. * * Hopefully in a future netty release that header value will be correct and we can then * stop doing this. Although potentially we _never_ want to pass these downstream to origins .... ? */ @ChannelHandler.Sharable public class Http2StreamHeaderCleaner extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; for (String name : req.headers().names()) { if (name.startsWith("x-http2-")) { req.headers().remove(name); } } } super.channelRead(ctx, msg); } }
/** * The Http2ServerDowngrader currently is always incorrectly setting the "x-http2-stream-id" * header to "0", which is confusing. And as we don't actually need it and the other "x-http2-" headers, we * strip them out here to avoid the confusion. * * Hopefully in a future netty release that header value will be correct and we can then * stop doing this. Although potentially we _never_ want to pass these downstream to origins .... ? */ @ChannelHandler.Sharable public class Http2StreamHeaderCleaner extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof HttpRequest) { HttpRequest req = (HttpRequest) msg; for (String name : req.headers().names()) { if (name.startsWith("x-http2-")) { req.headers().remove(name); } } } super.channelRead(ctx, msg); } }
@ChannelHandler.Sharable private static class ConnectionNegotiationErrorHandler extends ChannelHandlerAdapter { static final ConnectionNegotiationErrorHandler INSTANCE = new ConnectionNegotiationErrorHandler(); @Override public void exceptionCaught(final ChannelHandlerContext context, final Throwable cause) { log.debug("Server caught an exception before establishing an HTTP/2 connection.", cause); } }
@ChannelHandler.Sharable public abstract class SocksConnectHandler<T> extends RelayConnectHandler<T> { public SocksConnectHandler(LifeCycle server, MockServerLogger mockServerLogger, String host, int port) { super(server, mockServerLogger, host, port); } protected void removeCodecSupport(ChannelHandlerContext ctx) { ChannelPipeline pipeline = ctx.pipeline(); removeHandler(pipeline, SslHandler.class); removeHandler(pipeline, HttpServerCodec.class); removeHandler(pipeline, HttpContentDecompressor.class); removeHandler(pipeline, HttpObjectAggregator.class); removeHandler(pipeline, this); } }
/** * ChannelHandler which does nothing. */ @Sharable public final class NoopHandler extends ChannelHandlerAdapter { public static final NoopHandler INSTANCE = new NoopHandler(); private NoopHandler() { } }
@Sharable public class StringDecoder extends MessageToMessageDecoder<ByteBuf> {
/** * @author jamesdbloom */ @ChannelHandler.Sharable public class HttpContentLengthRemover extends MessageToMessageEncoder<DefaultHttpMessage> { @Override protected void encode(ChannelHandlerContext ctx, DefaultHttpMessage defaultHttpMessage, List out) { if (defaultHttpMessage.headers().contains(HttpHeaders.CONTENT_LENGTH, "", true)) { defaultHttpMessage.headers().remove(HttpHeaders.CONTENT_LENGTH); } ReferenceCountUtil.retain(defaultHttpMessage); out.add(defaultHttpMessage); } }
@Singleton @ChannelHandler.Sharable public class PromiseFailureHandler extends ChannelOutboundHandlerAdapter { public static final PromiseFailureHandler INSTANCE = new PromiseFailureHandler(); private static final Logger LOG = LoggerFactory.getLogger(PromiseFailureHandler.class); @Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { promise.addListener(Listener.INSTANCE); super.write(ctx, msg, promise); } private static final class Listener implements ChannelFutureListener { private static final Listener INSTANCE = new Listener(); @Override public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { LOG.info("Write on channel {} failed", future.channel(), future.cause()); } } } }
@ChannelHandler.Sharable private static class ConnectionNegotiationErrorHandler extends ChannelHandlerAdapter { static final ConnectionNegotiationErrorHandler INSTANCE = new ConnectionNegotiationErrorHandler(); @Override public void exceptionCaught(final ChannelHandlerContext context, final Throwable cause) { tryFailureAndLogRejectedCause(context.channel().attr(CHANNEL_READY_PROMISE_ATTRIBUTE_KEY).get(), cause); } }
/** * Listing 6.1 Releasing message resources * * @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a> */ @Sharable public class DiscardHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ReferenceCountUtil.release(msg); } }
@Singleton @ChannelHandler.Sharable public class ServerStatusHeaderHandler extends ChannelOutboundHandlerAdapter
@ChannelHandler.Sharable public class RemoteAddressHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { InetSocketAddress remoteAddress = (InetSocketAddress) ctx.channel().remoteAddress(); String hostAddress = remoteAddress != null ? remoteAddress.getAddress().getHostAddress() : null; if (msg instanceof Position) { Position position = (Position) msg; position.set(Position.KEY_IP, hostAddress); } ctx.fireChannelRead(msg); } }
@Sharable public class CommandBatchEncoder extends MessageToByteEncoder<CommandsData> {
@ChannelHandler.Sharable public class ProtobufEncoderNano extends MessageToMessageEncoder<MessageNano> { @Override
@Sharable public class CommandBatchEncoder extends MessageToByteEncoder<CommandsData> {
/** * Listing 6.3 Consuming and releasing an inbound message * * @author <a href="mailto:norman.maurer@gmail.com">Norman Maurer</a> */ @Sharable public class DiscardInboundHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { ReferenceCountUtil.release(msg); } }
@Singleton @ChannelHandler.Sharable public class ServerStatusHeaderHandler extends ChannelOutboundHandlerAdapter
@ChannelHandler.Sharable public class CommandResultEventHandler extends BaseEventHandler { @Override protected Map<Event, Position> analyzePosition(Position position) { Object commandResult = position.getAttributes().get(Position.KEY_RESULT); if (commandResult != null) { Event event = new Event(Event.TYPE_COMMAND_RESULT, position.getDeviceId(), position.getId()); event.set(Position.KEY_RESULT, (String) commandResult); return Collections.singletonMap(event, position); } return null; } }
/** * Encodes an {@link SocksMessage} into a {@link ByteBuf}. * {@link MessageToByteEncoder} implementation. * Use this with {@link SocksInitRequest}, {@link SocksInitResponse}, {@link SocksAuthRequest}, * {@link SocksAuthResponse}, {@link SocksCmdRequest} and {@link SocksCmdResponse} */ @ChannelHandler.Sharable public class SocksMessageEncoder extends MessageToByteEncoder<SocksMessage> { @Override @SuppressWarnings("deprecation") protected void encode(ChannelHandlerContext ctx, SocksMessage msg, ByteBuf out) throws Exception { msg.encodeAsByteBuf(out); } }
@Sharable public class ProtobufEncoder extends MessageToMessageEncoder<MessageLiteOrBuilder> { @Override