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 LOG.debug("Client SSL handler configured and added to the ChannelPipeline"); 049 channelPipeline.addLast("ssl", sslHandler); 050 } 051 052 List<ChannelUpstreamHandler> decoders = producer.getConfiguration().getDecoders(); 053 for (int x = 0; x < decoders.size(); x++) { 054 channelPipeline.addLast("decoder-" + x, decoders.get(x)); 055 } 056 057 List<ChannelDownstreamHandler> encoders = producer.getConfiguration().getEncoders(); 058 for (int x = 0; x < encoders.size(); x++) { 059 channelPipeline.addLast("encoder-" + x, encoders.get(x)); 060 } 061 062 // our handler must be added last 063 channelPipeline.addLast("handler", new ClientChannelHandler(producer, exchange, callback)); 064 065 return channelPipeline; 066 } 067 068 private SslHandler configureClientSSLOnDemand() throws Exception { 069 if (!producer.getConfiguration().isSsl()) { 070 return null; 071 } 072 073 if (producer.getConfiguration().getSslHandler() != null) { 074 return producer.getConfiguration().getSslHandler(); 075 } else { 076 if (producer.getConfiguration().getKeyStoreFile() == null) { 077 LOG.debug("keystorefile is null"); 078 } 079 if (producer.getConfiguration().getTrustStoreFile() == null) { 080 LOG.debug("truststorefile is null"); 081 } 082 if (producer.getConfiguration().getPassphrase().toCharArray() == null) { 083 LOG.debug("passphrase is null"); 084 } 085 SSLEngineFactory sslEngineFactory = new SSLEngineFactory( 086 producer.getConfiguration().getKeyStoreFormat(), 087 producer.getConfiguration().getSecurityProvider(), 088 producer.getConfiguration().getKeyStoreFile(), 089 producer.getConfiguration().getTrustStoreFile(), 090 producer.getConfiguration().getPassphrase().toCharArray()); 091 SSLEngine sslEngine = sslEngineFactory.createClientSSLEngine(); 092 return new SslHandler(sslEngine); 093 } 094 } 095 096 }