65e8b438ad65cfe99af5bf64ce545be3db6342d4
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.apihandler.common;
24
25
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;
37
38
39 @Component
40 public class ResponseHandler {
41     private static Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
42
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);
48         try {
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();
55         }
56         return response;
57     }
58
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();
65         }
66     }
67
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();
74         }
75     }
76
77     public int setStatus(int statusCode) {
78         int httpStatus;
79         switch (statusCode) {
80             case HttpStatus.SC_ACCEPTED:
81             case HttpStatus.SC_OK:
82                 httpStatus = HttpStatus.SC_ACCEPTED;
83                 break;
84             case HttpStatus.SC_BAD_REQUEST:
85                 httpStatus = HttpStatus.SC_BAD_REQUEST;
86                 break;
87             case HttpStatus.SC_UNAUTHORIZED:
88             case HttpStatus.SC_FORBIDDEN:
89                 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
90                 break;
91             case HttpStatus.SC_NOT_FOUND:
92                 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
93                 break;
94             case HttpStatus.SC_INTERNAL_SERVER_ERROR:
95                 httpStatus = HttpStatus.SC_BAD_GATEWAY;
96                 break;
97             case HttpStatus.SC_SERVICE_UNAVAILABLE:
98                 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
99                 break;
100             case HttpStatus.SC_NO_CONTENT:
101                 httpStatus = HttpStatus.SC_NO_CONTENT;
102                 break;
103             default:
104                 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
105                 break;
106         }
107         return httpStatus;
108     }
109 }