diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml index 2fa383eeb1d..e66aea25abb 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/pom.xml @@ -115,6 +115,10 @@ + + org.apache.felix + maven-scr-plugin + org.apache.felix maven-bundle-plugin @@ -137,7 +141,9 @@ org.wso2.carbon.device.mgt.common.push.notification, org.wso2.carbon.device.mgt.core.service, org.wso2.carbon.event.output.adapter.core, - org.wso2.carbon.event.output.adapter.core.exception + org.wso2.carbon.event.output.adapter.core.exception, + org.osgi.framework, + org.wso2.carbon.core diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/InvalidConfigurationException.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/InvalidConfigurationException.java index 337ef72644f..e6f186f4312 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/InvalidConfigurationException.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/InvalidConfigurationException.java @@ -18,5 +18,29 @@ */ package org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt; -public class InvalidConfigurationException { +/** + * This exception is thrown whether the mqtt configured with invalid configuration. + */ +public class InvalidConfigurationException extends RuntimeException { + + public InvalidConfigurationException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public InvalidConfigurationException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidConfigurationException(String msg) { + super(msg); + } + + public InvalidConfigurationException() { + super(); + } + + public InvalidConfigurationException(Throwable cause) { + super(cause); + } + } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java index 849b4812083..ee4105922d0 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTNotificationStrategy.java @@ -18,6 +18,8 @@ */ package org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; @@ -33,15 +35,16 @@ import java.util.Map; public class MQTTNotificationStrategy implements NotificationStrategy { - private static final String MQTT_ADAPTER_PROPERTY_NAME = "mqtt.adapter.name"; private static final String MQTT_ADAPTER_TOPIC = "mqtt.adapter.topic"; - private static final String MQTT_ADAPTER_NAME = "mqtt.push.notification.publisher"; + private String mqttAdapterName; + private static final Log log = LogFactory.getLog(MQTTNotificationStrategy.class); public MQTTNotificationStrategy(PushNotificationConfig config) { OutputEventAdapterConfiguration adapterConfig = new OutputEventAdapterConfiguration(); adapterConfig.setType(MQTTAdapterConstants.MQTT_ADAPTER_TYPE); - adapterConfig.setName(MQTT_ADAPTER_NAME); - adapterConfig.setMessageFormat(MessageType.JSON); + mqttAdapterName = config.getProperty(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_NAME); + adapterConfig.setName(mqttAdapterName); + adapterConfig.setMessageFormat(MessageType.TEXT); Map configProperties = new HashMap(); configProperties.put(MQTTAdapterConstants.MQTT_ADAPTER_PROPERTY_BROKER_URL, @@ -60,7 +63,7 @@ public class MQTTNotificationStrategy implements NotificationStrategy { try { MQTTDataHolder.getInstance().getOutputEventAdapterService().create(adapterConfig); } catch (OutputEventAdapterException e) { - throw new RuntimeException("Error occurred while initializing MQTT output event adapter", e); + throw new InvalidConfigurationException("Error occurred while initializing MQTT output event adapter", e); } } @@ -71,10 +74,10 @@ public class MQTTNotificationStrategy implements NotificationStrategy { @Override public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { - Map dynamicProperties = ctx.getProperties(); + Map dynamicProperties = new HashMap<>(); dynamicProperties.put("topic", (String) ctx.getOperation().getProperties().get(MQTT_ADAPTER_TOPIC)); - MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(MQTT_ADAPTER_NAME, dynamicProperties, - ctx.getOperation().getPayLoad()); + MQTTDataHolder.getInstance().getOutputEventAdapterService().publish(mqttAdapterName, dynamicProperties, + ctx.getOperation().getPayLoad()); } @Override diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTPushNotificationStrategyUtil.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTPushNotificationStrategyUtil.java deleted file mode 100644 index fe3d83f365f..00000000000 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/MQTTPushNotificationStrategyUtil.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.extensions.push.notification.provider.mqtt; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; - -public class MQTTPushNotificationStrategyUtil { - - private static final Log log = LogFactory.getLog(MQTTPushNotificationStrategyUtil.class); - - public static String getAuthenticatedUser() { - CarbonContext carbonContext = CarbonContext.getThreadLocalCarbonContext(); - String username = carbonContext.getUsername(); - String tenantDomain = carbonContext.getTenantDomain(); - if (username.endsWith(tenantDomain)) { - return username.substring(0, username.lastIndexOf("@")); - } - return username; - } - - public static String getAuthenticatedUserTenantDomain() { - return CarbonContext.getThreadLocalCarbonContext().getTenantDomain(); - } - - -} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/MQTTDataHolder.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/MQTTDataHolder.java index 90d68f036a2..d381f0042bf 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/MQTTDataHolder.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/MQTTDataHolder.java @@ -19,8 +19,12 @@ package org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.internal; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration; import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; +import java.util.List; +import java.util.Stack; + public class MQTTDataHolder { private OutputEventAdapterService outputEventAdapterService; diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/MQTTPushNotificationServiceComponent.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/MQTTPushNotificationServiceComponent.java index 25fed275cd7..8f0ca3dad65 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/MQTTPushNotificationServiceComponent.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/MQTTPushNotificationServiceComponent.java @@ -25,7 +25,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; /** - * @scr.component name="org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.internal.MQTTPushNotificationServiceComponent" immediate="true" + * @scr.component name="org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.internal.MQTTPushNotificationServiceComponent" immediate="true" * @scr.reference name="carbon.device.mgt.provider" * interface="org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService" * cardinality="1..1" @@ -34,7 +34,9 @@ import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; * unbind="unsetDeviceManagementProviderService" * @scr.reference name="event.output.adapter.service" * interface="org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService" - * cardinality="1..1" policy="dynamic" bind="setOutputEventAdapterService" + * cardinality="1..1" + * policy="dynamic" + * bind="setOutputEventAdapterService" * unbind="unsetOutputEventAdapterService" */ public class MQTTPushNotificationServiceComponent { diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/util/MQTTAdapterConstants.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/util/MQTTAdapterConstants.java index 534afb3ab71..60005764be2 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/util/MQTTAdapterConstants.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/mqtt/internal/util/MQTTAdapterConstants.java @@ -29,9 +29,8 @@ public final class MQTTAdapterConstants { public static final String MQTT_ADAPTER_PROPERTY_USERNAME = "username"; public static final String MQTT_ADAPTER_PROPERTY_DCR_URL = "dcrUrl"; public static final String MQTT_ADAPTER_PROPERTY_SCOPES = "scopes"; - public static final String MQTT_ADAPTER_PROPERTY_PASSWORD = "password"; - public static final String MQTT_ADAPTER_PROPERTY_CLIENT_ID = "clientId"; public static final String MQTT_ADAPTER_PROPERTY_CLEAR_SESSION = "cleanSession"; public static final String MQTT_ADAPTER_PROPERTY_MESSAGE_QOS = "qos"; + public static final String MQTT_ADAPTER_PROPERTY_NAME = "mqtt.adapter.name"; } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/pom.xml index 3ca844b271c..f0a2cc79c5c 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/pom.xml +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/pom.xml @@ -115,6 +115,10 @@ + + org.apache.felix + maven-scr-plugin + org.apache.felix maven-bundle-plugin @@ -132,8 +136,14 @@ org.apache.commons.logging, org.osgi.service.component, + org.wso2.carbon.context, + org.wso2.carbon.device.mgt.common.operation.mgt, org.wso2.carbon.device.mgt.common.push.notification, - org.wso2.carbon.device.mgt.core.service + org.wso2.carbon.device.mgt.core.service, + org.wso2.carbon.event.output.adapter.core, + org.wso2.carbon.event.output.adapter.core.exception, + org.osgi.framework, + org.wso2.carbon.core diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/InvalidConfigurationException.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/InvalidConfigurationException.java new file mode 100644 index 00000000000..d0de9a178f9 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/InvalidConfigurationException.java @@ -0,0 +1,46 @@ +/* + * 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.extensions.push.notification.provider.xmpp; + +/** + * This exception is thrown whether the xmpp configured with invalid configuration. + */ +public class InvalidConfigurationException extends RuntimeException { + + public InvalidConfigurationException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public InvalidConfigurationException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidConfigurationException(String msg) { + super(msg); + } + + public InvalidConfigurationException() { + super(); + } + + public InvalidConfigurationException(Throwable cause) { + super(cause); + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/XMPPBasedPushNotificationProvider.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/XMPPBasedPushNotificationProvider.java index df3799408f5..29d75ed991b 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/XMPPBasedPushNotificationProvider.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/XMPPBasedPushNotificationProvider.java @@ -31,7 +31,7 @@ public class XMPPBasedPushNotificationProvider implements PushNotificationProvid @Override public NotificationStrategy getNotificationStrategy(PushNotificationConfig pushNotificationConfig) { - return null; + return new XMPPNotificationStrategy(pushNotificationConfig); } } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/XMPPNotificationStrategy.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/XMPPNotificationStrategy.java index a8d460c7c27..a885f11cd42 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/XMPPNotificationStrategy.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/XMPPNotificationStrategy.java @@ -18,12 +18,56 @@ */ package org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.push.notification.NotificationContext; import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationExecutionFailedException; +import org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp.internal.XMPPDataHolder; +import org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp.internal.util.XMPPAdapterConstants; +import org.wso2.carbon.event.output.adapter.core.MessageType; +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterConfiguration; +import org.wso2.carbon.event.output.adapter.core.exception.OutputEventAdapterException; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; public class XMPPNotificationStrategy implements NotificationStrategy { + private static final String XMPP_CLIENT_JID = "xmpp.client.jid"; + private static final String XMPP_CLIENT_SUBJECT = "xmpp.client.subject"; + public static final String XMPP_CLIENT_MESSAGE_TYPE = "xmpp.client.messageType"; + private String xmppAdapterName; + private static final Log log = LogFactory.getLog(XMPPNotificationStrategy.class); + + public XMPPNotificationStrategy(PushNotificationConfig config) { + + OutputEventAdapterConfiguration outputEventAdapterConfiguration = new OutputEventAdapterConfiguration(); + xmppAdapterName = config.getProperty(XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_NAME); + outputEventAdapterConfiguration.setName(xmppAdapterName); + outputEventAdapterConfiguration.setType(XMPPAdapterConstants.XMPP_ADAPTER_TYPE); + outputEventAdapterConfiguration.setMessageFormat(MessageType.TEXT); + Map xmppAdapterProperties = new HashMap<>(); + xmppAdapterProperties.put(XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_HOST, config.getProperty( + XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_HOST)); + xmppAdapterProperties.put(XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_PORT, config.getProperty( + XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_PORT)); + xmppAdapterProperties.put(XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_USERNAME, config.getProperty( + XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_USERNAME)); + xmppAdapterProperties.put(XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_PASSWORD, config.getProperty( + XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_PASSWORD)); + xmppAdapterProperties.put(XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_JID, config.getProperty( + XMPPAdapterConstants.XMPP_ADAPTER_PROPERTY_JID)); + outputEventAdapterConfiguration.setStaticProperties(xmppAdapterProperties); + try { + XMPPDataHolder.getInstance().getOutputEventAdapterService().create(outputEventAdapterConfiguration); + } catch (OutputEventAdapterException e) { + throw new InvalidConfigurationException("Error occurred while initializing MQTT output event adapter", e); + } + } + @Override public void init() { @@ -31,7 +75,13 @@ public class XMPPNotificationStrategy implements NotificationStrategy { @Override public void execute(NotificationContext ctx) throws PushNotificationExecutionFailedException { - + Map dynamicProperties = new HashMap<>(); + Properties properties = ctx.getOperation().getProperties(); + dynamicProperties.put("jid", properties.getProperty(XMPP_CLIENT_JID)); + dynamicProperties.put("subject", properties.getProperty(XMPP_CLIENT_SUBJECT)); + dynamicProperties.put("messageType", properties.getProperty(XMPP_CLIENT_MESSAGE_TYPE)); + XMPPDataHolder.getInstance().getOutputEventAdapterService().publish(xmppAdapterName, dynamicProperties, + ctx.getOperation().getPayLoad()); } @Override diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/XMPPDataHolder.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/XMPPDataHolder.java index e9f38543a27..a961a621bfd 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/XMPPDataHolder.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/XMPPDataHolder.java @@ -19,11 +19,13 @@ package org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp.internal; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; public class XMPPDataHolder { private DeviceManagementProviderService deviceManagementProviderService; private static XMPPDataHolder thisInstance = new XMPPDataHolder(); + private OutputEventAdapterService outputEventAdapterService; public static XMPPDataHolder getInstance() { return thisInstance; @@ -37,4 +39,12 @@ public class XMPPDataHolder { this.deviceManagementProviderService = deviceManagementProviderService; } + public void setOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService) { + this.outputEventAdapterService = outputEventAdapterService; + } + + public OutputEventAdapterService getOutputEventAdapterService() { + return outputEventAdapterService; + } + } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/XMPPPushNotificationServiceComponent.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/XMPPPushNotificationServiceComponent.java index 3cf153c09f2..7085191dc84 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/XMPPPushNotificationServiceComponent.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/XMPPPushNotificationServiceComponent.java @@ -22,6 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; +import org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService; /** * @scr.component name="org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.internal.XMPPPushNotificationServiceComponent" immediate="true" @@ -31,6 +32,12 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; * policy="dynamic" * bind="setDeviceManagementProviderService" * unbind="unsetDeviceManagementProviderService" + * @scr.reference name="event.output.adapter.service" + * interface="org.wso2.carbon.event.output.adapter.core.OutputEventAdapterService" + * cardinality="1..1" + * policy="dynamic" + * bind="setOutputEventAdapterService" + * unbind="unsetOutputEventAdapterService" */ public class XMPPPushNotificationServiceComponent { @@ -67,4 +74,11 @@ public class XMPPPushNotificationServiceComponent { XMPPDataHolder.getInstance().setDeviceManagementProviderService(deviceManagementProviderService); } + protected void setOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService){ + XMPPDataHolder.getInstance().setOutputEventAdapterService(outputEventAdapterService); + } + + protected void unsetOutputEventAdapterService(OutputEventAdapterService outputEventAdapterService){ + XMPPDataHolder.getInstance().setOutputEventAdapterService(null); + } } diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/util/XMPPAdapterConstants.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/util/XMPPAdapterConstants.java new file mode 100644 index 00000000000..db08e7ae8b9 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp/src/main/java/org/wso2/carbon/device/mgt/extensions/push/notification/provider/xmpp/internal/util/XMPPAdapterConstants.java @@ -0,0 +1,38 @@ +/* + * 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.extensions.push.notification.provider.xmpp.internal.util; + +public final class XMPPAdapterConstants { + + private XMPPAdapterConstants() { + throw new AssertionError(); + } + + public static final String XMPP_ADAPTER_TYPE = "xmpp"; + public static final String XMPP_ADAPTER_PROPERTY_PASSWORD = "password"; + public static final String XMPP_ADAPTER_PROPERTY_JID = "jid"; + public static final String SUBJECT_PROPERTY_KEY = "subject"; + public static final String MESSAGE_TYPE_PROPERTY_KEY = "messageType"; + public static final String CHAT_PROPERTY_KEY = "chat"; + public static final String XMPP_ADAPTER_PROPERTY_HOST = "host"; + public static final String XMPP_ADAPTER_PROPERTY_PORT = "port"; + public static final String XMPP_ADAPTER_PROPERTY_USERNAME = "username"; + public static final String XMPP_ADAPTER_PROPERTY_NAME = "xmpp.adapter.name"; + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Device.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Device.java index 0404e347577..6c998d636f5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Device.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/api/Device.java @@ -184,7 +184,7 @@ public interface Device { @ApiResponses(value = { @ApiResponse(code = 200, message = "List of devices based on the type"), @ApiResponse(code = 500, message = "Error occurred while fetching the list of device types") }) - @Permission(scope = "device-list", permissions = {"/permission/admin/device-mgt/admin/devices/list"}) + @Permission(scope = "device-list", permissions = {"/permission/admin/device-mgt/user/devices/list"}) Response getDeviceTypes(); /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index 5403bf2a1b9..d55f553920b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -19,13 +19,19 @@ package org.wso2.carbon.device.mgt.core; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier; import org.wso2.carbon.device.mgt.common.ProvisioningConfig; +import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; +import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; +import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.DeviceManagerStartupListener; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; +import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerRepository; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import java.util.Collections; @@ -37,8 +43,10 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis private Map providers; private boolean isInited; private static final Log log = LogFactory.getLog(DeviceManagementPluginRepository.class); + private OperationManagerRepository operationManagerRepository; public DeviceManagementPluginRepository() { + this.operationManagerRepository = new OperationManagerRepository(); providers = Collections.synchronizedMap(new HashMap()); DeviceManagementServiceComponent.registerStartupListener(this); } @@ -60,8 +68,9 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis provider.init(); DeviceManagerUtil.registerDeviceType(deviceType, tenantId, isSharedWithAllTenants); DeviceManagementDataHolder.getInstance().setRequireDeviceAuthorization(deviceType, - provider.getDeviceManager().requireDeviceAuthorization()); - + provider.getDeviceManager() + .requireDeviceAuthorization()); + registerPushNotificationStrategy(provider); } } catch (DeviceManagementException e) { throw new DeviceManagementException("Error occurred while adding device management provider '" + @@ -79,16 +88,17 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis public void removeDeviceManagementProvider(DeviceManagementService provider) throws DeviceManagementException { String deviceTypeName = provider.getType(); - + DeviceTypeIdentifier deviceTypeIdentifier; ProvisioningConfig provisioningConfig = provider.getProvisioningConfig(); if (provisioningConfig.isSharedWithAllTenants()) { - DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName); + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName); providers.remove(deviceTypeIdentifier); } else { int providerTenantId = DeviceManagerUtil.getTenantId(provisioningConfig.getProviderTenantDomain()); - DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName, providerTenantId); + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceTypeName, providerTenantId); providers.remove(deviceTypeIdentifier); } + operationManagerRepository.removeOperationManager(deviceTypeIdentifier); } public DeviceManagementService getDeviceManagementService(String type, int tenantId) { @@ -112,6 +122,46 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis return tenantProviders; } + private void registerPushNotificationStrategy(DeviceManagementService deviceManagementService) + throws DeviceManagementException { + PushNotificationConfig pushNoteConfig = deviceManagementService.getPushNotificationConfig(); + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( + deviceManagementService.getProvisioningConfig().getProviderTenantDomain(), true); + try { + boolean isSharedWithAllTenants = deviceManagementService.getProvisioningConfig().isSharedWithAllTenants(); + DeviceTypeIdentifier deviceTypeIdentifier; + if (isSharedWithAllTenants) { + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceManagementService.getType()); + } else { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceManagementService.getType(), tenantId); + } + if (pushNoteConfig != null) { + NotificationStrategy notificationStrategy = + DeviceManagementDataHolder.getInstance().getPushNotificationProviderRepository().getProvider( + pushNoteConfig.getType()).getNotificationStrategy(pushNoteConfig); + operationManagerRepository.addOperationManager(deviceTypeIdentifier, new OperationManagerImpl( + notificationStrategy)); + } else { + operationManagerRepository.addOperationManager(deviceTypeIdentifier, new OperationManagerImpl()); + } + } finally { + PrivilegedCarbonContext.endTenantFlow(); + } + } + + public OperationManager getOperationManager(String deviceType, int tenantId) { + //Priority need to be given to the tenant before public. + DeviceTypeIdentifier deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType, tenantId); + OperationManager operationManager = operationManagerRepository.getOperationManager(deviceTypeIdentifier); + if (operationManager == null) { + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType); + operationManager = operationManagerRepository.getOperationManager(deviceTypeIdentifier); + } + return operationManager; + } + @Override public void notifyObserver() { synchronized (providers) { @@ -122,6 +172,7 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis ProvisioningConfig provisioningConfig = provider.getProvisioningConfig(); int tenantId = DeviceManagerUtil.getTenantId(provisioningConfig.getProviderTenantDomain()); DeviceManagerUtil.registerDeviceType(provider.getType(), tenantId, provisioningConfig.isSharedWithAllTenants()); + registerPushNotificationStrategy(provider); //TODO: //This is a temporory fix. //windows and IOS cannot resolve user info by extracting certs diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 637e81cda46..7ddf0c27873 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -101,31 +101,28 @@ public class OperationManagerImpl implements OperationManager { return null; } - List enrolments = this.getEnrollmentsByStatus(deviceIds); try { - OperationManagementDAOFactory.beginTransaction(); org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation operationDto = OperationDAOUtil.convertOperation(operation); int operationId = this.lookupOperationDAO(operation).addOperation(operationDto); - for (EnrolmentInfo enrolmentInfo : enrolments) { + //TODO have to create a sql to load device details from deviceDAO using single query. + for (DeviceIdentifier deviceId : deviceIds) { + Device device = getDevice(deviceId); if (operationDto.getControl() == org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Control.NO_REPEAT) { - operationDAO.updateEnrollmentOperationsStatus(enrolmentInfo.getId(), operationDto.getCode(), + operationDAO.updateEnrollmentOperationsStatus(device.getEnrolmentInfo().getId(), operationDto.getCode(), org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.PENDING, org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.REPEATED); } - operationMappingDAO.addOperationMapping(operationId, enrolmentInfo.getId()); + operationMappingDAO.addOperationMapping(operationId, device.getId()); if (notificationStrategy != null) { try { - - notificationStrategy.execute(new NotificationContext( - new DeviceIdentifier(enrolmentInfo.getDevice().getDeviceIdentifier(), - enrolmentInfo.getDevice().getType()))); + notificationStrategy.execute(new NotificationContext(deviceId, operation)); } catch (PushNotificationExecutionFailedException e) { log.error("Error occurred while sending push notifications to " + - enrolmentInfo.getDevice().getType() + " device carrying id '" + - enrolmentInfo.getDevice().getDeviceIdentifier() + "'", e); + deviceId.getType() + " device carrying id '" + + deviceId + "'", e); } } } @@ -184,6 +181,23 @@ public class OperationManagerImpl implements OperationManager { return enrolments; } + private Device getDevice(DeviceIdentifier deviceId) throws OperationManagementException { + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + try { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDevice(deviceId, tenantId); + } catch (SQLException e) { + throw new OperationManagementException("Error occurred while opening a connection the data " + + "source", e); + } catch (DeviceManagementDAOException e) { + OperationManagementDAOFactory.rollbackTransaction(); + throw new OperationManagementException( + "Error occurred while retrieving device info", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + @Override public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { int enrolmentId; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerRepository.java index fb85d91fcac..c9c863593c3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerRepository.java @@ -18,6 +18,7 @@ */ package org.wso2.carbon.device.mgt.core.operation.mgt; +import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import java.util.Map; @@ -25,21 +26,21 @@ import java.util.concurrent.ConcurrentHashMap; public class OperationManagerRepository { - private Map operationManagers; + private Map operationManagers; public OperationManagerRepository() { operationManagers = new ConcurrentHashMap<>(); } - public void addOperationManager(String type, OperationManager operationManager) { + public void addOperationManager(DeviceTypeIdentifier type, OperationManager operationManager) { operationManagers.put(type, operationManager); } - public OperationManager getOperationManager(String type) { + public OperationManager getOperationManager(DeviceTypeIdentifier type) { return operationManagers.get(type); } - public void removeOperationManager(String type) { + public void removeOperationManager(DeviceTypeIdentifier type) { operationManagers.remove(type); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index e5a096fb1eb..75c1146b727 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -178,6 +178,17 @@ public interface DeviceManagementProviderService { */ PaginationResult getDevicesByStatus(PaginationRequest request) throws DeviceManagementException; + /** + * This method is used to check whether the device is enrolled with the give user. + * + * @param deviceId identifier of the device that needs to be checked against the user. + * @param user username of the device owner. + * + * @return true if the user owns the device else will return false. + * @throws DeviceManagementException If some unusual behaviour is observed while fetching the device. + */ + public boolean isEnrolled(DeviceIdentifier deviceId, String user) throws DeviceManagementException; + License getLicense(String deviceType, String languageCode) throws DeviceManagementException; void addLicense(String deviceType, License license) throws DeviceManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 4c10e1bee7f..e7d4f7622e7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -36,8 +36,6 @@ import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; -import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; -import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; @@ -49,8 +47,6 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; -import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; -import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerRepository; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.email.sender.core.ContentProviderInfo; import org.wso2.carbon.email.sender.core.EmailContext; @@ -75,11 +71,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private DeviceTypeDAO deviceTypeDAO; private EnrollmentDAO enrollmentDAO; private DeviceManagementPluginRepository pluginRepository; - private OperationManagerRepository operationManagerRepository; public DeviceManagementProviderServiceImpl() { this.pluginRepository = new DeviceManagementPluginRepository(); - this.operationManagerRepository = new OperationManagerRepository(); initDataAccessObjects(); /* Registering a listener to retrieve events when some device management service plugin is installed after * the component is done getting initialized */ @@ -819,59 +813,63 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Activity addOperation(String type, Operation operation, List devices) throws OperationManagementException { - return DeviceManagementDataHolder.getInstance().getOperationManager().addOperation(operation, devices); + return pluginRepository.getOperationManager(type, this.getTenantId()).addOperation(operation, devices); } @Override public List getOperations(DeviceIdentifier deviceId) throws OperationManagementException { - return DeviceManagementDataHolder.getInstance().getOperationManager().getOperations(deviceId); + return pluginRepository.getOperationManager(deviceId.getType(), this.getTenantId()).getOperations(deviceId); } @Override public PaginationResult getOperations(DeviceIdentifier deviceId, PaginationRequest request) throws OperationManagementException { - return DeviceManagementDataHolder.getInstance().getOperationManager().getOperations(deviceId, request); + return pluginRepository.getOperationManager(deviceId.getType(), this.getTenantId()) + .getOperations(deviceId, request); } @Override public List getPendingOperations(DeviceIdentifier deviceId) throws OperationManagementException { - return DeviceManagementDataHolder.getInstance().getOperationManager().getPendingOperations(deviceId); + return pluginRepository.getOperationManager(deviceId.getType(), this.getTenantId()) + .getPendingOperations(deviceId); } @Override public Operation getNextPendingOperation(DeviceIdentifier deviceId) throws OperationManagementException { - return DeviceManagementDataHolder.getInstance().getOperationManager().getNextPendingOperation(deviceId); + return pluginRepository.getOperationManager(deviceId.getType(), this.getTenantId()) + .getNextPendingOperation(deviceId); } @Override public void updateOperation(DeviceIdentifier deviceId, Operation operation) throws OperationManagementException { - DeviceManagementDataHolder.getInstance().getOperationManager().updateOperation(deviceId, operation); + pluginRepository.getOperationManager(deviceId.getType(), this.getTenantId()) + .updateOperation(deviceId, operation); } @Override public void deleteOperation(String type, int operationId) throws OperationManagementException { - DeviceManagementDataHolder.getInstance().getOperationManager().deleteOperation(operationId); + pluginRepository.getOperationManager(type, this.getTenantId()).deleteOperation(operationId); } @Override public Operation getOperationByDeviceAndOperationId(DeviceIdentifier deviceId, int operationId) throws OperationManagementException { - return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationByDeviceAndOperationId( - deviceId, operationId); + return pluginRepository.getOperationManager(deviceId.getType(), this.getTenantId()) + .getOperationByDeviceAndOperationId(deviceId, operationId); } @Override public List getOperationsByDeviceAndStatus( DeviceIdentifier deviceId, Operation.Status status) throws OperationManagementException, DeviceManagementException { - return DeviceManagementDataHolder.getInstance().getOperationManager().getOperationsByDeviceAndStatus( - deviceId, status); + return pluginRepository.getOperationManager(deviceId.getType(), this.getTenantId()) + .getOperationsByDeviceAndStatus(deviceId, status); } @Override public Operation getOperation(String type, int operationId) throws OperationManagementException { - return DeviceManagementDataHolder.getInstance().getOperationManager().getOperation(operationId); + return pluginRepository.getOperationManager(type, this.getTenantId()).getOperation(operationId); } @Override @@ -1155,17 +1153,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public void registerDeviceManagementService(DeviceManagementService deviceManagementService) { try { pluginRepository.addDeviceManagementProvider(deviceManagementService); - PushNotificationConfig pushNoteConfig = deviceManagementService.getPushNotificationConfig(); - if (pushNoteConfig != null) { - NotificationStrategy notificationStrategy = - DeviceManagementDataHolder.getInstance().getPushNotificationProviderRepository().getProvider( - pushNoteConfig.getType()).getNotificationStrategy(pushNoteConfig); - operationManagerRepository.addOperationManager( - deviceManagementService.getType(), new OperationManagerImpl(notificationStrategy)); - } else { - operationManagerRepository.addOperationManager( - deviceManagementService.getType(), new OperationManagerImpl()); - } } catch (DeviceManagementException e) { log.error("Error occurred while registering device management plugin '" + deviceManagementService.getType() + "'", e); @@ -1176,7 +1163,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public void unregisterDeviceManagementService(DeviceManagementService deviceManagementService) { try { pluginRepository.removeDeviceManagementProvider(deviceManagementService); - operationManagerRepository.removeOperationManager(deviceManagementService.getType()); } catch (DeviceManagementException e) { log.error("Error occurred while un-registering device management plugin '" + deviceManagementService.getType() + "'", e); @@ -1244,6 +1230,25 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return result; } + @Override + public boolean isEnrolled(DeviceIdentifier deviceId, String user) throws DeviceManagementException { + try { + DeviceManagementDAOFactory.openConnection(); + Device device = deviceDAO.getDevice(deviceId, this.getTenantId()); + if (device != null && device.getEnrolmentInfo().getOwner().equals(user)) { + return true; + } + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the enrollment information device for" + + "id '" + deviceId.getId() + "' and user : " + user, e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + return false; + } + private int getTenantId() { return CarbonContext.getThreadLocalCarbonContext().getTenantId(); } diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml index 227ec2e5750..416495b0614 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml @@ -28,6 +28,7 @@ org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm.GCMBasedPushNotificationProvider org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt.MQTTBasedPushNotificationProvider + org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp.XMPPBasedPushNotificationProvider https://localhost:9443 diff --git a/pom.xml b/pom.xml index d5708f77d70..fd0e907c7da 100644 --- a/pom.xml +++ b/pom.xml @@ -1054,6 +1054,11 @@ org.wso2.carbon.identity.application.common ${carbon.identity.version} + + org.wso2.carbon.identity + org.wso2.carbon.identity.oauth2.grant.jwt + ${identity.jwt.extension.version} + @@ -1865,6 +1870,9 @@ 2.0.1 1.5.8 2.5 + + + 1.0.2