View Javadoc
1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, 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 GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
20   */
21  package goldengate.ftp.simpleimpl.control;
22  
23  import goldengate.common.command.exception.CommandAbstractException;
24  import goldengate.common.command.exception.Reply502Exception;
25  import goldengate.common.logging.GgInternalLogger;
26  import goldengate.common.logging.GgInternalLoggerFactory;
27  import goldengate.ftp.core.command.AbstractCommand;
28  import goldengate.ftp.core.command.FtpCommandCode;
29  import goldengate.ftp.core.command.service.MKD;
30  import goldengate.ftp.core.control.BusinessHandler;
31  import goldengate.ftp.core.data.FtpTransfer;
32  import goldengate.ftp.core.session.FtpSession;
33  import goldengate.ftp.filesystembased.FilesystemBasedFtpRestart;
34  import goldengate.ftp.simpleimpl.file.FileBasedAuth;
35  import goldengate.ftp.simpleimpl.file.FileBasedDir;
36  
37  import org.jboss.netty.channel.Channel;
38  import org.jboss.netty.channel.ExceptionEvent;
39  
40  /**
41   * BusinessHandler implementation that allows pre and post actions on any
42   * operations and specifically on transfer operations
43   *
44   * @author Frederic Bregier
45   *
46   */
47  public class SimpleBusinessHandler extends BusinessHandler {
48      /**
49       * Internal Logger
50       */
51      private static final GgInternalLogger logger = GgInternalLoggerFactory
52              .getLogger(SimpleBusinessHandler.class);
53  
54      @Override
55      public void afterRunCommandKo(CommandAbstractException e) {
56          // TODO Auto-generated method stub
57          if (getFtpSession().getCurrentCommand() instanceof MKD) {
58              // do nothing
59          } else {
60              logger.warn("GBBH: AFTKO: {} {}", getFtpSession(), e.getMessage());
61          }
62      }
63  
64      @Override
65      public void afterRunCommandOk() throws CommandAbstractException {
66          // TODO Auto-generated method stub
67          // logger.info("GBBH: AFTOK: {}", getFtpSession());
68      }
69  
70      @Override
71      public void beforeRunCommand() throws CommandAbstractException {
72          // TODO Auto-generated method stub
73          // logger.info("GBBH: BEFCD: {}", getFtpSession());
74      }
75  
76      @Override
77      protected void cleanSession() {
78          // TODO Auto-generated method stub
79          // logger.info("GBBH: CLNSE: {}", getFtpSession());
80      }
81  
82      @Override
83      public void exceptionLocalCaught(ExceptionEvent e) {
84          // TODO Auto-generated method stub
85          logger.warn("GBBH: EXCEP: {} {}", getFtpSession(), e.getCause()
86                  .getMessage());
87      }
88  
89      @Override
90      public void executeChannelClosed() {
91          // TODO Auto-generated method stub
92          //logger.info("GBBH: CLOSED: for user {} with session {} ",
93          //        getFtpSession().getAuth().getUser(), getFtpSession());
94      }
95  
96      @Override
97      public void executeChannelConnected(Channel channel) {
98          // TODO Auto-generated method stub
99          // logger.info("GBBH: CONNEC: {}", getFtpSession());
100     }
101 
102     @Override
103     public FileBasedAuth getBusinessNewAuth() {
104         return new FileBasedAuth(getFtpSession());
105     }
106 
107     @Override
108     public FileBasedDir getBusinessNewDir() {
109         return new FileBasedDir(getFtpSession());
110     }
111 
112     @Override
113     public FilesystemBasedFtpRestart getBusinessNewRestart() {
114         return new FilesystemBasedFtpRestart(getFtpSession());
115     }
116 
117     @Override
118     public String getHelpMessage(String arg) {
119         return "This FTP server is only intend as a Gateway.\n"
120                 + "This FTP server refers to RFC 959, 775, 2389, 2428, 3659 and supports XCRC, XMD5 and XSHA1 commands.\n"
121                 + "XCRC, XMD5 and XSHA1 take a simple filename as argument and return \"250 digest-value is the digest of filename\".";
122     }
123 
124     @Override
125     public String getFeatMessage() {
126         StringBuilder builder = new StringBuilder("Extensions supported:");
127         builder.append('\n');
128         builder.append(getDefaultFeatMessage());
129         builder.append("\nEnd");
130         return builder.toString();
131     }
132 
133     @Override
134     public String getOptsMessage(String[] args) throws CommandAbstractException {
135         if (args.length > 0) {
136             if (args[0].equalsIgnoreCase(FtpCommandCode.MLST.name()) ||
137                     args[0].equalsIgnoreCase(FtpCommandCode.MLSD.name())) {
138                 return getMLSxOptsMessage(args);
139             }
140             throw new Reply502Exception("OPTS not implemented for " + args[0]);
141         }
142         throw new Reply502Exception("OPTS not implemented");
143     }
144 
145     /* (non-Javadoc)
146      * @see goldengate.ftp.core.control.BusinessHandler#getSpecializedSiteCommand(goldengate.ftp.core.session.FtpSession, java.lang.String)
147      */
148     @Override
149     public AbstractCommand getSpecializedSiteCommand(FtpSession session,
150             String line) {
151         return null;
152     }
153 
154     /* (non-Javadoc)
155      * @see goldengate.ftp.core.control.BusinessHandler#afterTransferDoneBeforeAnswer(goldengate.ftp.core.data.FtpTransfer)
156      */
157     @Override
158     public void afterTransferDoneBeforeAnswer(FtpTransfer transfer)
159             throws CommandAbstractException {
160         if (transfer.getCommand() == FtpCommandCode.APPE) {
161             logger.info("GBBH: Transfer: {} " + transfer.getStatus() + " {}",
162                     transfer.getCommand(), transfer.getPath());
163         } else if (transfer.getCommand() == FtpCommandCode.RETR) {
164             logger.info("GBBH: Transfer: {} " + transfer.getStatus() + " {}",
165                     transfer.getCommand(), transfer.getPath());
166         } else if (transfer.getCommand() == FtpCommandCode.STOR) {
167             logger.info("GBBH: Transfer: {} " + transfer.getStatus() + " {}",
168                     transfer.getCommand(), transfer.getPath());
169         } else if (transfer.getCommand() == FtpCommandCode.STOU) {
170             logger.info("GBBH: Transfer: {} " + transfer.getStatus() + " {}",
171                     transfer.getCommand(), transfer.getPath());
172         } else {
173             logger.warn("GBBH: Transfer unknown: {} " + transfer.getStatus() +
174                     " {}", transfer.getCommand(), transfer.getPath());
175             // Nothing to do
176         }
177     }
178 }