View Javadoc
1   /**
2    * Copyright 2009, Frederic Bregier, and individual contributors by the @author tags. See the
3    * COPYRIGHT.txt in the distribution for a full listing of individual contributors.
4    * 
5    * This is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser
6    * General Public License as published by the Free Software Foundation; either version 3.0 of the
7    * License, or (at your option) any later version.
8    * 
9    * This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
10   * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11   * GNU Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public License along with this
14   * software; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
15   * Boston, MA 02110-1301 USA, or see the FSF site: http://www.fsf.org.
16   */
17  package org.waarp.gateway.ftp.control;
18  
19  import org.waarp.common.command.ReplyCode;
20  import org.waarp.common.command.exception.CommandAbstractException;
21  import org.waarp.common.database.DbSession;
22  import org.waarp.common.database.data.AbstractDbData.UpdatedInfo;
23  import org.waarp.common.database.exception.WaarpDatabaseException;
24  import org.waarp.common.logging.WaarpLogger;
25  import org.waarp.common.logging.WaarpLoggerFactory;
26  import org.waarp.ftp.core.command.FtpCommandCode;
27  import org.waarp.ftp.core.control.BusinessHandler;
28  import org.waarp.ftp.core.data.FtpTransfer;
29  import org.waarp.ftp.core.exception.FtpNoFileException;
30  import org.waarp.ftp.core.session.FtpSession;
31  import org.waarp.gateway.ftp.config.FileBasedConfiguration;
32  import org.waarp.gateway.ftp.database.DbConstant;
33  import org.waarp.gateway.ftp.database.data.DbTransferLog;
34  
35  /**
36   * Class to help to log any actions through the interface of Waarp
37   * 
38   * @author Frederic Bregier
39   * 
40   */
41  public class WaarpActionLogger {
42      /**
43       * Internal Logger
44       */
45      private static final WaarpLogger logger = WaarpLoggerFactory
46              .getLogger(WaarpActionLogger.class);
47  
48      /**
49       * Log the action
50       * 
51       * @param ftpSession
52       * @param message
53       * @param file
54       * @param handler
55       */
56      public static long logCreate(DbSession ftpSession,
57              String message, String file, BusinessHandler handler) {
58          FtpSession session = handler.getFtpSession();
59          String sessionContexte = session.toString();
60          logger.info(message + " " + sessionContexte);
61          if (ftpSession != null) {
62              FtpCommandCode code = session.getCurrentCommand().getCode();
63              if (FtpCommandCode.isStorOrRetrLikeCommand(code)) {
64                  boolean isSender =
65                          FtpCommandCode.isRetrLikeCommand(code);
66                  try {
67                      // Insert new one
68                      DbTransferLog log =
69                              new DbTransferLog(ftpSession,
70                                      session.getAuth().getUser(),
71                                      session.getAuth().getAccount(),
72                                      DbConstant.ILLEGALVALUE,
73                                      isSender, file,
74                                      code.name(),
75                                      ReplyCode.REPLY_000_SPECIAL_NOSTATUS, message,
76                                      UpdatedInfo.TOSUBMIT);
77                      logger.debug("Create FS: " + log.toString());
78                      if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
79                          if (isSender) {
80                              FileBasedConfiguration.fileBasedConfiguration.monitoring
81                                      .updateLastOutBand();
82                          } else {
83                              FileBasedConfiguration.fileBasedConfiguration.monitoring
84                                      .updateLastInBound();
85                          }
86                      }
87                      return log.getSpecialId();
88                  } catch (WaarpDatabaseException e1) {
89                      // Do nothing
90                  }
91              }
92          }
93          return DbConstant.ILLEGALVALUE;
94      }
95  
96      /**
97       * Log the action
98       * 
99       * @param ftpSession
100      * @param specialId
101      * @param message
102      * @param handler
103      * @param rcode
104      * @param info
105      */
106     public static long logAction(DbSession ftpSession, long specialId,
107             String message, BusinessHandler handler, ReplyCode rcode,
108             UpdatedInfo info) {
109         FtpSession session = handler.getFtpSession();
110         String sessionContexte = session.toString();
111         logger.info(message + " " + sessionContexte);
112         if (ftpSession != null && specialId != DbConstant.ILLEGALVALUE) {
113             FtpCommandCode code = session.getCurrentCommand().getCode();
114             if (FtpCommandCode.isStorOrRetrLikeCommand(code)) {
115                 try {
116                     // Try load
117                     DbTransferLog log =
118                             new DbTransferLog(ftpSession,
119                                     session.getAuth().getUser(),
120                                     session.getAuth().getAccount(), specialId);
121                     log.changeUpdatedInfo(info);
122                     log.setInfotransf(message);
123                     log.setReplyCodeExecutionStatus(rcode);
124                     log.update();
125                     logger.debug("Update FS: " + log.toString());
126                     return log.getSpecialId();
127                 } catch (WaarpDatabaseException e) {
128                     // Do nothing
129                 }
130             } else {
131                 if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
132                     FileBasedConfiguration.fileBasedConfiguration.monitoring.
133                             updateCodeNoTransfer(rcode);
134                 }
135             }
136         } else {
137             if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
138                 FileBasedConfiguration.fileBasedConfiguration.monitoring.
139                         updateCodeNoTransfer(rcode);
140             }
141         }
142         return specialId;
143     }
144 
145     /**
146      * Log the action in error
147      * 
148      * @param ftpSession
149      * @param specialId
150      * @param transfer
151      * @param message
152      * @param rcode
153      * @param handler
154      */
155     public static void logErrorAction(DbSession ftpSession, long specialId,
156             FtpTransfer transfer,
157             String message, ReplyCode rcode, BusinessHandler handler) {
158         FtpSession session = handler.getFtpSession();
159         String sessionContexte = session.toString();
160         logger.error(rcode.getCode() + ":" + message + " " + sessionContexte);
161         logger.debug("Log",
162                 new Exception("Log"));
163         if (ftpSession != null && specialId != DbConstant.ILLEGALVALUE) {
164             FtpCommandCode code = session.getCurrentCommand().getCode();
165             if (FtpCommandCode.isStorOrRetrLikeCommand(code)) {
166                 String file = null;
167                 if (transfer != null) {
168                     try {
169                         file = transfer.getFtpFile().getFile();
170                     } catch (CommandAbstractException e1) {
171                     } catch (FtpNoFileException e1) {
172                     }
173                 } else {
174                     file = null;
175                 }
176                 UpdatedInfo info = UpdatedInfo.INERROR;
177                 try {
178                     // Try load
179                     DbTransferLog log =
180                             new DbTransferLog(ftpSession,
181                                     session.getAuth().getUser(),
182                                     session.getAuth().getAccount(), specialId);
183                     log.changeUpdatedInfo(info);
184                     log.setInfotransf(message);
185                     if (rcode.getCode() < 400) {
186                         log.setReplyCodeExecutionStatus(ReplyCode.REPLY_426_CONNECTION_CLOSED_TRANSFER_ABORTED);
187                     } else {
188                         log.setReplyCodeExecutionStatus(rcode);
189                     }
190                     if (file != null) {
191                         log.setFilename(file);
192                     }
193                     log.update();
194                     if (FileBasedConfiguration.fileBasedConfiguration.ftpMib != null) {
195                         FileBasedConfiguration.fileBasedConfiguration.ftpMib.
196                                 notifyInfoTask(message, log);
197                     }
198                     logger.debug("Update FS: " + log.toString());
199                 } catch (WaarpDatabaseException e) {
200                     // Do nothing
201                 }
202             } else {
203                 if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
204                     FileBasedConfiguration.fileBasedConfiguration.monitoring.
205                             updateCodeNoTransfer(rcode);
206                 }
207                 if (rcode != ReplyCode.REPLY_450_REQUESTED_FILE_ACTION_NOT_TAKEN &&
208                         rcode != ReplyCode.REPLY_550_REQUESTED_ACTION_NOT_TAKEN) {
209                     if (FileBasedConfiguration.fileBasedConfiguration.ftpMib != null) {
210                         FileBasedConfiguration.fileBasedConfiguration.ftpMib.
211                                 notifyWarning(rcode.getMesg(), message);
212                     }
213                 }
214             }
215         } else {
216             if (FileBasedConfiguration.fileBasedConfiguration.monitoring != null) {
217                 FileBasedConfiguration.fileBasedConfiguration.monitoring.
218                         updateCodeNoTransfer(rcode);
219             }
220             if (rcode != ReplyCode.REPLY_450_REQUESTED_FILE_ACTION_NOT_TAKEN &&
221                     rcode != ReplyCode.REPLY_550_REQUESTED_ACTION_NOT_TAKEN) {
222                 if (FileBasedConfiguration.fileBasedConfiguration.ftpMib != null) {
223                     FileBasedConfiguration.fileBasedConfiguration.ftpMib.
224                             notifyWarning(rcode.getMesg(), message);
225                 }
226             }
227         }
228     }
229 }