From fbd31b77ef715ff792874dc3010deb9e6981d883 Mon Sep 17 00:00:00 2001 From: Hasunie Date: Tue, 20 Jun 2017 17:36:34 +0530 Subject: [PATCH] Windows enrollment issue was resolved from wso2/product-iots#1029 --- .../WindowsEnrollmentTokenException.java | 38 ++++++++++++++ .../windows/api/common/util/BSTValidator.java | 4 -- .../windows/api/common/util/DeviceUtil.java | 47 ++++------------- .../api/common/util/WindowsAPIUtils.java | 14 +++++ .../windows/impl/util/WindowsUtils.java | 52 ------------------- .../pom.xml | 10 ++-- 6 files changed, 67 insertions(+), 98 deletions(-) create mode 100644 components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/exceptions/WindowsEnrollmentTokenException.java delete mode 100644 components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsUtils.java diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/exceptions/WindowsEnrollmentTokenException.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/exceptions/WindowsEnrollmentTokenException.java new file mode 100644 index 0000000000..96fdda8177 --- /dev/null +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/exceptions/WindowsEnrollmentTokenException.java @@ -0,0 +1,38 @@ +package org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions; + + +public class WindowsEnrollmentTokenException extends Exception { + private static final long serialVersionUID = -2297311387874900305L; + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public WindowsEnrollmentTokenException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public WindowsEnrollmentTokenException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public WindowsEnrollmentTokenException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public WindowsEnrollmentTokenException() { + super(); + } + + public WindowsEnrollmentTokenException(Throwable cause) { + super(cause); + } +} diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/BSTValidator.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/BSTValidator.java index d6b51faba6..396c619cad 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/BSTValidator.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/BSTValidator.java @@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.mobile.windows.api.common.util; import org.apache.commons.codec.binary.Base64; -import org.apache.http.auth.AUTH; import org.apache.ws.security.WSSecurityException; import org.apache.ws.security.handler.RequestData; import org.apache.ws.security.message.token.BinarySecurity; @@ -29,12 +28,9 @@ import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.PluginConstants; import org.wso2.carbon.device.mgt.mobile.windows.api.common.authenticator.OAuthValidatorFactory; -import org.wso2.carbon.device.mgt.mobile.windows.api.common.beans.CacheEntry; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.AuthenticationException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.OAuthTokenValidationException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsDeviceEnrolmentException; -import org.wso2.carbon.device.mgt.mobile.windows.exception.WindowsDeviceMgtPluginException; -import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.MobileDeviceManagementDAOException; import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; import java.util.HashMap; diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/DeviceUtil.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/DeviceUtil.java index 51cd42b286..ac659f2eed 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/DeviceUtil.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/DeviceUtil.java @@ -22,11 +22,9 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WindowsDeviceEnrolmentException; -import org.wso2.carbon.device.mgt.mobile.windows.exception.WindowsDeviceMgtPluginException; import org.wso2.carbon.device.mgt.mobile.windows.impl.WindowsTokenService; import org.wso2.carbon.device.mgt.mobile.windows.impl.dao.MobileDeviceManagementDAOException; import org.wso2.carbon.device.mgt.mobile.windows.impl.dto.MobileCacheEntry; -import org.wso2.carbon.device.mgt.mobile.windows.impl.util.WindowsUtils; import java.util.UUID; @@ -35,20 +33,12 @@ import java.util.UUID; */ public class DeviceUtil { - private static final String TOKEN_CACHE_MANAGER = "TOKEN_CACHE_MANAGER"; - private static final String TOKEN_CACHE = "TOKEN_CACHE"; - private static final long CACHE_DURATION = 15l; - private static boolean isContextCacheInitialized = false; private static WindowsTokenService tokenService; private static final Log log = LogFactory.getLog(DeviceUtil.class); static { - try { - tokenService = WindowsUtils.getEnrollmentTokenService(); - } catch (WindowsDeviceMgtPluginException e) { - log.error("WindowsTokenService cannot be initialized.", e); - } + tokenService = WindowsAPIUtils.getEnrollmentTokenService(); } public static String generateRandomToken() { @@ -59,7 +49,7 @@ public class DeviceUtil { throws WindowsDeviceEnrolmentException { try { if(tokenService == null) { - tokenService = WindowsUtils.getEnrollmentTokenService(); + tokenService = WindowsAPIUtils.getEnrollmentTokenService(); } MobileCacheEntry existingCacheEntry = tokenService.getCacheToken(token); PrivilegedCarbonContext carbonCtx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); @@ -77,12 +67,7 @@ public class DeviceUtil { tokenService.updateCacheToken(existingCacheEntry); } } catch (MobileDeviceManagementDAOException e) { - String msg = "Error occured when saving cache token for device: " + deviceID; - log.error(msg); - throw new WindowsDeviceEnrolmentException(msg, e); - } catch (WindowsDeviceMgtPluginException e) { - String msg = "Error occured when retrieving enrollment token service"; - log.error(msg); + String msg = "Error occurred when saving cache token for device: " + deviceID; throw new WindowsDeviceEnrolmentException(msg, e); } } @@ -92,16 +77,11 @@ public class DeviceUtil { MobileCacheEntry tokenEntry; try { if (tokenService == null) { - tokenService = WindowsUtils.getEnrollmentTokenService(); + tokenService = WindowsAPIUtils.getEnrollmentTokenService(); } tokenEntry = tokenService.getCacheToken(token); - } catch (WindowsDeviceMgtPluginException e) { - String msg = "Error occured when retrieving enrollment token service."; - log.error(msg); - throw new WindowsDeviceEnrolmentException(msg, e); } catch (MobileDeviceManagementDAOException e) { - String msg = "Error occured when retrieving enrollment token."; - log.error(msg); + String msg = "Error occurred when retrieving enrollment token."; throw new WindowsDeviceEnrolmentException(msg, e); } return tokenEntry; @@ -112,16 +92,12 @@ public class DeviceUtil { MobileCacheEntry tokenEntry; try { if (tokenService == null) { - tokenService = WindowsUtils.getEnrollmentTokenService(); + tokenService = WindowsAPIUtils.getEnrollmentTokenService(); } tokenEntry = tokenService.getCacheTokenFromDeviceId(deviceId); - } catch (WindowsDeviceMgtPluginException e) { - String msg = "Error occured when retrieving enrollment token service."; - log.error(msg); - throw new WindowsDeviceEnrolmentException(msg, e); + } catch (MobileDeviceManagementDAOException e) { - String msg = "Error occured when retrieving enrollment token."; - log.error(msg); + String msg = "Error occurred when retrieving enrollment token."; throw new WindowsDeviceEnrolmentException(msg, e); } return tokenEntry; @@ -130,14 +106,11 @@ public class DeviceUtil { public static void removeTokenEntry(String token) { try { if (tokenService == null) { - tokenService = WindowsUtils.getEnrollmentTokenService(); + tokenService = WindowsAPIUtils.getEnrollmentTokenService(); } tokenService.removeCacheToken(token); - } catch (WindowsDeviceMgtPluginException e) { - String msg = "Error occured when retrieving enrollment token service."; - log.error(msg); } catch (MobileDeviceManagementDAOException e) { - String msg = "Error occured when removing enrollment token."; + String msg = "Error occurred when removing enrollment token."; log.error(msg); } } diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/WindowsAPIUtils.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/WindowsAPIUtils.java index 7eace79ca7..f5ba6fc24a 100644 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/WindowsAPIUtils.java +++ b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows.api/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/api/common/util/WindowsAPIUtils.java @@ -40,6 +40,8 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.mobile.windows.api.common.PluginConstants; import org.wso2.carbon.device.mgt.mobile.windows.api.common.beans.ErrorResponse; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.BadRequestException; +import org.wso2.carbon.device.mgt.mobile.windows.exception.WindowsDeviceMgtPluginException; +import org.wso2.carbon.device.mgt.mobile.windows.impl.WindowsTokenService; import org.wso2.carbon.identity.core.util.IdentityTenantUtil; import org.wso2.carbon.identity.oauth2.OAuth2TokenValidationService; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; @@ -90,6 +92,18 @@ public class WindowsAPIUtils { return notificationManagementService; } + public static WindowsTokenService getEnrollmentTokenService() { + WindowsTokenService tokenService; + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + tokenService = (WindowsTokenService) + ctx.getOSGiService(WindowsTokenService.class, null); + + if(tokenService == null) { + throw new IllegalStateException("WindowsTokenService is not initialized"); + } + return tokenService; + } + public static MediaType getResponseMediaType(String acceptHeader) { MediaType responseMediaType; if (MediaType.WILDCARD.equals(acceptHeader)) { diff --git a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsUtils.java b/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsUtils.java deleted file mode 100644 index 994500f89e..0000000000 --- a/components/mobile-plugins/windows-plugin/org.wso2.carbon.device.mgt.mobile.windows/src/main/java/org/wso2/carbon/device/mgt/mobile/windows/impl/util/WindowsUtils.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.device.mgt.mobile.windows.impl.util; - -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.mobile.windows.exception.WindowsDeviceMgtPluginException; -import org.wso2.carbon.device.mgt.mobile.windows.impl.WindowsTokenService; - -import java.util.Map; - -/** - * Contains utility methods used by Windows plugin. - */ -public class WindowsUtils { - public static String getDeviceProperty(Map deviceProperties, String property) { - - String deviceProperty = deviceProperties.get(property); - if (deviceProperty == null) { - return null; - } - return deviceProperty; - } - - public static WindowsTokenService getEnrollmentTokenService() throws WindowsDeviceMgtPluginException { - - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - WindowsTokenService tokenService = (WindowsTokenService) - ctx.getOSGiService(WindowsTokenService.class, null); - - if(tokenService == null) { - String msg = "WindowsTokenService is not initialized"; - throw new WindowsDeviceMgtPluginException(msg); - } - return tokenService; - } -} diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/pom.xml b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/pom.xml index 505931d4f9..9b59ae2b95 100644 --- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/pom.xml +++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/pom.xml @@ -202,11 +202,11 @@ - - - - - + + + org.wso2.carbon.devicemgt-plugins:org.wso2.carbon.device.mgt.mobile.windows:${carbon.devicemgt.plugins.version} + + org.wso2.carbon.core.server:${carbon.kernel.version}