Search in sources :

Example 1 with ChannelHandlerFactory

use of org.apache.camel.component.netty4.ChannelHandlerFactory in project camel by apache.

the class HttpClientInitializerFactory method initChannel.

@Override
protected void initChannel(Channel ch) throws Exception {
    // create a new pipeline
    ChannelPipeline pipeline = ch.pipeline();
    SslHandler sslHandler = configureClientSSLOnDemand();
    if (sslHandler != null) {
        //TODO 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> 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);
    }
    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);
    }
    pipeline.addLast("aggregator", new HttpObjectAggregator(configuration.getChunkedMaxContentLength()));
    if (producer.getConfiguration().getRequestTimeout() > 0) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Using request timeout {} millis", producer.getConfiguration().getRequestTimeout());
        }
        ChannelHandler timeout = new ReadTimeoutHandler(producer.getConfiguration().getRequestTimeout(), TimeUnit.MILLISECONDS);
        pipeline.addLast("timeout", timeout);
    }
    // handler to route Camel messages
    pipeline.addLast("handler", new HttpClientChannelHandler(producer));
}
Also used : HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) HttpClientChannelHandler(org.apache.camel.component.netty4.http.handlers.HttpClientChannelHandler) ChannelHandlerFactory(org.apache.camel.component.netty4.ChannelHandlerFactory) ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) HttpClientChannelHandler(org.apache.camel.component.netty4.http.handlers.HttpClientChannelHandler) ChannelHandler(io.netty.channel.ChannelHandler) HttpClientCodec(io.netty.handler.codec.http.HttpClientCodec) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler)

Example 2 with ChannelHandlerFactory

use of org.apache.camel.component.netty4.ChannelHandlerFactory 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 3 with ChannelHandlerFactory

use of org.apache.camel.component.netty4.ChannelHandlerFactory 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 4 with ChannelHandlerFactory

use of org.apache.camel.component.netty4.ChannelHandlerFactory in project camel by apache.

the class DefaultClientInitializerFactory method initChannel.

protected void initChannel(Channel ch) throws Exception {
    // create a new pipeline
    ChannelPipeline channelPipeline = ch.pipeline();
    SslHandler sslHandler = configureClientSSLOnDemand();
    if (sslHandler != null) {
        //TODO  must close on SSL exception
        //sslHandler.setCloseOnSSLException(true);
        LOG.debug("Client SSL handler configured and added to the ChannelPipeline: {}", sslHandler);
        addToPipeline("ssl", channelPipeline, sslHandler);
    }
    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();
        }
        addToPipeline("decoder-" + x, channelPipeline, 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();
        }
        addToPipeline("encoder-" + x, channelPipeline, encoder);
    }
    // do we use request timeout?
    if (producer.getConfiguration().getRequestTimeout() > 0) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Using request timeout {} millis", producer.getConfiguration().getRequestTimeout());
        }
        ChannelHandler timeout = new ReadTimeoutHandler(producer.getConfiguration().getRequestTimeout(), TimeUnit.MILLISECONDS);
        addToPipeline("timeout", channelPipeline, timeout);
    }
    // our handler must be added last
    addToPipeline("handler", channelPipeline, new ClientChannelHandler(producer));
    LOG.trace("Created ChannelPipeline: {}", channelPipeline);
}
Also used : ReadTimeoutHandler(io.netty.handler.timeout.ReadTimeoutHandler) ClientChannelHandler(org.apache.camel.component.netty4.handlers.ClientChannelHandler) ChannelHandler(io.netty.channel.ChannelHandler) ChannelPipeline(io.netty.channel.ChannelPipeline) SslHandler(io.netty.handler.ssl.SslHandler) ClientChannelHandler(org.apache.camel.component.netty4.handlers.ClientChannelHandler)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)4 ChannelPipeline (io.netty.channel.ChannelPipeline)4 SslHandler (io.netty.handler.ssl.SslHandler)4 HttpObjectAggregator (io.netty.handler.codec.http.HttpObjectAggregator)2 ReadTimeoutHandler (io.netty.handler.timeout.ReadTimeoutHandler)2 EventExecutorGroup (io.netty.util.concurrent.EventExecutorGroup)2 ChannelHandlerFactory (org.apache.camel.component.netty4.ChannelHandlerFactory)2 HttpClientCodec (io.netty.handler.codec.http.HttpClientCodec)1 HttpContentCompressor (io.netty.handler.codec.http.HttpContentCompressor)1 HttpRequestDecoder (io.netty.handler.codec.http.HttpRequestDecoder)1 HttpResponseEncoder (io.netty.handler.codec.http.HttpResponseEncoder)1 ClientChannelHandler (org.apache.camel.component.netty4.handlers.ClientChannelHandler)1 ServerChannelHandler (org.apache.camel.component.netty4.handlers.ServerChannelHandler)1 HttpClientChannelHandler (org.apache.camel.component.netty4.http.handlers.HttpClientChannelHandler)1