From 30eb8ca91d7c1cd1c7a95cb51462eebcf51a8248 Mon Sep 17 00:00:00 2001 From: Hasunie Date: Fri, 2 Jun 2017 12:44:48 +0530 Subject: [PATCH 1/2] adding debug logs to GlobalThrowableMapper --- .../android/exception/GlobalThrowableMapper.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/GlobalThrowableMapper.java b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/GlobalThrowableMapper.java index 6a7d45afb..31370bd88 100644 --- a/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/GlobalThrowableMapper.java +++ b/components/mobile-plugins/android-plugin/org.wso2.carbon.device.mgt.mobile.android.api/src/main/java/org/wso2/carbon/mdm/services/android/exception/GlobalThrowableMapper.java @@ -51,24 +51,27 @@ public class GlobalThrowableMapper implements ExceptionMapper { if (e instanceof JsonParseException) { String errorMessage = "Malformed request body."; if (log.isDebugEnabled()) { - log.error(errorMessage, e); + log.debug(errorMessage, e); } return AndroidDeviceUtils.buildBadRequestException(errorMessage).getResponse(); } if (e instanceof NotFoundException) { - return ((NotFoundException) e).getResponse(); + return ((NotFoundException) e).getResponse(); } if (e instanceof BadRequestException) { return ((BadRequestException) e).getResponse(); } if (e instanceof UnexpectedServerErrorException) { if (log.isDebugEnabled()) { - log.error("Unexpected server error", e); + log.debug("Unexpected server error", e); } return ((UnexpectedServerErrorException) e).getResponse(); } if (e instanceof ConstraintViolationException) { - return ((ParameterValidationException) e).getResponse(); + if (log.isDebugEnabled()) { + log.debug("Constraint violation issue.", e); + return ((ParameterValidationException) e).getResponse(); + } } if (e instanceof IllegalArgumentException) { ErrorDTO errorDetail = new ErrorDTO(); @@ -83,7 +86,7 @@ public class GlobalThrowableMapper implements ExceptionMapper { } if (e instanceof ClientErrorException) { if (log.isDebugEnabled()) { - log.error("Client error", e); + log.debug("Client error", e); } return ((ClientErrorException) e).getResponse(); } @@ -100,7 +103,7 @@ public class GlobalThrowableMapper implements ExceptionMapper { } if (e instanceof ForbiddenException) { if (log.isDebugEnabled()) { - log.error("Resource forbidden", e); + log.debug("Resource forbidden", e); } return ((ForbiddenException) e).getResponse(); } From fbd31b77ef715ff792874dc3010deb9e6981d883 Mon Sep 17 00:00:00 2001 From: Hasunie Date: Tue, 20 Jun 2017 17:36:34 +0530 Subject: [PATCH 2/2] 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 000000000..96fdda817 --- /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 d6b51faba..396c619ca 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 51cd42b28..ac659f2ee 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 7eace79ca..f5ba6fc24 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 994500f89..000000000 --- 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 505931d4f..9b59ae2b9 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}