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.kernel.database;
22  
23  import io.netty.handler.codec.http.HttpResponseStatus;
24  import org.waarp.common.database.DbSession;
25  import org.waarp.common.database.data.AbstractDbData.UpdatedInfo;
26  import org.waarp.common.database.exception.WaarpDatabaseException;
27  import org.waarp.common.logging.WaarpLogger;
28  import org.waarp.common.logging.WaarpLoggerFactory;
29  import org.waarp.gateway.kernel.HttpPage.PageRole;
30  import org.waarp.gateway.kernel.database.data.DbTransferLog;
31  import org.waarp.gateway.kernel.session.HttpSession;
32  
33  /**
34   * Class to help to log any actions through the interface of Waarp
35   */
36  public final class WaarpActionLogger {
37    /**
38     * Internal Logger
39     */
40    private static final WaarpLogger logger =
41        WaarpLoggerFactory.getLogger(WaarpActionLogger.class);
42  
43    private WaarpActionLogger() {
44    }
45  
46    /**
47     * Log the action
48     *
49     * @param dbSession
50     * @param message
51     * @param session
52     */
53    public static void logCreate(final DbSession dbSession, final String message,
54                                 final HttpSession session) {
55      final String sessionContexte = session.toString();
56      logger.info("{} {}", message, sessionContexte);
57      if (dbSession != null) {
58        final PageRole code = session.getCurrentCommand();
59        boolean isSender = false;
60        switch (code) {
61          case ERROR:
62          case HTML:
63          case MENU:
64            session.setLogid(DbConstantGateway.ILLEGALVALUE);
65            return;
66          case DELETE:
67          case GETDOWNLOAD:
68            isSender = false;
69            break;
70          case POST:
71          case POSTUPLOAD:
72          case PUT:
73            isSender = true;
74            break;
75          default:
76            break;
77        }
78        // Insert new one
79        try {
80          final DbTransferLog log =
81              new DbTransferLog(dbSession, session.getAuth().getUser(),
82                                session.getAuth().getAccount(),
83                                DbConstantGateway.ILLEGALVALUE, isSender,
84                                session.getFilename(), code.name(),
85                                HttpResponseStatus.OK, message,
86                                UpdatedInfo.TOSUBMIT);
87          logger.debug("Create FS: {}", log);
88          session.setLogid(log.getSpecialId());
89          return;
90        } catch (final WaarpDatabaseException e1) {
91          // Do nothing
92        }
93      }
94      session.setLogid(DbConstantGateway.ILLEGALVALUE);
95    }
96  
97    /**
98     * Log the action
99     *
100    * @param dbSession
101    * @param session
102    * @param message
103    * @param rcode
104    * @param info
105    */
106   public static void logAction(final DbSession dbSession,
107                                final HttpSession session, final String message,
108                                final HttpResponseStatus rcode,
109                                final UpdatedInfo info) {
110     final String sessionContexte = session.toString();
111     final long specialId = session.getLogid();
112     logger.info("{} {}", message, sessionContexte);
113     if (dbSession != null && specialId != DbConstantGateway.ILLEGALVALUE) {
114       final PageRole code = session.getCurrentCommand();
115       switch (code) {
116         case DELETE:
117         case GETDOWNLOAD:
118         case POST:
119         case POSTUPLOAD:
120         case PUT:
121           break;
122         case ERROR:
123         case HTML:
124         case MENU:
125         default:
126           return;
127       }
128       try {
129         // Try load
130         final DbTransferLog log =
131             new DbTransferLog(dbSession, session.getAuth().getUser(),
132                               session.getAuth().getAccount(), specialId);
133         log.changeUpdatedInfo(info);
134         log.setInfotransf(message);
135         log.setReplyCodeExecutionStatus(rcode);
136         log.update();
137         logger.debug("Update FS: {}", log);
138         session.setLogid(log.getSpecialId());
139       } catch (final WaarpDatabaseException e) {
140         // Do nothing
141       }
142     }
143   }
144 
145   /**
146    * Log the action in error
147    *
148    * @param dbSession
149    * @param session
150    * @param message
151    * @param rcode
152    */
153   public static void logErrorAction(final DbSession dbSession,
154                                     final HttpSession session,
155                                     final String message,
156                                     final HttpResponseStatus rcode) {
157     final String sessionContexte = session.toString();
158     final long specialId = session.getLogid();
159     logger.error(rcode.code() + ":" + message + ' ' + sessionContexte);
160     if (dbSession != null && specialId != DbConstantGateway.ILLEGALVALUE) {
161       final PageRole code = session.getCurrentCommand();
162       switch (code) {
163         case DELETE:
164         case GETDOWNLOAD:
165         case POST:
166         case POSTUPLOAD:
167         case PUT:
168           break;
169         case ERROR:
170         case HTML:
171         case MENU:
172         default:
173           return;
174       }
175       final UpdatedInfo info = UpdatedInfo.INERROR;
176       try {
177         // Try load
178         final DbTransferLog log =
179             new DbTransferLog(dbSession, session.getAuth().getUser(),
180                               session.getAuth().getAccount(), specialId);
181         log.changeUpdatedInfo(info);
182         log.setInfotransf(message);
183         if (rcode.code() < 400) {
184           log.setReplyCodeExecutionStatus(HttpResponseStatus.BAD_REQUEST);
185         } else {
186           log.setReplyCodeExecutionStatus(rcode);
187         }
188         if (session.getFilename() != null) {
189           log.setFilename(session.getFilename());
190         }
191         log.update();
192         logger.debug("Update FS: {}", log);
193       } catch (final WaarpDatabaseException e) {
194         // Do nothing
195       }
196     }
197   }
198 }