Example usage for io.netty.handler.ssl SslHandler SslHandler

List of usage examples for io.netty.handler.ssl SslHandler SslHandler

Introduction

In this page you can find the example usage for io.netty.handler.ssl SslHandler SslHandler.

Prototype

public SslHandler(SSLEngine engine) 

Source Link

Document

Creates a new instance which runs all delegated tasks directly on the EventExecutor .

Usage

From source file:com.seagate.kinetic.simulator.io.provider.nio.http.HttpChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    ChannelPipeline p = ch.pipeline();/* www  .j  a  v  a  2 s.  c  o  m*/

    boolean isSsl = Boolean.getBoolean("kinetic.io.https");

    if (isSsl) {
        SSLEngine engine = SslContextFactory.getServerContext().createSSLEngine();

        engine.setUseClientMode(false);

        p.addLast("ssl", new SslHandler(engine));

        logger.info("ssl handler added, https is enabled ...");
    }

    p.addLast("decoder", new HttpRequestDecoder(1024, 4 * 1024, 4 * 1024 * 1024));

    p.addLast("encoder", new HttpResponseEncoder());

    p.addLast("aggregator", new HttpObjectAggregator(4 * 1024 * 1024));

    p.addLast("handler", new HttpMessageServiceHandler(lcservice));

    logger.info("http channel initialized. ssl/tls enabled=" + isSsl);
}

From source file:com.seagate.kinetic.simulator.io.provider.nio.ssl.SslChannelInitializer.java

License:Open Source License

@Override
protected void initChannel(SocketChannel ch) throws Exception {

    ChannelPipeline pipeline = ch.pipeline();

    SSLEngine engine = SslContextFactory.getServerContext().createSSLEngine();

    engine.setUseClientMode(false);//from   w w  w . j  a  va  2s  . c  o  m

    // enable TLS v1.x protocols.
    TlsUtil.enableSupportedProtocols(engine);

    // add ssl handler
    pipeline.addLast("ssl", new SslHandler(engine));

    // decoder
    pipeline.addLast("decoder", new KineticDecoder());
    // encoder
    pipeline.addLast("encoder", new KineticEncoder());

    // pipeline.addLast("handler", new SslMessageServiceHandler(lcservice));

    pipeline.addLast("handler", new NioMessageServiceHandler(lcservice, true));

    logger.info("ssl nio channel initialized ... ");
}

From source file:com.squareup.okhttp.benchmarks.NettyHttpClient.java

License:Apache License

@Override
public void prepare(final Benchmark benchmark) {
    this.concurrencyLevel = benchmark.concurrencyLevel;
    this.targetBacklog = benchmark.targetBacklog;

    ChannelInitializer<SocketChannel> channelInitializer = new ChannelInitializer<SocketChannel>() {
        @Override/* ww w. ja v  a 2  s .  c  om*/
        public void initChannel(SocketChannel channel) throws Exception {
            ChannelPipeline pipeline = channel.pipeline();

            if (benchmark.tls) {
                SSLContext sslContext = SslContextBuilder.localhost();
                SSLEngine engine = sslContext.createSSLEngine();
                engine.setUseClientMode(true);
                pipeline.addLast("ssl", new SslHandler(engine));
            }

            pipeline.addLast("codec", new HttpClientCodec());
            pipeline.addLast("inflater", new HttpContentDecompressor());
            pipeline.addLast("handler", new HttpChannel(channel));
        }
    };

    bootstrap = new Bootstrap();
    bootstrap.group(new NioEventLoopGroup(concurrencyLevel))
            .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT).channel(NioSocketChannel.class)
            .handler(channelInitializer);
}

From source file:com.streamsets.pipeline.stage.origin.tcp.TCPServerSource.java

License:Apache License

