Search in sources :

Example 86 with SslHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler in project camel by apache.

the class HttpServerInitializerFactory method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    // create a new pipeline
    ChannelPipeline pipeline = ch.pipeline();
    SslHandler sslHandler = configureServerSSLOnDemand();
    if (sslHandler != null) {
        //TODO must close on SSL exception
        // sslHandler.setCloseOnSSLException(true);
        LOG.debug("Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
        pipeline.addLast("ssl", sslHandler);
    }
    pipeline.addLast("decoder", new HttpRequestDecoder(4096, configuration.getMaxHeaderSize(), 8192));
    List<ChannelHandler> decoders = consumer.getConfiguration().getDecoders();
    for (int x = 0; x < decoders.size(); x++) {
        ChannelHandler decoder = decoders.get(x);
        if (decoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            decoder = ((ChannelHandlerFactory) decoder).newChannelHandler();
        }
        pipeline.addLast("decoder-" + x, decoder);
    }
    pipeline.addLast("encoder", new HttpResponseEncoder());
    List<ChannelHandler> encoders = consumer.getConfiguration().getEncoders();
    for (int x = 0; x < encoders.size(); x++) {
        ChannelHandler encoder = encoders.get(x);
        if (encoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            encoder = ((ChannelHandlerFactory) encoder).newChannelHandler();
        }
        pipeline.addLast("encoder-" + x, encoder);
    }
    pipeline.addLast("aggregator", new HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
    if (supportCompressed()) {
        pipeline.addLast("deflater", new HttpContentCompressor());
    }
    int port = consumer.getConfiguration().getPort();
    ChannelHandler handler = consumer.getEndpoint().getComponent().getMultiplexChannelHandler(port).getChannelHandler();
    if (consumer.getConfiguration().isUsingExecutorService()) {
        EventExecutorGroup applicationExecutor = consumer.getEndpoint().getComponent().getExecutorService();
        pipeline.addLast(applicationExecutor, "handler", handler);
    } else {
        pipeline.addLast("handler", handler);
    }
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) ChannelHandlerFactory(org.apache.camel.component.netty4.ChannelHandlerFactory) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) HttpContentCompressor(io.netty.handler.codec.http.HttpContentCompressor) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 87 with SslHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler in project camel by apache.

