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