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.openecomp.mso.apihandler.common;
24 import java.io.IOException;
26 import org.apache.http.HttpEntity;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.HttpStatus;
29 import org.apache.http.util.EntityUtils;
30 import org.codehaus.jackson.map.ObjectMapper;
32 import org.openecomp.mso.apihandler.camundabeans.CamundaResponse;
33 import org.openecomp.mso.logger.MsoLogger;
34 import org.openecomp.mso.logger.MessageEnum;
36 public class ResponseHandler {
38 private CamundaResponse response;
40 private String responseBody="";
41 private HttpResponse httpResponse;
43 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);
45 public ResponseHandler(HttpResponse httpResponse, int type) {
46 this.httpResponse = httpResponse;
52 private void parseResponse() {
53 int statusCode = httpResponse.getStatusLine().getStatusCode();
54 msoLogger.debug("Returned status is: " + statusCode);
55 status = setStatus(statusCode);
56 msoLogger.debug("Parsed status is: " + status);
57 if(type==CommonConstants.CAMUNDA){
59 }else if(type==CommonConstants.CAMUNDATASK){
69 private void parseCamunda(){
71 HttpEntity entity = httpResponse.getEntity();
72 responseBody = EntityUtils.toString(entity);
73 } catch (IOException e) {
74 msoLogger.debug("IOException getting Camunda response body", e);
77 ObjectMapper mapper = new ObjectMapper();
79 response = mapper.readValue(responseBody, CamundaResponse.class);
80 } catch (IOException e) {
81 msoLogger.debug("IOException getting Camunda response body", e);
83 msoLogger.debug("json response is: " + responseBody);
85 responseBody = response.getResponse();
87 msoLogger.debug("response body is: " + responseBody);
90 if(status!=HttpStatus.SC_ACCEPTED){
91 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "Camunda", String.valueOf(status), responseBody, "Camunda", "parseCamunda", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda");
95 private void parseBpel(){
97 HttpEntity bpelEntity = httpResponse.getEntity();
100 if (bpelEntity!=null) {
101 responseBody = EntityUtils.toString(bpelEntity);
102 msoLogger.debug("response body is: " + responseBody);
105 if(status!=HttpStatus.SC_ACCEPTED){
106 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "BPEL", String.valueOf(status), responseBody, "BPEL", "parseBpel", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from BPEL");
109 catch (IOException e) {
110 msoLogger.debug("IOException getting BPEL response body", e);
114 private void parseCamundaTask(){
116 HttpEntity camundataskEntity = httpResponse.getEntity();
119 if (camundataskEntity!=null) {
120 responseBody = EntityUtils.toString(camundataskEntity);
121 msoLogger.debug("response body is: " + responseBody);
124 if(status!=HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED){
125 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "CAMUNDATASK", String.valueOf(status), responseBody, "CAMUNDATASK", "parseCamundaTask", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda Task");
128 catch (IOException e) {
129 msoLogger.debug("IOException getting Camunda Task response body", e);
133 private int setStatus(int statusCode){
136 case HttpStatus.SC_ACCEPTED:
137 case HttpStatus.SC_OK:
138 status = HttpStatus.SC_ACCEPTED;
140 case HttpStatus.SC_BAD_REQUEST:
141 status = HttpStatus.SC_BAD_REQUEST;
143 case HttpStatus.SC_UNAUTHORIZED:
144 case HttpStatus.SC_FORBIDDEN:
145 status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
147 case HttpStatus.SC_NOT_FOUND:
148 status = HttpStatus.SC_NOT_IMPLEMENTED;
150 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
151 status = HttpStatus.SC_BAD_GATEWAY;
153 case HttpStatus.SC_SERVICE_UNAVAILABLE:
154 status = HttpStatus.SC_SERVICE_UNAVAILABLE;
156 case HttpStatus.SC_NO_CONTENT:
157 status = HttpStatus.SC_NO_CONTENT;
160 status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
167 public CamundaResponse getResponse() {
172 public void setResponse(CamundaResponse response) {
173 this.response = response;
177 public String getResponseBody() {
182 public void setResponseBody(String responseBody) {
183 this.responseBody = responseBody;
187 public int getStatus() {