1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
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  
41  
42  public final class WaarpActionLogger {
43    
44  
45  
46    private static final WaarpLogger logger =
47        WaarpLoggerFactory.getLogger(WaarpActionLogger.class);
48  
49    private WaarpActionLogger() {
50    }
51  
52    
53  
54  
55  
56  
57  
58  
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            
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            
93          }
94        }
95      }
96      return DbConstant.ILLEGALVALUE;
97    }
98  
99    
100 
101 
102 
103 
104 
105 
106 
107 
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           
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           
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 
154 
155 
156 
157 
158 
159 
160 
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             
182           } catch (final FtpNoFileException ignored) {
183             
184           }
185         }
186         final UpdatedInfo info = UpdatedInfo.INERROR;
187         try {
188           
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           
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 }