From 182c8be98a9e2a4ea0ef1f0db964dd8af83d3b14 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Thu, 16 Apr 2020 22:49:44 +0530 Subject: [PATCH] Improve APPM and Invoker sources --- .../GenericApplicationDAOImpl.java | 7 +- .../mgt/core/impl/ApplicationManagerImpl.java | 7 +- .../application/mgt/core/util/APIUtil.java | 3 +- .../application/mgt/core/util/DAOUtil.java | 16 ----- .../management/ApplicationManagementTest.java | 2 + .../request/interceptor/InvokerHandler.java | 64 +++++++++++++------ .../ui/request/interceptor/LoginHandler.java | 6 +- .../interceptor/util/HandlerConstants.java | 2 - 8 files changed, 59 insertions(+), 48 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java index a4155d8ba2..8ad0152096 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/application/GenericApplicationDAOImpl.java @@ -148,7 +148,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic sql += "AND AP_APP.TYPE = ? "; } if (!StringUtils.isEmpty(filter.getAppName())) { - sql += " AND LOWER (AP_APP.NAME) "; + sql += "AND LOWER (AP_APP.NAME) "; if (filter.isFullMatch()) { sql += "= ? "; } else { @@ -178,10 +178,9 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic sql += "ORDER BY ID " + filter.getSortBy() +" "; } if (filter.getLimit() != -1) { - sql += " LIMIT ? OFFSET ? "; + sql += "LIMIT ? OFFSET ? "; } - sql += ") AS app_data ON app_data.ID = AP_APP.ID " + - "WHERE AP_APP.TENANT_ID = ?"; + sql += ") AS app_data ON app_data.ID = AP_APP.ID WHERE AP_APP.TENANT_ID = ?"; try { Connection conn = this.getDBConnection(); try (PreparedStatement stmt = conn.prepareStatement(sql)) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java index 2f81eb2411..c28f42b979 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/ApplicationManagerImpl.java @@ -73,7 +73,6 @@ import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO; import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.util.APIUtil; -import org.wso2.carbon.device.application.mgt.core.util.DAOUtil; import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException; @@ -1057,7 +1056,8 @@ public class ApplicationManagerImpl implements ApplicationManager { } try { - DeviceManagementProviderService deviceManagementProviderService = DAOUtil.getDeviceManagementService(); + DeviceManagementProviderService deviceManagementProviderService = DataHolder.getInstance() + .getDeviceManagementService(); return deviceManagementProviderService.getDeviceTypeVersion(deviceTypeName, lowestSupportingOsVersion) == null || (highestSupportingOsVersion != null && deviceManagementProviderService.getDeviceTypeVersion(deviceTypeName, highestSupportingOsVersion) @@ -1565,8 +1565,7 @@ public class ApplicationManagerImpl implements ApplicationManager { boolean isValidDeviceType = false; List deviceTypes; try { - deviceTypes = DAOUtil.getDeviceManagementService().getDeviceTypes(); - + deviceTypes = DataHolder.getInstance().getDeviceManagementService().getDeviceTypes(); for (DeviceType dt : deviceTypes) { if (dt.getName().equals(deviceType)) { isValidDeviceType = true; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java index a331762a0e..12d28191b7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/APIUtil.java @@ -43,6 +43,7 @@ import org.wso2.carbon.device.application.mgt.common.wrapper.WebAppWrapper; import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager; import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException; import org.wso2.carbon.device.application.mgt.core.exception.UnexpectedServerErrorException; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; @@ -197,7 +198,7 @@ public class APIUtil { throws BadRequestException, UnexpectedServerErrorException { List deviceTypes; try { - deviceTypes = DAOUtil.getDeviceManagementService().getDeviceTypes(); + deviceTypes = DataHolder.getInstance().getDeviceManagementService().getDeviceTypes(); if (deviceTypeAttr instanceof String) { for (DeviceType dt : deviceTypes) { if (dt.getName().equals(deviceTypeAttr)) { diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/DAOUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/DAOUtil.java index 284d5fc856..c56df53109 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/DAOUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/DAOUtil.java @@ -32,8 +32,6 @@ import org.wso2.carbon.device.application.mgt.common.dto.ReviewDTO; import org.wso2.carbon.device.application.mgt.common.dto.ScheduledSubscriptionDTO; import org.wso2.carbon.device.application.mgt.core.exception.UnexpectedServerErrorException; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -306,18 +304,4 @@ public class DAOUtil { } } } - - public static DeviceManagementProviderService getDeviceManagementService() { -// PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); -// DeviceManagementProviderService deviceManagementProviderService = -// (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null); -// if (deviceManagementProviderService == null) { -// String msg = "DeviceImpl Management provider service has not initialized."; -// log.error(msg); -// throw new IllegalStateException(msg); -// } - // return deviceManagementProviderService; - - return new DeviceManagementProviderServiceImpl(); - } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java index c684f6fd01..4ebdf3ce6c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/management/ApplicationManagementTest.java @@ -38,6 +38,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; import org.wso2.carbon.device.application.mgt.core.dto.ApplicationsDTO; import org.wso2.carbon.device.application.mgt.core.impl.ApplicationManagerImpl; +import org.wso2.carbon.device.application.mgt.core.internal.DataHolder; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; @@ -60,6 +61,7 @@ public class ApplicationManagementTest extends BaseTestCase { public void testAddApplication() throws Exception { ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); + DataHolder.getInstance().setDeviceManagementService(new DeviceManagementProviderServiceImpl()); ConnectionManagerUtil.beginDBTransaction(); applicationDAO.createApplication(ApplicationsDTO.getApp1(), -1234); ConnectionManagerUtil.commitDBTransaction(); diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/InvokerHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/InvokerHandler.java index 14eb30134c..348f51430b 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/InvokerHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/InvokerHandler.java @@ -36,6 +36,7 @@ import org.apache.http.HttpStatus; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.client.methods.HttpRequestBase; @@ -55,7 +56,6 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; -import java.util.Arrays; import java.util.Enumeration; import java.util.List; @@ -72,8 +72,7 @@ public class InvokerHandler extends HttpServlet { private static final Log log = LogFactory.getLog(InvokerHandler.class); private static final long serialVersionUID = -6508020875358160165L; private static AuthData authData; - private static String serverUrl; - private static String platform; + private static String apiEndpoint; @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) { @@ -91,7 +90,7 @@ public class InvokerHandler extends HttpServlet { } } if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { - log.error("Error occurred while invoking the API endpoint."); + log.error("Error occurred while invoking the POST API endpoint."); HandlerUtil.handleError(resp, proxyResponse); return; } @@ -119,7 +118,7 @@ public class InvokerHandler extends HttpServlet { } } if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { - log.error("Error occurred while invoking the API endpoint."); + log.error("Error occurred while invoking the GET API endpoint."); HandlerUtil.handleError(resp, proxyResponse); return; } @@ -130,6 +129,32 @@ public class InvokerHandler extends HttpServlet { } } + @Override + protected void doHead(HttpServletRequest req, HttpServletResponse resp) { + try { + if (validateRequest(req, resp)) { + HttpHead headRequest = new HttpHead(generateBackendRequestURL(req)); + copyRequestHeaders(req, headRequest, false); + headRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken()); + ProxyResponse proxyResponse = HandlerUtil.execute(headRequest); + if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) { + proxyResponse = retryRequestWithRefreshedToken(req, resp, headRequest); + if (proxyResponse == null) { + return; + } + } + if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { + log.error("Error occurred while invoking the HEAD API endpoint."); + HandlerUtil.handleError(resp, proxyResponse); + return; + } + HandlerUtil.handleSuccess(resp, proxyResponse); + } + } catch (IOException e) { + log.error("Error occurred when processing HEAD request.", e); + } + } + @Override protected void doPut(HttpServletRequest req, HttpServletResponse resp) { try { @@ -146,7 +171,7 @@ public class InvokerHandler extends HttpServlet { } } if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { - log.error("Error occurred while invoking the API endpoint."); + log.error("Error occurred while invoking the PUT API endpoint."); HandlerUtil.handleError(resp, proxyResponse); return; } @@ -174,7 +199,7 @@ public class InvokerHandler extends HttpServlet { } } if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) { - log.error("Error occurred while invoking the API endpoint."); + log.error("Error occurred while invoking the DELETE API endpoint."); HandlerUtil.handleError(resp, proxyResponse); return; } @@ -229,13 +254,7 @@ public class InvokerHandler extends HttpServlet { */ private String generateBackendRequestURL(HttpServletRequest req) { StringBuilder urlBuilder = new StringBuilder(); - String endpointUrl = Arrays.stream(HandlerConstants.SKIPPING_API_CONTEXT) - .anyMatch(contextPath -> contextPath.contains(req.getPathInfo())) ? - serverUrl : - req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.gateway.host") - + HandlerConstants.COLON + HandlerUtil.getGatewayPort(req.getScheme()); - - urlBuilder.append(endpointUrl).append(HandlerConstants.API_COMMON_CONTEXT).append(req.getPathInfo()); + urlBuilder.append(apiEndpoint).append(HandlerConstants.API_COMMON_CONTEXT).append(req.getPathInfo()); if (StringUtils.isNotEmpty(req.getQueryString())) { urlBuilder.append("?").append(req.getQueryString()); } @@ -277,9 +296,19 @@ public class InvokerHandler extends HttpServlet { */ private static boolean validateRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException { - serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + System.getProperty("iot.gateway.https.port"); - HttpSession session = req.getSession(false); + apiEndpoint = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.gateway.host") + + HandlerConstants.COLON + HandlerUtil.getGatewayPort(req.getScheme()); + + if ("reports".equalsIgnoreCase(req.getHeader("appName"))){ + apiEndpoint = System.getProperty("iot.reporting.event.host"); + if (StringUtils.isBlank(apiEndpoint)){ + log.error("Reporting Endpoint is not defined in the iot-server.sh properly."); + handleError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR); + return false; + } + } + HttpSession session = req.getSession(false); if (session == null) { log.error("Unauthorized, You are not logged in. Please log in to the portal"); handleError(resp, HttpStatus.SC_UNAUTHORIZED); @@ -287,7 +316,6 @@ public class InvokerHandler extends HttpServlet { } authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY); - platform = (String) session.getAttribute(HandlerConstants.PLATFORM); if (authData == null) { log.error("Unauthorized, Access token not found in the current session"); handleError(resp, HttpStatus.SC_UNAUTHORIZED); @@ -340,7 +368,7 @@ public class InvokerHandler extends HttpServlet { log.debug("refreshing the token"); } HttpPost tokenEndpoint = new HttpPost( - serverUrl + HandlerConstants.API_COMMON_CONTEXT + HandlerConstants.TOKEN_ENDPOINT); + apiEndpoint + HandlerConstants.API_COMMON_CONTEXT + HandlerConstants.TOKEN_ENDPOINT); HttpSession session = req.getSession(false); if (session == null) { log.error("Couldn't find a session, hence it is required to login and proceed."); diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java index 93fa173f07..b9f5403acc 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/LoginHandler.java @@ -221,16 +221,16 @@ public class LoginHandler extends HttpServlet { * Define username and password static parameters. */ private static void validateLoginRequest(HttpServletRequest req) throws LoginException { - String gatewayCarbonPort = System.getProperty("iot.core.https.port"); + String iotsCorePort = System.getProperty("iot.core.https.port"); if (HandlerConstants.HTTP_PROTOCOL.equals(req.getScheme())) { - gatewayCarbonPort = System.getProperty("iot.core.http.port"); + iotsCorePort = System.getProperty("iot.core.http.port"); } username = req.getParameter("username"); password = req.getParameter("password"); gatewayUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.gateway.host") + HandlerConstants.COLON + HandlerUtil.getGatewayPort(req.getScheme()); uiConfigUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.core.host") - + HandlerConstants.COLON + gatewayCarbonPort + HandlerConstants.UI_CONFIG_ENDPOINT; + + HandlerConstants.COLON + iotsCorePort + HandlerConstants.UI_CONFIG_ENDPOINT; if (username == null || password == null) { String msg = "Invalid login request. Username or Password is not received for login request."; log.error(msg); diff --git a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java index e4ff7fb5bf..289f0cfb80 100644 --- a/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java +++ b/components/ui-request-interceptor/io.entgra.ui.request.interceptor/src/main/java/io/entgra/ui/request/interceptor/util/HandlerConstants.java @@ -42,8 +42,6 @@ public class HandlerConstants { public static final String COLON = ":"; public static final String HTTP_PROTOCOL = "http"; - public static final String[] SKIPPING_API_CONTEXT = {"artifact", "conf"}; - public static final int INTERNAL_ERROR_CODE = 500; public static final long TIMEOUT = 1200; }