420ee7818b3a521a0ca41a52179b3f02add02606
[aai/schema-service.git] /
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.schemaservice.interceptors.post;
21
22 import com.att.eelf.configuration.EELFLogger;
23 import com.att.eelf.configuration.EELFManager;
24 import org.onap.aai.logging.LoggingContext;
25 import org.onap.aai.logging.LoggingContext.StatusCode;
26 import org.onap.aai.schemaservice.interceptors.AAIContainerFilter;
27 import org.springframework.beans.factory.annotation.Autowired;
28
29 import javax.annotation.Priority;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.ws.rs.container.ContainerRequestContext;
32 import javax.ws.rs.container.ContainerResponseContext;
33 import javax.ws.rs.container.ContainerResponseFilter;
34 import javax.ws.rs.core.Response.Status;
35 import javax.ws.rs.core.Response.StatusType;
36 import java.io.IOException;
37
38 @Priority(AAIResponseFilterPriority.RESET_LOGGING_CONTEXT)
39 public class ResetLoggingContext extends AAIContainerFilter implements ContainerResponseFilter {
40
41     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(ResetLoggingContext.class);
42
43     @Autowired
44     private HttpServletRequest httpServletRequest;
45
46     @Override
47     public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
48         throws IOException {
49
50         this.cleanLoggingContext(responseContext);
51
52     }
53
54     private void cleanLoggingContext(ContainerResponseContext responseContext) {
55         //String url = httpServletRequest.getRequestURL().toString();
56         boolean success = true;
57         String uri = httpServletRequest.getRequestURI();
58         String queryString = httpServletRequest.getQueryString();
59
60         if (queryString != null && !queryString.isEmpty()) {
61             uri = uri + "?" + queryString;
62         }
63         // For now, we use the the HTTP status code,
64         // This may change, once the requirements for response codes are defined
65
66         int httpStatusCode = responseContext.getStatus();
67         if (httpStatusCode < 100 || httpStatusCode > 599) {
68             httpStatusCode = Status.INTERNAL_SERVER_ERROR.getStatusCode();
69         }
70         LoggingContext.responseCode(Integer.toString(httpStatusCode));
71
72         StatusType sType = responseContext.getStatusInfo();
73         if (sType != null) {
74             Status.Family sFamily = sType.getFamily();
75             if (!(Status.Family.SUCCESSFUL.equals(sFamily) ||
76                 (Status.NOT_FOUND.equals(Status.fromStatusCode(httpStatusCode))))) {
77                 success = false;
78             }
79         } else {
80             if ((httpStatusCode < 200 || httpStatusCode > 299) && (!(Status.NOT_FOUND.equals(Status.fromStatusCode(httpStatusCode))))) {
81                 success = false;
82             }
83         }
84         if (success) {
85             LoggingContext.statusCode(StatusCode.COMPLETE);
86             LOGGER.info(uri + " call succeeded");
87         } else {
88             LoggingContext.statusCode(StatusCode.ERROR);
89             LOGGER.error(uri + " call failed with responseCode=" + httpStatusCode);
90         }
91         LoggingContext.clear();
92
93
94     }
95
96 }