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.control;
21  
22  import io.netty.buffer.ByteBuf;
23  import io.netty.channel.ChannelInitializer;
24  import io.netty.channel.ChannelPipeline;
25  import io.netty.channel.socket.SocketChannel;
26  import io.netty.handler.codec.DelimiterBasedFrameDecoder;
27  import io.netty.util.concurrent.EventExecutorGroup;
28  import org.waarp.common.command.ReplyCode;
29  import org.waarp.common.utility.WaarpNettyUtil;
30  import org.waarp.common.utility.WaarpStringUtils;
31  import org.waarp.ftp.core.config.FtpConfiguration;
32  import org.waarp.ftp.core.session.FtpSession;
33  
34  /**
35   * Pipeline factory for Control command connection
36   */
37  public class FtpInitializer extends ChannelInitializer<SocketChannel> {
38    /**
39     * CRLF, CRNUL, LF delimiters
40     */
41    protected static final ByteBuf[] delimiter = {
42        WaarpNettyUtil.wrappedBuffer(
43            ReplyCode.CRLF.getBytes(WaarpStringUtils.UTF8)),
44        WaarpNettyUtil.wrappedBuffer(
45            ReplyCode.CRNUL.getBytes(WaarpStringUtils.UTF8)),
46        WaarpNettyUtil.wrappedBuffer(ReplyCode.LF.getBytes(WaarpStringUtils.UTF8))
47    };
48  
49    protected static final FtpControlStringDecoder ftpControlStringDecoder =
50        new FtpControlStringDecoder(WaarpStringUtils.UTF8);
51  
52    protected static final FtpControlStringEncoder ftpControlStringEncoder =
53        new FtpControlStringEncoder(WaarpStringUtils.UTF8);
54  
55    /**
56     * Business Handler Class if any (Target Mode only)
57     */
58    protected final Class<? extends BusinessHandler> businessHandler;
59  
60    /**
61     * Configuration
62     */
63    protected final FtpConfiguration configuration;
64  
65    /**
66     * Constructor which Initializes some data for Server only
67     *
68     * @param businessHandler
69     * @param configuration
70     */
71    public FtpInitializer(final Class<? extends BusinessHandler> businessHandler,
72                          final FtpConfiguration configuration) {
73      this.businessHandler = businessHandler;
74      this.configuration = configuration;
75    }
76  
77    /**
78     * Create the pipeline with Handler, ObjectDecoder, ObjectEncoder.
79     */
80    @Override
81    public void initChannel(final SocketChannel ch) throws Exception {
82      final ChannelPipeline pipeline = ch.pipeline();
83      // Add the text line codec combination first,
84      pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, delimiter));
85      pipeline.addLast("decoder", ftpControlStringDecoder);
86      pipeline.addLast("encoder", ftpControlStringEncoder);
87      // Threaded execution for business logic
88      final EventExecutorGroup executorGroup =
89          configuration.getFtpInternalConfiguration().getExecutor();
90      // and then business logic. New one on every connection
91      final BusinessHandler newbusiness =
92          businessHandler.getDeclaredConstructor().newInstance();
93      final NetworkHandler newNetworkHandler =
94          new NetworkHandler(new FtpSession(configuration, newbusiness));
95      pipeline.addLast(executorGroup, "handler", newNetworkHandler);
96    }
97  }