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