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