399e0650f9d0739bfaaf2775cf5ace648ecf05c6
[so.git] / mso-api-handlers / mso-api-handler-common / src / main / java / org / onap / so / apihandler / filters / RequestIdFilter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 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.so.apihandler.filters;
21
22 import java.io.IOException;
23 import javax.annotation.Priority;
24 import javax.ws.rs.container.ContainerRequestContext;
25 import javax.ws.rs.container.ContainerRequestFilter;
26 import javax.ws.rs.ext.Provider;
27 import org.apache.http.HttpStatus;
28 import org.onap.logging.ref.slf4j.ONAPLogConstants;
29 import org.onap.so.db.request.beans.InfraActiveRequests;
30 import org.onap.so.db.request.client.RequestsDbClient;
31 import org.slf4j.MDC;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.stereotype.Component;
36
37 @Priority(2)
38 @Provider
39 @Component
40 public class RequestIdFilter implements ContainerRequestFilter {
41
42     protected static Logger logger = LoggerFactory.getLogger(RequestIdFilter.class);
43
44     @Autowired
45     private RequestsDbClient infraActiveRequestsClient;
46
47     @Override
48     public void filter(ContainerRequestContext context) throws IOException {
49         String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
50
51         InfraActiveRequests infraActiveRequests = infraActiveRequestsClient.getInfraActiveRequestbyRequestId(requestId);
52
53         if (infraActiveRequests != null) {
54             MDC.put(ONAPLogConstants.MDCs.RESPONSE_CODE, String.valueOf(HttpStatus.SC_BAD_REQUEST));
55             logger.error("RequestID exists in RequestDB.InfraActiveRequests : " + requestId);
56         }
57     }
58 }