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);
44 private static final String RESPONSE_BODY_MSG = "response body is: ";
46 public ResponseHandler(HttpResponse httpResponse, int type) {
47 this.httpResponse = httpResponse;
53 private void parseResponse() {
54 int statusCode = httpResponse.getStatusLine().getStatusCode();
55 msoLogger.debug("Returned status is: " + statusCode);
56 status = setStatus(statusCode);
57 msoLogger.debug("Parsed status is: " + status);
58 if(type==CommonConstants.CAMUNDA){
60 }else if(type==CommonConstants.CAMUNDATASK){
70 private void parseCamunda(){
72 HttpEntity entity = httpResponse.getEntity();
73 responseBody = EntityUtils.toString(entity);
74 } catch (IOException e) {
75 msoLogger.debug("IOException getting Camunda response body", e);
78 ObjectMapper mapper = new ObjectMapper();
80 response = mapper.readValue(responseBody, CamundaResponse.class);
81 } catch (IOException e) {
82 msoLogger.debug("IOException getting Camunda response body", e);
84 msoLogger.debug("json response is: " + responseBody);
86 responseBody = response.getResponse();
88 msoLogger.debug(RESPONSE_BODY_MSG + responseBody);
91 if(status!=HttpStatus.SC_ACCEPTED){
92 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "Camunda", String.valueOf(status), responseBody, "Camunda", "parseCamunda", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda");
96 private void parseBpel(){
98 HttpEntity bpelEntity = httpResponse.getEntity();
101 if (bpelEntity!=null) {
102 responseBody = EntityUtils.toString(bpelEntity);
103 msoLogger.debug(RESPONSE_BODY_MSG + responseBody);
106 if(status!=HttpStatus.SC_ACCEPTED){
107 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "BPEL", String.valueOf(status), responseBody, "BPEL", "parseBpel", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from BPEL");
110 catch (IOException e) {
111 msoLogger.debug("IOException getting BPEL response body", e);
115 private void parseCamundaTask(){
117 HttpEntity camundataskEntity = httpResponse.getEntity();
120 if (camundataskEntity!=null) {
121 responseBody = EntityUtils.toString(camundataskEntity);
122 msoLogger.debug(RESPONSE_BODY_MSG + responseBody);
125 if(status!=HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED){
126 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "CAMUNDATASK", String.valueOf(status), responseBody, "CAMUNDATASK", "parseCamundaTask", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda Task");
129 catch (IOException e) {
130 msoLogger.debug("IOException getting Camunda Task response body", e);
134 private int setStatus(int statusCode){
137 case HttpStatus.SC_ACCEPTED:
138 case HttpStatus.SC_OK:
139 httpStatus = HttpStatus.SC_ACCEPTED;
141 case HttpStatus.SC_BAD_REQUEST:
142 httpStatus = HttpStatus.SC_BAD_REQUEST;
144 case HttpStatus.SC_UNAUTHORIZED:
145 case HttpStatus.SC_FORBIDDEN:
146 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
148 case HttpStatus.SC_NOT_FOUND:
149 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
151 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
152 httpStatus = HttpStatus.SC_BAD_GATEWAY;
154 case HttpStatus.SC_SERVICE_UNAVAILABLE:
155 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
157 case HttpStatus.SC_NO_CONTENT:
158 httpStatus = HttpStatus.SC_NO_CONTENT;
161 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
168 public CamundaResponse getResponse() {
173 public void setResponse(CamundaResponse response) {
174 this.response = response;
178 public String getResponseBody() {
183 public void setResponseBody(String responseBody) {
184 this.responseBody = responseBody;
188 public int getStatus() {