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.component.netty.handlers.ClientChannelHandler;
023 import org.apache.camel.component.netty.ssl.SSLEngineFactory;
024 import org.apache.commons.logging.Log;
025 import org.apache.commons.logging.LogFactory;
026 import org.jboss.netty.channel.ChannelDownstreamHandler;
027 import org.jboss.netty.channel.ChannelPipeline;
028 import org.jboss.netty.channel.ChannelPipelineFactory;
029 import org.jboss.netty.channel.ChannelUpstreamHandler;
030 import org.jboss.netty.channel.Channels;
031 import org.jboss.netty.handler.ssl.SslHandler;
032
033 public class ClientPipelineFactory implements ChannelPipelineFactory {
034 private static final transient Log LOG = LogFactory.getLog(ClientPipelineFactory.class);
035 private NettyProducer producer;
036 private ChannelPipeline channelPipeline;
037
038 public ClientPipelineFactory(NettyProducer producer) {
039 this.producer = producer;
040 }
041
042 public ChannelPipeline getPipeline() throws Exception {
043 if (channelPipeline != null) {
044 // http://docs.jboss.org/netty/3.1/api/org/jboss/netty/handler/ssl/SslHandler.html
045 // To restart the SSL session, you must remove the existing closed SslHandler
046 // from the ChannelPipeline, insert a new SslHandler with a new SSLEngine into
047 // the pipeline, and start the handshake process as described in the first section.
048 if (channelPipeline.remove("ssl") != null) {
049 // reinitialize and add SSL first
050 if (LOG.isDebugEnabled()) {
051 LOG.debug("Client SSL handler re-initialized on the ChannelPipeline");
052 }
053 channelPipeline.addFirst("ssl", configureClientSSLOnDemand());
054 }
055 return channelPipeline;
056 }
057
058 channelPipeline = Channels.pipeline();
059
060 SslHandler sslHandler = configureClientSSLOnDemand();
061 if (sslHandler != null) {
062 if (LOG.isDebugEnabled()) {
063 LOG.debug("Client SSL handler configured and added to the ChannelPipeline");
064 }
065 channelPipeline.addLast("ssl", sslHandler);
066 }
067
068 List<ChannelUpstreamHandler> decoders = producer.getConfiguration().getDecoders();
069 for (int x = 0; x < decoders.size(); x++) {
070 channelPipeline.addLast("decoder-" + x, decoders.get(x));
071 }
072
073 List<ChannelDownstreamHandler> encoders = producer.getConfiguration().getEncoders();
074 for (int x = 0; x < encoders.size(); x++) {
075 channelPipeline.addLast("encoder-" + x, encoders.get(x));
076 }
077
078 if (producer.getConfiguration().getHandler() != null) {
079 channelPipeline.addLast("handler", producer.getConfiguration().getHandler());
080 } else {
081 channelPipeline.addLast("handler", new ClientChannelHandler(producer));
082 }
083
084 return channelPipeline;
085 }
086
087 private SslHandler configureClientSSLOnDemand() throws Exception {
088 if (!producer.getConfiguration().isSsl()) {
089 return null;
090 }
091
092 if (producer.getConfiguration().getSslHandler() != null) {
093 return producer.getConfiguration().getSslHandler();
094 } else {
095 if (producer.getConfiguration().getKeyStoreFile() == null) {
096 LOG.debug("keystorefile is null");
097 }
098 if (producer.getConfiguration().getTrustStoreFile() == null) {
099 LOG.debug("truststorefile is null");
100 }
101 if (producer.getConfiguration().getPassphrase().toCharArray() == null) {
102 LOG.debug("passphrase is null");
103 }
104 SSLEngineFactory sslEngineFactory = new SSLEngineFactory(
105 producer.getConfiguration().getKeyStoreFormat(),
106 producer.getConfiguration().getSecurityProvider(),
107 producer.getConfiguration().getKeyStoreFile(),
108 producer.getConfiguration().getTrustStoreFile(),
109 producer.getConfiguration().getPassphrase().toCharArray());
110 SSLEngine sslEngine = sslEngineFactory.createClientSSLEngine();
111 return new SslHandler(sslEngine);
112 }
113 }
114
115 }