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