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