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.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   * Pipeline factory for Control command connection
35   * 
36   * @author Frederic Bregier
37   * 
38   */
39  public class FtpInitializer extends ChannelInitializer<SocketChannel> {
40      /**
41       * CRLF, CRNUL, LF delimiters
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       * Business Handler Class if any (Target Mode only)
56       */
57      protected final Class<? extends BusinessHandler> businessHandler;
58  
59      /**
60       * Configuration
61       */
62      protected final FtpConfiguration configuration;
63  
64      /**
65       * Constructor which Initializes some data for Server only
66       * 
67       * @param businessHandler
68       * @param configuration
69       */
70      public FtpInitializer(Class<? extends BusinessHandler> businessHandler,
71              FtpConfiguration configuration) {
72          this.businessHandler = businessHandler;
73          this.configuration = configuration;
74      }
75  
76      /**
77       * Create the pipeline with Handler, ObjectDecoder, ObjectEncoder.
78       */
79      @Override
80      public void initChannel(SocketChannel ch) throws Exception {
81          ChannelPipeline pipeline = ch.pipeline();
82          // Add the text line codec combination first,
83          pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, delimiter));
84          pipeline.addLast("decoder", ftpControlStringDecoder);
85          pipeline.addLast("encoder", ftpControlStringEncoder);
86          // Threaded execution for business logic
87          EventExecutorGroup executorGroup = configuration.getFtpInternalConfiguration().getExecutor();
88          // and then business logic. New one on every connection
89          BusinessHandler newbusiness = businessHandler.newInstance();
90          NetworkHandler newNetworkHandler = new NetworkHandler(new FtpSession(configuration, newbusiness));
91          pipeline.addLast(executorGroup, "handler", newNetworkHandler);
92      }
93  }