From 96fc71c00b4b6d978bd4784d176a48466959421e Mon Sep 17 00:00:00 2001 From: lasanthaDLPDS Date: Tue, 23 Apr 2019 16:49:47 +0530 Subject: [PATCH] Fix minor issues in APPM proxy --- .../mgt/handler/InvokerHandler.java | 29 ++++++++++++++----- .../mgt/handler/util/HandlerUtil.java | 3 ++ .../main/resources/conf/application-mgt.xml | 2 +- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/InvokerHandler.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/InvokerHandler.java index daf0c9ee57..e4a0dd039e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/InvokerHandler.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.handler/src/main/java/org/wso2/carbon/device/application/mgt/handler/InvokerHandler.java @@ -45,7 +45,6 @@ import javax.servlet.http.HttpSession; import java.io.IOException; import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; -import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED; import static org.wso2.carbon.device.application.mgt.handler.util.HandlerUtil.execute; @MultipartConfig @WebServlet("/invoke") @@ -83,7 +82,6 @@ public class InvokerHandler extends HttpServlet { return; } } - if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { log.error("Error occurred while invoking the API endpoint."); HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse); @@ -136,23 +134,38 @@ public class InvokerHandler extends HttpServlet { * @throws IOException If and error occurs while witting error response to client side */ private static boolean validateRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException { + serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort(); + apiEndpoint = req.getParameter("api-endpoint"); + method = req.getParameter("method"); HttpSession session = req.getSession(false); if (session == null) { - resp.sendError(HTTP_UNAUTHORIZED, "Unauthorized, You are not logged in. Please log in to the portal"); + log.error("Unauthorized, You are not logged in. Please log in to the portal"); + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(HttpStatus.SC_UNAUTHORIZED); + proxyResponse.setExecutorResponse( + HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_UNAUTHORIZED)); + HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse); return false; } authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY); platform = (String) session.getAttribute(HandlerConstants.PLATFORM); if (authData == null) { - resp.sendError(HTTP_UNAUTHORIZED, "Unauthorized, Access token couldn't found in the current session"); + log.error("Unauthorized, Access token couldn't found in the current session"); + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(HttpStatus.SC_UNAUTHORIZED); + proxyResponse.setExecutorResponse( + HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_UNAUTHORIZED)); + HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse); return false; } - apiEndpoint = req.getParameter("api-endpoint"); - method = req.getParameter("method"); - serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort(); if (apiEndpoint == null || method == null) { - resp.sendError(HTTP_BAD_REQUEST, "Bad Request, Either api-endpoint or method is empty"); + log.error("Bad Request, Either api-endpoint or method is empty"); + ProxyResponse proxyResponse = new ProxyResponse(); + proxyResponse.setCode(HttpStatus.SC_BAD_REQUEST); + proxyResponse.setExecutorResponse( + HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil.getStatusKey(HttpStatus.SC_BAD_REQUEST)); + HandlerUtil.handleError(req, resp, serverUrl, platform, proxyResponse); return false; } return true; 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 ac7786d88b..e65dd2bb7e 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 @@ -161,6 +161,9 @@ public class HandlerUtil { proxyResponse.setExecutorResponse(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX + HandlerUtil .getStatusKey(HandlerConstants.INTERNAL_ERROR_CODE)); } + if (platform == null){ + platform = "default"; + } resp.setStatus(proxyResponse.getCode()); resp.setContentType("application/json"); diff --git a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml index b2361dc189..69591ca425 100644 --- a/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml +++ b/features/application-mgt/org.wso2.carbon.device.application.mgt.server.feature/src/main/resources/conf/application-mgt.xml @@ -154,7 +154,7 @@ app-mgt - /application-mgt + /apps /pages/error/client-errors/400 /pages/error/client-errors/401