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;
21  
22  import io.netty.channel.ChannelInitializer;
23  import io.netty.channel.ChannelPipeline;
24  import io.netty.channel.socket.SocketChannel;
25  import io.netty.handler.traffic.ChannelTrafficShapingHandler;
26  import org.waarp.ftp.core.command.FtpArgumentCode.TransferMode;
27  import org.waarp.ftp.core.command.FtpArgumentCode.TransferStructure;
28  import org.waarp.ftp.core.command.FtpArgumentCode.TransferSubType;
29  import org.waarp.ftp.core.command.FtpArgumentCode.TransferType;
30  import org.waarp.ftp.core.config.FtpConfiguration;
31  
32  /**
33   * Pipeline Factory for Data Network.
34   */
35  public class FtpDataInitializer extends ChannelInitializer<SocketChannel> {
36    /**
37     * Mode Codec
38     */
39    public static final String CODEC_MODE = "MODE";
40  
41    /**
42     * Limit Codec
43     */
44    public static final String CODEC_LIMIT = "LIMITATION";
45  
46    /**
47     * Type Codec
48     */
49    public static final String CODEC_TYPE = "TYPE";
50  
51    /**
52     * Structure Codec
53     */
54    public static final String CODEC_STRUCTURE = "STRUCTURE";
55  
56    /**
57     * Handler Codec
58     */
59    public static final String HANDLER = "handler";
60  
61    protected static final FtpDataTypeCodec ftpDataTypeCodec =
62        new FtpDataTypeCodec(TransferType.ASCII, TransferSubType.NONPRINT);
63  
64    protected static final FtpDataStructureCodec ftpDataStructureCodec =
65        new FtpDataStructureCodec(TransferStructure.FILE);
66  
67    /**
68     * Business Handler Class
69     */
70    protected final Class<? extends DataBusinessHandler> dataBusinessHandler;
71  
72    /**
73     * Configuration
74     */
75    protected final FtpConfiguration configuration;
76  
77    /**
78     * Is this factory for Active mode
79     */
80    protected final boolean isActive;
81  
82    /**
83     * Constructor which Initializes some data
84     *
85     * @param dataBusinessHandler
86     * @param configuration
87     * @param active
88     */
89    public FtpDataInitializer(
90        final Class<? extends DataBusinessHandler> dataBusinessHandler,
91        final FtpConfiguration configuration, final boolean active) {
92      this.dataBusinessHandler = dataBusinessHandler;
93      this.configuration = configuration;
94      isActive = active;
95    }
96  
97    /**
98     * Create the pipeline with Handler, ObjectDecoder, ObjectEncoder.
99     */
100   @Override
101   public void initChannel(final SocketChannel ch) throws Exception {
102     final ChannelPipeline pipeline = ch.pipeline();
103     // Add default codec but they will change during the channelConnected
104     pipeline.addFirst(CODEC_MODE, new FtpDataModeCodec(TransferMode.STREAM,
105                                                        TransferStructure.FILE));
106     pipeline.addLast(CODEC_LIMIT, configuration.getFtpInternalConfiguration()
107                                                .getGlobalTrafficShapingHandler());
108     final ChannelTrafficShapingHandler limitChannel =
109         configuration.getFtpInternalConfiguration()
110                      .newChannelTrafficShapingHandler();
111     if (limitChannel != null) {
112       pipeline.addLast(CODEC_LIMIT + "CHANNEL", limitChannel);
113     }
114     pipeline.addLast(CODEC_TYPE, ftpDataTypeCodec);
115     pipeline.addLast(CODEC_STRUCTURE, ftpDataStructureCodec);
116     // and then business logic. New one on every connection
117     final DataBusinessHandler newbusiness =
118         dataBusinessHandler.getDeclaredConstructor().newInstance();
119     final DataNetworkHandler newNetworkHandler =
120         new DataNetworkHandler(configuration, newbusiness, isActive);
121     pipeline.addLast(
122         configuration.getFtpInternalConfiguration().getDataExecutor(), HANDLER,
123         newNetworkHandler);
124   }
125 }