1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.waarp.ftp.core.control;
19
20 import io.netty.buffer.ByteBuf;
21 import io.netty.buffer.Unpooled;
22 import io.netty.channel.ChannelInitializer;
23 import io.netty.channel.ChannelPipeline;
24 import io.netty.channel.socket.SocketChannel;
25 import io.netty.handler.codec.DelimiterBasedFrameDecoder;
26 import io.netty.util.concurrent.EventExecutorGroup;
27
28 import org.waarp.common.command.ReplyCode;
29 import org.waarp.common.utility.WaarpStringUtils;
30 import org.waarp.ftp.core.config.FtpConfiguration;
31 import org.waarp.ftp.core.session.FtpSession;
32
33
34
35
36
37
38
39 public class FtpInitializer extends ChannelInitializer<SocketChannel> {
40
41
42
43 protected static final ByteBuf[] delimiter = new ByteBuf[] {
44 Unpooled.wrappedBuffer(ReplyCode.CRLF.getBytes(WaarpStringUtils.UTF8)),
45 Unpooled.wrappedBuffer(ReplyCode.CRNUL.getBytes(WaarpStringUtils.UTF8)),
46 Unpooled.wrappedBuffer(ReplyCode.LF.getBytes(WaarpStringUtils.UTF8)) };
47
48 protected static final FtpControlStringDecoder ftpControlStringDecoder = new FtpControlStringDecoder(
49 WaarpStringUtils.UTF8);
50
51 protected static final FtpControlStringEncoder ftpControlStringEncoder = new FtpControlStringEncoder(
52 WaarpStringUtils.UTF8);
53
54
55
56
57 protected final Class<? extends BusinessHandler> businessHandler;
58
59
60
61
62 protected final FtpConfiguration configuration;
63
64
65
66
67
68
69
70 public FtpInitializer(Class<? extends BusinessHandler> businessHandler,
71 FtpConfiguration configuration) {
72 this.businessHandler = businessHandler;
73 this.configuration = configuration;
74 }
75
76
77
78
79 @Override
80 public void initChannel(SocketChannel ch) throws Exception {
81 ChannelPipeline pipeline = ch.pipeline();
82
83 pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, delimiter));
84 pipeline.addLast("decoder", ftpControlStringDecoder);
85 pipeline.addLast("encoder", ftpControlStringEncoder);
86
87 EventExecutorGroup executorGroup = configuration.getFtpInternalConfiguration().getExecutor();
88
89 BusinessHandler newbusiness = businessHandler.newInstance();
90 NetworkHandler newNetworkHandler = new NetworkHandler(new FtpSession(configuration, newbusiness));
91 pipeline.addLast(executorGroup, "handler", newNetworkHandler);
92 }
93 }