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;
28 import org.apache.http.HttpEntity;
29 import org.apache.http.HttpResponse;
30 import org.apache.http.HttpStatus;
31 import org.apache.http.util.EntityUtils;
32 import org.onap.so.apihandler.camundabeans.CamundaResponse;
33 import org.onap.so.apihandlerinfra.Constants;
34 import org.onap.so.apihandlerinfra.exceptions.ApiException;
35 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
36 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
37 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
38 import org.onap.so.logger.ErrorCode;
39 import org.onap.so.logger.MessageEnum;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class ResponseHandler {
47 private CamundaResponse response;
49 private String responseBody="";
50 private HttpResponse httpResponse;
52 private static Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
54 public ResponseHandler(HttpResponse httpResponse, int type) throws ApiException{
55 this.httpResponse = httpResponse;
61 private void parseResponse() throws ApiException{
62 int statusCode = httpResponse.getStatusLine().getStatusCode();
63 status = setStatus(statusCode);
64 if(type==CommonConstants.CAMUNDA){
66 }else if(type==CommonConstants.CAMUNDATASK){
76 private void parseCamunda() throws ApiException{
78 HttpEntity entity = httpResponse.getEntity();
79 responseBody = EntityUtils.toString(entity);
80 } catch (IOException e) {
81 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.SchemaError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
84 ValidateException validateException = new ValidateException.Builder("IOException getting Camunda response body", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
85 .errorInfo(errorLoggerInfo).build();
86 throw validateException;
89 ObjectMapper mapper = new ObjectMapper();
91 response = mapper.readValue(responseBody, CamundaResponse.class);
92 } catch (IOException e) {
93 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
96 ValidateException validateException = new ValidateException.Builder("Cannot parse Camunda Response", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
97 .errorInfo(errorLoggerInfo).build();
98 throw validateException;
101 responseBody = response.getResponse();
103 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcesssError)
104 .targetEntity("Camunda").targetServiceName("parseCamunda").build();
105 BPMNFailureException bpmnFailureException = 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 = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
121 ValidateException validateException = new ValidateException.Builder("Could not convert BPEL response to string", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
122 .errorInfo(errorLoggerInfo).build();
123 throw validateException;
125 if(status!=HttpStatus.SC_ACCEPTED){
126 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcesssError)
127 .targetEntity("BPEL").targetServiceName("parseBpel").build();
130 BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
131 .errorInfo(errorLoggerInfo).build();
133 throw bpmnFailureException;
138 private void parseCamundaTask() throws ApiException{
140 HttpEntity camundataskEntity = httpResponse.getEntity();
143 if (camundataskEntity != null) {
144 responseBody = EntityUtils.toString(camundataskEntity);
146 }catch(IOException e) {
147 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
150 ValidateException validateException = new ValidateException.Builder("Could not convert CamundaTask response to string", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
151 .errorInfo(errorLoggerInfo).build();
152 throw validateException;
154 if(status!=HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED){
155 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcesssError)
156 .targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask").build();
159 BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
160 .errorInfo(errorLoggerInfo).build();
162 throw bpmnFailureException;
167 private int setStatus(int statusCode){
170 case HttpStatus.SC_ACCEPTED:
171 case HttpStatus.SC_OK:
172 httpStatus = HttpStatus.SC_ACCEPTED;
174 case HttpStatus.SC_BAD_REQUEST:
175 httpStatus = HttpStatus.SC_BAD_REQUEST;
177 case HttpStatus.SC_UNAUTHORIZED:
178 case HttpStatus.SC_FORBIDDEN:
179 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
181 case HttpStatus.SC_NOT_FOUND:
182 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
184 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
185 httpStatus = HttpStatus.SC_BAD_GATEWAY;
187 case HttpStatus.SC_SERVICE_UNAVAILABLE:
188 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
190 case HttpStatus.SC_NO_CONTENT:
191 httpStatus = HttpStatus.SC_NO_CONTENT;
194 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
201 public CamundaResponse getResponse() {
206 public void setResponse(CamundaResponse response) {
207 this.response = response;
211 public String getResponseBody() {
216 public void setResponseBody(String responseBody) {
217 this.responseBody = responseBody;
221 public int getStatus() {