Example usage for io.netty.channel ChannelPipeline addBefore

List of usage examples for io.netty.channel ChannelPipeline addBefore

Introduction

In this page you can find the example usage for io.netty.channel ChannelPipeline addBefore.

Prototype

ChannelPipeline addBefore(String baseName, String name, ChannelHandler handler);

Source Link

Document

Inserts a ChannelHandler before an existing handler of this pipeline.

Usage

From source file:org.asynchttpclient.providers.netty4.NettyAsyncProviderPipelineTest.java

License:Open Source License

@Test(groups = { "standalone", "netty_provider" })
public void asyncPipelineTest() throws Exception {

    NettyAsyncHttpProviderConfig nettyConfig = new NettyAsyncHttpProviderConfig();
    nettyConfig.setHttpAdditionalPipelineInitializer(new AdditionalPipelineInitializer() {
        public void initPipeline(ChannelPipeline pipeline) throws Exception {
            pipeline.addBefore("inflater", "copyEncodingHeader", new CopyEncodingHandler());
        }//w ww.  java 2  s. c o m
    });
    AsyncHttpClient p = getAsyncHttpClient(
            new AsyncHttpClientConfig.Builder().setAsyncHttpClientProviderConfig(nettyConfig).build());

    try {
        final CountDownLatch l = new CountDownLatch(1);
        Request request = new RequestBuilder("GET").setUrl(getTargetUrl()).build();
        p.executeRequest(request, new AsyncCompletionHandlerAdapter() {
            @Override
            public Response onCompleted(Response response) throws Exception {
                try {
                    assertEquals(response.getStatusCode(), 200);
                    assertEquals(response.getHeader("X-Original-Content-Encoding"), "<original encoding>");
                } finally {
                    l.countDown();
                }
                return response;
            }
        }).get();
        if (!l.await(TIMEOUT, TimeUnit.SECONDS)) {
            fail("Timeout out");
        }
    } finally {
        p.close();
    }
}

From source file:org.jfxvnc.net.rfb.codec.handshaker.RfbClientHandshaker.java

License:Apache License

public final ChannelFuture handshake(Channel channel, final ChannelPromise promise) {

    channel.writeAndFlush(Unpooled.wrappedBuffer(version.getBytes())).addListener((ChannelFuture future) -> {
        if (!future.isSuccess()) {
            promise.setFailure(future.cause());
            return;
        }//  ww  w . ja  va 2  s .  co m

        ChannelPipeline p = future.channel().pipeline();
        ChannelHandlerContext ctx = p.context(ProtocolHandshakeHandler.class);
        p.addBefore(ctx.name(), "rfb-handshake-decoder", newRfbClientDecoder());
        p.addBefore(ctx.name(), "rfb-handshake-encoder", newRfbClientEncoder());
        promise.setSuccess();

    });
    return promise;
}

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

License:Apache License

@Override
protected void decode(final ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {

    if (msg instanceof ImageRect) {
        render.render((ImageRect) msg, () -> {
            // logger.debug("render completed");
            // sendFramebufferUpdateRequest(ctx, true, 0, 0,
            // serverInit.getFrameBufferWidth(),
            // serverInit.getFrameBufferHeight());

        });/*from  w  ww  .j av  a  2  s .c o m*/
        return;
    }
    if (msg instanceof ServerDecoderEvent) {
        render.eventReceived((ServerDecoderEvent) msg);
        return;
    }

    if (!(msg instanceof ServerInitEvent)) {
        logger.error("unknown message: {}", msg);
        ctx.fireChannelRead(msg);
        return;
    }

    serverInit = (ServerInitEvent) msg;
    logger.debug("handshake completed with {}", serverInit);

    FrameDecoderHandler frameHandler = new FrameDecoderHandler(serverInit.getPixelFormat());
    if (!frameHandler.isPixelFormatSupported()) {
        ProtocolException e = new ProtocolException(String.format("pixelformat: (%s bpp) not supported yet",
                serverInit.getPixelFormat().getBitPerPixel()));
        exceptionCaught(ctx, e);
        return;
    }

    ChannelPipeline cp = ctx.pipeline();

    cp.addBefore(ctx.name(), "rfb-encoding-encoder", new PreferedEncodingEncoder());
    PreferedEncoding prefEncodings = getPreferedEncodings(frameHandler.getSupportedEncodings());
    ctx.write(prefEncodings);

    cp.addBefore(ctx.name(), "rfb-pixelformat-encoder", new PixelFormatEncoder());
    ctx.write(serverInit.getPixelFormat());
    ctx.flush();

    cp.addBefore(ctx.name(), "rfb-frame-handler", frameHandler);
    cp.addBefore(ctx.name(), "rfb-keyevent-encoder", new KeyButtonEventEncoder());
    cp.addBefore(ctx.name(), "rfb-pointerevent-encoder", new PointerEventEncoder());
    cp.addBefore(ctx.name(), "rfb-cuttext-encoder", new ClientCutTextEncoder());

    render.eventReceived(getConnectInfoEvent(ctx, prefEncodings));

    render.registerInputEventListener(event -> ctx.writeAndFlush(event, ctx.voidPromise()));

    logger.debug("request full framebuffer update");
    sendFramebufferUpdateRequest(ctx, false, 0, 0, serverInit.getFrameBufferWidth(),
            serverInit.getFrameBufferHeight());

    logger.trace("channel pipeline: {}", cp.toMap().keySet());
}

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));
    }//from  w ww .  j av a  2s . com
}

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();
    }/* w  w  w  . jav  a 2s  .  co m*/
    channel.writeAndFlush(Unpooled.buffer(1).writeByte(securityType.getType()), promise);
    return promise;
}

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

License:Open Source License

@Override
public void initChannel(SocketChannel ch) throws Exception {
    if (log.isDebugEnabled()) {
        log.debug("Initializing source channel pipeline");
    }//from   www  .  j  a v  a  2 s  . c  o m

    ChannelPipeline pipeline = ch.pipeline();

    if (sslConfig != null) {
        pipeline.addLast(Constants.SSL_HANDLER, new SslHandler(new SSLHandlerFactory(sslConfig).build()));
    }

    pipeline.addLast("encoder", new HttpResponseEncoder());
    configureHTTPPipeline(pipeline);

    if (socketIdleTimeout > 0) {
        pipeline.addBefore(Constants.HTTP_SOURCE_HANDLER, Constants.IDLE_STATE_HANDLER, new IdleStateHandler(
                socketIdleTimeout, socketIdleTimeout, socketIdleTimeout, TimeUnit.MILLISECONDS));
    }
}