Replaced all tabs with spaces in java and pom.xml
[so.git] / mso-api-handlers / mso-api-handler-common / src / main / java / org / onap / so / apihandler / common / ResponseHandler.java
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.HttpEntity;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.HttpStatus;
30 import org.apache.http.util.EntityUtils;
31 import org.onap.so.apihandler.camundabeans.CamundaResponse;
32 import org.onap.so.apihandlerinfra.Constants;
33 import org.onap.so.apihandlerinfra.exceptions.ApiException;
34 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
35 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
36 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
37 import org.onap.so.logger.ErrorCode;
38 import org.onap.so.logger.MessageEnum;
39 import com.fasterxml.jackson.databind.ObjectMapper;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 public class ResponseHandler {
44
45     private CamundaResponse response;
46     private int status;
47     private String responseBody = "";
48     private HttpResponse httpResponse;
49     private int type;
50     private static Logger logger = LoggerFactory.getLogger(ResponseHandler.class);
51
52     public ResponseHandler(HttpResponse httpResponse, int type) throws ApiException {
53         this.httpResponse = httpResponse;
54         this.type = type;
55         parseResponse();
56     }
57
58
59     private void parseResponse() throws ApiException {
60         int statusCode = httpResponse.getStatusLine().getStatusCode();
61         status = setStatus(statusCode);
62         if (type == CommonConstants.CAMUNDA) {
63             parseCamunda();
64         } else if (type == CommonConstants.CAMUNDATASK) {
65             parseCamundaTask();
66         } else {
67             parseBpel();
68         }
69
70     }
71
72
73
74     private void parseCamunda() throws ApiException {
75         try {
76             HttpEntity entity = httpResponse.getEntity();
77             responseBody = EntityUtils.toString(entity);
78         } catch (IOException e) {
79             ErrorLoggerInfo errorLoggerInfo =
80                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, ErrorCode.SchemaError)
81                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
82
83
84             ValidateException validateException =
85                     new ValidateException.Builder("IOException getting Camunda response body",
86                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
87                                     .errorInfo(errorLoggerInfo).build();
88             throw validateException;
89         }
90
91         ObjectMapper mapper = new ObjectMapper();
92         try {
93             response = mapper.readValue(responseBody, CamundaResponse.class);
94         } catch (IOException e) {
95             ErrorLoggerInfo errorLoggerInfo =
96                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
97                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
98
99
100             ValidateException validateException =
101                     new ValidateException.Builder("Cannot parse Camunda Response", HttpStatus.SC_BAD_REQUEST,
102                             ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
103             throw validateException;
104         }
105         if (response != null) {
106             responseBody = response.getResponse();
107         } else {
108             ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER,
109                     ErrorCode.BusinessProcesssError).targetEntity("Camunda").targetServiceName("parseCamunda").build();
110             BPMNFailureException bpmnFailureException =
111                     new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
112                             .errorInfo(errorLoggerInfo).build();
113         }
114     }
115
116     private void parseBpel() throws ApiException {
117
118         HttpEntity bpelEntity = httpResponse.getEntity();
119
120         try {
121             if (bpelEntity != null) {
122                 responseBody = EntityUtils.toString(bpelEntity);
123
124             }
125         } catch (IOException e) {
126             ErrorLoggerInfo errorLoggerInfo =
127                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
128             ValidateException validateException =
129                     new ValidateException.Builder("Could not convert BPEL response to string",
130                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
131                                     .errorInfo(errorLoggerInfo).build();
132             throw validateException;
133         }
134         if (status != HttpStatus.SC_ACCEPTED) {
135             ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER,
136                     ErrorCode.BusinessProcesssError).targetEntity("BPEL").targetServiceName("parseBpel").build();
137
138
139             BPMNFailureException bpmnFailureException =
140                     new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
141                             .errorInfo(errorLoggerInfo).build();
142
143             throw bpmnFailureException;
144         }
145
146     }
147
148     private void parseCamundaTask() throws ApiException {
149
150         HttpEntity camundataskEntity = httpResponse.getEntity();
151
152         try {
153             if (camundataskEntity != null) {
154                 responseBody = EntityUtils.toString(camundataskEntity);
155             }
156         } catch (IOException e) {
157             ErrorLoggerInfo errorLoggerInfo =
158                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
159
160
161             ValidateException validateException =
162                     new ValidateException.Builder("Could not convert CamundaTask response to string",
163                             HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
164                                     .errorInfo(errorLoggerInfo).build();
165             throw validateException;
166         }
167         if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED) {
168             ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER,
169                     ErrorCode.BusinessProcesssError).targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask")
170                             .build();
171
172
173             BPMNFailureException bpmnFailureException =
174                     new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
175                             .errorInfo(errorLoggerInfo).build();
176
177             throw bpmnFailureException;
178         }
179
180     }
181
182     private int setStatus(int statusCode) {
183         int httpStatus;
184         switch (statusCode) {
185             case HttpStatus.SC_ACCEPTED:
186             case HttpStatus.SC_OK:
187                 httpStatus = HttpStatus.SC_ACCEPTED;
188                 break;
189             case HttpStatus.SC_BAD_REQUEST:
190                 httpStatus = HttpStatus.SC_BAD_REQUEST;
191                 break;
192             case HttpStatus.SC_UNAUTHORIZED:
193             case HttpStatus.SC_FORBIDDEN:
194                 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
195                 break;
196             case HttpStatus.SC_NOT_FOUND:
197                 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
198                 break;
199             case HttpStatus.SC_INTERNAL_SERVER_ERROR:
200                 httpStatus = HttpStatus.SC_BAD_GATEWAY;
201                 break;
202             case HttpStatus.SC_SERVICE_UNAVAILABLE:
203                 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
204                 break;
205             case HttpStatus.SC_NO_CONTENT:
206                 httpStatus = HttpStatus.SC_NO_CONTENT;
207                 break;
208             default:
209                 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
210                 break;
211         }
212         return httpStatus;
213     }
214
215
216     public CamundaResponse getResponse() {
217         return response;
218     }
219
220
221     public void setResponse(CamundaResponse response) {
222         this.response = response;
223     }
224
225
226     public String getResponseBody() {
227         return responseBody;
228     }
229
230
231     public void setResponseBody(String responseBody) {
232         this.responseBody = responseBody;
233     }
234
235
236     public int getStatus() {
237         return status;
238     }
239
240 }