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    
021    import javax.net.ssl.SSLEngine;
022    
023    import org.apache.camel.component.netty.handlers.ServerChannelHandler;
024    import org.apache.camel.component.netty.ssl.SSLEngineFactory;
025    import org.apache.commons.logging.Log;
026    import org.apache.commons.logging.LogFactory;
027    import org.jboss.netty.channel.ChannelDownstreamHandler;
028    import org.jboss.netty.channel.ChannelPipeline;
029    import org.jboss.netty.channel.ChannelPipelineFactory;
030    import org.jboss.netty.channel.ChannelUpstreamHandler;
031    import org.jboss.netty.channel.Channels;
032    import org.jboss.netty.handler.ssl.SslHandler;
033    
034    public class ServerPipelineFactory implements ChannelPipelineFactory {
035        private static final transient Log LOG = LogFactory.getLog(ServerPipelineFactory.class);
036        private NettyConsumer consumer;
037            
038        public ServerPipelineFactory(NettyConsumer consumer) {
039            this.consumer = consumer; 
040        }    
041    
042        public ChannelPipeline getPipeline() throws Exception {
043            ChannelPipeline channelPipeline = Channels.pipeline();
044    
045            SslHandler sslHandler = configureServerSSLOnDemand();
046            if (sslHandler != null) {
047                if (LOG.isDebugEnabled()) {
048                    LOG.debug("Server SSL handler configured and added as an interceptor against the ChannelPipeline");
049                }
050                channelPipeline.addLast("ssl", sslHandler);            
051            }
052            List<ChannelUpstreamHandler> decoders = consumer.getConfiguration().getDecoders();
053            for (int x = 0; x < decoders.size(); x++) {
054                channelPipeline.addLast("decoder-" + x, decoders.get(x));
055            }
056    
057            List<ChannelDownstreamHandler> encoders = consumer.getConfiguration().getEncoders();
058            for (int x = 0; x < encoders.size(); x++) {
059                channelPipeline.addLast("encoder-" + x, encoders.get(x));
060            }
061            if (consumer.getConfiguration().getHandler() != null) {
062                channelPipeline.addLast("handler", consumer.getConfiguration().getHandler());
063            } else {
064                channelPipeline.addLast("handler", new ServerChannelHandler(consumer));
065            }
066             
067            return channelPipeline;
068        }
069        
070        private SslHandler configureServerSSLOnDemand() throws Exception {
071            if (!consumer.getConfiguration().isSsl()) {
072                return null;
073            }
074    
075            if (consumer.getConfiguration().getSslHandler() != null) {
076                return consumer.getConfiguration().getSslHandler();
077            } else {
078                SSLEngineFactory sslEngineFactory = new SSLEngineFactory(
079                    consumer.getConfiguration().getKeyStoreFormat(),
080                    consumer.getConfiguration().getSecurityProvider(),
081                    consumer.getConfiguration().getKeyStoreFile(), 
082                    consumer.getConfiguration().getTrustStoreFile(), 
083                    consumer.getConfiguration().getPassphrase().toCharArray());
084                SSLEngine sslEngine = sslEngineFactory.createServerSSLEngine();
085                return new SslHandler(sslEngine);
086            }
087        }   
088    
089    }