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){
67 private void parseCamunda(){
69 HttpEntity entity = httpResponse.getEntity();
70 responseBody = EntityUtils.toString(entity);
71 } catch (IOException e) {
72 msoLogger.debug("IOException getting Camunda response body", e);
75 ObjectMapper mapper = new ObjectMapper();
77 response = mapper.readValue(responseBody, CamundaResponse.class);
78 } catch (IOException e) {
79 msoLogger.debug("IOException getting Camunda response body", e);
81 msoLogger.debug("json response is: " + responseBody);
83 responseBody = response.getResponse();
85 msoLogger.debug("response body is: " + responseBody);
88 if(status!=HttpStatus.SC_ACCEPTED){
89 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "Camunda", String.valueOf(status), responseBody, "Camunda", "parseCamunda", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda");
93 private void parseBpel(){
95 HttpEntity bpelEntity = httpResponse.getEntity();
98 if (bpelEntity!=null) {
99 responseBody = EntityUtils.toString(bpelEntity);
100 msoLogger.debug("response body is: " + responseBody);
103 if(status!=HttpStatus.SC_ACCEPTED){
104 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "BPEL", String.valueOf(status), responseBody, "BPEL", "parseBpel", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from BPEL");
107 catch (IOException e) {
108 msoLogger.debug("IOException getting BPEL response body", e);
115 private int setStatus(int statusCode){
118 case HttpStatus.SC_ACCEPTED:
119 case HttpStatus.SC_OK:
120 status = HttpStatus.SC_ACCEPTED;
122 case HttpStatus.SC_BAD_REQUEST:
123 status = HttpStatus.SC_BAD_REQUEST;
125 case HttpStatus.SC_UNAUTHORIZED:
126 case HttpStatus.SC_FORBIDDEN:
127 status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
129 case HttpStatus.SC_NOT_FOUND:
130 status = HttpStatus.SC_NOT_IMPLEMENTED;
132 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
133 status = HttpStatus.SC_BAD_GATEWAY;
135 case HttpStatus.SC_SERVICE_UNAVAILABLE:
136 status = HttpStatus.SC_SERVICE_UNAVAILABLE;
139 status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
146 public CamundaResponse getResponse() {
151 public void setResponse(CamundaResponse response) {
152 this.response = response;
156 public String getResponseBody() {
161 public void setResponseBody(String responseBody) {
162 this.responseBody = responseBody;
166 public int getStatus() {