Merge "update install and configure document"
[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  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.apihandler.common;
22
23
24 import java.io.IOException;
25
26 import org.apache.http.HttpEntity;
27 import org.apache.http.HttpResponse;
28 import org.apache.http.HttpStatus;
29 import org.apache.http.util.EntityUtils;
30 import org.onap.so.apihandler.camundabeans.CamundaResponse;
31 import org.onap.so.apihandlerinfra.Constants;
32 import org.onap.so.apihandlerinfra.exceptions.ApiException;
33 import org.onap.so.apihandlerinfra.exceptions.BPMNFailureException;
34 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
35 import org.onap.so.apihandlerinfra.logging.ErrorLoggerInfo;
36 import org.onap.so.logger.MessageEnum;
37 import org.onap.so.logger.MsoLogger;
38
39 import com.fasterxml.jackson.databind.ObjectMapper;
40
41 public class ResponseHandler {
42
43         private CamundaResponse response;
44         private int status;
45         private String responseBody="";
46         private HttpResponse httpResponse;
47         private int type;
48         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH, ResponseHandler.class);
49
50         public ResponseHandler(HttpResponse httpResponse, int type) throws ApiException{
51                 this.httpResponse = httpResponse;
52                 this.type=type;
53                 parseResponse();
54         }
55
56
57         private void parseResponse() throws ApiException{
58                 int statusCode = httpResponse.getStatusLine().getStatusCode();
59                 status = setStatus(statusCode);
60                 if(type==CommonConstants.CAMUNDA){
61                         parseCamunda();
62                 }else if(type==CommonConstants.CAMUNDATASK){
63                         parseCamundaTask();
64                 }else {
65                         parseBpel();
66                 }
67                 
68         }
69         
70
71         
72         private void parseCamunda() throws ApiException{
73                 try{
74                                 HttpEntity entity = httpResponse.getEntity();
75                                 responseBody = EntityUtils.toString(entity);
76                         } catch (IOException e) {
77                                 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_VALIDATION_ERROR, MsoLogger.ErrorCode.SchemaError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
78
79
80                                 ValidateException validateException = new ValidateException.Builder("IOException getting Camunda response body", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
81                                         .errorInfo(errorLoggerInfo).build();
82                                 throw validateException;
83                         }
84                 
85                         ObjectMapper mapper = new ObjectMapper(); 
86                         try {
87                                 response = mapper.readValue(responseBody, CamundaResponse.class);
88                         } catch (IOException e) {
89                                 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_REQUEST_VALIDATION_ERROR, MsoLogger.ErrorCode.SchemaError).errorSource(Constants.MSO_PROP_APIHANDLER_INFRA).build();
90
91
92                                 ValidateException validateException = new ValidateException.Builder("Cannot parse Camunda Response", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
93                                                 .errorInfo(errorLoggerInfo).build();
94                                 throw validateException;
95                         }
96                         if(response!=null){
97                                 responseBody = response.getResponse();
98                         }else{
99                 ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, MsoLogger.ErrorCode.BusinessProcesssError)
100                         .targetEntity("Camunda").targetServiceName("parseCamunda").build();
101                 BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
102                         .errorInfo(errorLoggerInfo).build();
103             }
104         }
105         
106         private void parseBpel() throws ApiException{
107
108                 HttpEntity bpelEntity = httpResponse.getEntity();
109
110                 try {
111                         if (bpelEntity != null) {
112                                 responseBody = EntityUtils.toString(bpelEntity);
113
114                         }
115                 }catch(IOException e){
116                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
117                         ValidateException validateException = new ValidateException.Builder("Could not convert BPEL response to string", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
118                                         .errorInfo(errorLoggerInfo).build();
119                         throw validateException;
120                 }
121                 if(status!=HttpStatus.SC_ACCEPTED){
122                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, MsoLogger.ErrorCode.BusinessProcesssError)
123                                         .targetEntity("BPEL").targetServiceName("parseBpel").build();
124
125
126                         BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
127                                         .errorInfo(errorLoggerInfo).build();
128
129                         throw bpmnFailureException;
130                 }
131
132         }
133         
134         private void parseCamundaTask() throws ApiException{
135
136                 HttpEntity camundataskEntity = httpResponse.getEntity();
137
138                 try {
139                         if (camundataskEntity != null) {
140                                 responseBody = EntityUtils.toString(camundataskEntity);
141                         }
142                 }catch(IOException e) {
143                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_GENERAL_EXCEPTION, MsoLogger.ErrorCode.DataError).build();
144
145
146                         ValidateException validateException = new ValidateException.Builder("Could not convert CamundaTask response to string", HttpStatus.SC_BAD_REQUEST, ErrorNumbers.SVC_BAD_PARAMETER).cause(e)
147                                         .errorInfo(errorLoggerInfo).build();
148                         throw validateException;
149                 }
150                 if(status!=HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED){
151                         ErrorLoggerInfo errorLoggerInfo = new ErrorLoggerInfo.Builder(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, MsoLogger.ErrorCode.BusinessProcesssError)
152                                         .targetEntity("CAMUNDATASK").targetServiceName("parseCamundaTask").build();
153
154
155                         BPMNFailureException bpmnFailureException = new BPMNFailureException.Builder(String.valueOf(status), status, ErrorNumbers.ERROR_FROM_BPEL)
156                                         .errorInfo(errorLoggerInfo).build();
157
158                         throw bpmnFailureException;
159                 } 
160
161         }
162
163         private int setStatus(int statusCode){
164                 int httpStatus;
165                 switch(statusCode) {
166                 case HttpStatus.SC_ACCEPTED:
167                 case HttpStatus.SC_OK:          
168                         httpStatus = HttpStatus.SC_ACCEPTED;
169                         break;
170                 case HttpStatus.SC_BAD_REQUEST:
171                         httpStatus = HttpStatus.SC_BAD_REQUEST;
172                         break;
173                 case HttpStatus.SC_UNAUTHORIZED:
174                 case HttpStatus.SC_FORBIDDEN:
175                         httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
176                         break;
177                 case HttpStatus.SC_NOT_FOUND:
178                         httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
179                         break;
180                 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
181                         httpStatus = HttpStatus.SC_BAD_GATEWAY;
182                         break;
183                 case HttpStatus.SC_SERVICE_UNAVAILABLE:
184                         httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
185                         break;
186                 case HttpStatus.SC_NO_CONTENT:
187                         httpStatus = HttpStatus.SC_NO_CONTENT;
188                         break;
189                 default:
190                         httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
191                         break;
192                 }
193                 return httpStatus;
194         }
195
196
197         public CamundaResponse getResponse() {
198                 return response;
199         }
200
201
202         public void setResponse(CamundaResponse response) {
203                 this.response = response;
204         }
205
206
207         public String getResponseBody() {
208                 return responseBody;
209         }
210
211
212         public void setResponseBody(String responseBody) {
213                 this.responseBody = responseBody;
214         }
215
216
217         public int getStatus() {
218                 return status;
219         }
220
221 }