From 0ffb1420234ede2047f76ac3528164f9256a6efe Mon Sep 17 00:00:00 2001 From: Hasunie Date: Mon, 19 Dec 2016 08:32:47 +0530 Subject: [PATCH] fixing windows10 enrollment endpoiny issues --- .../operations/util/HeartBeatDeviceInfo.java | 4 + .../impl/EnrollmentServiceImpl.java | 117 ++++++++++++------ .../DeviceManagementAdminServiceImpl.java | 20 +-- .../impl/DeviceManagementServiceImpl.java | 33 +---- .../main/resources/devicetypes/windows.xml | 12 +- 5 files changed, 102 insertions(+), 84 deletions(-) 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/operations/util/HeartBeatDeviceInfo.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/operations/util/HeartBeatDeviceInfo.java index 730d339562..2bbf1c08db 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/operations/util/HeartBeatDeviceInfo.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/operations/util/HeartBeatDeviceInfo.java @@ -81,6 +81,10 @@ public class HeartBeatDeviceInfo { availableStorage.setCode(PluginConstants.SyncML.TOTAL_STORAGE); deviceInfoOperations.add(availableStorage); + Operation remainingBattery = new Operation(); + remainingBattery.setCode(PluginConstants.SyncML.BATTERY_CHARGE_REMAINING); + deviceInfoOperations.add(remainingBattery); + return deviceInfoOperations; } } 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/services/enrollment/impl/EnrollmentServiceImpl.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/services/enrollment/impl/EnrollmentServiceImpl.java index 35d0a2ac33..7cd57a495b 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/services/enrollment/impl/EnrollmentServiceImpl.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/services/enrollment/impl/EnrollmentServiceImpl.java @@ -1,3 +1,21 @@ +/* + * Copyright (c) 2016, 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.api.services.enrollment.impl; import org.apache.commons.codec.binary.Base64; @@ -12,13 +30,10 @@ import org.w3c.dom.*; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.certificate.mgt.core.service.CertificateManagementServiceImpl; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; import org.wso2.carbon.device.mgt.mobile.windows.api.common.PluginConstants; import org.wso2.carbon.device.mgt.mobile.windows.api.common.beans.CacheEntry; -import org.wso2.carbon.device.mgt.mobile.windows.api.common.beans.Device; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.CertificateGenerationException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.SyncmlMessageFormatException; import org.wso2.carbon.device.mgt.mobile.windows.api.common.exceptions.WAPProvisioningException; @@ -29,6 +44,8 @@ import org.wso2.carbon.device.mgt.mobile.windows.api.operations.util.SyncmlCrede import org.wso2.carbon.device.mgt.mobile.windows.api.services.enrollment.EnrollmentService; import org.wso2.carbon.device.mgt.mobile.windows.api.services.enrollment.beans.*; import org.wso2.carbon.device.mgt.mobile.windows.api.services.syncml.beans.WindowsDevice; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.core.PolicyManagerService; import org.xml.sax.SAXException; import javax.annotation.Resource; @@ -59,8 +76,7 @@ import java.util.List; /** - * Implementation class of CertificateEnrollmentService interface. This class implements MS-WSTEP - * protocol. + * Implementation class of Windows10 Enrollment process. */ @WebService(endpointInterface = PluginConstants.ENROLLMENT_SERVICE_ENDPOINT, targetNamespace = PluginConstants.DEVICE_ENROLLMENT_SERVICE_TARGET_NAMESPACE) @@ -86,7 +102,6 @@ public class EnrollmentServiceImpl implements EnrollmentService { String headerTo = null; String encodedWap; List
headers = getHeaders(); - WindowsDevice windowsDevice = new WindowsDevice(); for (Header headerElement : headers != null ? headers : null) { String nodeName = headerElement.getName().getLocalPart(); if (PluginConstants.SECURITY.equals(nodeName)) { @@ -98,32 +113,13 @@ public class EnrollmentServiceImpl implements EnrollmentService { headerTo = toElement.getFirstChild().getTextContent(); } } - windowsDevice.setDeviceType(DeviceManagementConstants.MobileDeviceTypes. - MOBILE_DEVICE_TYPE_WINDOWS); - windowsDevice.setUser(getRequestedUser(headerBinarySecurityToken)); - List contextItems = additionalContext.getcontextitem(); - for (int x= 0; x< contextItems.size(); x++) { - switch (x) { - case PluginConstants.WindowsEnrollmentProperties.WIN_DEVICE_NAME: - windowsDevice.setDeviceName(contextItems.get(x).getValue()); - case PluginConstants.WindowsEnrollmentProperties.WIN_DEVICE_IMEI: - windowsDevice.setImei(contextItems.get(x).getValue()); - case PluginConstants.WindowsEnrollmentProperties.WIN_DEVICE_ID: - windowsDevice.setDeviceId(contextItems.get(x).getValue()); - case PluginConstants.WindowsEnrollmentProperties.WIN_DEVICE_VERSION: - windowsDevice.setOsVersion(contextItems.get(x).getValue()); - } - } - - ///////// - - org.wso2.carbon.device.mgt.common.Device device = generateDevice(windowsDevice); try { - WindowsAPIUtils.getDeviceManagementService().enrollDevice(device); + enrollDevice(additionalContext, headerBinarySecurityToken); } catch (DeviceManagementException e) { - e.printStackTrace(); + throw new WindowsDeviceEnrolmentException("Error occurred while enrolling the device."); + } catch (PolicyManagementException e) { + throw new WindowsDeviceEnrolmentException("Error occurred while enforcing windows policies."); } - ///////// String[] splitEmail = headerTo.split("(/ENROLLMENTSERVER)"); String email = splitEmail[PluginConstants.CertificateEnrolment.EMAIL_SEGMENT]; @@ -187,6 +183,7 @@ public class EnrollmentServiceImpl implements EnrollmentService { } } + /** * Method used to Convert the Document object into a String. * @@ -296,7 +293,7 @@ public class EnrollmentServiceImpl implements EnrollmentService { Node authNameNode = appServerAttribute.getNamedItem(PluginConstants.CertificateEnrolment.VALUE); String userName = getRequestedUser(headerBst); //CacheEntry cacheEntry = (CacheEntry) DeviceUtil.getCacheEntry(headerBst); - // String userName = cacheEntry.getUsername(); + // String userName = cacheEntry.getUsername(); authNameNode.setTextContent(userName); DeviceUtil.removeToken(headerBst); String password = DeviceUtil.generateRandomToken(); @@ -346,29 +343,41 @@ public class EnrollmentServiceImpl implements EnrollmentService { return CastUtils.cast((List) message.get(Header.HEADER_LIST)); } + /** + * This method to getting RSTR requested user from the Cache. + * + * @param bst Binary Security token which has given from BST Endpoint. + * @return User for given token. + */ private String getRequestedUser(String bst) { CacheEntry cacheEntry = (CacheEntry) DeviceUtil.getCacheEntry(bst); String userName = cacheEntry.getUsername(); return userName; } - private org.wso2.carbon.device.mgt.common.Device generateDevice(WindowsDevice windowsDevice) { + /** + * This Method to generate windows device. + * + * @param windowsDevice Requested Device with properties. + * @return Value added Device. + */ + private Device generateDevice(WindowsDevice windowsDevice) { - org.wso2.carbon.device.mgt.common.Device generatedDevice = new org.wso2.carbon.device.mgt.common.Device(); + Device generatedDevice = new Device(); - org.wso2.carbon.device.mgt.common.Device.Property OSVersionProperty = new org.wso2.carbon.device.mgt.common.Device.Property(); + Device.Property OSVersionProperty = new Device.Property(); OSVersionProperty.setName(PluginConstants.SyncML.OS_VERSION); OSVersionProperty.setValue(windowsDevice.getOsVersion()); - org.wso2.carbon.device.mgt.common.Device.Property IMSEIProperty = new org.wso2.carbon.device.mgt.common.Device.Property(); + Device.Property IMSEIProperty = new Device.Property(); IMSEIProperty.setName(PluginConstants.SyncML.IMSI); IMSEIProperty.setValue(windowsDevice.getImsi()); - org.wso2.carbon.device.mgt.common.Device.Property IMEIProperty = new org.wso2.carbon.device.mgt.common.Device.Property(); + Device.Property IMEIProperty = new Device.Property(); IMEIProperty.setName(PluginConstants.SyncML.IMEI); IMEIProperty.setValue(windowsDevice.getImei()); - List propertyList = new ArrayList<>(); + List propertyList = new ArrayList<>(); propertyList.add(OSVersionProperty); propertyList.add(IMSEIProperty); propertyList.add(IMEIProperty); @@ -386,5 +395,39 @@ public class EnrollmentServiceImpl implements EnrollmentService { return generatedDevice; } + + /** + * This method to enroll windows10 Device. + * + * @param requestContextItems Context values to enroll the device. + * @param headerBinarySecurityToken SOAP request header value to identify the user. + * @throws DeviceManagementException Exception occurs while enrolling the Device. + * @throws PolicyManagementException Exception occurs while getting effective policies. + */ + private void enrollDevice(AdditionalContext requestContextItems, String headerBinarySecurityToken) + throws DeviceManagementException, PolicyManagementException { + WindowsDevice windowsDevice = new WindowsDevice(); + windowsDevice.setDeviceType(DeviceManagementConstants.MobileDeviceTypes. + MOBILE_DEVICE_TYPE_WINDOWS); + windowsDevice.setUser(getRequestedUser(headerBinarySecurityToken)); + List contextItems = requestContextItems.getcontextitem(); + for (int x = 0; x < contextItems.size(); x++) { + switch (x) { + case PluginConstants.WindowsEnrollmentProperties.WIN_DEVICE_NAME: + windowsDevice.setDeviceName(contextItems.get(x).getValue()); + case PluginConstants.WindowsEnrollmentProperties.WIN_DEVICE_IMEI: + windowsDevice.setImei(contextItems.get(x).getValue()); + case PluginConstants.WindowsEnrollmentProperties.WIN_DEVICE_ID: + windowsDevice.setDeviceId(contextItems.get(x).getValue()); + case PluginConstants.WindowsEnrollmentProperties.WIN_DEVICE_VERSION: + windowsDevice.setOsVersion(contextItems.get(x).getValue()); + } + } + Device device = generateDevice(windowsDevice); + WindowsAPIUtils.getDeviceManagementService().enrollDevice(device); + PolicyManagerService policyManagerService = WindowsAPIUtils.getPolicyManagerService(); + policyManagerService.getEffectivePolicy(new DeviceIdentifier(windowsDevice.getDeviceId(), device.getType())); + + } } 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/services/impl/DeviceManagementAdminServiceImpl.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/services/impl/DeviceManagementAdminServiceImpl.java index 0c9164da61..1bcd6da0a9 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/services/impl/DeviceManagementAdminServiceImpl.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/services/impl/DeviceManagementAdminServiceImpl.java @@ -46,7 +46,7 @@ import java.util.List; */ @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) -@Path("/operation/admin/devices") +@Path("/admin/devices") public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminService { private static Log log = LogFactory.getLog(OperationImpl.class); @@ -103,7 +103,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe * @throws WindowsDeviceEnrolmentException */ @POST - @Path("/disenroll") + @Path("/disenroll-devices") public Response disenroll(@HeaderParam("Accept") String acceptHeader, List deviceIDs) throws WindowsDeviceEnrolmentException { @@ -128,11 +128,11 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe log.error(errorMessage, e); throw new WindowsOperationsException(message, responseMediaType); } catch (InvalidDeviceException e) { - String errorMessage = "Invalid Device Identifiers found."; - log.error(errorMessage, e); - throw new BadRequestException( - new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); - } + String errorMessage = "Invalid Device Identifiers found."; + log.error(errorMessage, e); + throw new BadRequestException( + new ErrorResponse.ErrorResponseBuilder().setCode(400l).setMessage(errorMessage).build()); + } } /** @@ -144,7 +144,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe * @throws WindowsDeviceEnrolmentException */ @POST - @Path("/wipe-data") + @Path("/wipe-devices") public Response wipe(@HeaderParam("Accept") String acceptHeader, List deviceids) throws WindowsDeviceEnrolmentException { @@ -187,7 +187,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe * @throws WindowsDeviceEnrolmentException */ @POST - @Path("/ring-device") + @Path("/ring-devices") public Response ring(@HeaderParam("Accept") String acceptHeader, List deviceIDs) throws WindowsDeviceEnrolmentException { @@ -235,7 +235,7 @@ public class DeviceManagementAdminServiceImpl implements DeviceManagementAdminSe * @throws WindowsDeviceEnrolmentException */ @POST - @Path("/lock-reset") + @Path("/lock-reset-devices") public Response lockReset(@HeaderParam("Accept") String acceptHeader, List deviceIDs) throws WindowsDeviceEnrolmentException { 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/services/impl/DeviceManagementServiceImpl.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/services/impl/DeviceManagementServiceImpl.java index ef68548e1e..f754ff80cb 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/services/impl/DeviceManagementServiceImpl.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/services/impl/DeviceManagementServiceImpl.java @@ -67,10 +67,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { String token; String response; SyncmlDocument syncmlDocument; - List deviceInfoOperations; List pendingOperations; OperationHandler operationHandler = new OperationHandler(); - DeviceInfo deviceInfo = new DeviceInfo(); OperationReply operationReply = new OperationReply(); try { @@ -91,11 +89,10 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (modifyEnrollWithMoreDetail(request)) { pendingOperations = operationHandler.getPendingOperations(syncmlDocument); - response = operationReply.generateReply(syncmlDocument,pendingOperations); return Response.status(Response.Status.OK).entity(response).build(); } else { - String msg = "Error occurred in device enrollment."; + String msg = "Error occurred in while modify the enrollment."; log.error(msg); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } @@ -104,7 +101,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { log.error(msg); return Response.status(Response.Status.UNAUTHORIZED).entity(msg).build(); } - } else if (sessionId >= PluginConstants.SyncML.SYNCML_SECOND_SESSION_ID) { + } else { if ((syncmlDocument.getBody().getAlert() != null)) { if (!syncmlDocument.getBody().getAlert().getData().equals(Constants.DISENROLL_ALERT_DATA)) { pendingOperations = operationHandler.getPendingOperations(syncmlDocument); @@ -125,10 +122,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { return Response.ok().entity(operationReply.generateReply( syncmlDocument, pendingOperations)).build(); } - } else { - String msg = "Failure occurred in Device request message."; - log.error(msg); - return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } } } catch (SyncmlMessageFormatException e) { @@ -198,35 +191,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { existingDevice.setDeviceIdentifier(syncmlDocument.getHeader().getSource().getLocURI()); existingDevice.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS); status = WindowsAPIUtils.getDeviceManagementService().modifyEnrollment(existingDevice); - // call effective policy for the enrolling device. - //PolicyManagerService policyManagerService = WindowsAPIUtils.getPolicyManagerService(); - //policyManagerService.getEffectivePolicy(deviceIdentifier); return status; - } } catch (DeviceManagementException e) { throw new WindowsDeviceEnrolmentException("Failure occurred while enrolling device.", e); -// } catch (PolicyManagementException e) { -// throw new WindowsOperationException("Error occurred while getting effective policy.", e); } finally { PrivilegedCarbonContext.endTenantFlow(); } return status; } - -// public void generateDeviceInfo(Document requestedInfo) { -// SyncmlDocument syncmlDocument; -// syncmlDocument = SyncmlParser.parseSyncmlPayload(requestedInfo); -// DeviceIdentifier deviceIdentifier = convertToDeviceIdentifierObject(syncmlDocument. -// getHeader().getSource().getLocURI()); -// try { -// List itemList = syncmlDocument.getBody().getResults().getItem(); -// Device existingDevice = WindowsAPIUtils.getDeviceManagementService().getDevice(deviceIdentifier); -// org.wso2.carbon.device.mgt.common.device.details.DeviceInfo existingDeviceInfo = existingDevice.getDeviceInfo(); -// existingDeviceInfo.s -// } catch (DeviceManagementException e) { -// e.printStackTrace(); -// } -// -// } } diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/devicetypes/windows.xml b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/devicetypes/windows.xml index f30e8d0eff..1313b68673 100644 --- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/devicetypes/windows.xml +++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/devicetypes/windows.xml @@ -65,31 +65,31 @@ Disenroll Lock the device - + Wipe Data Lock the device - + Ring Lock the device - + Device Lock Lock the device - + - Device Lock + Device Lock Reset Lock the device - +