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.onap.so.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.onap.so.apihandler.camundabeans.CamundaResponse;
31 import org.onap.so.apihandlerinfra.Constants;
32 import org.onap.so.apihandlerinfra.exceptions.ApiException;
33 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
34 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
35 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
36 import org.onap.so.logger.MessageEnum;
37 import org.onap.so.logger.MsoLogger;
39 import com.fasterxml.jackson.databind.ObjectMapper;
41 public class ResponseHandler {
43 private CamundaResponse response;
45 private String responseBody="";
46 private HttpResponse httpResponse;
48 private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, ResponseHandler.class);
50 public ResponseHandler(HttpResponse httpResponse, int type) throws ApiException{
51 this.httpResponse = httpResponse;
57 private void parseResponse() throws ApiException{
58 int statusCode = httpResponse.getStatusLine().getStatusCode();
59 status = setStatus(statusCode);
60 if(type==CommonConstants.CAMUNDA){
62 }else if(type==CommonConstants.CAMUNDATASK){
72 private void parseCamunda() throws ApiException{
74 HttpEntity entity = httpResponse.getEntity();
75 responseBody = EntityUtils.toString(entity);
76 } catch (IOException e) {
77 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, MsoLogger.ErrorCode.SchemaError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
80 ValidateException validateException = new ValidateException.Builder("IOException getting Camunda response body", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
81 .errorInfo(errorLoggerInfo).build();
82 throw validateException;
85 ObjectMapper mapper = new ObjectMapper();
87 response = mapper.readValue(responseBody, CamundaResponse.class);
88 } catch (IOException e) {
89 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, MsoLogger.ErrorCode.SchemaError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
92 ValidateException validateException = new ValidateException.Builder("Cannot parse Camunda Response", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
93 .errorInfo(errorLoggerInfo).build();
94 throw validateException;
97 responseBody = response.getResponse();
99 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, MsoLogger.ErrorCode.BusinessProcesssError)
100 .targetEntity("Camunda").targetServiceName("parseCamunda").build();
101 BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
102 .errorInfo(errorLoggerInfo).build();
106 private void parseBpel() throws ApiException{
108 HttpEntity bpelEntity = httpResponse.getEntity();
111 if (bpelEntity != null) {
112 responseBody = EntityUtils.toString(bpelEntity);
115 }catch(IOException e){
116 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
117 ValidateException validateException = new ValidateException.Builder("Could not convert BPEL response to string", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
118 .errorInfo(errorLoggerInfo).build();
119 throw validateException;
121 if(status!=HttpStatus.SC_ACCEPTED){
122 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, MsoLogger.ErrorCode.BusinessProcesssError)
123 .targetEntity("BPEL").targetServiceName("parseBpel").build();
126 BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
127 .errorInfo(errorLoggerInfo).build();
129 throw bpmnFailureException;
134 private void parseCamundaTask() throws ApiException{
136 HttpEntity camundataskEntity = httpResponse.getEntity();
139 if (camundataskEntity != null) {
140 responseBody = EntityUtils.toString(camundataskEntity);
142 }catch(IOException e) {
143 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
146 ValidateException validateException = new ValidateException.Builder("Could not convert CamundaTask response to string", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
147 .errorInfo(errorLoggerInfo).build();
148 throw validateException;
150 if(status!=HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED){
151 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, MsoLogger.ErrorCode.BusinessProcesssError)
152 .targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask").build();
155 BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
156 .errorInfo(errorLoggerInfo).build();
158 throw bpmnFailureException;
163 private int setStatus(int statusCode){
166 case HttpStatus.SC_ACCEPTED:
167 case HttpStatus.SC_OK:
168 httpStatus = HttpStatus.SC_ACCEPTED;
170 case HttpStatus.SC_BAD_REQUEST:
171 httpStatus = HttpStatus.SC_BAD_REQUEST;
173 case HttpStatus.SC_UNAUTHORIZED:
174 case HttpStatus.SC_FORBIDDEN:
175 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
177 case HttpStatus.SC_NOT_FOUND:
178 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
180 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
181 httpStatus = HttpStatus.SC_BAD_GATEWAY;
183 case HttpStatus.SC_SERVICE_UNAVAILABLE:
184 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
186 case HttpStatus.SC_NO_CONTENT:
187 httpStatus = HttpStatus.SC_NO_CONTENT;
190 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
197 public CamundaResponse getResponse() {
202 public void setResponse(CamundaResponse response) {
203 this.response = response;
207 public String getResponseBody() {
212 public void setResponseBody(String responseBody) {
213 this.responseBody = responseBody;
217 public int getStatus() {