Search in sources :

Example 16 with SslHandler

use of org.jboss.netty.handler.ssl.SslHandler in project camel by apache.

the class DefaultServerPipelineFactory method getPipeline.

@Override
public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline channelPipeline = Channels.pipeline();
    SslHandler sslHandler = configureServerSSLOnDemand();
    if (sslHandler != null) {
        // 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().isOrderedThreadPoolExecutor()) {
        // this must be added just before the ServerChannelHandler
        // use ordered thread pool, to ensure we process the events in order, and can send back
        // replies in the expected order. eg this is required by TCP.
        // and use a Camel thread factory so we have consistent thread namings
        ExecutionHandler executionHandler = new ExecutionHandler(consumer.getEndpoint().getComponent().getExecutorService());
        addToPipeline("executionHandler", channelPipeline, executionHandler);
        LOG.debug("Using OrderedMemoryAwareThreadPoolExecutor with core pool size: {}", consumer.getConfiguration().getMaximumPoolSize());
    }
    // our handler must be added last
    addToPipeline("handler", channelPipeline, new ServerChannelHandler(consumer));
    LOG.trace("Created ChannelPipeline: {}", channelPipeline);
    return channelPipeline;
}
Also used : ServerChannelHandler(org.apache.camel.component.netty.handlers.ServerChannelHandler) ExecutionHandler(org.jboss.netty.handler.execution.ExecutionHandler) ServerChannelHandler(org.apache.camel.component.netty.handlers.ServerChannelHandler) ChannelHandler(org.jboss.netty.channel.ChannelHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) SslHandler(org.jboss.netty.handler.ssl.SslHandler)

Example 17 with SslHandler

use of org.jboss.netty.handler.ssl.SslHandler in project camel by apache.

the class HttpClientPipelineFactory method getPipeline.

@Override
public ChannelPipeline getPipeline() throws Exception {
    ChannelPipeline pipeline = Channels.pipeline();
    SslHandler sslHandler = configureClientSSLOnDemand();
    if (sslHandler != null) {
        // must close on SSL exception
        sslHandler.setCloseOnSSLException(true);
        LOG.debug("Client SSL handler configured and added as an interceptor against the ChannelPipeline: {}", sslHandler);
        pipeline.addLast("ssl", sslHandler);
    }
    pipeline.addLast("http", new HttpClientCodec());
    List<ChannelHandler> decoders = producer.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);
    }
    List<ChannelHandler> encoders = producer.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);
    }
    if (producer.getConfiguration().getRequestTimeout() > 0) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Using request timeout {} millis", producer.getConfiguration().getRequestTimeout());
        }
        ChannelHandler timeout = new ReadTimeoutHandler(producer.getEndpoint().getTimer(), producer.getConfiguration().getRequestTimeout(), TimeUnit.MILLISECONDS);
        pipeline.addLast("timeout", timeout);
    }
    // handler to route Camel messages
    pipeline.addLast("handler", new HttpClientChannelHandler(producer));
    return pipeline;
}
Also used : HttpClientChannelHandler(org.apache.camel.component.netty.http.handlers.HttpClientChannelHandler) ChannelHandlerFactory(org.apache.camel.component.netty.ChannelHandlerFactory) ReadTimeoutHandler(org.jboss.netty.handler.timeout.ReadTimeoutHandler) HttpClientChannelHandler(org.apache.camel.component.netty.http.handlers.HttpClientChannelHandler) ChannelHandler(org.jboss.netty.channel.ChannelHandler) HttpClientCodec(org.jboss.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) SslHandler(org.jboss.netty.handler.ssl.SslHandler)

Example 18 with SslHandler

use of org.jboss.netty.handler.ssl.SslHandler in project camel by apache.

the class HttpServerPipelineFactory method getPipeline.

