2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.apihandler.common;
26 import java.io.IOException;
27 import org.apache.http.HttpEntity;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.HttpStatus;
30 import org.apache.http.util.EntityUtils;
31 import org.onap.so.apihandler.camundabeans.CamundaResponse;
32 import org.onap.so.apihandlerinfra.Constants;
33 import org.onap.so.apihandlerinfra.exceptions.ApiException;
34 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
35 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
36 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
37 import org.onap.so.logger.ErrorCode;
38 import org.onap.so.logger.MessageEnum;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class ResponseHandler {
45 private CamundaResponse response;
47 private String responseBody = "";
48 private HttpResponse httpResponse;
50 private static Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
52 public ResponseHandler(HttpResponse httpResponse, int type) throws ApiException {
53 this.httpResponse = httpResponse;
59 private void parseResponse() throws ApiException {
60 int statusCode = httpResponse.getStatusLine().getStatusCode();
61 status = setStatus(statusCode);
62 if (type == CommonConstants.CAMUNDA) {
64 } else if (type == CommonConstants.CAMUNDATASK) {
74 private void parseCamunda() throws ApiException {
76 HttpEntity entity = httpResponse.getEntity();
77 responseBody = EntityUtils.toString(entity);
78 } catch (IOException e) {
79 ErrorLoggerInfo errorLoggerInfo =
80 new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.SchemaError)
81 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
83 throw new ValidateException.Builder("IOException getting Camunda response body", HttpStatus.SC_BAD_REQUEST,
84 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
87 ObjectMapper mapper = new ObjectMapper();
89 response = mapper.readValue(responseBody, CamundaResponse.class);
90 } catch (IOException e) {
91 ErrorLoggerInfo errorLoggerInfo =
92 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
93 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
95 throw new ValidateException.Builder("Cannot parse Camunda Response", HttpStatus.SC_BAD_REQUEST,
96 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
98 if (response != null) {
99 responseBody = response.getResponse();
101 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER,
102 ErrorCode.BusinessProcesssError).targetEntity("Camunda").targetServiceName("parseCamunda").build();
103 BPMNFailureException bpmnFailureException =
104 new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
105 .errorInfo(errorLoggerInfo).build();
109 private void parseBpel() throws ApiException {
111 HttpEntity bpelEntity = httpResponse.getEntity();
114 if (bpelEntity != null) {
115 responseBody = EntityUtils.toString(bpelEntity);
118 } catch (IOException e) {
119 ErrorLoggerInfo errorLoggerInfo =
120 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
122 throw new ValidateException.Builder("Could not convert BPEL response to string", HttpStatus.SC_BAD_REQUEST,
123 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
125 if (status != HttpStatus.SC_ACCEPTED) {
126 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER,
127 ErrorCode.BusinessProcesssError).targetEntity("BPEL").targetServiceName("parseBpel").build();
129 throw new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
130 .errorInfo(errorLoggerInfo).build();
135 private void parseCamundaTask() throws ApiException {
137 HttpEntity camundataskEntity = httpResponse.getEntity();
140 if (camundataskEntity != null) {
141 responseBody = EntityUtils.toString(camundataskEntity);
143 } catch (IOException e) {
144 ErrorLoggerInfo errorLoggerInfo =
145 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
147 throw new ValidateException.Builder("Could not convert CamundaTask response to string",
148 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo)
151 if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED) {
152 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER,
153 ErrorCode.BusinessProcesssError).targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask")
156 throw new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
157 .errorInfo(errorLoggerInfo).build();
162 private int setStatus(int statusCode) {
164 switch (statusCode) {
165 case HttpStatus.SC_ACCEPTED:
166 case HttpStatus.SC_OK:
167 httpStatus = HttpStatus.SC_ACCEPTED;
169 case HttpStatus.SC_BAD_REQUEST:
170 httpStatus = HttpStatus.SC_BAD_REQUEST;
172 case HttpStatus.SC_UNAUTHORIZED:
173 case HttpStatus.SC_FORBIDDEN:
174 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
176 case HttpStatus.SC_NOT_FOUND:
177 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
179 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
180 httpStatus = HttpStatus.SC_BAD_GATEWAY;
182 case HttpStatus.SC_SERVICE_UNAVAILABLE:
183 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
185 case HttpStatus.SC_NO_CONTENT:
186 httpStatus = HttpStatus.SC_NO_CONTENT;
189 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
196 public CamundaResponse getResponse() {
201 public void setResponse(CamundaResponse response) {
202 this.response = response;
206 public String getResponseBody() {
211 public void setResponseBody(String responseBody) {
212 this.responseBody = responseBody;
216 public int getStatus() {