2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.apihandler.filters;
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;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.stereotype.Component;
48 public class ResponseUpdateFilter implements ContainerResponseFilter {
50 protected static Logger logger = LoggerFactory.getLogger(ResponseUpdateFilter.class);
53 private HttpServletRequest httpServletRequest;
56 private Providers providers;
59 protected RequestsDbClient infraActiveRequestsClient;
62 public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
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);
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());
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);
89 if (error.getServiceException() != null && error.getServiceException().getText() != null
90 && !error.getServiceException().getText().isEmpty()) {
91 currentRequest.setStatusMessage(error.getServiceException().getText());
93 currentRequest.setStatusMessage("Unknown Error Occured during processing");
95 currentRequest.setRequestStatus(Status.FAILED.toString());
96 currentRequest.setEndTime(endTimeStamp);
97 currentRequest.setProgress(100L);
98 infraActiveRequestsClient.updateInfraActiveRequests(currentRequest);