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.ObjectMapper;
39 public class ResponseHandler {
40 private static Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
42 public CamundaResponse getCamundaResponse(ResponseEntity<String> camundaResponse) throws ValidateException {
43 String responseBody = camundaResponse.getBody();
44 CamundaResponse response = null;
45 ObjectMapper mapper = new ObjectMapper();
47 response = mapper.readValue(responseBody, CamundaResponse.class);
48 } catch (IOException | NullPointerException e) {
49 logger.error("Cannot parse Camunda Response: ", e);
50 throw new ValidateException.Builder(
51 "Cannot parse Camunda ResponseBody. BPMN HTTP status: " + camundaResponse.getStatusCodeValue(),
52 HttpStatus.SC_INTERNAL_SERVER_ERROR, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).build();
57 public void acceptedResponse(ResponseEntity<String> response) throws BPMNFailureException {
58 int status = setStatus(response.getStatusCodeValue());
59 if (status != HttpStatus.SC_ACCEPTED) {
60 logger.info("Camunda did not return a valid response");
61 throw new BPMNFailureException.Builder(String.valueOf(status), HttpStatus.SC_INTERNAL_SERVER_ERROR,
62 ErrorNumbers.ERROR_FROM_BPEL).build();
66 public void acceptedOrNoContentResponse(ResponseEntity<String> response) throws BPMNFailureException {
67 int status = setStatus(response.getStatusCodeValue());
68 if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED) {
69 logger.info("Camunda did not return a valid response");
70 throw new BPMNFailureException.Builder(String.valueOf(status), HttpStatus.SC_INTERNAL_SERVER_ERROR,
71 ErrorNumbers.ERROR_FROM_BPEL).build();
75 public int setStatus(int statusCode) {
78 case HttpStatus.SC_ACCEPTED:
79 case HttpStatus.SC_OK:
80 httpStatus = HttpStatus.SC_ACCEPTED;
82 case HttpStatus.SC_BAD_REQUEST:
83 httpStatus = HttpStatus.SC_BAD_REQUEST;
85 case HttpStatus.SC_UNAUTHORIZED:
86 case HttpStatus.SC_FORBIDDEN:
87 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
89 case HttpStatus.SC_NOT_FOUND:
90 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
92 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
93 httpStatus = HttpStatus.SC_BAD_GATEWAY;
95 case HttpStatus.SC_SERVICE_UNAVAILABLE:
96 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
98 case HttpStatus.SC_NO_CONTENT:
99 httpStatus = HttpStatus.SC_NO_CONTENT;
102 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;