001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.component.netty;
018    
019    import java.util.List;
020    import java.util.concurrent.TimeUnit;
021    import javax.net.ssl.SSLEngine;
022    
023    import org.apache.camel.AsyncCallback;
024    import org.apache.camel.Exchange;
025    import org.apache.camel.component.netty.handlers.ClientChannelHandler;
026    import org.apache.camel.component.netty.ssl.SSLEngineFactory;
027    import org.apache.commons.logging.Log;
028    import org.apache.commons.logging.LogFactory;
029    import org.jboss.netty.channel.ChannelDownstreamHandler;
030    import org.jboss.netty.channel.ChannelPipeline;
031    import org.jboss.netty.channel.ChannelPipelineFactory;
032    import org.jboss.netty.channel.ChannelUpstreamHandler;
033    import org.jboss.netty.channel.Channels;
034    import org.jboss.netty.handler.ssl.SslHandler;
035    import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
036    import org.jboss.netty.util.HashedWheelTimer;
037    import org.jboss.netty.util.Timer;
038    
039    public class ClientPipelineFactory implements ChannelPipelineFactory {
040        private static final transient Log LOG = LogFactory.getLog(ClientPipelineFactory.class);
041        private final NettyProducer producer;
042        private final Exchange exchange;
043        private final AsyncCallback callback;
044    
045        public ClientPipelineFactory(NettyProducer producer, Exchange exchange, AsyncCallback callback) {
046            this.producer = producer;
047            this.exchange = exchange;
048            this.callback = callback;
049        }
050    
051        public ChannelPipeline getPipeline() throws Exception {
052            // create a new pipeline
053            ChannelPipeline channelPipeline = Channels.pipeline();
054    
055            SslHandler sslHandler = configureClientSSLOnDemand();
056            if (sslHandler != null) {
057                if (LOG.isDebugEnabled()) {
058                    LOG.debug("Client SSL handler configured and added to the ChannelPipeline");
059                }
060                channelPipeline.addLast("ssl", sslHandler);
061            }
062    
063            // use read timeout handler to handle timeout while waiting for a remote reply (while reading from the remote host)
064            if (producer.getConfiguration().getTimeout() > 0) {
065                Timer timer = new HashedWheelTimer();
066                channelPipeline.addLast("timeout", new ReadTimeoutHandler(timer, producer.getConfiguration().getTimeout(), TimeUnit.MILLISECONDS));
067            }
068    
069            List<ChannelUpstreamHandler> decoders = producer.getConfiguration().getDecoders();
070            for (int x = 0; x < decoders.size(); x++) {
071                channelPipeline.addLast("decoder-" + x, decoders.get(x));
072            }
073    
074            List<ChannelDownstreamHandler> encoders = producer.getConfiguration().getEncoders();
075            for (int x = 0; x < encoders.size(); x++) {
076                channelPipeline.addLast("encoder-" + x, encoders.get(x));
077            }
078    
079            // our handler must be added last
080            channelPipeline.addLast("handler", new ClientChannelHandler(producer, exchange, callback));
081    
082            return channelPipeline;
083        }
084    
085        private SslHandler configureClientSSLOnDemand() throws Exception {
086            if (!producer.getConfiguration().isSsl()) {
087                return null;
088            }
089    
090            if (producer.getConfiguration().getSslHandler() != null) {
091                return producer.getConfiguration().getSslHandler();
092            } else {
093                if (producer.getConfiguration().getKeyStoreFile() == null) {
094                    LOG.debug("keystorefile is null");
095                }
096                if (producer.getConfiguration().getTrustStoreFile() == null) {
097                    LOG.debug("truststorefile is null");
098                }
099                if (producer.getConfiguration().getPassphrase().toCharArray() == null) {
100                    LOG.debug("passphrase is null");
101                }
102                SSLEngineFactory sslEngineFactory = new SSLEngineFactory(
103                    producer.getConfiguration().getKeyStoreFormat(),
104                    producer.getConfiguration().getSecurityProvider(),
105                    producer.getConfiguration().getKeyStoreFile(),
106                    producer.getConfiguration().getTrustStoreFile(),
107                    producer.getConfiguration().getPassphrase().toCharArray());
108                SSLEngine sslEngine = sslEngineFactory.createClientSSLEngine();
109                return new SslHandler(sslEngine);
110            }
111        }
112    
113    }