the class DefaultServerInitializerFactory method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    // create a new pipeline
    ChannelPipeline channelPipeline = ch.pipeline();
    SslHandler sslHandler = configureServerSSLOnDemand();
    if (sslHandler != null) {
        //TODO  must close on SSL exception
        //sslHandler.setCloseOnSSLException(true);
        LOG.debug("Server SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
        addToPipeline("ssl", channelPipeline, sslHandler);
    }
    List<ChannelHandler> encoders = consumer.getConfiguration().getEncoders();
    for (int x = 0; x < encoders.size(); x++) {
        ChannelHandler encoder = encoders.get(x);
        if (encoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            encoder = ((ChannelHandlerFactory) encoder).newChannelHandler();
        }
        addToPipeline("encoder-" + x, channelPipeline, encoder);
    }
    List<ChannelHandler> decoders = consumer.getConfiguration().getDecoders();
    for (int x = 0; x < decoders.size(); x++) {
        ChannelHandler decoder = decoders.get(x);
        if (decoder instanceof ChannelHandlerFactory) {
            // use the factory to create a new instance of the channel as it may not be shareable
            decoder = ((ChannelHandlerFactory) decoder).newChannelHandler();
        }
        addToPipeline("decoder-" + x, channelPipeline, decoder);
    }
    if (consumer.getConfiguration().isUsingExecutorService()) {
        // Just use EventExecutorGroup from the Netty Component
        EventExecutorGroup applicationExecutor = consumer.getEndpoint().getComponent().getExecutorService();
        addToPipeline("handler", channelPipeline, applicationExecutor, new ServerChannelHandler(consumer));
    } else {
        // still use the worker event loop group here
        addToPipeline("handler", channelPipeline, new ServerChannelHandler(consumer));
    }
    LOG.trace("Created ChannelPipeline: {}", channelPipeline);
}
Also used : EventExecutorGroup(io.netty.util.concurrent.EventExecutorGroup) ServerChannelHandler(org.apache.camel.component.netty4.handlers.ServerChannelHandler) ChannelHandler(io.netty.channel.ChannelHandler) ServerChannelHandler(org.apache.camel.component.netty4.handlers.ServerChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 88 with SslHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler in project camel by apache.

the class NettyConfiguration method parseURI.

public void parseURI(URI uri, Map<String, Object> parameters, NettyComponent component, String... supportedProtocols) throws Exception {
    protocol = uri.getScheme();
    boolean found = false;
    for (String supportedProtocol : supportedProtocols) {
        if (protocol != null && protocol.equalsIgnoreCase(supportedProtocol)) {
            found = true;
            break;
        }
    }
    if (!found) {
        throw new IllegalArgumentException("Unrecognized Netty protocol: " + protocol + " for uri: " + uri);
    }
    setHost(uri.getHost());
    if (uri.getPort() != -1) {
        setPort(uri.getPort());
    }
    ssl = component.getAndRemoveOrResolveReferenceParameter(parameters, "ssl", boolean.class, false);
    sslHandler = component.getAndRemoveOrResolveReferenceParameter(parameters, "sslHandler", SslHandler.class, sslHandler);
    passphrase = component.getAndRemoveOrResolveReferenceParameter(parameters, "passphrase", String.class, passphrase);
    keyStoreFormat = component.getAndRemoveOrResolveReferenceParameter(parameters, "keyStoreFormat", String.class, keyStoreFormat == null ? "JKS" : keyStoreFormat);
    securityProvider = component.getAndRemoveOrResolveReferenceParameter(parameters, "securityProvider", String.class, securityProvider == null ? "SunX509" : securityProvider);
    keyStoreFile = component.getAndRemoveOrResolveReferenceParameter(parameters, "keyStoreFile", File.class, keyStoreFile);
    trustStoreFile = component.getAndRemoveOrResolveReferenceParameter(parameters, "trustStoreFile", File.class, trustStoreFile);
    keyStoreResource = component.getAndRemoveOrResolveReferenceParameter(parameters, "keyStoreResource", String.class, keyStoreResource);
    trustStoreResource = component.getAndRemoveOrResolveReferenceParameter(parameters, "trustStoreResource", String.class, trustStoreResource);
    // clientPipelineFactory is @deprecated and to be removed
    clientInitializerFactory = component.getAndRemoveOrResolveReferenceParameter(parameters, "clientPipelineFactory", ClientInitializerFactory.class, clientInitializerFactory);
    clientInitializerFactory = component.getAndRemoveOrResolveReferenceParameter(parameters, "clientInitializerFactory", ClientInitializerFactory.class, clientInitializerFactory);
    // serverPipelineFactory is @deprecated and to be removed
    serverInitializerFactory = component.getAndRemoveOrResolveReferenceParameter(parameters, "serverPipelineFactory", ServerInitializerFactory.class, serverInitializerFactory);
    serverInitializerFactory = component.getAndRemoveOrResolveReferenceParameter(parameters, "serverInitializerFactory", ServerInitializerFactory.class, serverInitializerFactory);
    // set custom encoders and decoders first
    List<ChannelHandler> referencedEncoders = component.resolveAndRemoveReferenceListParameter(parameters, "encoders", ChannelHandler.class, null);
    addToHandlersList(encoders, referencedEncoders, ChannelHandler.class);
    List<ChannelHandler> referencedDecoders = component.resolveAndRemoveReferenceListParameter(parameters, "decoders", ChannelHandler.class, null);
    addToHandlersList(decoders, referencedDecoders, ChannelHandler.class);
    // then set parameters with the help of the camel context type converters
    EndpointHelper.setReferenceProperties(component.getCamelContext(), this, parameters);
    EndpointHelper.setProperties(component.getCamelContext(), this, parameters);
    // additional netty options, we don't want to store an empty map, so set it as null if empty
    options = IntrospectionSupport.extractProperties(parameters, "option.");
    if (options != null && options.isEmpty()) {
        options = null;
    }
    // add default encoders and decoders
    if (encoders.isEmpty() && decoders.isEmpty()) {
        if (isAllowDefaultCodec()) {
            if ("udp".equalsIgnoreCase(protocol)) {
                encoders.add(ChannelHandlerFactories.newDatagramPacketEncoder());
            }
            // are we textline or object?
            if (isTextline()) {
                Charset charset = getEncoding() != null ? Charset.forName(getEncoding()) : CharsetUtil.UTF_8;
                encoders.add(ChannelHandlerFactories.newStringEncoder(charset, protocol));
                ByteBuf[] delimiters = delimiter == TextLineDelimiter.LINE ? Delimiters.lineDelimiter() : Delimiters.nulDelimiter();
                decoders.add(ChannelHandlerFactories.newDelimiterBasedFrameDecoder(decoderMaxLineLength, delimiters, protocol));
                decoders.add(ChannelHandlerFactories.newStringDecoder(charset, protocol));
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Using textline encoders and decoders with charset: {}, delimiter: {} and decoderMaxLineLength: {}", new Object[] { charset, delimiter, decoderMaxLineLength });
                }
            } else if ("udp".equalsIgnoreCase(protocol) && isUdpByteArrayCodec()) {
                encoders.add(ChannelHandlerFactories.newByteArrayEncoder(protocol));
                decoders.add(ChannelHandlerFactories.newByteArrayDecoder(protocol));
            } else {
                // object serializable is then used
                encoders.add(ChannelHandlerFactories.newObjectEncoder(protocol));
                decoders.add(ChannelHandlerFactories.newObjectDecoder(protocol));
                LOG.debug("Using object encoders and decoders");
            }
            if ("udp".equalsIgnoreCase(protocol)) {
                decoders.add(ChannelHandlerFactories.newDatagramPacketDecoder());
            }
        } else {
            LOG.debug("No encoders and decoders will be used");
        }
    } else {
        LOG.debug("Using configured encoders and/or decoders");
    }
}
Also used : Charset(java.nio.charset.Charset) ChannelHandler(io.netty.channel.ChannelHandler) ByteBuf(io.netty.buffer.ByteBuf) File(java.io.File) SslHandler(io.netty.handler.ssl.SslHandler)

