View Javadoc

1   /**
2      This file is part of Waarp Project.
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 Waarp 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     Waarp 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 Waarp .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package org.waarp.ftp.core.control.ftps;
22  
23  import io.netty.channel.Channel;
24  import io.netty.channel.ChannelHandlerContext;
25  
26  import org.waarp.common.crypto.ssl.WaarpSslUtility;
27  import org.waarp.common.logging.WaarpLogger;
28  import org.waarp.common.logging.WaarpLoggerFactory;
29  import org.waarp.ftp.core.control.NetworkHandler;
30  import org.waarp.ftp.core.session.FtpSession;
31  
32  /**
33   * @author "Frederic Bregier"
34   *
35   */
36  public class SslNetworkHandler extends NetworkHandler {
37      /**
38       * Internal Logger
39       */
40      private static final WaarpLogger logger = WaarpLoggerFactory.getLogger(SslNetworkHandler.class);
41  
42      /**
43       * @param session
44       */
45      public SslNetworkHandler(FtpSession session) {
46          super(session);
47      }
48  
49      @Override
50      public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
51          Channel channel = ctx.channel();
52          logger.debug("Add channel to ssl " + channel.toString());
53          WaarpSslUtility.addSslOpenedChannel(channel);
54          getFtpSession().prepareSsl();
55          super.channelRegistered(ctx);
56      }
57  
58      /**
59       * To be extended to inform of an error to SNMP support
60       * 
61       * @param error1
62       * @param error2
63       */
64      protected void callForSnmp(String error1, String error2) {
65          // ignore
66      }
67  
68      @Override
69      public void channelActive(final ChannelHandlerContext ctx) throws Exception {
70          if (!WaarpSslUtility.waitForHandshake(ctx.channel())) {
71              callForSnmp("SSL Connection Error", "During Ssl Handshake");
72              getFtpSession().setSsl(false);
73              return;
74          } else {
75              getFtpSession().setSsl(true);
76          }
77          super.channelActive(ctx);
78      }
79  
80  }