private boolean register(String deviceId, String name) { try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(deviceId); deviceIdentifier.setType(VirtualFireAlarmConstants.DEVICE_TYPE); if (APIUtil.getDeviceManagementService().isEnrolled(deviceIdentifier)) { return false; } Device device = new Device(); device.setDeviceIdentifier(deviceId); EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); enrolmentInfo.setDateOfEnrolment(new Date().getTime()); enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE); enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); device.setName(name); device.setType(VirtualFireAlarmConstants.DEVICE_TYPE); enrolmentInfo.setOwner(APIUtil.getAuthenticatedUser()); device.setEnrolmentInfo(enrolmentInfo); return APIUtil.getDeviceManagementService().enrollDevice(device); } catch (DeviceManagementException e) { log.error(e.getMessage(), e); return false; } }
/** * @param applicationOperationDevice holds the information needs to retrieve device list. * @return List of devices * @throws MobileApplicationException */ public List<Device> getDevices(ApplicationOperationDevice applicationOperationDevice) throws MobileApplicationException { List<Device> devices; try { List<org.wso2.carbon.device.mgt.common.Device> deviceList = MDMServiceAPIUtils.getDeviceManagementService(applicationOperationDevice.getTenantId()) .getDevicesOfUser(applicationOperationDevice.getCurrentUser().getUsername()); devices = new ArrayList<>(deviceList.size()); if (log.isDebugEnabled()) { log.debug("device list got from mdm " + deviceList.toString()); } for (org.wso2.carbon.device.mgt.common.Device commonDevice : deviceList) { if (MDMAppConstants.ACTIVE.equals( commonDevice.getEnrolmentInfo().getStatus().toString().toLowerCase())) { Device device = new Device(); org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier deviceIdentifier = new org.wso2.carbon.appmgt.mobile.beans.DeviceIdentifier(); deviceIdentifier.setId(commonDevice.getDeviceIdentifier()); deviceIdentifier.setType(commonDevice.getType()); device.setDeviceIdentifier(deviceIdentifier); device.setName(commonDevice.getName()); device.setModel(commonDevice.getName()); device.setType(MDMAppConstants.MOBILE_DEVICE); String imgUrl; if (MDMAppConstants.ANDROID.equalsIgnoreCase(commonDevice.getType())) { imgUrl = String.format( applicationOperationDevice.getConfigParams().get(MDMAppConstants.IMAGE_URL), MDMAppConstants.NEXUS); } else if (MDMAppConstants.IOS.equalsIgnoreCase(commonDevice.getType())) { imgUrl = String.format( applicationOperationDevice.getConfigParams().get(MDMAppConstants.IMAGE_URL), MDMAppConstants.IPHONE); } else { imgUrl = String.format( applicationOperationDevice.getConfigParams().get(MDMAppConstants.IMAGE_URL), MDMAppConstants.NONE); } device.setImage(imgUrl); device.setPlatform(commonDevice.getType()); devices.add(device); } } } catch (DeviceManagementException e) { logError("Error While retrieving Device List.", e); throw new MobileApplicationException(e.getMessage()); } return devices; }
@Path("device/download") @GET @Produces("application/zip") public Response downloadSketch( @QueryParam("deviceName") String deviceName, @QueryParam("sketchType") String sketchType) { try { ZipArchive zipFile = createDownloadFile(APIUtil.getAuthenticatedUser(), deviceName, sketchType); Response.ResponseBuilder response = Response.ok(FileUtils.readFileToByteArray(zipFile.getZipFile())); response.status(Response.Status.OK); response.type("application/zip"); response.header( "Content-Disposition", "attachment; filename=\"" + zipFile.getFileName() + "\""); Response resp = response.build(); zipFile.getZipFile().delete(); return resp; } catch (IllegalArgumentException ex) { return Response.status(400).entity(ex.getMessage()).build(); // bad request } catch (DeviceManagementException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); } catch (JWTClientException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); } catch (APIManagerException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); } catch (IOException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); } catch (UserStoreException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); } catch (VirtualFirealarmDeviceMgtPluginException ex) { log.error(ex.getMessage(), ex); return Response.status(500).entity(ex.getMessage()).build(); } }