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.AsyncCallback; 023 import org.apache.camel.Exchange; 024 import org.apache.camel.component.netty.handlers.ClientChannelHandler; 025 import org.apache.camel.component.netty.ssl.SSLEngineFactory; 026 import org.apache.commons.logging.Log; 027 import org.apache.commons.logging.LogFactory; 028 import org.jboss.netty.channel.ChannelDownstreamHandler; 029 import org.jboss.netty.channel.ChannelPipeline; 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 DefaultClientPipelineFactory extends ClientPipelineFactory { 035 private static final transient Log LOG = LogFactory.getLog(ClientPipelineFactory.class); 036 037 public DefaultClientPipelineFactory(NettyProducer producer, Exchange exchange, AsyncCallback callback) { 038 super(producer, exchange, callback); 039 } 040 041 public ChannelPipeline getPipeline() throws Exception { 042 // create a new pipeline 043 ChannelPipeline channelPipeline = Channels.pipeline(); 044 045 SslHandler sslHandler = configureClientSSLOnDemand(); 046 if (sslHandler != null) { 047 if (LOG.isDebugEnabled()) { 048 LOG.debug("Client SSL handler configured and added to the ChannelPipeline"); 049 } 050 channelPipeline.addLast("ssl", sslHandler); 051 } 052 053 List<ChannelUpstreamHandler> decoders = producer.getConfiguration().getDecoders(); 054 for (int x = 0; x < decoders.size(); x++) { 055 channelPipeline.addLast("decoder-" + x, decoders.get(x)); 056 } 057 058 List<ChannelDownstreamHandler> encoders = producer.getConfiguration().getEncoders(); 059 for (int x = 0; x < encoders.size(); x++) { 060 channelPipeline.addLast("encoder-" + x, encoders.get(x)); 061 } 062 063 // our handler must be added last 064 channelPipeline.addLast("handler", new ClientChannelHandler(producer, exchange, callback)); 065 066 return channelPipeline; 067 } 068 069 private SslHandler configureClientSSLOnDemand() throws Exception { 070 if (!producer.getConfiguration().isSsl()) { 071 return null; 072 } 073 074 if (producer.getConfiguration().getSslHandler() != null) { 075 return producer.getConfiguration().getSslHandler(); 076 } else { 077 if (producer.getConfiguration().getKeyStoreFile() == null) { 078 LOG.debug("keystorefile is null"); 079 } 080 if (producer.getConfiguration().getTrustStoreFile() == null) { 081 LOG.debug("truststorefile is null"); 082 } 083 if (producer.getConfiguration().getPassphrase().toCharArray() == null) { 084 LOG.debug("passphrase is null"); 085 } 086 SSLEngineFactory sslEngineFactory = new SSLEngineFactory( 087 producer.getConfiguration().getKeyStoreFormat(), 088 producer.getConfiguration().getSecurityProvider(), 089 producer.getConfiguration().getKeyStoreFile(), 090 producer.getConfiguration().getTrustStoreFile(), 091 producer.getConfiguration().getPassphrase().toCharArray()); 092 SSLEngine sslEngine = sslEngineFactory.createClientSSLEngine(); 093 return new SslHandler(sslEngine); 094 } 095 } 096 097 }