100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
#20 Service to support push notifications in manager Add Notification Service to store Notification Token
Mica Hallin6cf664378ebd
diff --git a/manager/server/src/main/java/org/openremote/manager/server/Main.java b/manager/server/src/main/java/org/openremote/manager/server/Main.javaindex d6b2c2978..12ddc2333 100644--- a/manager/server/src/main/java/org/openremote/manager/server/Main.java+++ b/manager/server/src/main/java/org/openremote/manager/server/Main.java@@ -34,6 +34,7 @@ import org.openremote.manager.server.i18n.I18NService;import org.openremote.manager.server.map.MapService;import org.openremote.manager.server.rules.AssetRulesService;import org.openremote.manager.server.rules.RulesStorageService;+import org.openremote.manager.server.notification.NotificationService;import org.openremote.manager.server.security.ManagerIdentityService;import org.openremote.manager.server.setup.SetupService;import org.openremote.manager.server.web.ManagerWebService;@@ -65,6 +66,7 @@ public class Main {ServiceLoader.load(Protocol.class).forEach(this::add);addAll(Arrays.asList(new MapService(),+ new NotificationService(),new MessageBrokerService(),new ManagerWebService()));diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.javanew file mode 100644index 000000000..2b913a978--- /dev/null+++ b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationResourceImpl.java@@ -0,0 +1,37 @@+package org.openremote.manager.server.notification;++import elemental.json.JsonObject;+import org.openremote.container.web.WebResource;+import org.openremote.manager.server.security.ManagerIdentityService;+import org.openremote.manager.server.security.UserConfiguration;+import org.openremote.manager.shared.http.BadRequestException;+import org.openremote.manager.shared.http.RequestParams;+import org.openremote.manager.shared.notification.NotificationResource;++import javax.ws.rs.BeanParam;+import javax.ws.rs.FormParam;+import javax.ws.rs.Path;+import java.util.logging.Logger;++public class NotificationResourceImpl extends WebResource implements NotificationResource {++ private static final Logger LOG = Logger.getLogger(NotificationResourceImpl.class.getName());++ private ManagerIdentityService managerIdentityService = null;++ public NotificationResourceImpl(ManagerIdentityService managerIdentityService) {+ this.managerIdentityService = managerIdentityService;+ }+++ @Override+ @Path("notification_token")+ public void setNotificationToken(@BeanParam RequestParams requestParams, @FormParam("token") String token, @FormParam("device_id") String deviceId) {+ if (token == null || deviceId == null) {+ throw new BadRequestException();+ }+ UserConfiguration userConfiguration = managerIdentityService.getUserConfiguration(getRealm(), getAccessToken().getSubject());+ userConfiguration.getNotificationTokenForDeviceId().put(deviceId, token);+ managerIdentityService.mergeUserConfiguration(userConfiguration);+ }+}diff --git a/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.javanew file mode 100644index 000000000..eaf506a0b--- /dev/null+++ b/manager/server/src/main/java/org/openremote/manager/server/notification/NotificationService.java@@ -0,0 +1,51 @@+/*+ * Copyright 2016, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.manager.server.notification;++import org.openremote.container.Container;+import org.openremote.container.ContainerService;+import org.openremote.container.web.WebService;+import org.openremote.manager.server.security.ManagerIdentityService;++import java.util.logging.Logger;++public class NotificationService implements ContainerService {++ private static final Logger LOG = Logger.getLogger(NotificationService.class.getName());++ @Override+ public void init(Container container) throws Exception {+ container.getService(WebService.class).getApiSingletons().add(+ new NotificationResourceImpl(container.getService(ManagerIdentityService.class))+ );+ }++ @Override+ public void start(Container container) throws Exception {+ LOG.info("Starting Notification Service ...");+ }++ @Override+ public void stop(Container container) throws Exception {+ LOG.info("Stopping Notification Service ...");+ }+++}\ No newline at end of filediff --git a/manager/server/src/main/java/org/openremote/manager/server/security/ManagerIdentityService.java b/manager/server/src/main/java/org/openremote/manager/server/security/ManagerIdentityService.javaindex ec31803e8..41501e865 100644--- a/manager/server/src/main/java/org/openremote/manager/server/security/ManagerIdentityService.java+++ b/manager/server/src/main/java/org/openremote/manager/server/security/ManagerIdentityService.java@@ -207,4 +207,18 @@ public class ManagerIdentityService extends IdentityService {return getClass().getSimpleName() + "{" +'}';}++ public UserConfiguration getUserConfiguration(String realm, String subject) {+ UserKey userKey = new UserKey(realm,subject);+ UserConfiguration userConfiguration = persistenceService.doReturningTransaction(em -> em.find(UserConfiguration.class, userKey));+ if (userConfiguration == null) {+ LOG.info("No UserConfiguration Found, creating new one");+ userConfiguration = new UserConfiguration(userKey);+ }+ return userConfiguration;+ }++ public UserConfiguration mergeUserConfiguration(UserConfiguration userConfiguration) {+ return persistenceService.doReturningTransaction(em -> em.merge(userConfiguration));+ }}diff --git a/manager/server/src/main/java/org/openremote/manager/server/security/UserConfiguration.java b/manager/server/src/main/java/org/openremote/manager/server/security/UserConfiguration.javanew file mode 100644index 000000000..dcb8159c5--- /dev/null+++ b/manager/server/src/main/java/org/openremote/manager/server/security/UserConfiguration.java@@ -0,0 +1,96 @@+/*+ * Copyright 2016, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.manager.server.security;++import javax.persistence.*;+import java.io.Serializable;+import java.util.HashMap;+import java.util.Map;++@Entity+@Table(name = "USER_CONFIGURATION")+public class UserConfiguration {++ @EmbeddedId+ private UserKey userKey;++ @ElementCollection(fetch = FetchType.EAGER)+ @MapKeyColumn(name="DEVICE_ID")+ @Column(name="TOKEN")+ @CollectionTable(name="USER_CONFIGURATION_NOTIFICATION_TOKEN")+ private Map<String,String> notificationTokenForDeviceId = new HashMap<String, String>();++ private UserConfiguration() {};++ public UserConfiguration(UserKey userKey) {+ this.userKey = userKey;+ }+ public UserKey getUserKey() {+ return userKey;+ }++ public void setUserKey(UserKey userKey) {+ this.userKey = userKey;+ }++ public Map<String, String> getNotificationTokenForDeviceId() {+ return notificationTokenForDeviceId;+ }++ public void setNotificationTokenForDeviceId(Map<String, String> notificationTokenForDeviceId) {+ this.notificationTokenForDeviceId = notificationTokenForDeviceId;+ }+}++@Embeddable+class UserKey implements Serializable {++ @Column(name = "ID_REALM", nullable = false)+ private String realm;+++ @Column(name = "ID_USER_ID", nullable = false)+ private String subject;++ private UserKey() {+ }++ public UserKey(String realm, String subject) {+ this.realm = realm;+ this.subject = subject;+ }++ public String getRealm() {+ return realm;+ }++ public String getSubject() {+ return subject;+ }++ @Override+ public String toString() {+ return "UserKey{" ++ "realm='" + realm + '\'' ++ ", subject='" + subject + '\'' ++ '}';+ }++}\ No newline at end of filediff --git a/manager/server/src/main/resources/META-INF/persistence.xml b/manager/server/src/main/resources/META-INF/persistence.xmlindex c8b8d263b..64522e510 100644--- a/manager/server/src/main/resources/META-INF/persistence.xml+++ b/manager/server/src/main/resources/META-INF/persistence.xml@@ -10,6 +10,7 @@<class>org.openremote.container.persistence</class><class>org.openremote.manager.server.asset.ServerAsset</class>+ <class>org.openremote.manager.server.security.UserConfiguration</class><class>org.openremote.model.datapoint.AssetDatapoint</class><class>org.openremote.manager.shared.rules.GlobalRulesDefinition</class><class>org.openremote.manager.shared.rules.TenantRulesDefinition</class>diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java b/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.javanew file mode 100644index 000000000..901d126de--- /dev/null+++ b/manager/shared/src/main/java/org/openremote/manager/shared/notification/NotificationResource.java@@ -0,0 +1,40 @@+/*+ * Copyright 2016, OpenRemote Inc.+ *+ * See the CONTRIBUTORS.txt file in the distribution for a+ * full listing of individual contributors.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <http://www.gnu.org/licenses/>.+ */+package org.openremote.manager.shared.notification;++import jsinterop.annotations.JsType;+import org.openremote.manager.shared.http.RequestParams;+import org.openremote.manager.shared.http.SuccessStatusCode;++import javax.annotation.security.RolesAllowed;+import javax.ws.rs.BeanParam;+import javax.ws.rs.FormParam;+import javax.ws.rs.PUT;+import javax.ws.rs.Path;++@Path("notification")+@JsType(isNative = true)+public interface NotificationResource {++ @PUT+ @SuccessStatusCode(204)+ @RolesAllowed({"write:user"})+ void setNotificationToken(@BeanParam RequestParams requestParams,@FormParam("token") String token , @FormParam("device_id") String deviceId);+}diff --git a/manager/shared/src/main/java/org/openremote/manager/shared/security/ClientRole.java b/manager/shared/src/main/java/org/openremote/manager/shared/security/ClientRole.javaindex 677c071ab..b5658e25c 100644--- a/manager/shared/src/main/java/org/openremote/manager/shared/security/ClientRole.java+++ b/manager/shared/src/main/java/org/openremote/manager/shared/security/ClientRole.java@@ -36,6 +36,7 @@ public enum ClientRole {WRITE_ADMIN("write:admin", "Write system settings, tenants, and users"),WRITE_ASSETS("write:assets", "Write asset data"),+ WRITE_USER("write:user", "Write data of the authenticated user"),WRITE_RULES("write:rules", "Write rule definitions"),READ("read", "Read all data", new ClientRole[]{diff --git a/manager/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy b/manager/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovyindex e3efffed4..afd673131 100644--- a/manager/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy+++ b/manager/test/src/main/groovy/org/openremote/test/ManagerContainerTrait.groovy@@ -6,6 +6,7 @@ import org.openremote.container.ContainerServiceimport org.openremote.container.message.MessageBrokerServiceimport org.openremote.container.message.MessageBrokerSetupServiceimport org.openremote.container.persistence.PersistenceService+import org.openremote.manager.server.notification.NotificationServiceimport org.openremote.manager.server.asset.AssetProcessingServiceimport org.openremote.manager.server.rules.AssetRulesServiceimport org.openremote.manager.server.rules.RulesStorageService@@ -37,6 +38,7 @@ trait ManagerContainerTrait extends ContainerTrait {new AgentService(),*Lists.newArrayList(ServiceLoader.load(Protocol.class)),new MapService(),+ new NotificationService(),new MessageBrokerService(),new ManagerWebService(),*additionalServices