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 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;
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;
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
40 * <p>Sample configuration for a Spring environment:</p>
42 * <jaxrs:providers>
43 * <bean class="org.openecomp.sdc.logging.ws.rs.LoggingResponseFilter"/>
44 * </jaxrs:providers>
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>
50 * @see ContainerResponseFilter
54 public class LoggingResponseFilter implements ContainerResponseFilter {
56 private static final Logger LOGGER = LoggerFactory.getLogger(LoggingResponseFilter.class);
59 public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
61 Tracker tracker = (Tracker) requestContext.getProperty(LOGGING_TRACKER_KEY);
63 if (tracker == null) {
64 LOGGER.debug("No logging tracker received");
68 tracker.postRequest(new ContainerResponseResult(responseContext.getStatusInfo()));
71 private static class ContainerResponseResult implements RequestProcessingResult {
73 private final Response.StatusType statusInfo;
75 private ContainerResponseResult(Response.StatusType statusInfo) {
76 this.statusInfo = statusInfo;
80 public int getStatus() {
81 return statusInfo.getStatusCode();
85 public StatusCode getStatusCode() {
86 Response.Status.Family family = statusInfo.getFamily();
87 return family.equals(SUCCESSFUL) || family.equals(REDIRECTION) ? COMPLETE : ERROR;
91 public String getStatusPhrase() {
92 return statusInfo.getReasonPhrase();