@Override
public ChannelPipeline getPipeline() throws Exception {
    // Create a default pipeline implementation.
    ChannelPipeline pipeline = Channels.pipeline();
    SslHandler sslHandler = configureServerSSLOnDemand();
    if (sslHandler != null) {
        // 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("aggregator", new HttpChunkAggregator(configuration.getChunkedMaxContentLength()));
    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);
    }
    if (supportCompressed()) {
        pipeline.addLast("deflater", new HttpContentCompressor());
    }
    if (consumer.getConfiguration().isOrderedThreadPoolExecutor()) {
        // this must be added just before the HttpServerMultiplexChannelHandler
        // use ordered thread pool, to ensure we process the events in order, and can send back
        // replies in the expected order. eg this is required by TCP.
        // and use a Camel thread factory so we have consistent thread namings
        ExecutionHandler executionHandler = new ExecutionHandler(consumer.getEndpoint().getComponent().getExecutorService());
        pipeline.addLast("executionHandler", executionHandler);
        LOG.debug("Using OrderedMemoryAwareThreadPoolExecutor with core pool size: {}", consumer.getConfiguration().getMaximumPoolSize());
    }
    int port = consumer.getConfiguration().getPort();
    ChannelHandler handler = consumer.getEndpoint().getComponent().getMultiplexChannelHandler(port).getChannelHandler();
    pipeline.addLast("handler", handler);
    return pipeline;
}
Also used : HttpResponseEncoder(org.jboss.netty.handler.codec.http.HttpResponseEncoder) ChannelHandlerFactory(org.apache.camel.component.netty.ChannelHandlerFactory) HttpRequestDecoder(org.jboss.netty.handler.codec.http.HttpRequestDecoder) HttpContentCompressor(org.jboss.netty.handler.codec.http.HttpContentCompressor) HttpChunkAggregator(org.jboss.netty.handler.codec.http.HttpChunkAggregator) ExecutionHandler(org.jboss.netty.handler.execution.ExecutionHandler) ChannelHandler(org.jboss.netty.channel.ChannelHandler) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) SslHandler(org.jboss.netty.handler.ssl.SslHandler)

Example 19 with SslHandler

use of org.jboss.netty.handler.ssl.SslHandler in project cdap by caskdata.

the class HttpRequestHandler method messageReceived.

@Override
public void messageReceived(final ChannelHandlerContext ctx, MessageEvent event) throws Exception {
    if (channelClosed) {
        return;
    }
    final Channel inboundChannel = event.getChannel();
    Object msg = event.getMessage();
    if (msg instanceof HttpChunk) {
        // This case below should never happen this would mean we get Chunks before HTTPMessage.
        if (chunkSender == null) {
            throw new HandlerException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "Chunk received and event sender is null");
        }
        chunkSender.send(msg);
    } else if (msg instanceof HttpRequest) {
        // Discover and forward event.
        HttpRequest request = (HttpRequest) msg;
        request = applyProxyRules(request);
        // Suspend incoming traffic until connected to the outbound service.
        inboundChannel.setReadable(false);
        WrappedDiscoverable discoverable = getDiscoverable(request, (InetSocketAddress) inboundChannel.getLocalAddress());
        // If no event sender, make new connection, otherwise reuse existing one.
        MessageSender sender = discoveryLookup.get(discoverable);
        if (sender == null || !sender.isConnected()) {
            InetSocketAddress address = discoverable.getSocketAddress();
            ChannelFuture future = clientBootstrap.connect(address);
            final Channel outboundChannel = future.getChannel();
            outboundChannel.getPipeline().addAfter("request-encoder", "outbound-handler", new OutboundHandler(inboundChannel));
            if (Arrays.equals(Constants.Security.SSL_URI_SCHEME.getBytes(), discoverable.getPayload())) {
                SSLContext clientContext;
                try {
                    clientContext = SSLContext.getInstance("TLS");
                    clientContext.init(null, PermissiveTrustManagerFactory.getTrustManagers(), null);
                } catch (NoSuchAlgorithmException | KeyManagementException e) {
                    throw new RuntimeException("SSL is enabled for app-fabric but failed to create SSLContext in the router " + "client.", e);
                }
                SSLEngine engine = clientContext.createSSLEngine();
                engine.setUseClientMode(true);
                engine.setEnabledProtocols(new String[] { "TLSv1.2", "TLSv1.1", "TLSv1" });
                outboundChannel.getPipeline().addFirst("ssl", new SslHandler(engine));
                LOG.trace("Adding ssl handler to the pipeline.");
            }
            sender = new MessageSender(inboundChannel, future);
            discoveryLookup.put(discoverable, sender);
            // Remember the in-flight outbound channel
            inboundChannel.setAttachment(outboundChannel);
            outboundChannel.getCloseFuture().addListener(new ChannelFutureListener() {

                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    inboundChannel.getPipeline().execute(new Runnable() {

                        @Override
                        public void run() {
                            // close the inbound channel as well if it carries the in-flight request
                            if (outboundChannel.equals(inboundChannel.getAttachment())) {
                                closeOnFlush(inboundChannel);
                            }
                        }
                    });
                }
            });
        } else {
            Channel outboundChannel = (Channel) inboundChannel.getAttachment();
            if (outboundChannel != null) {
                // Set outbound channel to be readable in case previous request has set it as non-readable
                outboundChannel.setReadable(true);
            }
        }
        // Send the message.
        sender.send(request);
        inboundChannel.setReadable(true);
        //Save the channelFuture for subsequent chunks
        if (request.isChunked()) {
            chunkSender = sender;
        }
    } else {
        super.messageReceived(ctx, event);
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) ChannelFuture(org.jboss.netty.channel.ChannelFuture) HandlerException(co.cask.cdap.common.HandlerException) InetSocketAddress(java.net.InetSocketAddress) SSLEngine(javax.net.ssl.SSLEngine) Channel(org.jboss.netty.channel.Channel) SSLContext(javax.net.ssl.SSLContext) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) SslHandler(org.jboss.netty.handler.ssl.SslHandler) HttpChunk(org.jboss.netty.handler.codec.http.HttpChunk)

