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.ftps;
21  
22  import io.netty.channel.ChannelPipeline;
23  import io.netty.channel.socket.SocketChannel;
24  import io.netty.handler.codec.DelimiterBasedFrameDecoder;
25  import io.netty.handler.ssl.SslHandler;
26  import io.netty.util.concurrent.EventExecutorGroup;
27  import org.waarp.common.crypto.ssl.WaarpSecureKeyStore;
28  import org.waarp.common.crypto.ssl.WaarpSslContextFactory;
29  import org.waarp.ftp.core.config.FtpConfiguration;
30  import org.waarp.ftp.core.control.BusinessHandler;
31  import org.waarp.ftp.core.control.FtpInitializer;
32  import org.waarp.ftp.core.session.FtpSession;
33  
34  /**
35   *
36   */
37  public class FtpsInitializer extends FtpInitializer {
38  
39    public static WaarpSslContextFactory waarpSslContextFactory;
40    public static WaarpSecureKeyStore waarpSecureKeyStore;
41  
42    /**
43     * Constructor which Initializes some data for Server only
44     *
45     * @param businessHandler
46     * @param configuration
47     */
48    public FtpsInitializer(final Class<? extends BusinessHandler> businessHandler,
49                           final FtpConfiguration configuration) {
50      super(businessHandler, configuration);
51    }
52  
53    /**
54     * Create the pipeline with Handler, ObjectDecoder, ObjectEncoder.
55     */
56    @Override
57    public void initChannel(final SocketChannel ch) throws Exception {
58      final ChannelPipeline pipeline = ch.pipeline();
59      // Server: no renegotiation still, but possible clientAuthent
60      final SslHandler handler = waarpSslContextFactory.createHandlerServer(
61          waarpSslContextFactory.needClientAuthentication(), ch);
62      pipeline.addLast("SSL", handler);
63      // Add the text line codec combination first,
64      pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, delimiter));
65      pipeline.addLast("decoder", ftpControlStringDecoder);
66      pipeline.addLast("encoder", ftpControlStringEncoder);
67      // Threaded execution for business logic
68  
69      final EventExecutorGroup executorGroup =
70          configuration.getFtpInternalConfiguration().getExecutor();
71      // and then business logic. New one on every connection
72      final BusinessHandler newbusiness =
73          businessHandler.getDeclaredConstructor().newInstance();
74      final SslNetworkHandler newNetworkHandler =
75          new SslNetworkHandler(new FtpSession(configuration, newbusiness));
76      pipeline.addLast(executorGroup, "handler", newNetworkHandler);
77    }
78  }