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.control;
21  
22  import io.netty.channel.Channel;
23  import org.waarp.common.command.exception.CommandAbstractException;
24  import org.waarp.common.command.exception.Reply502Exception;
25  import org.waarp.common.logging.WaarpLogger;
26  import org.waarp.common.logging.WaarpLoggerFactory;
27  import org.waarp.ftp.core.command.AbstractCommand;
28  import org.waarp.ftp.core.command.FtpCommandCode;
29  import org.waarp.ftp.core.command.service.MKD;
30  import org.waarp.ftp.core.control.BusinessHandler;
31  import org.waarp.ftp.core.data.FtpTransfer;
32  import org.waarp.ftp.core.session.FtpSession;
33  import org.waarp.ftp.filesystembased.FilesystemBasedFtpRestart;
34  import org.waarp.ftp.simpleimpl.file.FileBasedAuth;
35  import org.waarp.ftp.simpleimpl.file.FileBasedDir;
36  
37  /**
38   * BusinessHandler implementation that allows pre and post actions on any
39   * operations and specifically on
40   * transfer operations
41   */
42  public class SimpleBusinessHandler extends BusinessHandler {
43    private static final String GBBH_TRANSFER = "GBBH: Transfer: {} ";
44    /**
45     * Internal Logger
46     */
47    private static final WaarpLogger logger =
48        WaarpLoggerFactory.getLogger(SimpleBusinessHandler.class);
49  
50    @Override
51    public void afterRunCommandKo(final CommandAbstractException e) {
52      // TO DO Auto-generated method stub
53      if (getFtpSession().getCurrentCommand() instanceof MKD) {
54        // do nothing
55      } else {
56        logger.debug("GBBH: AFTKO: {} {}", getFtpSession(), e.getMessage());
57      }
58    }
59  
60    @Override
61    public void afterRunCommandOk() throws CommandAbstractException {
62      // TO DO Auto-generated method stub
63      // logger.info("GBBH: AFTOK: {}", getFtpSession())
64    }
65  
66    @Override
67    public void beforeRunCommand() throws CommandAbstractException {
68      // TO DO Auto-generated method stub
69      // logger.info("GBBH: BEFCD: {}", getFtpSession())
70    }
71  
72    @Override
73    protected void cleanSession() {
74      // TO DO Auto-generated method stub
75      // logger.info("GBBH: CLNSE: {}", getFtpSession())
76    }
77  
78    @Override
79    public void exceptionLocalCaught(final Throwable e) {
80      // TO DO Auto-generated method stub
81      logger.warn("GBBH: EXCEP: {} {}", getFtpSession(), e.getMessage());
82    }
83  
84    @Override
85    public void executeChannelClosed() {
86      // TO DO Auto-generated method stub
87      // logger.info("GBBH: CLOSED: for user {} with session {} ",
88      // getFtpSession().getAuth().getUser(), getFtpSession())
89    }
90  
91    @Override
92    public void executeChannelConnected(final Channel channel) {
93      // TO DO Auto-generated method stub
94      // logger.info("GBBH: CONNEC: {}", getFtpSession())
95    }
96  
97    @Override
98    public final FileBasedAuth getBusinessNewAuth() {
99      return new FileBasedAuth(getFtpSession());
100   }
101 
102   @Override
103   public final FileBasedDir getBusinessNewDir() {
104     return new FileBasedDir(getFtpSession());
105   }
106 
107   @Override
108   public final FilesystemBasedFtpRestart getBusinessNewRestart() {
109     return new FilesystemBasedFtpRestart(getFtpSession());
110   }
111 
112   @Override
113   public final String getHelpMessage(final String arg) {
114     return "This FTP server is only intend as a Gateway.\n" +
115            "This FTP server refers to RFC 959, 775, 2389, 2428, 3659, 4217 " +
116            "and supports XDIGEST, XCRC, XMD5 and XSHA1 commands.\n" +
117            "XCRC, XMD5 and XSHA1 take a simple filename as argument, XDIGEST " +
118            "taking algorithm (among CRC32, ADLER32, MD5, MD2, " +
119            "SHA-1, SHA-256, SHA-384, SHA-512) followed by filename " +
120            "as arguments," +
121            " and return \"250 digest-value is the digest of filename\".";
122   }
123 
124   @Override
125   public final String getFeatMessage() {
126     final StringBuilder builder =
127         new StringBuilder("Extensions supported:").append('\n').append(
128             getDefaultFeatMessage());
129     if (getFtpSession().getConfiguration().getFtpInternalConfiguration()
130                        .isAcceptAuthProt()) {
131       builder.append('\n').append(getSslFeatMessage());
132     }
133     builder.append("\nEnd");
134     return builder.toString();
135   }
136 
137   @Override
138   public final String getOptsMessage(final String[] args)
139       throws CommandAbstractException {
140     if (args.length > 0) {
141       if (args[0].equalsIgnoreCase(FtpCommandCode.MLST.name()) ||
142           args[0].equalsIgnoreCase(FtpCommandCode.MLSD.name())) {
143         return getMLSxOptsMessage(args);
144       }
145       throw new Reply502Exception("OPTS not implemented for " + args[0]);
146     }
147     throw new Reply502Exception("OPTS not implemented");
148   }
149 
150   @Override
151   public AbstractCommand getSpecializedSiteCommand(final FtpSession session,
152                                                    final String line) {
153     return null;
154   }
155 
156   @Override
157   public final void afterTransferDoneBeforeAnswer(final FtpTransfer transfer)
158       throws CommandAbstractException {
159     if (transfer.getCommand() == FtpCommandCode.APPE) {
160       logger.info(GBBH_TRANSFER + "{} {}", transfer.getCommand(),
161                   transfer.getStatus(), transfer.getPath());
162     } else if (transfer.getCommand() == FtpCommandCode.RETR) {
163       logger.info(GBBH_TRANSFER + "{} {}", transfer.getCommand(),
164                   transfer.getStatus(), transfer.getPath());
165     } else if (transfer.getCommand() == FtpCommandCode.STOR) {
166       logger.info(GBBH_TRANSFER + "{} {}", transfer.getCommand(),
167                   transfer.getStatus(), transfer.getPath());
168     } else if (transfer.getCommand() == FtpCommandCode.STOU) {
169       logger.info(GBBH_TRANSFER + "{} {}", transfer.getCommand(),
170                   transfer.getStatus(), transfer.getPath());
171     } else {
172       logger.warn("GBBH: Transfer unknown: {} {} {}", transfer.getCommand(),
173                   transfer.getStatus(), transfer.getPath());
174       // Nothing to do
175     }
176   }
177 }