2fd426dec2c5057f941680c39662f89c5d80f224
[so.git] /
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.onap.logging.ref.slf4j.ONAPLogConstants;
28 import org.onap.so.apihandler.common.ErrorNumbers;
29 import org.onap.so.apihandlerinfra.exceptions.DuplicateRequestIdException;
30 import org.onap.so.db.request.beans.InfraActiveRequests;
31 import org.onap.so.db.request.client.RequestsDbClient;
32 import org.onap.so.serviceinstancebeans.RequestError;
33 import org.onap.so.serviceinstancebeans.ServiceException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36 import org.slf4j.MDC;
37 import org.springframework.beans.factory.annotation.Autowired;
38 import org.springframework.stereotype.Component;
39 import com.fasterxml.jackson.core.JsonProcessingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42 @Priority(2)
43 @Provider
44 @Component
45 public class RequestIdFilter implements ContainerRequestFilter {
46
47     private static Logger logger = LoggerFactory.getLogger(RequestIdFilter.class);
48
49     @Autowired
50     private RequestsDbClient infraActiveRequestsClient;
51
52     @Override
53     public void filter(ContainerRequestContext context) throws IOException {
54         String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
55
56         logger.info("Checking if requestId: {} already exists in requestDb InfraActiveRequests table", requestId);
57         InfraActiveRequests infraActiveRequests = infraActiveRequestsClient.getInfraActiveRequestbyRequestId(requestId);
58
59         if (infraActiveRequests != null) {
60             logger.error(
61                     "RequestId: {} already exists in RequestDB InfraActiveRequests table, throwing DuplicateRequestIdException",
62                     requestId);
63             throw new DuplicateRequestIdException(createRequestError(requestId, "InfraActiveRequests"));
64         }
65     }
66
67     protected String createRequestError(String requestId, String requestTable) {
68         ObjectMapper mapper = new ObjectMapper();
69         RequestError error = new RequestError();
70         ServiceException serviceException = new ServiceException();
71         serviceException.setMessageId(ErrorNumbers.SVC_BAD_PARAMETER);
72         serviceException
73                 .setText("RequestId: " + requestId + " already exists in the RequestDB " + requestTable + " table");
74         error.setServiceException(serviceException);
75         String errorMessage = null;
76
77         try {
78             errorMessage = mapper.writeValueAsString(error);
79         } catch (JsonProcessingException e) {
80             return "Unable to write requestError to String when requestId already exists in the RequestDb due to error: "
81                     + e.getMessage();
82         }
83         return errorMessage;
84     }
85 }