119a0a560ea0859a972e8267930d41dc8349c6b9
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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
21 package org.onap.so.apihandler.filters;
22
23 import java.io.IOException;
24 import java.sql.Timestamp;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.ws.rs.container.ContainerRequestContext;
27 import javax.ws.rs.container.ContainerResponseContext;
28 import javax.ws.rs.container.ContainerResponseFilter;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.Response;
31 import javax.ws.rs.ext.Provider;
32 import javax.ws.rs.ext.Providers;
33 import org.onap.logging.ref.slf4j.ONAPLogConstants;
34 import org.onap.so.constants.Status;
35 import org.onap.so.db.request.beans.InfraActiveRequests;
36 import org.onap.so.db.request.client.RequestsDbClient;
37 import org.onap.so.serviceinstancebeans.RequestError;
38 import org.onap.so.serviceinstancebeans.ServiceException;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.slf4j.MDC;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
44
45 @Component
46 @Provider
47 @ResponseUpdater
48 public class ResponseUpdateFilter implements ContainerResponseFilter {
49
50     protected static Logger logger = LoggerFactory.getLogger(ResponseUpdateFilter.class);
51
52     @Context
53     private HttpServletRequest httpServletRequest;
54
55     @Context
56     private Providers providers;
57
58     @Autowired
59     protected RequestsDbClient infraActiveRequestsClient;
60
61     @Override
62     public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
63             throws IOException {
64         try {
65             logger.info("updating requests status");
66             updateRequestDBToFailed(responseContext);
67         } catch (Exception e) {
68             logger.warn("Error in outgoing JAX-RS Inteceptor updating request db to failed", e);
69         }
70     }
71
72     private void updateRequestDBToFailed(ContainerResponseContext responseContext) {
73         String requestId = MDC.get(ONAPLogConstants.MDCs.REQUEST_ID);
74         if (requestId != null && !Response.Status.Family.familyOf(responseContext.getStatus())
75                 .equals(Response.Status.Family.SUCCESSFUL)) {
76             InfraActiveRequests currentRequest = infraActiveRequestsClient.getInfraActiveRequestbyRequestId(requestId);
77             if (currentRequest != null) {
78                 Timestamp endTimeStamp = new Timestamp(System.currentTimeMillis());
79                 RequestError error;
80                 try {
81                     error = (RequestError) responseContext.getEntity();
82                 } catch (Exception e) {
83                     logger.warn("Error Casting Entity to Request Error, generating unknown Error", e);
84                     error = new RequestError();
85                     ServiceException serviceException = new ServiceException();
86                     serviceException.setText("Unknown Error Occured during processing");
87                     error.setServiceException(serviceException);
88                 }
89                 if (error.getServiceException() != null && error.getServiceException().getText() != null
90                         && !error.getServiceException().getText().isEmpty()) {
91                     currentRequest.setStatusMessage(error.getServiceException().getText());
92                 } else {
93                     currentRequest.setStatusMessage("Unknown Error Occured during processing");
94                 }
95                 currentRequest.setRequestStatus(Status.FAILED.toString());
96                 currentRequest.setEndTime(endTimeStamp);
97                 currentRequest.setProgress(100L);
98                 infraActiveRequestsClient.updateInfraActiveRequests(currentRequest);
99             }
100         }
101     }
102 }
103