fa9e815db88c3b544eef3e7dab309384fa197188
[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.jaxrs;
18
19 import static javax.ws.rs.core.Response.Status.Family.REDIRECTION;
20 import static javax.ws.rs.core.Response.Status.Family.SUCCESSFUL;
21 import static org.openecomp.sdc.logging.api.StatusCode.COMPLETE;
22 import static org.openecomp.sdc.logging.api.StatusCode.ERROR;
23 import static org.openecomp.sdc.logging.servlet.jaxrs.LoggingRequestFilter.LOGGING_TRACKER_KEY;
24
25 import javax.ws.rs.container.ContainerRequestContext;
26 import javax.ws.rs.container.ContainerResponseContext;
27 import javax.ws.rs.container.ContainerResponseFilter;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.ext.Provider;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.logging.api.StatusCode;
33 import org.openecomp.sdc.logging.servlet.RequestProcessingResult;
34 import org.openecomp.sdc.logging.servlet.Tracker;
35
36 /**
37  * <p>Takes care of logging when an HTTP request leaves the application. This includes writing to audit and clearing
38  * logging context. This filter <b>only works properly in tandem</b> with {@link LoggingRequestFilter} or a similar
39  * implementation.</p>
40  * <p>Sample configuration for a Spring environment:</p>
41  * <pre>
42  *     &lt;jaxrs:providers&gt;
43  *         &lt;bean class="org.openecomp.sdc.logging.ws.rs.LoggingResponseFilter"/&gt;
44  *     &lt;/jaxrs:providers&gt;
45  * </pre>
46  * <p><i>It is highly recommended to configure a custom JAX-RS exception mapper so that this filter will not be bypassed
47  * due to unhandled application or container exceptions.</i></p>
48  *
49  * @author evitaliy
50  * @see ContainerResponseFilter
51  * @since 29 Oct 17
52  */
53 @Provider
54 public class LoggingResponseFilter implements ContainerResponseFilter {
55
56     private static final Logger LOGGER = LoggerFactory.getLogger(LoggingResponseFilter.class);
57
58     @Override
59     public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
60
61         Tracker tracker = (Tracker) requestContext.getProperty(LOGGING_TRACKER_KEY);
62
63         if (tracker == null) {
64             LOGGER.debug("No logging tracker received");
65             return;
66         }
67
68         tracker.postRequest(new ContainerResponseResult(responseContext.getStatusInfo()));
69     }
70
71     private static class ContainerResponseResult implements RequestProcessingResult {
72
73         private final Response.StatusType statusInfo;
74
75         private ContainerResponseResult(Response.StatusType statusInfo) {
76             this.statusInfo = statusInfo;
77         }
78
79         @Override
80         public int getStatus() {
81             return statusInfo.getStatusCode();
82         }
83
84         @Override
85         public StatusCode getStatusCode() {
86             Response.Status.Family family = statusInfo.getFamily();
87             return family.equals(SUCCESSFUL) || family.equals(REDIRECTION) ? COMPLETE : ERROR;
88         }
89
90         @Override
91         public String getStatusPhrase() {
92             return statusInfo.getReasonPhrase();
93         }
94     }
95 }
96