af1258e963fc7be0fb23d9eb042a3c095949fcc6
[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.ObjectMapper;
36
37
38 @Component
39 public class ResponseHandler {
40     private static Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
41
42     public CamundaResponse getCamundaResponse(ResponseEntity<String> camundaResponse) throws ValidateException {
43         String responseBody = camundaResponse.getBody();
44         CamundaResponse response = null;
45         ObjectMapper mapper = new ObjectMapper();
46         try {
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();
53         }
54         return response;
55     }
56
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();
63         }
64     }
65
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();
72         }
73     }
74
75     public int setStatus(int statusCode) {
76         int httpStatus;
77         switch (statusCode) {
78             case HttpStatus.SC_ACCEPTED:
79             case HttpStatus.SC_OK:
80                 httpStatus = HttpStatus.SC_ACCEPTED;
81                 break;
82             case HttpStatus.SC_BAD_REQUEST:
83                 httpStatus = HttpStatus.SC_BAD_REQUEST;
84                 break;
85             case HttpStatus.SC_UNAUTHORIZED:
86             case HttpStatus.SC_FORBIDDEN:
87                 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
88                 break;
89             case HttpStatus.SC_NOT_FOUND:
90                 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
91                 break;
92             case HttpStatus.SC_INTERNAL_SERVER_ERROR:
93                 httpStatus = HttpStatus.SC_BAD_GATEWAY;
94                 break;
95             case HttpStatus.SC_SERVICE_UNAVAILABLE:
96                 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
97                 break;
98             case HttpStatus.SC_NO_CONTENT:
99                 httpStatus = HttpStatus.SC_NO_CONTENT;
100                 break;
101             default:
102                 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
103                 break;
104         }
105         return httpStatus;
106     }
107 }