bedce185c57cf54e92e404651b4345baef233896
[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.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             throw new ValidateException.Builder("IOException getting Camunda response body", HttpStatus.SC_BAD_REQUEST,
84                     ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
85         }
86
87         ObjectMapper mapper = new ObjectMapper();
88         try {
89             response = mapper.readValue(responseBody, CamundaResponse.class);
90         } catch (IOException e) {
91             ErrorLoggerInfo errorLoggerInfo =
92                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, ErrorCode.SchemaError)
93                             .errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
94
95             throw new ValidateException.Builder("Cannot parse Camunda Response", HttpStatus.SC_BAD_REQUEST,
96                     ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
97         }
98         if (response != null) {
99             responseBody = response.getResponse();
100         } else {
101             ErrorLoggerInfo errorLoggerInfo =
102                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcessError)
103                             .targetEntity("Camunda").targetServiceName("parseCamunda").build();
104             BPMNFailureException bpmnFailureException =
105                     new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
106                             .errorInfo(errorLoggerInfo).build();
107         }
108     }
109
110     private void parseBpel() throws ApiException {
111
112         HttpEntity bpelEntity = httpResponse.getEntity();
113
114         try {
115             if (bpelEntity != null) {
116                 responseBody = EntityUtils.toString(bpelEntity);
117
118             }
119         } catch (IOException e) {
120             ErrorLoggerInfo errorLoggerInfo =
121                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
122
123             throw new ValidateException.Builder("Could not convert BPEL response to string", HttpStatus.SC_BAD_REQUEST,
124                     ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo).build();
125         }
126         if (status != HttpStatus.SC_ACCEPTED) {
127             ErrorLoggerInfo errorLoggerInfo =
128                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcessError)
129                             .targetEntity("BPEL").targetServiceName("parseBpel").build();
130
131             throw new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
132                     .errorInfo(errorLoggerInfo).build();
133         }
134
135     }
136
137     private void parseCamundaTask() throws ApiException {
138
139         HttpEntity camundataskEntity = httpResponse.getEntity();
140
141         try {
142             if (camundataskEntity != null) {
143                 responseBody = EntityUtils.toString(camundataskEntity);
144             }
145         } catch (IOException e) {
146             ErrorLoggerInfo errorLoggerInfo =
147                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, ErrorCode.DataError).build();
148
149             throw new ValidateException.Builder("Could not convert CamundaTask response to string",
150                     HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e).errorInfo(errorLoggerInfo)
151                             .build();
152         }
153         if (status != HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED) {
154             ErrorLoggerInfo errorLoggerInfo =
155                     new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, ErrorCode.BusinessProcessError)
156                             .targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask").build();
157
158             throw new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
159                     .errorInfo(errorLoggerInfo).build();
160         }
161
162     }
163
164     private int setStatus(int statusCode) {
165         int httpStatus;
166         switch (statusCode) {
167             case HttpStatus.SC_ACCEPTED:
168             case HttpStatus.SC_OK:
169                 httpStatus = HttpStatus.SC_ACCEPTED;
170                 break;
171             case HttpStatus.SC_BAD_REQUEST:
172                 httpStatus = HttpStatus.SC_BAD_REQUEST;
173                 break;
174             case HttpStatus.SC_UNAUTHORIZED:
175             case HttpStatus.SC_FORBIDDEN:
176                 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
177                 break;
178             case HttpStatus.SC_NOT_FOUND:
179                 httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
180                 break;
181             case HttpStatus.SC_INTERNAL_SERVER_ERROR:
182                 httpStatus = HttpStatus.SC_BAD_GATEWAY;
183                 break;
184             case HttpStatus.SC_SERVICE_UNAVAILABLE:
185                 httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
186                 break;
187             case HttpStatus.SC_NO_CONTENT:
188                 httpStatus = HttpStatus.SC_NO_CONTENT;
189                 break;
190             default:
191                 httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
192                 break;
193         }
194         return httpStatus;
195     }
196
197
198     public CamundaResponse getResponse() {
199         return response;
200     }
201
202
203     public void setResponse(CamundaResponse response) {
204         this.response = response;
205     }
206
207
208     public String getResponseBody() {
209         return responseBody;
210     }
211
212
213     public void setResponseBody(String responseBody) {
214         this.responseBody = responseBody;
215     }
216
217
218     public int getStatus() {
219         return status;
220     }
221
222 }