From 707d8851071de0687e24308dad3298cf62a6a41e Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Wed, 24 Apr 2019 14:31:43 +0530 Subject: [PATCH] Improve APPM proxy response --- .../mgt/handler/util/HandlerUtil.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java index b138c7686b..cf14b96651 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/util/HandlerUtil.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.application.mgt.handler.util; import com.google.gson.Gson; import com.google.gson.JsonObject; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.http.HttpResponse; @@ -27,6 +28,8 @@ import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; +import org.json.JSONException; +import org.json.JSONObject; import org.wso2.carbon.device.application.mgt.common.ProxyResponse; import javax.servlet.http.HttpServletRequest; @@ -201,18 +204,30 @@ public class HandlerUtil { return; } - Gson gson = new Gson(); resp.setStatus(proxyResponse.getCode()); resp.setContentType("application/json"); resp.setCharacterEncoding("UTF-8"); - proxyResponse.setExecutorResponse(null); - try (PrintWriter writer = resp.getWriter()) { - if (proxyResponse.getCode() == HttpStatus.SC_OK){ - writer.write(gson.toJson(proxyResponse.getData())); - } else{ - writer.write(proxyResponse.getData()); + + JSONObject response = new JSONObject(); + String redirectUrl = proxyResponse.getUrl(); + String responseData = proxyResponse.getData(); + + if (!StringUtils.isEmpty(redirectUrl)){ + response.put("url", redirectUrl); + } + if (!StringUtils.isEmpty(responseData)){ + try { + JSONObject responseDataJsonObj = new JSONObject(responseData); + response.put("data", responseDataJsonObj); + } catch (JSONException e) { + log.debug("Response data is not valid json string"); + response.put("data", responseData); } } + + try (PrintWriter writer = resp.getWriter()) { + writer.write(response.toString()); + } } }