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.simpleimpl;
21  
22  import org.waarp.common.file.filesystembased.FilesystemBasedFileParameterImpl;
23  import org.waarp.common.logging.SysErrLogger;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.common.logging.WaarpSlf4JLoggerFactory;
27  import org.waarp.ftp.core.exception.FtpNoConnectionException;
28  import org.waarp.ftp.simpleimpl.config.FileBasedConfiguration;
29  import org.waarp.ftp.simpleimpl.config.FileBasedSslConfiguration;
30  import org.waarp.ftp.simpleimpl.control.SimpleBusinessHandler;
31  import org.waarp.ftp.simpleimpl.data.FileSystemBasedDataBusinessHandler;
32  
33  /**
34   * Example of FTP Server using simple authentication (XML FileInterface based),
35   * and standard Directory and
36   * FileInterface implementation (Filesystem based).
37   */
38  public class SimpleGatewaySslFtpServer {
39  
40    /**
41     * Take 2 simple XML files as configuration.
42     *
43     * @param args
44     */
45    public static void main(final String[] args) {
46      if (args.length != 3) {
47        SysErrLogger.FAKE_LOGGER.syserr(
48            "Usage: " + SimpleGatewaySslFtpServer.class.getName() +
49            " <config-file> <ssl-config-file> SSL|AUTH");
50        return;
51      }
52      WaarpLoggerFactory.setDefaultFactoryIfNotSame(
53          new WaarpSlf4JLoggerFactory(null));
54      /*
55       * Internal Logger
56       */
57      final WaarpLogger logger =
58          WaarpLoggerFactory.getLogger(SimpleGatewaySslFtpServer.class);
59      final String config = args[0];
60      final FileBasedConfiguration configuration =
61          new FileBasedConfiguration(SimpleGatewaySslFtpServer.class,
62                                     SimpleBusinessHandler.class,
63                                     FileSystemBasedDataBusinessHandler.class,
64                                     new FilesystemBasedFileParameterImpl());
65      if (!configuration.setConfigurationFromXml(config)) {
66        SysErrLogger.FAKE_LOGGER.syserr("Bad configuration");
67        return;
68      }
69      if (!FileBasedSslConfiguration.setConfigurationServerFromXml(configuration,
70                                                                   args[1])) {
71        SysErrLogger.FAKE_LOGGER.syserr("Bad Ssl configuration");
72        return;
73      }
74      if ("SSL".equalsIgnoreCase(args[2])) {
75        // native SSL support
76        configuration.getFtpInternalConfiguration().setUsingNativeSsl(true);
77        configuration.getFtpInternalConfiguration().setAcceptAuthProt(false);
78      } else if ("AUTH".equalsIgnoreCase(args[2])) {
79        // AUTH, PROT, ... support
80        configuration.getFtpInternalConfiguration().setUsingNativeSsl(false);
81        configuration.getFtpInternalConfiguration().setAcceptAuthProt(true);
82      } else {
83        // unknown option
84        SysErrLogger.FAKE_LOGGER.syserr(
85            "Bad Ssl Option configuration: SSL ot AUTH " + "but " + args[2]);
86        return;
87      }
88      // Start server.
89      try {
90        configuration.serverStartup();
91      } catch (final FtpNoConnectionException e) {
92        logger.error("FTP not started: Mode " +
93                     (configuration.getFtpInternalConfiguration()
94                                   .isUsingNativeSsl()? "SSL" : "AUTH"), e);
95      }
96      logger.info("FTP started: Mode {}",
97                  (configuration.getFtpInternalConfiguration().isUsingNativeSsl()?
98                      "SSL" : "AUTH"));
99    }
100 
101 }