Example 89 with SslHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler in project camel by apache.

the class NettyEndpoint method getSSLSession.

protected SSLSession getSSLSession(ChannelHandlerContext ctx) {
    final SslHandler sslHandler = ctx.pipeline().get(SslHandler.class);
    SSLSession sslSession = null;
    if (sslHandler != null) {
        sslSession = sslHandler.engine().getSession();
    }
    return sslSession;
}
Also used : SSLSession(javax.net.ssl.SSLSession) SslHandler(io.netty.handler.ssl.SslHandler)

Example 90 with SslHandler

use of org.apache.flink.shaded.netty4.io.netty.handler.ssl.SslHandler in project camel by apache.

the class LumberjackUtil method sendMessages.

static List<Integer> sendMessages(int port, SSLContextParameters sslContextParameters) throws InterruptedException {
    NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();
    try {
        // This list will hold the acknowledgment response sequence numbers
        List<Integer> responses = new ArrayList<>();
        // This initializer configures the SSL and an acknowledgment recorder
        ChannelInitializer<Channel> initializer = new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                if (sslContextParameters != null) {
                    SSLEngine sslEngine = sslContextParameters.createSSLContext(null).createSSLEngine();
                    sslEngine.setUseClientMode(true);
                    pipeline.addLast(new SslHandler(sslEngine));
                }
                // Add the response recorder
                pipeline.addLast(new SimpleChannelInboundHandler<ByteBuf>() {

                    @Override
                    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
                        assertEquals(msg.readUnsignedByte(), (short) '2');
                        assertEquals(msg.readUnsignedByte(), (short) 'A');
                        synchronized (responses) {
                            responses.add(msg.readInt());
                        }
                    }
                });
            }
        };
        // Connect to the server
        Channel channel = //
        new Bootstrap().group(//
        eventLoopGroup).channel(//
        NioSocketChannel.class).handler(//
        initializer).connect("127.0.0.1", port).sync().channel();
        // Send the 2 window frames
        TimeUnit.MILLISECONDS.sleep(100);
        channel.writeAndFlush(readSample("io/window10"));
        TimeUnit.MILLISECONDS.sleep(100);
        channel.writeAndFlush(readSample("io/window15"));
        TimeUnit.MILLISECONDS.sleep(100);
        channel.close();
        synchronized (responses) {
            return responses;
        }
    } finally {
        eventLoopGroup.shutdownGracefully();
    }
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) IOException(java.io.IOException) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup)

Aggregations

SslHandler (io.netty.handler.ssl.SslHandler)141 SSLEngine (javax.net.ssl.SSLEngine)51 ChannelPipeline (io.netty.channel.ChannelPipeline)37 Channel (io.netty.channel.Channel)29 ChannelHandler (io.netty.channel.ChannelHandler)23 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)23 SslContext (io.netty.handler.ssl.SslContext)21 IOException (java.io.IOException)16 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)15 Test (org.junit.Test)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)14 ChannelInitializer (io.netty.channel.ChannelInitializer)13 SocketChannel (io.netty.channel.socket.SocketChannel)13 SSLSession (javax.net.ssl.SSLSession)12 ByteBuf (io.netty.buffer.ByteBuf)11 ChunkedWriteHandler (io.netty.handler.stream.ChunkedWriteHandler)11 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)11 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)10 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)10 File (java.io.File)10