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 =
102 new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcessError)
103 .targetEntity("Camunda").targetServiceName("parseCamunda").build();
104 BPMNFailureException bpmnFailureException =
105 new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
106 .errorInfo(errorLoggerInfo).build();
110 private void parseBpel() throws ApiException {
112 HttpEntity bpelEntity = httpResponse.getEntity();
115 if (bpelEntity != null) {
116 responseBody = EntityUtils.toString(bpelEntity);
119 } catch (IOException e) {
120 ErrorLoggerInfo errorLoggerInfo =
121 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
123 throw new ValidateException.Builder("Could not convert BPEL response to string", HttpStatus.SC_BAD_REQUEST,
124 ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
126 if (status != HttpStatus.SC_ACCEPTED) {
127 ErrorLoggerInfo errorLoggerInfo =
128 new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcessError)
129 .targetEntity("BPEL").targetServiceName("parseBpel").build();
131 throw new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
132 .errorInfo(errorLoggerInfo).build();
137 private void parseCamundaTask() throws ApiException {
139 HttpEntity camundataskEntity = httpResponse.getEntity();
142 if (camundataskEntity != null) {
143 responseBody = EntityUtils.toString(camundataskEntity);
145 } catch (IOException e) {
146 ErrorLoggerInfo errorLoggerInfo =
147 new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
149 throw new ValidateException.Builder("Could not convert CamundaTask response to string",
150 HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo)
153 if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED) {
154 ErrorLoggerInfo errorLoggerInfo =
155 new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcessError)
156 .targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask").build();
158 throw new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
159 .errorInfo(errorLoggerInfo).build();
164 private int setStatus(int statusCode) {
166 switch (statusCode) {
167 case HttpStatus.SC_ACCEPTED:
168 case HttpStatus.SC_OK:
169 httpStatus = HttpStatus.SC_ACCEPTED;
171 case HttpStatus.SC_BAD_REQUEST:
172 httpStatus = HttpStatus.SC_BAD_REQUEST;
174 case HttpStatus.SC_UNAUTHORIZED:
175 case HttpStatus.SC_FORBIDDEN:
176 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
178 case HttpStatus.SC_NOT_FOUND:
179 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
181 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
182 httpStatus = HttpStatus.SC_BAD_GATEWAY;
184 case HttpStatus.SC_SERVICE_UNAVAILABLE:
185 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
187 case HttpStatus.SC_NO_CONTENT:
188 httpStatus = HttpStatus.SC_NO_CONTENT;
191 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
198 public CamundaResponse getResponse() {
203 public void setResponse(CamundaResponse response) {
204 this.response = response;
208 public String getResponseBody() {
213 public void setResponseBody(String responseBody) {
214 this.responseBody = responseBody;
218 public int getStatus() {