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  
21  package org.waarp.gateway.ftp.control;
22  
23  import org.waarp.common.command.ReplyCode;
24  import org.waarp.common.command.exception.CommandAbstractException;
25  import org.waarp.common.database.DbConstant;
26  import org.waarp.common.database.DbSession;
27  import org.waarp.common.database.data.AbstractDbData.UpdatedInfo;
28  import org.waarp.common.database.exception.WaarpDatabaseException;
29  import org.waarp.common.logging.WaarpLogger;
30  import org.waarp.common.logging.WaarpLoggerFactory;
31  import org.waarp.ftp.core.command.FtpCommandCode;
32  import org.waarp.ftp.core.control.BusinessHandler;
33  import org.waarp.ftp.core.data.FtpTransfer;
34  import org.waarp.ftp.core.exception.FtpNoFileException;
35  import org.waarp.ftp.core.session.FtpSession;
36  import org.waarp.gateway.ftp.config.FileBasedConfiguration;
37  import org.waarp.gateway.ftp.database.data.DbTransferLog;
38  
39  /**
40   * Class to help to log any actions through the interface of Waarp
41   */
42  public final class WaarpActionLogger {
43    /**
44     * Internal Logger
45     */
46    private static final WaarpLogger logger =
47        WaarpLoggerFactory.getLogger(WaarpActionLogger.class);
48  
49    private WaarpActionLogger() {
50    }
51  
52    /**
53     * Log the action
54     *
55     * @param ftpSession
56     * @param message
57     * @param file
58     * @param handler
59     */
60    public static long logCreate(final DbSession ftpSession, final String message,
61                                 final String file,
62                                 final BusinessHandler handler) {
63      final FtpSession session = handler.getFtpSession();
64      final String sessionContexte = session.toString();
65      logger.info("{} {}", message, sessionContexte);
66      if (ftpSession != null) {
67        final FtpCommandCode code = session.getCurrentCommand().getCode();
68        if (FtpCommandCode.isStorOrRetrLikeCommand(code)) {
69          final boolean isSender = FtpCommandCode.isRetrLikeCommand(code);
70          try {
71            // Insert new one
72            final DbTransferLog log =
73                new DbTransferLog(ftpSession, session.getAuth().getUser(),
74                                  session.getAuth().getAccount(),
75                                  DbConstant.ILLEGALVALUE, isSender, file,
76                                  code.name(),
77                                  ReplyCode.REPLY_000_SPECIAL_NOSTATUS, message,
78                                  UpdatedInfo.TOSUBMIT);
79            logger.debug("Create FS: {}", log);
80            if (FileBasedConfiguration.fileBasedConfiguration.getMonitoring() !=
81                null) {
82              if (isSender) {
83                FileBasedConfiguration.fileBasedConfiguration.getMonitoring()
84                                                             .updateLastOutBand();
85              } else {
86                FileBasedConfiguration.fileBasedConfiguration.getMonitoring()
87                                                             .updateLastInBound();
88              }
89            }
90            return log.getSpecialId();
91          } catch (final WaarpDatabaseException e1) {
92            // Do nothing
93          }
94        }
95      }
96      return DbConstant.ILLEGALVALUE;
97    }
98  
99    /**
100    * Log the action
101    *
102    * @param ftpSession
103    * @param specialId
104    * @param message
105    * @param handler
106    * @param rcode
107    * @param info
108    */
109   public static long logAction(final DbSession ftpSession, final long specialId,
110                                final String message,
111                                final BusinessHandler handler,
112                                final ReplyCode rcode, final UpdatedInfo info) {
113     final FtpSession session = handler.getFtpSession();
114     final String sessionContexte = session.toString();
115     logger.info("{} {}", message, sessionContexte);
116     if (ftpSession != null && specialId != DbConstant.ILLEGALVALUE) {
117       final FtpCommandCode code = session.getCurrentCommand().getCode();
118       if (FtpCommandCode.isStorOrRetrLikeCommand(code)) {
119         try {
120           // Try load
121           final DbTransferLog log =
122               new DbTransferLog(ftpSession, session.getAuth().getUser(),
123                                 session.getAuth().getAccount(), specialId);
124           log.changeUpdatedInfo(info);
125           log.setInfotransf(message);
126           log.setReplyCodeExecutionStatus(rcode);
127           log.update();
128           logger.debug("Update FS: {}", log);
129           return log.getSpecialId();
130         } catch (final WaarpDatabaseException e) {
131           // Do nothing
132         }
133       } else {
134         if (FileBasedConfiguration.fileBasedConfiguration.getMonitoring() !=
135             null) {
136           FileBasedConfiguration.fileBasedConfiguration.getMonitoring()
137                                                        .updateCodeNoTransfer(
138                                                            rcode);
139         }
140       }
141     } else {
142       if (FileBasedConfiguration.fileBasedConfiguration.getMonitoring() !=
143           null) {
144         FileBasedConfiguration.fileBasedConfiguration.getMonitoring()
145                                                      .updateCodeNoTransfer(
146                                                          rcode);
147       }
148     }
149     return specialId;
150   }
151 
152   /**
153    * Log the action in error
154    *
155    * @param ftpSession
156    * @param specialId
157    * @param transfer
158    * @param message
159    * @param rcode
160    * @param handler
161    */
162   public static void logErrorAction(final DbSession ftpSession,
163                                     final long specialId,
164                                     final FtpTransfer transfer,
165                                     final String message, final ReplyCode rcode,
166                                     final BusinessHandler handler) {
167     final FtpSession session = handler.getFtpSession();
168     final String sessionContexte = session.toString();
169     logger.error(rcode.getCode() + ":" + message + ' ' + sessionContexte);
170     if (logger.isDebugEnabled()) {
171       logger.debug("Log", new Exception("Trace only"));
172     }
173     if (ftpSession != null && specialId != DbConstant.ILLEGALVALUE) {
174       final FtpCommandCode code = session.getCurrentCommand().getCode();
175       if (FtpCommandCode.isStorOrRetrLikeCommand(code)) {
176         String file = null;
177         if (transfer != null) {
178           try {
179             file = transfer.getFtpFile().getFile();
180           } catch (final CommandAbstractException ignored) {
181             // nothing
182           } catch (final FtpNoFileException ignored) {
183             // nothing
184           }
185         }
186         final UpdatedInfo info = UpdatedInfo.INERROR;
187         try {
188           // Try load
189           final DbTransferLog log =
190               new DbTransferLog(ftpSession, session.getAuth().getUser(),
191                                 session.getAuth().getAccount(), specialId);
192           log.changeUpdatedInfo(info);
193           log.setInfotransf(message);
194           if (rcode.getCode() < 400) {
195             log.setReplyCodeExecutionStatus(
196                 ReplyCode.REPLY_426_CONNECTION_CLOSED_TRANSFER_ABORTED);
197           } else {
198             log.setReplyCodeExecutionStatus(rcode);
199           }
200           if (file != null) {
201             log.setFilename(file);
202           }
203           log.update();
204           if (FileBasedConfiguration.fileBasedConfiguration.getFtpMib() !=
205               null) {
206             FileBasedConfiguration.fileBasedConfiguration.getFtpMib()
207                                                          .notifyInfoTask(
208                                                              message, log);
209           }
210           logger.debug("Update FS: {}", log);
211         } catch (final WaarpDatabaseException e) {
212           // Do nothing
213         }
214       } else {
215         if (FileBasedConfiguration.fileBasedConfiguration.getMonitoring() !=
216             null) {
217           FileBasedConfiguration.fileBasedConfiguration.getMonitoring()
218                                                        .updateCodeNoTransfer(
219                                                            rcode);
220         }
221         if (rcode != ReplyCode.REPLY_450_REQUESTED_FILE_ACTION_NOT_TAKEN &&
222             rcode != ReplyCode.REPLY_550_REQUESTED_ACTION_NOT_TAKEN) {
223           if (FileBasedConfiguration.fileBasedConfiguration.getFtpMib() !=
224               null) {
225             FileBasedConfiguration.fileBasedConfiguration.getFtpMib()
226                                                          .notifyWarning(
227                                                              rcode.getMesg(),
228                                                              message);
229           }
230         }
231       }
232     } else {
233       if (FileBasedConfiguration.fileBasedConfiguration.getMonitoring() !=
234           null) {
235         FileBasedConfiguration.fileBasedConfiguration.getMonitoring()
236                                                      .updateCodeNoTransfer(
237                                                          rcode);
238       }
239       if (rcode != ReplyCode.REPLY_450_REQUESTED_FILE_ACTION_NOT_TAKEN &&
240           rcode != ReplyCode.REPLY_550_REQUESTED_ACTION_NOT_TAKEN) {
241         if (FileBasedConfiguration.fileBasedConfiguration.getFtpMib() != null) {
242           FileBasedConfiguration.fileBasedConfiguration.getFtpMib()
243                                                        .notifyWarning(
244                                                            rcode.getMesg(),
245                                                            message);
246         }
247       }
248     }
249   }
250 }