View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, 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 by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.waarp.ftp.core.data.handler.ftps;
21  
22  import io.netty.channel.ChannelPipeline;
23  import io.netty.channel.socket.SocketChannel;
24  import io.netty.handler.ssl.SslHandler;
25  import io.netty.handler.traffic.ChannelTrafficShapingHandler;
26  import org.waarp.common.crypto.ssl.WaarpSslUtility;
27  import org.waarp.ftp.core.command.FtpArgumentCode.TransferMode;
28  import org.waarp.ftp.core.command.FtpArgumentCode.TransferStructure;
29  import org.waarp.ftp.core.config.FtpConfiguration;
30  import org.waarp.ftp.core.control.ftps.FtpsInitializer;
31  import org.waarp.ftp.core.data.handler.DataBusinessHandler;
32  import org.waarp.ftp.core.data.handler.DataNetworkHandler;
33  import org.waarp.ftp.core.data.handler.FtpDataInitializer;
34  import org.waarp.ftp.core.data.handler.FtpDataModeCodec;
35  
36  /**
37   *
38   */
39  public class FtpsDataInitializer extends FtpDataInitializer {
40  
41    /**
42     * Constructor which Initializes some data
43     *
44     * @param dataBusinessHandler
45     * @param configuration
46     * @param active
47     */
48    public FtpsDataInitializer(
49        final Class<? extends DataBusinessHandler> dataBusinessHandler,
50        final FtpConfiguration configuration, final boolean active) {
51      super(dataBusinessHandler, configuration, active);
52    }
53  
54    /**
55     * Create the pipeline with Handler, ObjectDecoder, ObjectEncoder.
56     */
57    @Override
58    public void initChannel(final SocketChannel ch) throws Exception {
59      final ChannelPipeline pipeline = ch.pipeline();
60      // Add SSL as first element in the pipeline
61      final SslHandler sslHandler =
62          FtpsInitializer.waarpSslContextFactory.createHandlerServer(
63              FtpsInitializer.waarpSslContextFactory.needClientAuthentication(),
64              ch);
65      WaarpSslUtility.addSslOpenedChannel(ch);
66      pipeline.addLast("ssl", sslHandler);
67      // Add default codec but they will change during the channelActive
68      pipeline.addLast(FtpDataInitializer.CODEC_MODE,
69                       new FtpDataModeCodec(TransferMode.STREAM,
70                                            TransferStructure.FILE));
71      pipeline.addLast(FtpDataInitializer.CODEC_LIMIT,
72                       configuration.getFtpInternalConfiguration()
73                                    .getGlobalTrafficShapingHandler());
74      final ChannelTrafficShapingHandler limitChannel =
75          configuration.getFtpInternalConfiguration()
76                       .newChannelTrafficShapingHandler();
77      if (limitChannel != null) {
78        pipeline.addLast(FtpDataInitializer.CODEC_LIMIT + "CHANNEL",
79                         limitChannel);
80      }
81      pipeline.addLast(FtpDataInitializer.CODEC_TYPE, ftpDataTypeCodec);
82      pipeline.addLast(FtpDataInitializer.CODEC_STRUCTURE, ftpDataStructureCodec);
83      // and then business logic. New one on every connection
84      final DataBusinessHandler newbusiness =
85          dataBusinessHandler.getDeclaredConstructor().newInstance();
86      final DataNetworkHandler newNetworkHandler =
87          new DataNetworkHandler(configuration, newbusiness, isActive);
88      pipeline.addLast(
89          configuration.getFtpInternalConfiguration().getDataExecutor(),
90          FtpDataInitializer.HANDLER, newNetworkHandler);
91    }
92  }