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