2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.logging.servlet.jaxrs;
19 import static org.openecomp.sdc.logging.api.StatusCode.COMPLETE;
20 import static org.openecomp.sdc.logging.api.StatusCode.ERROR;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.ws.rs.container.ContainerRequestContext;
24 import javax.ws.rs.container.ContainerResponseContext;
25 import javax.ws.rs.container.ContainerResponseFilter;
26 import javax.ws.rs.core.Context;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.ext.Provider;
29 import org.openecomp.sdc.logging.api.AuditData;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.logging.api.LoggingContext;
33 import org.openecomp.sdc.logging.api.StatusCode;
36 * <p>Takes care of logging when an HTTP request leaves the application. This includes writing to audit and clearing
37 * logging context. This filter <b>only works properly in tandem</b> with {@link LoggingRequestFilter} or a similar
39 * <p>Sample configuration for a Spring environment:</p>
41 * <jaxrs:providers>
42 * <bean class="org.openecomp.sdc.logging.ws.rs.LoggingResponseFilter"/>
43 * </jaxrs:providers>
45 * <p><i>It is highly recommended to configure a custom JAX-RS exception mapper so that this filter will not be bypassed
46 * due to unhandled application or container exceptions.</i></p>
51 * @see ContainerResponseFilter
54 public class LoggingResponseFilter implements ContainerResponseFilter {
56 private final Logger logger = LoggerFactory.getLogger(this.getClass());
59 * Tracks reporting configuration problems to the log. We want to report them only once, and not to write to log
60 * upon every request, as the configuration will not change in runtime.
62 private boolean reportBadConfiguration = true;
64 private HttpServletRequest httpRequest;
67 * Injection of HTTP request object from JAX-RS context.
69 * @param httpRequest automatically injected by JAX-RS container
72 public void setHttpRequest(HttpServletRequest httpRequest) {
73 this.httpRequest = httpRequest;
77 public void filter(ContainerRequestContext containerRequestContext,
78 ContainerResponseContext containerResponseContext) {
81 writeAudit(containerRequestContext, containerResponseContext);
83 LoggingContext.clear();
87 private void writeAudit(ContainerRequestContext containerRequestContext,
88 ContainerResponseContext containerResponseContext) {
90 if (!logger.isAuditEnabled()) {
94 long start = readStartTime(containerRequestContext);
95 long end = System.currentTimeMillis();
97 Response.StatusType statusInfo = containerResponseContext.getStatusInfo();
98 int responseCode = statusInfo.getStatusCode();
99 StatusCode statusCode = isSuccess(responseCode) ? COMPLETE : ERROR;
101 AuditData auditData = AuditData.builder().startTime(start).endTime(end).statusCode(statusCode)
102 .responseCode(Integer.toString(responseCode))
103 .responseDescription(statusInfo.getReasonPhrase())
104 .clientIpAddress(httpRequest.getRemoteAddr()).build();
105 logger.audit(auditData);
108 private boolean isSuccess(int responseCode) {
109 return responseCode > 199 && responseCode < 400;
112 private long readStartTime(ContainerRequestContext containerRequestContext) {
114 Object startTime = containerRequestContext.getProperty(LoggingRequestFilter.START_TIME_KEY);
115 if (startTime == null) {
116 return handleMissingStartTime();
119 return parseStartTime(startTime);
122 private long handleMissingStartTime() {
123 reportConfigProblem("{} key was not found in JAX-RS request context. "
124 + "Make sure you configured a request filter", LoggingRequestFilter.START_TIME_KEY);
128 private long parseStartTime(Object startTime) {
131 return Long.class.cast(startTime);
132 } catch (ClassCastException e) {
133 reportConfigProblem("{} key in JAX-RS request context contains an object of type '{}', but 'java.lang.Long'"
134 + " is expected", LoggingRequestFilter.START_TIME_KEY, startTime.getClass().getName());
139 private void reportConfigProblem(String message, Object... arguments) {
141 if (reportBadConfiguration) {
142 reportBadConfiguration = false;
143 logger.error(message, arguments);