1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
34
35 public class FtpDataInitializer extends ChannelInitializer<SocketChannel> {
36
37
38
39 public static final String CODEC_MODE = "MODE";
40
41
42
43
44 public static final String CODEC_LIMIT = "LIMITATION";
45
46
47
48
49 public static final String CODEC_TYPE = "TYPE";
50
51
52
53
54 public static final String CODEC_STRUCTURE = "STRUCTURE";
55
56
57
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
69
70 protected final Class<? extends DataBusinessHandler> dataBusinessHandler;
71
72
73
74
75 protected final FtpConfiguration configuration;
76
77
78
79
80 protected final boolean isActive;
81
82
83
84
85
86
87
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
99
100 @Override
101 public void initChannel(final SocketChannel ch) throws Exception {
102 final ChannelPipeline pipeline = ch.pipeline();
103
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
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 }