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