Example usage for io.netty.channel ChannelPipeline addFirst

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

Introduction

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

Prototype

ChannelPipeline addFirst(EventExecutorGroup group, ChannelHandler... handlers);

Source Link

Document

Inserts ChannelHandler s at the first position of this pipeline.

Usage

From source file:net.tomp2p.connection.Sender.java

License:Apache License

private boolean addOrReplace(ChannelPipeline pipeline, String before, String name,
        ChannelHandler channelHandler) {
    List<String> names = pipeline.names();
    if (names.contains(name)) {
        pipeline.replace(name, name, channelHandler);
        return false;
    } else {// ww w  .  j a va  2  s . c o m
        if (before == null) {
            pipeline.addFirst(name, channelHandler);
        } else {
            pipeline.addBefore(before, name, channelHandler);
        }
        return true;
    }
}

From source file:netty.WebSocketServerInitializer.java

License:Apache License

@Override
public void initChannel(final SocketChannel ch) throws Exception {
    System.out.println("Adding channled!");
    final ChannelPipeline pipeline = ch.pipeline();
    if (sslCtx != null) {
        pipeline.addFirst("ssl", sslCtx.newHandler(ch.alloc()));
        //pipeline.addLast("ssl", new SslHandler(sslCtx)); //sslCtx.newHandler(ch.alloc()));
    }/*  ww w  .j  av a  2s.  c om*/
    pipeline.addLast(new HttpServerCodec());
    pipeline.addLast(new HttpObjectAggregator(65536));
    pipeline.addLast(new WebSocketServerHandler());
}

From source file:org.asynchttpclient.netty.channel.ChannelManager.java

License:Open Source License

public SslHandler addSslHandler(ChannelPipeline pipeline, Uri uri, String virtualHost) {
    String peerHost;/*from   w  ww.  j a v a 2s  .c o  m*/
    int peerPort;

    if (virtualHost != null) {
        int i = virtualHost.indexOf(':');
        if (i == -1) {
            peerHost = virtualHost;
            peerPort = uri.getSchemeDefaultPort();
        } else {
            peerHost = virtualHost.substring(0, i);
            peerPort = Integer.valueOf(virtualHost.substring(i + 1));
        }

    } else {
        peerHost = uri.getHost();
        peerPort = uri.getExplicitPort();
    }

    SslHandler sslHandler = createSslHandler(peerHost, peerPort);
    pipeline.addFirst(ChannelManager.SSL_HANDLER, sslHandler);
    return sslHandler;
}

From source file:org.asynchttpclient.providers.netty.channel.ChannelManager.java

License:Open Source License

public void upgradeProtocol(ChannelPipeline pipeline, String scheme, String host, int port)
        throws IOException, GeneralSecurityException {
    if (pipeline.get(HTTP_HANDLER) != null)
        pipeline.remove(HTTP_HANDLER);/*from  ww  w .ja  va 2s.c o m*/

    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);
    }
}

From source file:org.asynchttpclient.providers.netty.channel.ChannelManager.java

License:Open Source License

/**
 * Always make sure the channel who got cached support the proper protocol.
 * It could only occurs when a HttpMethod. CONNECT is used against a proxy
 * that requires upgrading from http to https.
 *///from   w  w w  .  ja  v a 2 s .  c  o m
public void verifyChannelPipeline(ChannelPipeline pipeline, String scheme)
        throws IOException, GeneralSecurityException {

    boolean sslHandlerConfigured = isSslHandlerConfigured(pipeline);

    if (isSecure(scheme)) {
        if (!sslHandlerConfigured)
            pipeline.addFirst(SSL_HANDLER, new SslInitializer(this));

    } else if (sslHandlerConfigured)
        pipeline.remove(SSL_HANDLER);
}

From source file:org.asynchttpclient.providers.netty.channel.Channels.java

License:Apache License

/**
 * Always make sure the channel who got cached support the proper protocol. It could only occurs when a HttpMethod.
 * CONNECT is used against a proxy that requires upgrading from http to https.
 *///w  w  w .  j  a va  2 s  . com
public void verifyChannelPipeline(ChannelPipeline pipeline, String scheme)
        throws IOException, GeneralSecurityException {

    boolean isSecure = isSecure(scheme);
    if (pipeline.get(SSL_HANDLER) != null) {
        if (!isSecure)
            pipeline.remove(SSL_HANDLER);

    } else if (isSecure)
        pipeline.addFirst(SSL_HANDLER, new SslInitializer(Channels.this));
}

From source file:org.asynchttpclient.providers.netty.channel.Channels.java

License:Apache License

public void upgradeProtocol(ChannelPipeline p, String scheme, String host, int port)
        throws IOException, GeneralSecurityException {
    if (p.get(HTTP_HANDLER) != null) {
        p.remove(HTTP_HANDLER);/* w  w w  .  ja  va  2  s.c  o  m*/
    }

    if (isSecure(scheme)) {
        if (p.get(SSL_HANDLER) == null) {
            p.addFirst(HTTP_HANDLER, newHttpClientCodec());
            p.addFirst(SSL_HANDLER, createSslHandler(host, port));
        } else {
            p.addAfter(SSL_HANDLER, HTTP_HANDLER, newHttpClientCodec());
        }

    } else {
        p.addFirst(HTTP_HANDLER, newHttpClientCodec());
    }

    if (isWebSocket(scheme)) {
        p.replace(HTTP_PROCESSOR, WS_PROCESSOR, wsProcessor);
    }
}

