1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
33
34
35
36
37 public class FtpDataInitializer extends ChannelInitializer<SocketChannel> {
38
39
40
41 public static final String CODEC_MODE = "MODE";
42
43
44
45
46 public static final String CODEC_LIMIT = "LIMITATION";
47
48
49
50
51 public static final String CODEC_TYPE = "TYPE";
52
53
54
55
56 public static final String CODEC_STRUCTURE = "STRUCTURE";
57
58
59
60
61 public static final String PIPELINE_EXECUTOR = "pipelineExecutor";
62
63
64
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
76
77 protected final Class<? extends DataBusinessHandler> dataBusinessHandler;
78
79
80
81
82 protected final FtpConfiguration configuration;
83
84
85
86
87 protected final boolean isActive;
88
89
90
91
92
93
94
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
106
107
108 @Override
109 public void initChannel(SocketChannel ch) throws Exception {
110 ChannelPipeline pipeline = ch.pipeline();
111
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
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 }