private void createAndStartTCPServer(List<ConfigIssue> issues, String portsField) {
    tcpServer = new TCPConsumingServer(config.enableEpoll, config.numThreads, addresses,
            new ChannelInitializer<SocketChannel>() {
                @Override//from  w  ww  .j  a  va  2 s  .c  o  m
                public void initChannel(SocketChannel ch) throws Exception {
                    if (config.tlsConfigBean.isEnabled()) {
                        // Add TLS handler into pipeline in the first position
                        ch.pipeline().addFirst("TLS", new SslHandler(config.tlsConfigBean.createSslEngine()));
                    }

                    ch.pipeline().addLast(
                            // first, decode the ByteBuf into some POJO type extending MessageToRecord
                            buildByteBufToMessageDecoderChain(issues).toArray(new ChannelHandler[0]));

                    ch.pipeline().addLast(
                            // next, handle MessageToRecord instances to build SDC records and errors
                            new TCPObjectToRecordHandler(getContext(), config.batchSize, config.maxWaitTime,
                                    pipelineIdsToFail::put, getContext().createELEval(RECORD_PROCESSED_EL_NAME),
                                    getContext().createELVars(), config.recordProcessedAckMessage,
                                    getContext().createELEval(BATCH_COMPLETED_EL_NAME),
                                    getContext().createELVars(), config.batchCompletedAckMessage,
                                    config.timeZoneID, Charset.forName(config.ackMessageCharset)));

                    if (config.readTimeout > 0) {
                        ch.pipeline().addLast(new ReadTimeoutHandler(config.readTimeout));
                    }
                }
            });
    if (issues.isEmpty()) {
        try {
            tcpServer.listen();
            tcpServer.start();
        } catch (Exception ex) {
            tcpServer.destroy();
            tcpServer = null;

            if (ex instanceof SocketException && privilegedPortUsage) {
                issues.add(getContext().createConfigIssue(Groups.TCP.name(), portsField, Errors.TCP_04,
                        config.ports, ex));
            } else {
                LOG.debug("Caught exception while starting up TCP server: {}", ex);
                issues.add(getContext().createConfigIssue(null, null, Errors.TCP_00, addresses.toString(),
                        ex.toString(), ex));
            }
        }
    }
}

From source file:com.tc.websocket.server.WebSocketServerInitializer.java

License:Apache License

@Override
public void initChannel(SocketChannel ch) throws Exception {

    IConfig cfg = Config.getInstance();/* w ww . j  a  v a 2s.  co m*/

    //if we need to check for ByteBuf leaks.
    if (cfg.isLeakDetector()) {
        ResourceLeakDetector.setLevel(Level.ADVANCED);
    }

    //so we get enough data to build our pipeline
    ch.config().setRecvByteBufAllocator(new FixedRecvByteBufAllocator(1024));

    ChannelPipeline pipeline = ch.pipeline();

    int incomingPort = ch.localAddress().getPort();

    //if users are coming in on a different port than the proxy port we need to redirect them.
    if (cfg.isProxy() && cfg.getPort() != incomingPort) {
        redirectBuilder.apply(pipeline);
        return;
    }

    if (cfg.isEncrypted()) {
        SslContext sslContext = factory.createSslContext(Config.getInstance());
        SSLEngine engine = sslContext.newEngine(ch.alloc());
        engine.setUseClientMode(false);
        engine.setNeedClientAuth(cfg.isCertAuth());
        ch.pipeline().addFirst("ssl", new SslHandler(engine));
    }

    if (cfg.isProxy()) {
        pipeline.channel().config().setAutoRead(false);
        pipeline.addLast(
                guicer.inject(new ProxyFrontendHandler(cfg.getProxyBackendHost(), cfg.getProxyBackendPort())));

    } else {
        websocketBuilder.apply(pipeline);
    }

}

From source file:com.titilink.camel.rest.client.HttpClientInitializer.java

License:LGPL

@Override
public void initChannel(SocketChannel ch) throws Exception {
    // Create a default pipeline implementation.
    ChannelPipeline p = ch.pipeline();/*from   w w w .j  a v  a 2 s.c  o m*/

    //        p.addLast("log", new LoggingHandler(LogLevel.INFO));
    // Enable HTTPS if necessary.
    if (ssl) {
        SSLEngine engine = ClientSslContextFactory.getClientContext().createSSLEngine();
        engine.setUseClientMode(true);

        p.addLast("ssl", new SslHandler(engine));
    }

    p.addLast("codec", new HttpClientCodec());

    // Remove the following line if you don't want automatic content decompression.
    p.addLast("inflater", new HttpContentDecompressor());

    // Uncomment the following line if you don't want to handle HttpChunks.
    //p.addLast("aggregator", new HttpObjectAggregator(1048576));

    p.addLast("handler", new HttpClientHandler());
}

From source file:com.titilink.camel.rest.server.HttpServerInitializer.java

License:LGPL

@Override
public void initChannel(SocketChannel ch) throws Exception {
    //ChannelPipeline
    ChannelPipeline pipeline = ch.pipeline();

    if (supportSSL) {
        SSLEngine engine = ServerSslContextFactory.getServerContext().createSSLEngine();
        engine.setUseClientMode(false);/*from  w ww  .j av  a2s .com*/
        pipeline.addLast("ssl", new SslHandler(engine));
    }

    //Protocol??
    pipeline.addLast("decoder", new HttpRequestDecoder());
    pipeline.addLast("aggregator", new HttpObjectAggregator(196608));
    pipeline.addLast("encoder", new HttpResponseEncoder());
    pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());

    pipeline.addLast("handler", new HttpServerHandler(httpConnector));
}

From source file:com.vmware.dcp.common.http.netty.NettyHttpClientRequestInitializer.java

License:Open Source License

