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 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<ChannelDownstreamHandler> encoders = consumer.getConfiguration().getEncoders(); 053 for (int x = 0; x < encoders.size(); x++) { 054 channelPipeline.addLast("encoder-" + x, encoders.get(x)); 055 } 056 057 List<ChannelUpstreamHandler> decoders = consumer.getConfiguration().getDecoders(); 058 for (int x = 0; x < decoders.size(); x++) { 059 channelPipeline.addLast("decoder-" + x, decoders.get(x)); 060 } 061 062 // our handler must be added last 063 channelPipeline.addLast("handler", new ServerChannelHandler(consumer)); 064 065 return channelPipeline; 066 } 067 068 private SslHandler configureServerSSLOnDemand() throws Exception { 069 if (!consumer.getConfiguration().isSsl()) { 070 return null; 071 } 072 073 if (consumer.getConfiguration().getSslHandler() != null) { 074 return consumer.getConfiguration().getSslHandler(); 075 } else { 076 SSLEngineFactory sslEngineFactory = new SSLEngineFactory( 077 consumer.getConfiguration().getKeyStoreFormat(), 078 consumer.getConfiguration().getSecurityProvider(), 079 consumer.getConfiguration().getKeyStoreFile(), 080 consumer.getConfiguration().getTrustStoreFile(), 081 consumer.getConfiguration().getPassphrase().toCharArray()); 082 SSLEngine sslEngine = sslEngineFactory.createServerSSLEngine(); 083 return new SslHandler(sslEngine); 084 } 085 } 086 087 }