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();
84 ValidateException validateException =
85 new ValidateException.Builder("IOException getting Camunda response body",
86 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
87 .errorInfo(errorLoggerInfo).build();
88 throw validateException;
91 ObjectMapper mapper = new ObjectMapper();
93 response = mapper.readValue(responseBody, CamundaResponse.class);
94 } catch (IOException e) {
95 ErrorLoggerInfo errorLoggerInfo =
96 new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
97 .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
100 ValidateException validateException =
101 new ValidateException.Builder("Cannot parse Camunda Response", HttpStatus.SC_BAD_REQUEST,
102 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
103 throw validateException;
105 if (response != null) {
106 responseBody = response.getResponse();
108 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER,
109 ErrorCode.BusinessProcesssError).targetEntity("Camunda").targetServiceName("parseCamunda").build();
110 BPMNFailureException bpmnFailureException =
111 new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
112 .errorInfo(errorLoggerInfo).build();
116 private void parseBpel() throws ApiException {
118 HttpEntity bpelEntity = httpResponse.getEntity();
121 if (bpelEntity != null) {
122 responseBody = EntityUtils.toString(bpelEntity);
125 } catch (IOException e) {
126 ErrorLoggerInfo errorLoggerInfo =
127 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
128 ValidateException validateException =
129 new ValidateException.Builder("Could not convert BPEL response to string",
130 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
131 .errorInfo(errorLoggerInfo).build();
132 throw validateException;
134 if (status != HttpStatus.SC_ACCEPTED) {
135 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER,
136 ErrorCode.BusinessProcesssError).targetEntity("BPEL").targetServiceName("parseBpel").build();
139 BPMNFailureException bpmnFailureException =
140 new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
141 .errorInfo(errorLoggerInfo).build();
143 throw bpmnFailureException;
148 private void parseCamundaTask() throws ApiException {
150 HttpEntity camundataskEntity = httpResponse.getEntity();
153 if (camundataskEntity != null) {
154 responseBody = EntityUtils.toString(camundataskEntity);
156 } catch (IOException e) {
157 ErrorLoggerInfo errorLoggerInfo =
158 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
161 ValidateException validateException =
162 new ValidateException.Builder("Could not convert CamundaTask response to string",
163 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
164 .errorInfo(errorLoggerInfo).build();
165 throw validateException;
167 if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED) {
168 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER,
169 ErrorCode.BusinessProcesssError).targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask")
173 BPMNFailureException bpmnFailureException =
174 new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
175 .errorInfo(errorLoggerInfo).build();
177 throw bpmnFailureException;
182 private int setStatus(int statusCode) {
184 switch (statusCode) {
185 case HttpStatus.SC_ACCEPTED:
186 case HttpStatus.SC_OK:
187 httpStatus = HttpStatus.SC_ACCEPTED;
189 case HttpStatus.SC_BAD_REQUEST:
190 httpStatus = HttpStatus.SC_BAD_REQUEST;
192 case HttpStatus.SC_UNAUTHORIZED:
193 case HttpStatus.SC_FORBIDDEN:
194 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
196 case HttpStatus.SC_NOT_FOUND:
197 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
199 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
200 httpStatus = HttpStatus.SC_BAD_GATEWAY;
202 case HttpStatus.SC_SERVICE_UNAVAILABLE:
203 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
205 case HttpStatus.SC_NO_CONTENT:
206 httpStatus = HttpStatus.SC_NO_CONTENT;
209 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
216 public CamundaResponse getResponse() {
221 public void setResponse(CamundaResponse response) {
222 this.response = response;
226 public String getResponseBody() {
231 public void setResponseBody(String responseBody) {
232 this.responseBody = responseBody;
236 public int getStatus() {