2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (c) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.apihandler.common;
26 import java.io.IOException;
27 import org.apache.http.HttpStatus;
28 import org.onap.so.apihandler.camundabeans.CamundaResponse;
29 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
30 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33 import org.springframework.http.ResponseEntity;
34 import org.springframework.stereotype.Component;
35 import com.fasterxml.jackson.databind.DeserializationFeature;
36 import com.fasterxml.jackson.databind.ObjectMapper;
40 public class ResponseHandler {
41 private static Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
43 public CamundaResponse getCamundaResponse(ResponseEntity<String> camundaResponse) throws ValidateException {
44 String responseBody = camundaResponse.getBody();
45 CamundaResponse response = null;
46 ObjectMapper mapper = new ObjectMapper();
47 mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
49 response = mapper.readValue(responseBody, CamundaResponse.class);
50 } catch (IOException | NullPointerException e) {
51 logger.error("Cannot parse Camunda Response: ", e);
52 throw new ValidateException.Builder(
53 "Cannot parse Camunda ResponseBody. BPMN HTTP status: " + camundaResponse.getStatusCodeValue(),
54 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).build();
59 public void acceptedResponse(ResponseEntity<String> response) throws BPMNFailureException {
60 int status = setStatus(response.getStatusCodeValue());
61 if (status != HttpStatus.SC_ACCEPTED) {
62 logger.info("Camunda did not return a valid response");
63 throw new BPMNFailureException.Builder(String.valueOf(status), HttpStatus.SC_INTERNAL_SERVER_ERROR,
64 ErrorNumbers.ERROR_FROM_BPEL).build();
68 public void acceptedOrNoContentResponse(ResponseEntity<String> response) throws BPMNFailureException {
69 int status = setStatus(response.getStatusCodeValue());
70 if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED) {
71 logger.info("Camunda did not return a valid response");
72 throw new BPMNFailureException.Builder(String.valueOf(status), HttpStatus.SC_INTERNAL_SERVER_ERROR,
73 ErrorNumbers.ERROR_FROM_BPEL).build();
77 public int setStatus(int statusCode) {
80 case HttpStatus.SC_ACCEPTED:
81 case HttpStatus.SC_OK:
82 httpStatus = HttpStatus.SC_ACCEPTED;
84 case HttpStatus.SC_BAD_REQUEST:
85 httpStatus = HttpStatus.SC_BAD_REQUEST;
87 case HttpStatus.SC_UNAUTHORIZED:
88 case HttpStatus.SC_FORBIDDEN:
89 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
91 case HttpStatus.SC_NOT_FOUND:
92 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
94 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
95 httpStatus = HttpStatus.SC_BAD_GATEWAY;
97 case HttpStatus.SC_SERVICE_UNAVAILABLE:
98 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
100 case HttpStatus.SC_NO_CONTENT:
101 httpStatus = HttpStatus.SC_NO_CONTENT;
104 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;