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