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.openecomp.mso.apihandler.camundabeans.CamundaResponse;
31 import org.openecomp.mso.logger.MessageEnum;
32 import org.openecomp.mso.logger.MsoLogger;
33 import org.openecomp.mso.utils.RootIgnoringObjectMapper;
35 import com.fasterxml.jackson.databind.ObjectMapper;
37 public class ResponseHandler {
39 private CamundaResponse response;
41 private String content = "";
42 private HttpResponse httpResponse;
44 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);
45 private static final String RESPONSE_CONTENT_MSG = "response content is: ";
47 public ResponseHandler(HttpResponse httpResponse, int type) {
48 this.httpResponse = httpResponse;
54 private void parseResponse() {
55 int statusCode = httpResponse.getStatusLine().getStatusCode();
56 msoLogger.debug("Returned status is: " + statusCode);
57 status = setStatus(statusCode);
58 msoLogger.debug("Parsed status is: " + status);
59 if(type==CommonConstants.CAMUNDA){
61 }else if(type==CommonConstants.CAMUNDATASK){
71 @SuppressWarnings("unchecked")
72 private void parseCamunda(){
74 HttpEntity entity = httpResponse.getEntity();
75 content = EntityUtils.toString(entity);
76 } catch (IOException e) {
77 msoLogger.debug("IOException getting Camunda response content", e);
80 ObjectMapper mapper = new RootIgnoringObjectMapper<CamundaResponse>(CamundaResponse.class);
83 response = mapper.readValue(content, CamundaResponse.class);
84 } catch (IOException e) {
85 msoLogger.debug("IOException getting Camunda response content", e);
87 msoLogger.debug("json response is: " + content);
89 content = response.getContent();
91 msoLogger.debug(RESPONSE_CONTENT_MSG + content);
94 if(status!=HttpStatus.SC_ACCEPTED){
95 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "Camunda", String.valueOf(status), content, "Camunda", "parseCamunda", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda");
99 private void parseBpel(){
101 HttpEntity bpelEntity = httpResponse.getEntity();
104 if (bpelEntity!=null) {
105 content = EntityUtils.toString(bpelEntity);
106 msoLogger.debug(RESPONSE_CONTENT_MSG + content);
109 if(status!=HttpStatus.SC_ACCEPTED){
110 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "BPEL", String.valueOf(status), content, "BPEL", "parseBpel", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from BPEL");
113 catch (IOException e) {
114 msoLogger.debug("IOException getting BPEL response content", e);
118 private void parseCamundaTask(){
120 HttpEntity camundataskEntity = httpResponse.getEntity();
123 if (camundataskEntity!=null) {
124 content = EntityUtils.toString(camundataskEntity);
125 msoLogger.debug(RESPONSE_CONTENT_MSG + content);
128 if(status!=HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED){
129 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "CAMUNDATASK", String.valueOf(status), content, "CAMUNDATASK", "parseCamundaTask", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda Task");
132 catch (IOException e) {
133 msoLogger.debug("IOException getting Camunda Task response content", e);
137 private int setStatus(int statusCode){
140 case HttpStatus.SC_ACCEPTED:
141 case HttpStatus.SC_OK:
142 httpStatus = HttpStatus.SC_ACCEPTED;
144 case HttpStatus.SC_BAD_REQUEST:
145 httpStatus = HttpStatus.SC_BAD_REQUEST;
147 case HttpStatus.SC_UNAUTHORIZED:
148 case HttpStatus.SC_FORBIDDEN:
149 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
151 case HttpStatus.SC_NOT_FOUND:
152 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
154 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
155 httpStatus = HttpStatus.SC_BAD_GATEWAY;
157 case HttpStatus.SC_SERVICE_UNAVAILABLE:
158 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
160 case HttpStatus.SC_NO_CONTENT:
161 httpStatus = HttpStatus.SC_NO_CONTENT;
164 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
171 public CamundaResponse getResponse() {
176 public void setResponse(CamundaResponse response) {
177 this.response = response;
181 public String getContent() {
186 public void setContent(String content) {
187 this.content = content;
191 public int getStatus() {