4e7cf4cddd1bd17e2421e3ff53de7e4971699d59
[so.git] / mso-api-handlers / mso-api-handler-common / src / main / java / org / openecomp / mso / 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.openecomp.mso.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.codehaus.jackson.map.ObjectMapper;
31
32 import org.openecomp.mso.apihandler.camundabeans.CamundaResponse;
33 import org.openecomp.mso.logger.MsoLogger;
34 import org.openecomp.mso.logger.MessageEnum;
35
36 public class ResponseHandler {
37
38         private CamundaResponse response;
39         private int status;
40         private String responseBody="";
41         private HttpResponse httpResponse;
42         private int type;
43         private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.APIH);
44     private static final String RESPONSE_BODY_MSG = "response body is: ";
45
46         public ResponseHandler(HttpResponse httpResponse, int type) {
47                 this.httpResponse = httpResponse;
48                 this.type=type;
49                 parseResponse();
50         }
51
52
53         private void parseResponse() {
54                 int statusCode = httpResponse.getStatusLine().getStatusCode();
55                 msoLogger.debug("Returned status  is: " + statusCode);          
56                 status = setStatus(statusCode);
57                 msoLogger.debug("Parsed status  is: " + status);
58                 if(type==CommonConstants.CAMUNDA){
59                         parseCamunda();
60                 }else if(type==CommonConstants.CAMUNDATASK){
61                         parseCamundaTask();
62                 }else {
63                         parseBpel();
64                 }
65                 
66         }
67         
68
69         
70         private void parseCamunda(){
71                 try{
72                                 HttpEntity entity = httpResponse.getEntity();
73                                 responseBody = EntityUtils.toString(entity);
74                         } catch (IOException e) {
75                                 msoLogger.debug("IOException getting Camunda response body", e);
76                         }
77                 
78                         ObjectMapper mapper = new ObjectMapper(); 
79                         try {
80                                 response = mapper.readValue(responseBody, CamundaResponse.class);
81                         } catch (IOException e) {
82                                 msoLogger.debug("IOException getting Camunda response body", e);
83                         }
84                         msoLogger.debug("json response is: " + responseBody);
85                         if(response!=null){
86                                 responseBody = response.getResponse();
87                         }
88                         msoLogger.debug(RESPONSE_BODY_MSG + responseBody);
89                         
90                 
91                 if(status!=HttpStatus.SC_ACCEPTED){
92                         msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "Camunda", String.valueOf(status), responseBody, "Camunda", "parseCamunda", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda");
93                 }
94         }
95         
96         private void parseBpel(){
97
98                 HttpEntity bpelEntity = httpResponse.getEntity();
99
100                 try {
101                         if (bpelEntity!=null) {
102                                 responseBody = EntityUtils.toString(bpelEntity);
103                                 msoLogger.debug(RESPONSE_BODY_MSG + responseBody);
104
105                         }
106                         if(status!=HttpStatus.SC_ACCEPTED){
107                                 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "BPEL", String.valueOf(status), responseBody, "BPEL", "parseBpel", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from BPEL");
108                         }
109                 } 
110                 catch (IOException e) {
111                         msoLogger.debug("IOException getting BPEL response body", e);
112                 }
113         }
114         
115         private void parseCamundaTask(){
116
117                 HttpEntity camundataskEntity = httpResponse.getEntity();
118
119                 try {
120                         if (camundataskEntity!=null) {
121                                 responseBody = EntityUtils.toString(camundataskEntity);
122                                 msoLogger.debug(RESPONSE_BODY_MSG + responseBody);
123
124                         }
125                         if(status!=HttpStatus.SC_NO_CONTENT && status != HttpStatus.SC_ACCEPTED){
126                                 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "CAMUNDATASK", String.valueOf(status), responseBody, "CAMUNDATASK", "parseCamundaTask", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda Task");
127                         }
128                 } 
129                 catch (IOException e) {
130                         msoLogger.debug("IOException getting Camunda Task response body", e);
131                 }
132         }
133
134         private int setStatus(int statusCode){
135                 int httpStatus;
136                 switch(statusCode) {
137                 case HttpStatus.SC_ACCEPTED:
138                 case HttpStatus.SC_OK:          
139                         httpStatus = HttpStatus.SC_ACCEPTED;
140                         break;
141                 case HttpStatus.SC_BAD_REQUEST:
142                         httpStatus = HttpStatus.SC_BAD_REQUEST;
143                         break;
144                 case HttpStatus.SC_UNAUTHORIZED:
145                 case HttpStatus.SC_FORBIDDEN:
146                         httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
147                         break;
148                 case HttpStatus.SC_NOT_FOUND:
149                         httpStatus = HttpStatus.SC_NOT_IMPLEMENTED;
150                         break;
151                 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
152                         httpStatus = HttpStatus.SC_BAD_GATEWAY;
153                         break;
154                 case HttpStatus.SC_SERVICE_UNAVAILABLE:
155                         httpStatus = HttpStatus.SC_SERVICE_UNAVAILABLE;
156                         break;
157                 case HttpStatus.SC_NO_CONTENT:
158                         httpStatus = HttpStatus.SC_NO_CONTENT;
159                         break;
160                 default:
161                         httpStatus = HttpStatus.SC_INTERNAL_SERVER_ERROR;
162                         break;
163                 }
164                 return httpStatus;
165         }
166
167
168         public CamundaResponse getResponse() {
169                 return response;
170         }
171
172
173         public void setResponse(CamundaResponse response) {
174                 this.response = response;
175         }
176
177
178         public String getResponseBody() {
179                 return responseBody;
180         }
181
182
183         public void setResponseBody(String responseBody) {
184                 this.responseBody = responseBody;
185         }
186
187
188         public int getStatus() {
189                 return status;
190         }
191
192 }