Example 20 with SslHandler

use of org.jboss.netty.handler.ssl.SslHandler in project opennms by OpenNMS.

the class AsyncBasicDetectorNettyImpl method isServiceDetected.

/**
 * {@inheritDoc}
 */
@Override
public final DetectFuture isServiceDetected(final InetAddress address) {
    DetectFuture detectFuture = new DetectFutureFailedImpl(this, new IllegalStateException());
    try {
        ClientBootstrap bootstrap = new ClientBootstrap(m_factory);
        bootstrap.setPipelineFactory(new ChannelPipelineFactory() {

            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline retval = Channels.pipeline();
                // Upstream handlers
                // retval.addLast("retryHandler", new RetryChannelHandler());
                appendToPipeline(retval);
                // Downstream handlers
                retval.addLast("detectorHandler", getDetectorHandler(getConversation()));
                if (isUseSSLFilter()) {
                    // Use a relaxed SSL context
                    retval.addLast("sslHandler", new SslHandler(createClientSSLContext().createSSLEngine()));
                }
                return retval;
            }
        });
        bootstrap.setOption("tcpNoDelay", true);
        bootstrap.setOption("keepAlive", true);
        SocketAddress remoteAddress = new InetSocketAddress(address, getPort());
        ChannelFuture future = bootstrap.connect(remoteAddress);
        future.addListener(new RetryChannelFutureListener(remoteAddress, this.getRetries()));
        detectFuture = new DetectFutureNettyImpl(this, future);
    } catch (Throwable e) {
        detectFuture = new DetectFutureFailedImpl(this, e);
    }
    return detectFuture;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) ServiceDetectionFailedException(org.opennms.netmgt.provision.support.DetectFutureNettyImpl.ServiceDetectionFailedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ChannelPipeline(org.jboss.netty.channel.ChannelPipeline) SslHandler(org.jboss.netty.handler.ssl.SslHandler) DetectFuture(org.opennms.netmgt.provision.DetectFuture) ClientBootstrap(org.jboss.netty.bootstrap.ClientBootstrap) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ChannelPipelineFactory(org.jboss.netty.channel.ChannelPipelineFactory)

Aggregations

SslHandler (org.jboss.netty.handler.ssl.SslHandler)24 SSLEngine (javax.net.ssl.SSLEngine)16 ChannelPipeline (org.jboss.netty.channel.ChannelPipeline)10 SslConfiguration (com.cloudhopper.smpp.ssl.SslConfiguration)8 SslContextFactory (com.cloudhopper.smpp.ssl.SslContextFactory)8 InetSocketAddress (java.net.InetSocketAddress)7 ChannelHandler (org.jboss.netty.channel.ChannelHandler)5 Channel (org.jboss.netty.channel.Channel)4 ChannelFuture (org.jboss.netty.channel.ChannelFuture)4 HttpRequestDecoder (org.jboss.netty.handler.codec.http.HttpRequestDecoder)4 HttpResponseEncoder (org.jboss.netty.handler.codec.http.HttpResponseEncoder)4 SmppSessionPduDecoder (com.cloudhopper.smpp.channel.SmppSessionPduDecoder)3 RecoverablePduException (com.cloudhopper.smpp.type.RecoverablePduException)3 UnrecoverablePduException (com.cloudhopper.smpp.type.UnrecoverablePduException)3 SocketAddress (java.net.SocketAddress)3 SSLContext (javax.net.ssl.SSLContext)3 DefaultPduTranscoder (com.cloudhopper.smpp.transcoder.DefaultPduTranscoder)2 DefaultPduTranscoderContext (com.cloudhopper.smpp.transcoder.DefaultPduTranscoderContext)2 Executor (java.util.concurrent.Executor)2 ChannelHandlerFactory (org.apache.camel.component.netty.ChannelHandlerFactory)2