|
|
|
@ -20,16 +20,42 @@ package org.wso2.carbon.device.mgt.core.service;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.apache.http.HttpResponse;
|
|
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
|
|
import org.apache.http.protocol.HTTP;
|
|
|
|
|
import org.wso2.carbon.CarbonConstants;
|
|
|
|
|
import org.wso2.carbon.context.CarbonContext;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.*;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.Device;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceEnrollmentInfoNotification;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManager;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceNotFoundException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceNotification;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DevicePropertyNotification;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.EnrolmentInfo;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.FeatureManager;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.InitialOperationConfig;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.InvalidDeviceException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.MonitoringOperation;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.PaginationRequest;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.PaginationResult;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.app.mgt.Application;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotificationConfiguration;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotifier;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.enrollment.notification.EnrollmentNotifierException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException;
|
|
|
|
@ -48,6 +74,8 @@ import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.DeviceManagementConstants;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
|
|
|
@ -75,6 +103,11 @@ import org.wso2.carbon.email.sender.core.TypedValue;
|
|
|
|
|
import org.wso2.carbon.email.sender.core.service.EmailSenderService;
|
|
|
|
|
import org.wso2.carbon.user.api.UserStoreException;
|
|
|
|
|
|
|
|
|
|
import javax.xml.bind.JAXBContext;
|
|
|
|
|
import javax.xml.bind.JAXBException;
|
|
|
|
|
import javax.xml.bind.Marshaller;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.StringWriter;
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
@ -193,6 +226,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
|
if (enrolmentInfo.equals(newEnrolmentInfo)) {
|
|
|
|
|
device.setId(existingDevice.getId());
|
|
|
|
|
device.getEnrolmentInfo().setDateOfEnrolment(enrolmentInfo.getDateOfEnrolment());
|
|
|
|
|
device.getEnrolmentInfo().setDateOfLastUpdate(enrolmentInfo.getDateOfLastUpdate());
|
|
|
|
|
device.getEnrolmentInfo().setId(enrolmentInfo.getId());
|
|
|
|
|
this.modifyEnrollment(device);
|
|
|
|
|
status = true;
|
|
|
|
@ -200,7 +234,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!status) {
|
|
|
|
|
int enrolmentId, updateStatus = 0;
|
|
|
|
|
int updateStatus = 0;
|
|
|
|
|
EnrolmentInfo enrollment;
|
|
|
|
|
try {
|
|
|
|
|
//Remove the existing enrollment
|
|
|
|
|
DeviceManagementDAOFactory.beginTransaction();
|
|
|
|
@ -210,12 +245,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
|
}
|
|
|
|
|
if ((updateStatus > 0) || EnrolmentInfo.Status.REMOVED.
|
|
|
|
|
equals(existingEnrolmentInfo.getStatus())) {
|
|
|
|
|
enrolmentId = enrollmentDAO.
|
|
|
|
|
enrollment = enrollmentDAO.
|
|
|
|
|
addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId);
|
|
|
|
|
if (enrollment == null ){
|
|
|
|
|
DeviceManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
throw new DeviceManagementException(
|
|
|
|
|
"Enrollment data persistence is failed in a re-enrollment. Device id : "
|
|
|
|
|
+ existingDevice.getId() + " Device Identifier: " + device
|
|
|
|
|
.getDeviceIdentifier());
|
|
|
|
|
}
|
|
|
|
|
device.setEnrolmentInfo(enrollment);
|
|
|
|
|
DeviceManagementDAOFactory.commitTransaction();
|
|
|
|
|
this.removeDeviceFromCache(deviceIdentifier);
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("An enrolment is successfully added with the id '" + enrolmentId +
|
|
|
|
|
log.debug("An enrolment is successfully added with the id '" + enrollment.getId() +
|
|
|
|
|
"' associated with " + "the device identified by key '" +
|
|
|
|
|
device.getDeviceIdentifier() + "', which belongs to " + "platform '" +
|
|
|
|
|
device.getType() + " upon the user '" + device.getEnrolmentInfo().getOwner() +
|
|
|
|
@ -241,13 +284,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
int enrolmentId;
|
|
|
|
|
EnrolmentInfo enrollment;
|
|
|
|
|
try {
|
|
|
|
|
DeviceManagementDAOFactory.beginTransaction();
|
|
|
|
|
DeviceType type = deviceTypeDAO.getDeviceType(device.getType(), tenantId);
|
|
|
|
|
if (type != null) {
|
|
|
|
|
int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId);
|
|
|
|
|
enrolmentId = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId);
|
|
|
|
|
enrollment = enrollmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId);
|
|
|
|
|
if (enrollment == null ){
|
|
|
|
|
DeviceManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
throw new DeviceManagementException(
|
|
|
|
|
"Enrollment data persistence is failed in a new enrollment. Device id: " + deviceId
|
|
|
|
|
+ " Device Identifier: " + device.getDeviceIdentifier());
|
|
|
|
|
}
|
|
|
|
|
device.setEnrolmentInfo(enrollment);
|
|
|
|
|
DeviceManagementDAOFactory.commitTransaction();
|
|
|
|
|
} else {
|
|
|
|
|
DeviceManagementDAOFactory.rollbackTransaction();
|
|
|
|
@ -274,17 +324,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("An enrolment is successfully created with the id '" + enrolmentId + "' associated with " +
|
|
|
|
|
log.debug("An enrolment is successfully created with the id '" + enrollment.getId() + "' associated with " +
|
|
|
|
|
"the device identified by key '" + device.getDeviceIdentifier() + "', which belongs to " +
|
|
|
|
|
"platform '" + device.getType() + " upon the user '" +
|
|
|
|
|
device.getEnrolmentInfo().getOwner() + "'");
|
|
|
|
|
}
|
|
|
|
|
status = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (status) {
|
|
|
|
|
addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership());
|
|
|
|
|
addInitialOperations(deviceIdentifier, device.getType());
|
|
|
|
|
sendNotification(device);
|
|
|
|
|
}
|
|
|
|
|
extractDeviceLocationToUpdate(device);
|
|
|
|
|
return status;
|
|
|
|
@ -2805,4 +2855,120 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
*
|
|
|
|
|
* <p>
|
|
|
|
|
* If the device enrollment is succeeded and the enrollment notification sending is enabled, this method executes.
|
|
|
|
|
* If it is configured to send enrollment notification by using the extension, initiate the instance from
|
|
|
|
|
* configured instance class and execute the notify method to send enrollment notification.
|
|
|
|
|
* </p>
|
|
|
|
|
*
|
|
|
|
|
*<p>
|
|
|
|
|
* In default, if it is enabled the enrollment notification sending and disabled the notifying through extension,
|
|
|
|
|
* it uses pre-defined API to send enrollment notification. In that case, invoke the
|
|
|
|
|
* /api/device-mgt/enrollment-notification API with the constructed payload.
|
|
|
|
|
*</p>
|
|
|
|
|
* @param device {@link Device} object
|
|
|
|
|
*/
|
|
|
|
|
private void sendNotification(Device device) {
|
|
|
|
|
DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig();
|
|
|
|
|
EnrollmentNotificationConfiguration enrollmentNotificationConfiguration = config
|
|
|
|
|
.getEnrollmentNotificationConfiguration();
|
|
|
|
|
try {
|
|
|
|
|
if (enrollmentNotificationConfiguration != null && enrollmentNotificationConfiguration.isEnabled()) {
|
|
|
|
|
if (enrollmentNotificationConfiguration.getNotifyThroughExtension()) {
|
|
|
|
|
Class<?> clz = Class.forName(enrollmentNotificationConfiguration.getExtensionClass());
|
|
|
|
|
EnrollmentNotifier enrollmentNotifier = (EnrollmentNotifier) clz.newInstance();
|
|
|
|
|
enrollmentNotifier.notify(device);
|
|
|
|
|
} else {
|
|
|
|
|
String internalServerAddr = enrollmentNotificationConfiguration.getNotyfyingInternalHost();
|
|
|
|
|
if (internalServerAddr == null) {
|
|
|
|
|
internalServerAddr = "https://localhost:8243";
|
|
|
|
|
}
|
|
|
|
|
invokeApi(device, internalServerAddr);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug(
|
|
|
|
|
"Either Enrollment Notification Configuration is disabled or not defined in the cdm-config.xml");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
|
log.error("Extension class cannot be located", e);
|
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
|
log.error("Can't access the class or its nullary constructor is not accessible.", e);
|
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
|
log.error("Extension class instantiation is failed", e);
|
|
|
|
|
} catch (EnrollmentNotifierException e) {
|
|
|
|
|
log.error("Error occured while sending enrollment notification." + e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void invokeApi(Device device, String internalServerAddr) throws EnrollmentNotifierException {
|
|
|
|
|
try (CloseableHttpClient client = HttpClients.createDefault()) {
|
|
|
|
|
HttpPost apiEndpoint = new HttpPost(
|
|
|
|
|
internalServerAddr + DeviceManagementConstants.ENROLLMENT_NOTIFICATION_API_ENDPOINT);
|
|
|
|
|
apiEndpoint.setHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_XML.toString());
|
|
|
|
|
apiEndpoint.setEntity(constructEnrollmentNotificationPayload(device));
|
|
|
|
|
HttpResponse response = client.execute(apiEndpoint);
|
|
|
|
|
if (response != null) {
|
|
|
|
|
log.info("Enrollment Notification is sent through a configured API. Response code: " + response
|
|
|
|
|
.getStatusLine().getStatusCode());
|
|
|
|
|
} else {
|
|
|
|
|
log.error("Response is 'NUll' for the Enrollment notification sending API call.");
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
throw new EnrollmentNotifierException("Error occured when invoking API. API endpoint: " + internalServerAddr
|
|
|
|
|
+ DeviceManagementConstants.ENROLLMENT_NOTIFICATION_API_ENDPOINT, e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
|
*
|
|
|
|
|
* Convert device object into XML string and construct {@link StringEntity} object and returns.
|
|
|
|
|
* <p>
|
|
|
|
|
* First create {@link JAXBContext} and thereafter create {@link Marshaller} by usig created {@link JAXBContext}.
|
|
|
|
|
* Then enable formatting and get the converted xml string output of {@link Device}.
|
|
|
|
|
* </p>
|
|
|
|
|
*
|
|
|
|
|
* @param device {@link Device} object
|
|
|
|
|
* @return {@link StringEntity}
|
|
|
|
|
* @throws EnrollmentNotifierException, if error occured while converting {@link Device} object into XML sting
|
|
|
|
|
*/
|
|
|
|
|
private static StringEntity constructEnrollmentNotificationPayload(Device device)
|
|
|
|
|
throws EnrollmentNotifierException {
|
|
|
|
|
try {
|
|
|
|
|
DevicePropertyNotification devicePropertyNotification = new DevicePropertyNotification();
|
|
|
|
|
for (Device.Property property : device.getProperties()) {
|
|
|
|
|
if ("SERIAL".equals(property.getName())) {
|
|
|
|
|
devicePropertyNotification.setSerial(property.getValue());
|
|
|
|
|
}
|
|
|
|
|
if ("IMEI".equals((property.getName()))) {
|
|
|
|
|
devicePropertyNotification.setImei(property.getValue());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
DeviceEnrollmentInfoNotification deviceEnrollmentInfoNotification = new DeviceEnrollmentInfoNotification();
|
|
|
|
|
deviceEnrollmentInfoNotification.setOwner(device.getEnrolmentInfo().getOwner());
|
|
|
|
|
deviceEnrollmentInfoNotification.setDateOfEnrolment(device.getEnrolmentInfo().getDateOfEnrolment());
|
|
|
|
|
deviceEnrollmentInfoNotification.setDateOfLastUpdate(device.getEnrolmentInfo().getDateOfLastUpdate());
|
|
|
|
|
deviceEnrollmentInfoNotification.setOwnership(device.getEnrolmentInfo().getOwnership().toString());
|
|
|
|
|
deviceEnrollmentInfoNotification.setStatus(device.getEnrolmentInfo().getStatus().toString());
|
|
|
|
|
|
|
|
|
|
DeviceNotification deviceNotification = new DeviceNotification(device.getDeviceIdentifier(), device.getName(),
|
|
|
|
|
device.getType(), device.getDescription(), devicePropertyNotification,
|
|
|
|
|
deviceEnrollmentInfoNotification);
|
|
|
|
|
|
|
|
|
|
JAXBContext jaxbContext = JAXBContext.newInstance(DeviceNotification.class);
|
|
|
|
|
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
|
|
|
|
|
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
|
jaxbMarshaller.marshal(deviceNotification, sw);
|
|
|
|
|
String payload = sw.toString();
|
|
|
|
|
return new StringEntity(payload, ContentType.APPLICATION_XML);
|
|
|
|
|
} catch (JAXBException e) {
|
|
|
|
|
throw new EnrollmentNotifierException(
|
|
|
|
|
"Error occured when converting Device object into xml string. Hence enrollment notification payload "
|
|
|
|
|
+ "constructing is failed", e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|