Initial OpenECOMP MSO commit
[so.git] / mso-api-handlers / mso-api-handler-common / src / main / java / org / openecomp / mso / apihandler / common / ResponseHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * OPENECOMP - MSO
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{
60                         parseBpel();
61                 }
62                 
63         }
64         
65
66         
67         private void parseCamunda(){
68                 try{
69                                 HttpEntity entity = httpResponse.getEntity();
70                                 responseBody = EntityUtils.toString(entity);
71                         } catch (IOException e) {
72                                 msoLogger.debug("IOException getting Camunda response body", e);
73                         }
74                 
75                         ObjectMapper mapper = new ObjectMapper(); 
76                         try {
77                                 response = mapper.readValue(responseBody, CamundaResponse.class);
78                         } catch (IOException e) {
79                                 msoLogger.debug("IOException getting Camunda response body", e);
80                         }
81                         msoLogger.debug("json response is: " + responseBody);
82                         if(response!=null){
83                                 responseBody = response.getResponse();
84                         }
85                         msoLogger.debug("response body is: " + responseBody);
86                         
87                 
88                 if(status!=HttpStatus.SC_ACCEPTED){
89                         msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "Camunda", String.valueOf(status), responseBody, "Camunda", "parseCamunda", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from Camunda");
90                 }
91         }
92         
93         private void parseBpel(){
94
95                 HttpEntity bpelEntity = httpResponse.getEntity();
96
97                 try {
98                         if (bpelEntity!=null) {
99                                 responseBody = EntityUtils.toString(bpelEntity);
100                                 msoLogger.debug("response body is: " + responseBody);
101
102                         }
103                         if(status!=HttpStatus.SC_ACCEPTED){
104                                 msoLogger.error(MessageEnum.APIH_ERROR_FROM_BPEL_SERVER, "BPEL", String.valueOf(status), responseBody, "BPEL", "parseBpel", MsoLogger.ErrorCode.BusinessProcesssError, "Error in APIH from BPEL");
105                         }
106                 } 
107                 catch (IOException e) {
108                         msoLogger.debug("IOException getting BPEL response body", e);
109                 }
110         }
111         
112
113
114
115         private int setStatus(int statusCode){
116                 int status = 0;
117                 switch(statusCode) {
118                 case HttpStatus.SC_ACCEPTED:
119                 case HttpStatus.SC_OK:
120                         status = HttpStatus.SC_ACCEPTED;
121                         break;
122                 case HttpStatus.SC_BAD_REQUEST:
123                         status = HttpStatus.SC_BAD_REQUEST;
124                         break;
125                 case HttpStatus.SC_UNAUTHORIZED:
126                 case HttpStatus.SC_FORBIDDEN:
127                         status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
128                         break;
129                 case HttpStatus.SC_NOT_FOUND:
130                         status = HttpStatus.SC_NOT_IMPLEMENTED;
131                         break;
132                 case HttpStatus.SC_INTERNAL_SERVER_ERROR:
133                         status = HttpStatus.SC_BAD_GATEWAY;
134                         break;
135                 case HttpStatus.SC_SERVICE_UNAVAILABLE:
136                         status = HttpStatus.SC_SERVICE_UNAVAILABLE;
137                         break;
138                 default:
139                         status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
140                         break;
141                 }
142                 return status;
143         }
144
145
146         public CamundaResponse getResponse() {
147                 return response;
148         }
149
150
151         public void setResponse(CamundaResponse response) {
152                 this.response = response;
153         }
154
155
156         public String getResponseBody() {
157                 return responseBody;
158         }
159
160
161         public void setResponseBody(String responseBody) {
162                 this.responseBody = responseBody;
163         }
164
165
166         public int getStatus() {
167                 return status;
168         }
169
170
171
172
173 }