View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package openr66.protocol.networkhandler.ssl;
22  
23  import goldengate.common.crypto.ssl.GgSecureKeyStore;
24  import goldengate.common.crypto.ssl.GgSslContextFactory;
25  
26  import java.util.concurrent.ExecutorService;
27  import java.util.concurrent.TimeUnit;
28  
29  import openr66.protocol.configuration.Configuration;
30  import openr66.protocol.exception.OpenR66ProtocolNoDataException;
31  import openr66.protocol.networkhandler.NetworkServerPipelineFactory;
32  import openr66.protocol.networkhandler.packet.NetworkPacketCodec;
33  
34  import org.jboss.netty.channel.ChannelPipeline;
35  import org.jboss.netty.channel.ChannelPipelineFactory;
36  import org.jboss.netty.channel.Channels;
37  import org.jboss.netty.handler.execution.ExecutionHandler;
38  import org.jboss.netty.handler.timeout.IdleStateHandler;
39  import org.jboss.netty.handler.traffic.ChannelTrafficShapingHandler;
40  import org.jboss.netty.handler.traffic.GlobalTrafficShapingHandler;
41  import org.jboss.netty.util.HashedWheelTimer;
42  
43  /**
44   * @author Frederic Bregier
45   *
46   */
47  public class NetworkSslServerPipelineFactory implements ChannelPipelineFactory {
48      private final boolean isClient;
49      public static GgSslContextFactory ggSslContextFactory;
50      public static GgSecureKeyStore ggSecureKeyStore;
51      private final ExecutorService executorService;
52      /**
53       * Global HashedWheelTimer
54       */
55      public HashedWheelTimer timer = (HashedWheelTimer) Configuration.configuration.getTimerClose();
56  
57      /**
58       *
59       * @param isClient
60       *            True if this Factory is to be used in Client mode
61       */
62      public NetworkSslServerPipelineFactory(boolean isClient, ExecutorService executor) {
63          super();
64          this.isClient = isClient;
65          this.executorService = executor;
66      }
67  
68      @Override
69      public ChannelPipeline getPipeline() {
70          final ChannelPipeline pipeline = Channels.pipeline();
71          // Add SSL handler first to encrypt and decrypt everything.
72          if (isClient) {
73              // Not server: no clientAuthent, no renegotiation
74              pipeline.addLast("ssl",
75                  ggSslContextFactory.initPipelineFactory(false,
76                          false, false, executorService));
77          } else {
78              // Server: no renegotiation still, but possible clientAuthent
79              pipeline.addLast("ssl",
80                      ggSslContextFactory.initPipelineFactory(true,
81                              ggSslContextFactory.needClientAuthentication(),
82                              false, executorService));
83          }
84  
85          pipeline.addLast("codec", new NetworkPacketCodec());
86          GlobalTrafficShapingHandler handler = Configuration.configuration
87                  .getGlobalTrafficShapingHandler();
88          if (handler != null) {
89              pipeline.addLast(NetworkServerPipelineFactory.LIMIT, handler);
90          }
91          ChannelTrafficShapingHandler trafficChannel = null;
92          try {
93              trafficChannel =
94                  Configuration.configuration
95                  .newChannelTrafficShapingHandler();
96              pipeline.addLast(NetworkServerPipelineFactory.LIMITCHANNEL, trafficChannel);
97          } catch (OpenR66ProtocolNoDataException e) {
98          }
99          pipeline.addLast("pipelineExecutor", new ExecutionHandler(
100                 Configuration.configuration.getServerPipelineExecutor()));
101         
102         pipeline.addLast(NetworkServerPipelineFactory.TIMEOUT,
103                 new IdleStateHandler(timer,
104                         0, 0, 
105                         Configuration.configuration.TIMEOUTCON, 
106                         TimeUnit.MILLISECONDS));
107         pipeline.addLast("handler", new NetworkSslServerHandler(!this.isClient));
108         return pipeline;
109     }
110 }