af9b5061fd06ec5978e97fd163d8f69b2f8511c3
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.logging.servlet;
18
19 import java.util.Objects;
20 import javax.servlet.http.HttpServletRequest;
21 import org.openecomp.sdc.logging.api.AuditData;
22 import org.openecomp.sdc.logging.api.Logger;
23 import org.openecomp.sdc.logging.api.LoggerFactory;
24
25 /**
26  * Tracks and logs audit information when a request is being processed. An instance of this class cannot be reused, and
27  * the pre- and post-request methods must be called only once.
28  *
29  * @author evitaliy
30  * @since 31 Jul 2018
31  */
32 public class AuditTracker implements Tracker {
33
34     private final Logger logger;
35     private volatile long started;
36     private volatile String clientIpAddress;
37
38     /**
39      * Allows passing a class that will be used to log audit.
40      *
41      * @param resourceType audit will be logged through the logger of this class
42      */
43     public AuditTracker(Class<?> resourceType) {
44         this.logger = LoggerFactory.getLogger(resourceType);
45     }
46
47     /**
48      * Allows passing a logger that will be used to log audit.
49      *
50      * @param logger audit will be logged through this logger, cannot be null
51      */
52     public AuditTracker(Logger logger) {
53         this.logger = Objects.requireNonNull(logger);
54     }
55
56     @Override
57     public synchronized void preRequest(HttpServletRequest request) {
58
59         if (this.started > 0) {
60             throw new IllegalStateException("Pre-request has been already called");
61         }
62
63         this.started = System.currentTimeMillis();
64         this.clientIpAddress = request.getRemoteAddr();
65     }
66
67     @Override
68     public synchronized void postRequest(RequestProcessingResult result) {
69
70         if (this.started == 0) {
71             throw new IllegalStateException("Pre-request must be called first");
72         }
73
74         if (!logger.isAuditEnabled()) {
75             return;
76         }
77
78         long end = System.currentTimeMillis();
79         AuditData auditData = AuditData.builder().startTime(started).endTime(end).statusCode(result.getStatusCode())
80                                       .responseCode(Integer.toString(result.getStatus()))
81                                       .responseDescription(result.getStatusPhrase()).clientIpAddress(clientIpAddress)
82                                       .build();
83         logger.audit(auditData);
84     }
85 }