From source file:org.eclipse.moquette.server.netty.NettyAcceptor.java

License:Open Source License

private void initializePlainTCPTransport(IMessaging messaging, Properties props) throws IOException {
    final NettyMQTTHandler handler = new NettyMQTTHandler();
    handler.setMessaging(messaging);/*from ww  w. j a va2 s .c  o m*/
    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    int port = Integer.parseInt(props.getProperty(Constants.PORT_PROPERTY_NAME));
    initFactory(host, port, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) {
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler());
            //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:org.eclipse.moquette.server.netty.NettyAcceptor.java

License:Open Source License

private void initializeWebSocketTransport(IMessaging messaging, Properties props) throws IOException {
    String webSocketPortProp = props.getProperty(Constants.WEB_SOCKET_PORT_PROPERTY_NAME);
    if (webSocketPortProp == null) {
        //Do nothing no WebSocket configured
        LOG.info("WebSocket is disabled");
        return;/*from  w  w w .j a  va  2s . c  o  m*/
    }
    int port = Integer.parseInt(webSocketPortProp);

    final NettyMQTTHandler handler = new NettyMQTTHandler();
    handler.setMessaging(messaging);

    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    initFactory(host, port, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) {
            pipeline.addLast("httpEncoder", new HttpResponseEncoder());
            pipeline.addLast("httpDecoder", new HttpRequestDecoder());
            pipeline.addLast("aggregator", new HttpObjectAggregator(65536));
            pipeline.addLast("webSocketHandler",
                    new WebSocketServerProtocolHandler("/mqtt"/*"/mqtt"*/, "mqttv3.1, mqttv3.1.1"));
            //pipeline.addLast("webSocketHandler", new WebSocketServerProtocolHandler(null, "mqtt"));
            pipeline.addLast("ws2bytebufDecoder", new WebSocketFrameToByteBufDecoder());
            pipeline.addLast("bytebuf2wsEncoder", new ByteBufToWebSocketFrameEncoder());
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler());
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}

From source file:org.eclipse.moquette.server.netty.NettyAcceptor.java

License:Open Source License

private void initializeSSLTCPTransport(IMessaging messaging, Properties props) throws IOException {
    String sslPortProp = props.getProperty(Constants.SSL_PORT_PROPERTY_NAME);
    if (sslPortProp == null) {
        //Do nothing no SSL configured
        LOG.info("SSL is disabled");
        return;/*from  ww w . j  a v a2  s . c o  m*/
    }
    final String jksPath = props.getProperty(Constants.JKS_PATH_PROPERTY_NAME);
    if (jksPath == null || jksPath.isEmpty()) {
        //key_store_password or key_manager_password are empty
        LOG.warn("You have configured the SSL port but not the jks_path, SSL not started");
        return;
    }

    //if we have the port also the jks then keyStorePassword and keyManagerPassword 
    //has to be defined
    final String keyStorePassword = props.getProperty(Constants.KEY_STORE_PASSWORD_PROPERTY_NAME);
    final String keyManagerPassword = props.getProperty(Constants.KEY_MANAGER_PASSWORD_PROPERTY_NAME);
    if (keyStorePassword == null || keyStorePassword.isEmpty()) {
        //key_store_password or key_manager_password are empty
        LOG.warn("You have configured the SSL port but not the key_store_password, SSL not started");
        return;
    }
    if (keyManagerPassword == null || keyManagerPassword.isEmpty()) {
        //key_manager_password or key_manager_password are empty
        LOG.warn("You have configured the SSL port but not the key_manager_password, SSL not started");
        return;
    }

    int sslPort = Integer.parseInt(sslPortProp);
    String host = props.getProperty(Constants.HOST_PROPERTY_NAME);
    LOG.info("Starting SSL on port {} using keystore at {}", sslPort, jksPath);

    final NettyMQTTHandler handler = new NettyMQTTHandler();
    handler.setMessaging(messaging);
    initFactory(host, sslPort, new PipelineInitializer() {
        @Override
        void init(ChannelPipeline pipeline) throws Exception {
            InputStream jksInputStream = jksDatastore(jksPath);
            SSLContext serverContext = SSLContext.getInstance("TLS");
            final KeyStore ks = KeyStore.getInstance("JKS");
            ks.load(jksInputStream, keyStorePassword.toCharArray());
            final KeyManagerFactory kmf = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(ks, keyManagerPassword.toCharArray());
            serverContext.init(kmf.getKeyManagers(), null, null);

            SSLEngine engine = serverContext.createSSLEngine();
            engine.setUseClientMode(false);
            final SslHandler sslHandler = new SslHandler(engine);

            pipeline.addLast("ssl", sslHandler);
            //pipeline.addFirst("metrics", new BytesMetricsHandler(m_metricsCollector));
            pipeline.addFirst("idleStateHandler",
                    new IdleStateHandler(0, 0, Constants.DEFAULT_CONNECT_TIMEOUT));
            pipeline.addAfter("idleStateHandler", "idleEventHandler", new MoquetteIdleTimoutHandler());
            //pipeline.addLast("logger", new LoggingHandler("Netty", LogLevel.ERROR));
            pipeline.addLast("decoder", new MQTTDecoder());
            pipeline.addLast("encoder", new MQTTEncoder());
            pipeline.addLast("metrics", new MessageMetricsHandler(m_metricsCollector));
            pipeline.addLast("handler", handler);
        }
    });
}