View Javadoc

1   /**
2    * This file is part of Waarp Project.
3    * 
4    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
5    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
6    * 
7    * All Waarp Project is free software: you can redistribute it and/or modify it under the terms of
8    * the GNU General Public License as published by the Free Software Foundation, either version 3 of
9    * the License, or (at your option) any later version.
10   * 
11   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
12   * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
13   * Public License for more details.
14   * 
15   * You should have received a copy of the GNU General Public License along with Waarp . If not, see
16   * <http://www.gnu.org/licenses/>.
17   */
18  package org.waarp.ftp.core.data.handler;
19  
20  import io.netty.channel.ChannelInitializer;
21  import io.netty.channel.ChannelPipeline;
22  import io.netty.channel.socket.SocketChannel;
23  import io.netty.handler.traffic.ChannelTrafficShapingHandler;
24  
25  import org.waarp.ftp.core.command.FtpArgumentCode.TransferMode;
26  import org.waarp.ftp.core.command.FtpArgumentCode.TransferStructure;
27  import org.waarp.ftp.core.command.FtpArgumentCode.TransferSubType;
28  import org.waarp.ftp.core.command.FtpArgumentCode.TransferType;
29  import org.waarp.ftp.core.config.FtpConfiguration;
30  
31  /**
32   * Pipeline Factory for Data Network.
33   * 
34   * @author Frederic Bregier
35   * 
36   */
37  public class FtpDataInitializer extends ChannelInitializer<SocketChannel> {
38      /**
39       * Mode Codec
40       */
41      public static final String CODEC_MODE = "MODE";
42  
43      /**
44       * Limit Codec
45       */
46      public static final String CODEC_LIMIT = "LIMITATION";
47  
48      /**
49       * Type Codec
50       */
51      public static final String CODEC_TYPE = "TYPE";
52  
53      /**
54       * Structure Codec
55       */
56      public static final String CODEC_STRUCTURE = "STRUCTURE";
57  
58      /**
59       * Pipeline Executor Codec
60       */
61      public static final String PIPELINE_EXECUTOR = "pipelineExecutor";
62  
63      /**
64       * Handler Codec
65       */
66      public static final String HANDLER = "handler";
67  
68      protected static final FtpDataTypeCodec ftpDataTypeCodec = new FtpDataTypeCodec(
69              TransferType.ASCII, TransferSubType.NONPRINT);
70  
71      protected static final FtpDataStructureCodec ftpDataStructureCodec = new FtpDataStructureCodec(
72              TransferStructure.FILE);
73  
74      /**
75       * Business Handler Class
76       */
77      protected final Class<? extends DataBusinessHandler> dataBusinessHandler;
78  
79      /**
80       * Configuration
81       */
82      protected final FtpConfiguration configuration;
83  
84      /**
85       * Is this factory for Active mode
86       */
87      protected final boolean isActive;
88  
89      /**
90       * Constructor which Initializes some data
91       * 
92       * @param dataBusinessHandler
93       * @param configuration
94       * @param active
95       */
96      public FtpDataInitializer(
97              Class<? extends DataBusinessHandler> dataBusinessHandler,
98              FtpConfiguration configuration, boolean active) {
99          this.dataBusinessHandler = dataBusinessHandler;
100         this.configuration = configuration;
101         isActive = active;
102     }
103 
104     /**
105      * Create the pipeline with Handler, ObjectDecoder, ObjectEncoder.
106      * 
107      */
108     @Override
109     public void initChannel(SocketChannel ch) throws Exception {
110         ChannelPipeline pipeline = ch.pipeline();
111         // Add default codec but they will change during the channelConnected
112         pipeline.addFirst(CODEC_MODE, new FtpDataModeCodec(TransferMode.STREAM,
113                 TransferStructure.FILE));
114         pipeline.addLast(CODEC_LIMIT, configuration
115                 .getFtpInternalConfiguration()
116                 .getGlobalTrafficShapingHandler());
117         ChannelTrafficShapingHandler limitChannel =
118                 configuration
119                         .getFtpInternalConfiguration()
120                         .newChannelTrafficShapingHandler();
121         if (limitChannel != null) {
122             pipeline.addLast(CODEC_LIMIT + "CHANNEL", limitChannel);
123         }
124         pipeline.addLast(CODEC_TYPE, ftpDataTypeCodec);
125         pipeline.addLast(CODEC_STRUCTURE, ftpDataStructureCodec);
126         // and then business logic. New one on every connection
127         DataBusinessHandler newbusiness = dataBusinessHandler.newInstance();
128         DataNetworkHandler newNetworkHandler = new DataNetworkHandler(
129                 configuration, newbusiness, isActive);
130         pipeline.addLast(configuration.getFtpInternalConfiguration().getDataExecutor(),
131                 HANDLER, newNetworkHandler);
132     }
133 }