@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();/*from w ww.j  a va2 s. c o  m*/
    ch.config().setAllocator(NettyChannelContext.ALLOCATOR);
    ch.config().setSendBufferSize(NettyChannelContext.BUFFER_SIZE);
    ch.config().setReceiveBufferSize(NettyChannelContext.BUFFER_SIZE);
    if (this.pool.getSSLContext() != null) {
        SSLEngine engine = this.pool.getSSLContext().createSSLEngine();
        engine.setUseClientMode(true);
        p.addLast(SSL_HANDLER, new SslHandler(engine));
    }
    p.addLast(ENCODER_HANDLER, new HttpRequestEncoder());
    p.addLast(DECODER_HANDLER, new HttpResponseDecoder());
    p.addLast(AGGREGATOR_HANDLER, new HttpObjectAggregator(SocketContext.MAX_REQUEST_SIZE));
    p.addLast(DCP_HANDLER, new NettyHttpServerResponseHandler(this.pool));
}

From source file:com.vmware.xenon.common.http.netty.NettyHttpClientRequestInitializer.java

License:Open Source License

/**
 * initChannel is called by Netty when a channel is first used.
 *///from  w  w w.j a  v a 2 s.c  o  m
@Override
public void initChannel(SocketChannel ch) {
    ChannelPipeline p = ch.pipeline();
    ch.config().setAllocator(NettyChannelContext.ALLOCATOR);
    ch.config().setSendBufferSize(NettyChannelContext.BUFFER_SIZE);
    ch.config().setReceiveBufferSize(NettyChannelContext.BUFFER_SIZE);
    if (this.pool.getSSLContext() != null) {
        if (this.isHttp2Only) {
            throw new IllegalArgumentException("HTTP/2 with SSL is not supported");
        }
        SSLEngine engine = this.pool.getSSLContext().createSSLEngine();
        engine.setUseClientMode(true);
        p.addLast(SSL_HANDLER, new SslHandler(engine));
    }

    HttpClientCodec http1_codec = new HttpClientCodec(NettyChannelContext.MAX_INITIAL_LINE_LENGTH,
            NettyChannelContext.MAX_HEADER_SIZE, NettyChannelContext.MAX_CHUNK_SIZE, false, false);

    // The HttpClientCodec combines the HttpRequestEncoder and the HttpResponseDecoder, and it
    // also provides a method for upgrading the protocol, which we use to support HTTP/2.
    p.addLast(HTTP1_CODEC, http1_codec);

    if (this.isHttp2Only) {
        try {
            NettyHttpToHttp2Handler connectionHandler = makeHttp2ConnectionHandler();
            Http2ClientUpgradeCodec upgradeCodec = new Http2ClientUpgradeCodec(connectionHandler);
            HttpClientUpgradeHandler upgradeHandler = new HttpClientUpgradeHandler(http1_codec, upgradeCodec,
                    this.requestPayloadSizeLimit);

            p.addLast(UPGRADE_HANDLER, upgradeHandler);
            p.addLast(UPGRADE_REQUEST, new UpgradeRequestHandler());

            // This promise will be triggered when we negotiate the settings.
            // That's important because we can't send user data until the negotiation is done
            ChannelPromise settingsPromise = ch.newPromise();
            p.addLast("settings-handler", new Http2SettingsHandler(settingsPromise));
            ch.attr(NettyChannelContext.SETTINGS_PROMISE_KEY).set(settingsPromise);
            p.addLast(EVENT_LOGGER, new NettyHttp2UserEventLogger(this.debugLogging));
        } catch (Throwable ex) {
            Utils.log(NettyHttpClientRequestInitializer.class,
                    NettyHttpClientRequestInitializer.class.getSimpleName(), Level.WARNING,
                    "Channel Initializer exception: %s", ex);
            throw ex;
        }
    } else {
        // The HttpObjectAggregator is not needed for HTTP/2. For HTTP/1.1 it
        // aggregates the HttpMessage and HttpContent into the FullHttpResponse
        p.addLast(AGGREGATOR_HANDLER, new HttpObjectAggregator(this.requestPayloadSizeLimit));
    }
    p.addLast(XENON_HANDLER, new NettyHttpServerResponseHandler(this.pool));
}

From source file:com.xmpp.push.androidpn.server.xmpp.handler.XMPPDecodeHandler.java

License:Apache License

public XMPPDecodeHandler(String serverName, SSLConfig sslConfig) {
    super();/*w  ww.ja va  2s. co  m*/

    this.serverName = checkNotNull(serverName);
    this.sslConfig = checkNotNull(sslConfig);

    status = Status.CONNECT;

    SSLContext sslContext = sslConfig.getSslContext();
    sslEngine = sslContext.createSSLEngine();

    sslEngine.setUseClientMode(false);

    // sslEngine.setNeedClientAuth(true);
    sslEngine.setWantClientAuth(true);

    // init sslHandler
    sslHandler = new SslHandler(sslEngine);
}