Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Manager: Introduced mail agent for connecting to mail servers (#1005)

Rich Turner677a3b113b1c
diff --git a/.ci_cd/env/.env b/.ci_cd/env/.env
index f537ea2c0..d8b4e0148 100644
--- a/.ci_cd/env/.env
+++ b/.ci_cd/env/.env
@@ -1,9 +1,6 @@
AWS_ACCOUNT_NAME=openremote
CLEAN_INSTALL=true
-DAILY_RESTART=true
DAILY_BACKUP=false
-ROLLBACK_ON_ERROR=true
ENV_COMPOSE_FILE=profile/demo.yml
-OR_SETUP_RUN_ON_RESTART=true
OR_SETUP_TYPE=demo
OR_EMAIL_TO="[email protected]"
diff --git a/.ci_cd/env/demo.env b/.ci_cd/env/demo.env
index badc232c4..8d31c4ef0 100644
--- a/.ci_cd/env/demo.env
+++ b/.ci_cd/env/demo.env
@@ -1,2 +1,4 @@
OR_HOSTNAME=demo.openremote.app
OR_ADDITIONAL_HOSTNAMES=demo.openremote.io
+OR_SETUP_RUN_ON_RESTART=true
+DAILY_RESTART=true
diff --git a/.ci_cd/env/staging.env b/.ci_cd/env/staging.env
index ae6171c49..f6ccf71e9 100644
--- a/.ci_cd/env/staging.env
+++ b/.ci_cd/env/staging.env
@@ -1,2 +1,4 @@
OR_HOSTNAME=staging.demo.openremote.app
OR_ADDITIONAL_HOSTNAMES=staging.demo.openremote.io
+OR_SETUP_RUN_ON_RESTART=true
+DAILY_RESTART=true
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailAgent.java b/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailAgent.java
new file mode 100644
index 000000000..3f0a747ae
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailAgent.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2023, 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.agent.protocol.mail;
+
+import org.openremote.model.asset.agent.Agent;
+import org.openremote.model.asset.agent.AgentLink;
+import org.openremote.model.auth.UsernamePassword;
+import org.openremote.model.value.AttributeDescriptor;
+import org.openremote.model.value.ValueType;
+
+import javax.validation.constraints.NotNull;
+import java.util.Optional;
+
+public abstract class AbstractMailAgent<T extends AbstractMailAgent<T, U, V>, U extends AbstractMailProtocol<T, U, V>, V extends AgentLink<V>> extends Agent<T, U, V> {
+
+ @NotNull
+ public static final AttributeDescriptor<String> PROTOCOL = new AttributeDescriptor<>("protocol", ValueType.TEXT);
+ @NotNull
+ public static final AttributeDescriptor<UsernamePassword> USERNAME_AND_PASSWORD = new AttributeDescriptor<>("usernamePassword", ValueType.USERNAME_AND_PASSWORD).withOptional(false);
+ @NotNull
+ public static final AttributeDescriptor<String> HOST = new AttributeDescriptor<>("host", ValueType.HOSTNAME_OR_IP_ADDRESS).withOptional(false);
+ @NotNull
+ public static final AttributeDescriptor<Integer> PORT = new AttributeDescriptor<>("port", ValueType.PORT).withOptional(false);
+ public static final AttributeDescriptor<Integer> CHECK_INTERVAL_SECONDS = new AttributeDescriptor<>("checkIntervalSeconds", ValueType.POSITIVE_INTEGER).withOptional(true);
+ public static final AttributeDescriptor<Boolean> DELETE_PROCESSED_MAIL = new AttributeDescriptor<>("deleteProcessedMail", ValueType.BOOLEAN).withOptional(true);
+ public static final AttributeDescriptor<Boolean> PREFER_HTML = new AttributeDescriptor<>("preferHTML", ValueType.BOOLEAN).withOptional(true);
+ public static final AttributeDescriptor<String> MAIL_FOLDER_NAME = new AttributeDescriptor<>("mailFolderName", ValueType.TEXT).withOptional(true);
+
+ protected AbstractMailAgent() {
+ }
+
+ public AbstractMailAgent(String name) {
+ super(name);
+ }
+
+ public Optional<String> getProtocol() {
+ return getAttributes().getValue(PROTOCOL);
+ }
+
+ @SuppressWarnings("unchecked")
+ public T setProtocol(String value) {
+ getAttributes().getOrCreate(PROTOCOL).setValue(value);
+ return (T) this;
+ }
+
+ public Optional<Integer> getCheckIntervalSeconds() {
+ return getAttributes().getValue(CHECK_INTERVAL_SECONDS);
+ }
+
+ @SuppressWarnings("unchecked")
+ public T setCheckIntervalSeconds(Integer value) {
+ getAttributes().getOrCreate(CHECK_INTERVAL_SECONDS).setValue(value);
+ return (T) this;
+ }
+
+ public Optional<Boolean> getDeleteProcessedMail() {
+ return getAttributes().getValue(DELETE_PROCESSED_MAIL);
+ }
+
+ @SuppressWarnings("unchecked")
+ public T setDeleteProcessedMail(Boolean value) {
+ getAttributes().getOrCreate(DELETE_PROCESSED_MAIL).setValue(value);
+ return (T) this;
+ }
+
+ public Optional<Boolean> getPreferHTML() {
+ return getAttributes().getValue(PREFER_HTML);
+ }
+
+ @SuppressWarnings("unchecked")
+ public T setPreferHTML(Boolean value) {
+ getAttributes().getOrCreate(PREFER_HTML).setValue(value);
+ return (T) this;
+ }
+
+ public Optional<String> getMailFolderName() {
+ return getAttributes().getValue(MAIL_FOLDER_NAME);
+ }
+
+ @SuppressWarnings("unchecked")
+ public T setMailFolderName(String value) {
+ getAttributes().getOrCreate(MAIL_FOLDER_NAME).setValue(value);
+ return (T) this;
+ }
+
+ @SuppressWarnings("unchecked")
+ public T setUsernamePassword(UsernamePassword value) {
+ super.setUsernamePassword(value);
+ return (T) this;
+ }
+
+ @SuppressWarnings("unchecked")
+ public T setHost(String value) {
+ super.setHost(value);
+ return (T) this;
+ }
+
+ @SuppressWarnings("unchecked")
+ public T setPort(Integer value) {
+ super.setPort(value);
+ return (T) this;
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailProtocol.java
new file mode 100644
index 000000000..2e72c80ac
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailProtocol.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2023, 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.agent.protocol.mail;
+
+import org.openremote.agent.protocol.AbstractProtocol;
+import org.openremote.container.persistence.PersistenceService;
+import org.openremote.model.Container;
+import org.openremote.model.asset.agent.AgentLink;
+import org.openremote.model.asset.agent.ConnectionStatus;
+import org.openremote.model.attribute.Attribute;
+import org.openremote.model.attribute.AttributeEvent;
+import org.openremote.model.attribute.AttributeRef;
+import org.openremote.model.attribute.AttributeState;
+import org.openremote.model.auth.UsernamePassword;
+import org.openremote.model.mail.MailMessage;
+import org.openremote.model.syslog.SyslogCategory;
+
+import java.nio.file.Path;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;
+
+public abstract class AbstractMailProtocol<T extends AbstractMailAgent<T, U, V>, U extends AbstractMailProtocol<T, U, V>, V extends AgentLink<V>> extends AbstractProtocol<T, V> {
+ protected MailClient mailClient;
+ protected ConcurrentMap<AttributeRef, Function<MailMessage, String>> attributeMessageProcessorMap = new ConcurrentHashMap<>();
+ protected static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, AbstractMailProtocol.class);
+ protected static int INITIAL_CHECK_DELAY_MILLIS = 10000;
+
+ public AbstractMailProtocol(T agent) {
+ super(agent);
+ }
+
+ @Override
+ protected void doStart(Container container) throws Exception {
+
+ UsernamePassword usernamePassword = getAgent().getUsernamePassword().orElseThrow();
+ Path storageDir = container.getService(PersistenceService.class).getStorageDir();
+
+ Path persistenceDir = storageDir.resolve("protocol").resolve("mail");
+
+ mailClient = new MailClientBuilder(
+ container.getExecutorService(),
+ getAgent().getProtocol().orElseThrow(),
+ getAgent().getHost().orElseThrow(),
+ getAgent().getPort().orElseThrow(),
+ usernamePassword.getUsername(),
+ usernamePassword.getPassword()
+ )
+ .setCheckIntervalMillis(
+ getAgent().getCheckIntervalSeconds().orElse(MailClientBuilder.DEFAULT_CHECK_INTERVAL_MILLIS)
+ )
+ .setDeleteMessageOnceProcessed(
+ getAgent().getDeleteProcessedMail().orElse(false)
+ )
+ .setFolder(getAgent().getMailFolderName().orElse(null))
+ .setPersistenceDir(persistenceDir)
+ // Set an initial delay to allow attributes to be linked before we read messages - not perfect but it should do
+ .setCheckInitialDelayMillis(INITIAL_CHECK_DELAY_MILLIS)
+ .setPreferHTML(getAgent().getPreferHTML().orElse(false))
+ .setEarliestMessageDate(agent.getCreatedOn())
+ .build();
+
+ mailClient.addConnectionListener(this::onConnectionEvent);
+ mailClient.addMessageListener(this::onMailMessage);
+ mailClient.connect();
+ }
+
+ @Override
+ protected void doStop(Container container) throws Exception {
+ if (mailClient != null) {
+ mailClient.disconnect();
+ }
+ setConnectionStatus(ConnectionStatus.STOPPED);
+ }
+
+ @Override
+ protected void doLinkAttribute(String assetId, Attribute<?> attribute, V agentLink) throws RuntimeException {
+
+ Predicate<MailMessage> messageFilter = getAttributeMailMessageFilter(assetId, attribute, agentLink);
+
+ Function<MailMessage, String> mailMessageProcessor = mailMessage -> {
+ if (messageFilter == null || messageFilter.test(mailMessage)) {
+ return getMailMessageAttributeValue(assetId, attribute, agentLink, mailMessage);
+ }
+
+ return null;
+ };
+
+ attributeMessageProcessorMap.put(new AttributeRef(assetId, attribute.getName()), mailMessageProcessor);
+ }
+
+ @Override
+ protected void doUnlinkAttribute(String assetId, Attribute<?> attribute, V agentLink) {
+ attributeMessageProcessorMap.remove(new AttributeRef(assetId, attribute.getName()));
+ }
+
+ @Override
+ protected void doLinkedAttributeWrite(Attribute<?> attribute, V agentLink, AttributeEvent event, Object processedValue) {
+ // Not supported
+ }
+
+ protected void onConnectionEvent(ConnectionStatus status) {
+ setConnectionStatus(status);
+ }
+
+ protected void onMailMessage(MailMessage mailMessage) {
+ attributeMessageProcessorMap.forEach(((attributeRef, mailMessageStringFunction) -> {
+ String value = mailMessageStringFunction.apply(mailMessage);
+ if (value != null) {
+ updateLinkedAttribute(new AttributeState(attributeRef, value));
+ } else if (LOG.isLoggable(Level.FINEST)) {
+ LOG.log(Level.FINEST, "MailMessage failed to match linked attribute:" + attributeRef + ", " + mailMessage);
+ }
+ }));
+ }
+
+ protected abstract Predicate<MailMessage> getAttributeMailMessageFilter(String assetId, Attribute<?> attribute, V agentLink);
+
+ protected abstract String getMailMessageAttributeValue(String assetId, Attribute<?> attribute, V agentLink, MailMessage mailMessage);
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mail/MailAgent.java b/agent/src/main/java/org/openremote/agent/protocol/mail/MailAgent.java
new file mode 100644
index 000000000..ab89603e8
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/MailAgent.java
@@ -0,0 +1,44 @@
+/*
+ * Copyright 2023, 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.agent.protocol.mail;
+
+import org.openremote.model.asset.agent.AgentDescriptor;
+
+import javax.persistence.Entity;
+
+@Entity
+public class MailAgent extends AbstractMailAgent<MailAgent, MailProtocol, MailAgentLink> {
+
+ public static final AgentDescriptor<MailAgent, MailProtocol, MailAgentLink> DESCRIPTOR = new AgentDescriptor<>(
+ MailAgent.class, MailProtocol.class, MailAgentLink.class
+ );
+
+ public MailAgent() {
+ }
+
+ public MailAgent(String name) {
+ super(name);
+ }
+
+ @Override
+ public MailProtocol getProtocolInstance() {
+ return new MailProtocol(this);
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mail/MailAgentLink.java b/agent/src/main/java/org/openremote/agent/protocol/mail/MailAgentLink.java
new file mode 100644
index 000000000..d3e39ae29
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/MailAgentLink.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright 2023, 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.agent.protocol.mail;
+
+import com.fasterxml.jackson.annotation.JsonPropertyDescription;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.openremote.model.asset.agent.AgentLink;
+import org.openremote.model.query.filter.StringPredicate;
+
+public class MailAgentLink extends AgentLink<MailAgentLink> {
+
+ @JsonPropertyDescription("The predicate to apply to incoming mail message subjects to determine if the message is" +
+ " intended for the linked attribute. This must be defined to enable attributes to be updated by the linked" +
+ " agent.")
+ protected StringPredicate subjectMatchPredicate;
+ @JsonPropertyDescription("The predicate to apply to incoming mail message from address(es) to determine if the" +
+ " message is intended for the linked attribute. This must be defined to enable attributes to be updated by the" +
+ " linked agent.")
+ protected StringPredicate fromMatchPredicate;
+
+ @JsonPropertyDescription("Use the subject as value instead of the body")
+ protected Boolean useSubject;
+
+ @JsonSerialize
+ protected String getType() {
+ return getClass().getSimpleName();
+ }
+
+ // For Hydrators
+ protected MailAgentLink() {}
+
+ public MailAgentLink(String id) {
+ super(id);
+ }
+
+ public StringPredicate getSubjectMatchPredicate() {
+ return subjectMatchPredicate;
+ }
+
+ public MailAgentLink setSubjectMatchPredicate(StringPredicate subjectMatchPredicate) {
+ this.subjectMatchPredicate = subjectMatchPredicate;
+ return this;
+ }
+
+ public StringPredicate getFromMatchPredicate() {
+ return fromMatchPredicate;
+ }
+
+ public MailAgentLink setFromMatchPredicate(StringPredicate fromMatchPredicate) {
+ this.fromMatchPredicate = fromMatchPredicate;
+ return this;
+ }
+
+ public Boolean getUseSubject() {
+ return useSubject;
+ }
+
+ public MailAgentLink setUseSubject(Boolean useSubject) {
+ this.useSubject = useSubject;
+ return this;
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mail/MailClient.java b/agent/src/main/java/org/openremote/agent/protocol/mail/MailClient.java
new file mode 100644
index 000000000..24a43bcb2
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/MailClient.java
@@ -0,0 +1,350 @@
+/*
+ * Copyright 2023, 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.agent.protocol.mail;
+
+import io.undertow.util.Headers;
+import org.openremote.container.util.MailUtil;
+import org.openremote.model.asset.agent.ConnectionStatus;
+import org.openremote.model.mail.MailMessage;
+import org.openremote.model.syslog.SyslogCategory;
+
+import javax.mail.*;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import java.util.logging.Level;
+
+public class MailClient {
+
+ public static final System.Logger LOG = System.getLogger(MailClient.class.getName() + "." + SyslogCategory.AGENT.name());
+ protected MailClientBuilder config;
+ protected Session session;
+ protected Date lastMessageDate;
+ protected Future<?> mailChecker;
+ protected boolean persistenceFileAccessible;
+ protected final AtomicBoolean connected = new AtomicBoolean(false);
+ protected List<Consumer<ConnectionStatus>> connectionListeners = new CopyOnWriteArrayList<>();
+ protected List<Consumer<MailMessage>> messageListeners = new CopyOnWriteArrayList<>();
+
+ MailClient(MailClientBuilder config) {
+ this.config = config;
+
+ if (config.getPersistenceDir() != null) {
+ if (!Files.exists(config.getPersistenceDir())) {
+ LOG.log(System.Logger.Level.INFO, "Persistence directory doesn't exist, attempting to create it: " + config.getPersistenceDir());
+ try {
+ Files.createDirectories(config.getPersistenceDir());
+ } catch (IOException e) {
+ LOG.log(System.Logger.Level.INFO, "Persistence directory creation failed", e);
+ }
+ } else if (!Files.isDirectory(config.getPersistenceDir())) {
+ LOG.log(System.Logger.Level.INFO, "Persistence directory is not a directory: " + config.getPersistenceDir());
+ } else {
+ persistenceFileAccessible = true;
+ lastMessageDate = readLastMessageDate(config);
+ }
+ }
+
+ if (config.getEarliestMessageDate() != null && (lastMessageDate == null || lastMessageDate.before(config.getEarliestMessageDate()))) {
+ lastMessageDate = config.getEarliestMessageDate();
+ }
+
+ this.session = Session.getInstance(config.getProperties());
+ }
+
+ public boolean connect() {
+ synchronized (connected) {
+ if (connected.get()) {
+ return true;
+ }
+ return doConnect();
+ }
+ }
+
+ protected boolean doConnect() {
+
+ // Try and connect to the mailbox folder
+ try {
+ withFolder((folder) -> {
+ connected.set(true);
+ mailChecker = config.getScheduledExecutorService().scheduleWithFixedDelay(this::checkForMessages, config.getCheckInitialDelayMillis(), config.getCheckIntervalMillis(), TimeUnit.MILLISECONDS);
+ });
+
+ updateConnectionStatus(ConnectionStatus.CONNECTED);
+ return true;
+ } catch (Exception e) {
+ updateConnectionStatus(ConnectionStatus.ERROR);
+ return false;
+ }
+
+ // For IMAP folders we can use listener for new messages for POP3 we need to open/close the folder
+ // we'll just use a mechanism that should work for both for now
+// emailFolder.addMessageCountListener(new MessageCountAdapter() {
+// public void messagesAdded(MessageCountEvent ev) {
+// Message[] messages = ev.getMessages();
+// LOG.log(System.Logger.Level.TRACE, "Got " + messages.length + " messages");
+//
+// if (!initialised) {
+// initialised = true;
+// // Filter out earlier messages on first pass
+// Arrays.stream(messages)
+// .filter(message -> {
+// try {
+// if (lastMessageDate != null) {
+// if (message.getReceivedDate().before(lastMessageDate)) {
+// return false;
+// }
+// }
+// } catch (MessagingException e) {
+// LOG.log(System.Logger.Level.TRACE, "Failed to read message received date", e);
+// return false;
+// }
+// return true;
+// })
+// .forEach(message -> onMessage(message));
+// } else {
+// Arrays.stream(messages).forEach(message -> onMessage(message));
+// }
+// }
+// });
+ }
+
+ protected void withFolder(Consumer<Folder> folderConsumer) throws Exception {
+ try {
+ LOG.log(System.Logger.Level.INFO, "Connecting to mail server: " + config.getHost());
+
+ try (Store mailStore = session.getStore()) {
+ mailStore.connect(config.getUser(), config.getPassword());
+ Folder mailFolder = mailStore.getFolder(config.getFolder());
+
+ try {
+ if (mailFolder == null || !mailFolder.exists()) {
+ LOG.log(System.Logger.Level.WARNING, "Mailbox folder doesn't exist: " + config.getFolder());
+ throw new Exception();
+ }
+
+ mailFolder.open(config.isDeleteMessageOnceProcessed() ? Folder.READ_WRITE : Folder.READ_ONLY);
+ folderConsumer.accept(mailFolder);
+ } finally {
+ if (mailFolder != null) {
+ if (mailFolder.isOpen()) {
+ if (config.isDeleteMessageOnceProcessed()) {
+ mailFolder.close(true);
+ } else {
+ mailFolder.close();
+ }
+ }
+ }
+ }
+ }
+ } catch (Exception e) {
+ if (e instanceof AuthenticationFailedException) {
+ LOG.log(System.Logger.Level.ERROR, "Failed to connect to mailbox due to auth issue", e);
+ } else if (e instanceof MessagingException) {
+ LOG.log(System.Logger.Level.ERROR, "Failed to connect to mailbox", e);
+ }
+ throw e;
+ }
+ }
+
+ public void disconnect() {
+ synchronized (connected) {
+
+ if (!connected.get()) {
+ return;
+ }
+
+ LOG.log(System.Logger.Level.INFO, "Disconnecting server: " + config.getHost());
+ mailChecker.cancel(true);
+ mailChecker = null;
+ connected.set(false);
+ updateConnectionStatus(ConnectionStatus.DISCONNECTED);
+ }
+ }
+
+ public boolean isConnected() {
+ return connected.get();
+ }
+
+ public void addConnectionListener(Consumer<ConnectionStatus> listener) {
+ connectionListeners.add(listener);
+ }
+
+ public void removeConnectionListener(Consumer<ConnectionStatus> listener) {
+ connectionListeners.remove(listener);
+ }
+
+ public void addMessageListener(Consumer<MailMessage> listener) {
+ messageListeners.add(listener);
+ }
+
+ public void removeMessageListener(Consumer<MailMessage> listener) {
+ messageListeners.remove(listener);
+ }
+
+ protected void updateConnectionStatus(ConnectionStatus connectionStatus) {
+ connectionListeners.forEach(listener -> listener.accept(connectionStatus));
+ }
+
+ protected void onMessage(MailMessage message) {
+ try {
+ messageListeners.forEach(listener -> listener.accept(message));
+ } catch (Exception e) {
+ LOG.log(System.Logger.Level.ERROR, "Failed to process received message", e);
+ }
+ }
+
+ protected void checkForMessages() {
+
+ try {
+ withFolder((emailFolder) -> {
+ updateConnectionStatus(ConnectionStatus.CONNECTED);
+ try {
+ Message[] messages = emailFolder.getMessages();
+
+ // Fetch profile only works for IMAP - For POP3 the headers will be loaded and cached once we read one of them
+ FetchProfile fp = new FetchProfile();
+ fp.add(FetchProfile.Item.ENVELOPE);
+ fp.add(Headers.CONTENT_TYPE_STRING);
+ emailFolder.fetch(messages, fp);
+ Date lastRunLastMessageDate = lastMessageDate;
+
+ Arrays.stream(messages)
+ .filter(message -> {
+ try {
+ if (message.getFlags().contains(Flags.Flag.SEEN)) {
+ LOG.log(System.Logger.Level.TRACE, "Message has already been seen so ignoring: num=" + message.getMessageNumber());
+ return false;
+ }
+ if (lastRunLastMessageDate != null) {
+ Date sentDate = message.getSentDate();
+
+ if (sentDate.before(lastRunLastMessageDate) || sentDate.equals(lastRunLastMessageDate)) {
+ LOG.log(System.Logger.Level.TRACE, "Message is older than last message date so ignoring: " + messageToString(message));
+ return false;
+ }
+ }
+ } catch (MessagingException e) {
+ LOG.log(System.Logger.Level.TRACE, "Failed to read message details: num=" + message.getMessageNumber());
+ return false;
+ }
+
+ return true;
+ })
+ .forEach(message -> {
+ try {
+ MailMessage mailMessage = MailUtil.toMailMessage(message, config.isPreferHTML());
+
+ if (mailMessage == null) {
+ LOG.log(System.Logger.Level.INFO, "Unsupported mail message only messages with a text/* part are supported:" + messageToString(message));
+ } else {
+ onMessage(mailMessage);
+
+ if (config.isDeleteMessageOnceProcessed()) {
+ message.setFlag(Flags.Flag.DELETED, true);
+ }
+ if (lastMessageDate == null || message.getSentDate().after(lastMessageDate)) {
+ lastMessageDate = message.getSentDate();
+ }
+ }
+ } catch (IOException e) {
+ LOG.log(System.Logger.Level.INFO, "Failed to read message content: " + messageToString(message), e);
+ } catch (MessagingException e) {
+ LOG.log(System.Logger.Level.INFO, "An exception occurred whilst processing a message: " + messageToString(message), e);
+ }
+ });
+
+ if (persistenceFileAccessible && lastMessageDate != null && (lastRunLastMessageDate == null || lastMessageDate.after(lastRunLastMessageDate))) {
+ writeLastMessageDate(config, lastMessageDate);
+ }
+ } catch (MessagingException e) {
+ LOG.log(System.Logger.Level.WARNING, "An error occurred whilst trying to read mail", e);
+ }
+ });
+ updateConnectionStatus(ConnectionStatus.WAITING);
+ } catch (Exception e) {
+ LOG.log(System.Logger.Level.WARNING, "An error occurred whilst processing mail messages", e);
+ updateConnectionStatus(ConnectionStatus.ERROR);
+ }
+ }
+
+ public MailClientBuilder getConfig() {
+ return config;
+ }
+
+ protected static Date readLastMessageDate(MailClientBuilder config) {
+ Path filePath = getLastMessageFilePath(config);
+ LOG.log(System.Logger.Level.INFO, "Trying to read last message date from: " + filePath.toAbsolutePath());
+
+ if (!Files.exists(filePath)) {
+ LOG.log(System.Logger.Level.INFO, "Last message date file does not currently exist");
+ return null;
+ }
+
+ try {
+ String millisStr = Files.readString(filePath);
+ return new Date(Long.parseLong(millisStr));
+ } catch (IOException e) {
+ LOG.log(System.Logger.Level.WARNING, "Failed to read last message date from: " + filePath, e);
+ }
+
+ return null;
+ }
+
+ protected static boolean writeLastMessageDate(MailClientBuilder config, Date lastMessageDate) {
+ Path filePath = getLastMessageFilePath(config);
+ LOG.log(System.Logger.Level.INFO, "Trying to write last message date to: " + filePath.toAbsolutePath());
+ try {
+ Files.writeString(filePath, Long.toString(lastMessageDate.getTime()), StandardOpenOption.CREATE, StandardOpenOption.WRITE);
+ return true;
+ } catch (IOException e) {
+ LOG.log(System.Logger.Level.WARNING, "Failed to write last message date to: " + filePath, e);
+ }
+ return false;
+ }
+
+ protected static final Path getLastMessageFilePath(MailClientBuilder config) {
+ String fileName = config.getHost() + "." + config.getPort() + "." + config.getUser();
+ return config.getPersistenceDir().resolve(fileName);
+ }
+
+ public static final String messageToString(Message message) {
+ String subject = null;
+ Date sentDate = null;
+
+ try {
+ subject = message.getSubject();
+ sentDate = message.getSentDate();
+ } catch (MessagingException e) {
+ LOG.log(System.Logger.Level.TRACE, "Message to string failed to extract basic message headers");
+ }
+
+ return "Message{subject=" + subject + ", sentDate=" + sentDate + "}";
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mail/MailClientBuilder.java b/agent/src/main/java/org/openremote/agent/protocol/mail/MailClientBuilder.java
new file mode 100644
index 000000000..898ea8a01
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/MailClientBuilder.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2023, 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.agent.protocol.mail;
+
+import java.nio.file.Path;
+import java.util.Date;
+import java.util.Properties;
+import java.util.concurrent.ScheduledExecutorService;
+
+public class MailClientBuilder {
+ public static final String DEFAULT_FOLDER_NAME = "INBOX";
+ public static final int DEFAULT_CHECK_INTERVAL_MILLIS = 5 * 60000;
+ protected static int MIN_CHECK_INTERVAL_MILLIS = 10000;
+ protected ScheduledExecutorService scheduledExecutorService;
+ protected int checkInitialDelayMillis = 0;
+ protected int checkIntervalMillis = DEFAULT_CHECK_INTERVAL_MILLIS;
+ protected String folder;
+ protected boolean preferHTML;
+ protected Date earliestMessageDate;
+ protected Path persistenceDir;
+ protected String protocol;
+ protected String host;
+ protected int port;
+ protected String user;
+ protected String password;
+ protected boolean deleteMessageOnceProcessed;
+ protected Properties properties = new Properties();
+
+ public MailClientBuilder(ScheduledExecutorService scheduledExecutorService, String protocol, String host, int port, String user, String password) {
+ this.scheduledExecutorService = scheduledExecutorService;
+ this.protocol = protocol;
+ this.host = host;
+ this.port = port;
+ this.user = user;
+ this.password = password;
+
+ properties.put("mail.store.protocol", protocol);
+ properties.put("mail." + protocol + ".host", host);
+ properties.put("mail." + protocol + ".port", port);
+ }
+
+ public MailClientBuilder setCheckIntervalMillis(int checkIntervalMillis) {
+ this.checkIntervalMillis = Math.max(checkIntervalMillis, MIN_CHECK_INTERVAL_MILLIS);
+ return this;
+ }
+
+ public MailClientBuilder setCheckInitialDelayMillis(int checkInitialDelayMillis) {
+ this.checkInitialDelayMillis = Math.max(checkInitialDelayMillis, 0);
+ return this;
+ }
+
+ public MailClientBuilder setFolder(String folder) {
+ this.folder = folder;
+ return this;
+ }
+
+ public MailClientBuilder setEarliestMessageDate(Date earliestMessageDate) {
+ this.earliestMessageDate = earliestMessageDate;
+ return this;
+ }
+
+ /**
+ * Directory to store information about received message dates between restarts
+ */
+ public MailClientBuilder setPersistenceDir(Path persistenceDir) {
+ this.persistenceDir = persistenceDir;
+ return this;
+ }
+
+ public MailClientBuilder setDeleteMessageOnceProcessed(boolean deleteMessageOnceProcessed) {
+ this.deleteMessageOnceProcessed = deleteMessageOnceProcessed;
+ return this;
+ }
+
+ public MailClientBuilder setProperty(String property, Object value) {
+ properties.put(property, value);
+ return this;
+ }
+
+ public MailClientBuilder setPreferHTML(boolean preferHTML) {
+ this.preferHTML = preferHTML;
+ return this;
+ }
+
+ public String getFolder() {
+ return folder != null ? folder : DEFAULT_FOLDER_NAME;
+ }
+
+ public ScheduledExecutorService getScheduledExecutorService() {
+ return scheduledExecutorService;
+ }
+
+ public int getCheckIntervalMillis() {
+ return checkIntervalMillis;
+ }
+
+ public int getCheckInitialDelayMillis() {
+ return checkInitialDelayMillis;
+ }
+
+ public Date getEarliestMessageDate() {
+ return earliestMessageDate;
+ }
+
+ public Path getPersistenceDir() {
+ return persistenceDir;
+ }
+
+ public String getProtocol() {
+ return protocol;
+ }
+
+ public String getHost() {
+ return host;
+ }
+
+ public int getPort() {
+ return port;
+ }
+
+ public String getUser() {
+ return user;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public boolean isDeleteMessageOnceProcessed() {
+ return deleteMessageOnceProcessed;
+ }
+
+ public boolean isPreferHTML() {
+ return preferHTML;
+ }
+
+ public Properties getProperties() {
+ return properties;
+ }
+
+ public MailClient build() {
+ return new MailClient(this);
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mail/MailProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/mail/MailProtocol.java
new file mode 100644
index 000000000..86687bff2
--- /dev/null
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/MailProtocol.java
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2023, 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.agent.protocol.mail;
+
+import org.openremote.model.attribute.Attribute;
+import org.openremote.model.auth.UsernamePassword;
+import org.openremote.model.mail.MailMessage;
+import org.openremote.model.query.filter.StringPredicate;
+
+import java.util.Arrays;
+import java.util.function.Predicate;
+
+public class MailProtocol extends AbstractMailProtocol<MailAgent, MailProtocol, MailAgentLink> {
+
+ public static final String PROTOCOL_DISPLAY_NAME = "Mail Client";
+
+ public MailProtocol(MailAgent agent) {
+ super(agent);
+ }
+
+
+ @Override
+ public String getProtocolName() {
+ return PROTOCOL_DISPLAY_NAME;
+ }
+
+ @Override
+ public String getProtocolInstanceUri() {
+ return "mailClient://" + agent.getHost().orElse("") + ":" + agent.getPort().map(Object::toString).orElse("") + "?username=" + agent.getUsernamePassword().map(UsernamePassword::getUsername).orElse("");
+ }
+
+ @Override
+ protected String getMailMessageAttributeValue(String assetId, Attribute<?> attribute, MailAgentLink agentLink, MailMessage mailMessage) {
+ boolean useSubject = agentLink.getUseSubject() != null && agentLink.getUseSubject();
+ return useSubject ? mailMessage.getSubject() : mailMessage.getContent();
+ }
+
+ @Override
+ protected Predicate<MailMessage> getAttributeMailMessageFilter(String assetId, Attribute<?> attribute, MailAgentLink agentLink) {
+
+ StringPredicate fromPredicate = agentLink.getFromMatchPredicate();
+ StringPredicate subjectPredicate = agentLink.getSubjectMatchPredicate();
+
+ Predicate<MailMessage> messageFilter = null;
+
+ // Do message filtering
+ if (fromPredicate != null) {
+ Predicate<Object> valuePredicate = fromPredicate.asPredicate(timerService::getCurrentTimeMillis);
+ messageFilter = m -> {
+ String[] froms = m.getFrom();
+ return Arrays.stream(froms).anyMatch(valuePredicate);
+ };
+ }
+ if (subjectPredicate != null) {
+ Predicate<Object> valuePredicate = subjectPredicate.asPredicate(timerService::getCurrentTimeMillis);
+ Predicate<MailMessage> subjectFilter = m -> {
+ String subject = m.getSubject();
+ return valuePredicate.test(subject);
+ };
+ messageFilter = messageFilter != null ? messageFilter.and(subjectFilter) : subjectFilter;
+ }
+
+ return messageFilter;
+ }
+}
diff --git a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgentLink.java b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgentLink.java
index e84ccfeed..27dbdd198 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgentLink.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mqtt/MQTTAgentLink.java
@@ -37,7 +37,7 @@ public class MQTTAgentLink extends AgentLink<MQTTAgentLink> {
// For Hydrators
protected MQTTAgentLink() {}
- protected MQTTAgentLink(String id) {
+ public MQTTAgentLink(String id) {
super(id);
}
diff --git a/container/src/main/java/org/openremote/container/util/MailUtil.java b/container/src/main/java/org/openremote/container/util/MailUtil.java
new file mode 100644
index 000000000..2dd16d91d
--- /dev/null
+++ b/container/src/main/java/org/openremote/container/util/MailUtil.java
@@ -0,0 +1,117 @@
+/*
+ * Copyright 2023, 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.container.util;
+
+import org.openremote.model.mail.MailMessage;
+
+import javax.mail.*;
+import javax.mail.internet.InternetAddress;
+
+import java.io.IOException;
+import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+
+public class MailUtil {
+
+ protected static class MessageContent {
+
+ protected List<Header> headers;
+ protected String mimeType;
+ protected String content;
+
+ protected MessageContent(String mimeType, String content, List<Header> headers) {
+ this.mimeType = mimeType;
+ this.content = content;
+ this.headers = headers;
+ }
+ }
+
+ protected MailUtil() {
+ }
+
+ public static MailMessage toMailMessage(Message message, boolean preferHTML) throws MessagingException, IOException {
+ MessageContent messageContent = getMessageContent(message, new ArrayList<>(), true, preferHTML);
+ if (messageContent == null) {
+ return null;
+ }
+
+ List<Header> headers = messageContent.headers;
+ Map<String, List<String>> headerStrings = headers.stream()
+ .collect(Collectors.groupingBy(
+ Header::getName,
+ Collectors.mapping(Header::getValue, Collectors.toList())));
+
+ return new MailMessage(
+ messageContent.content,
+ messageContent.mimeType,
+ headerStrings,
+ message.getSubject(),
+ message.getSentDate(),
+ Arrays.stream(message.getFrom()).map(a -> ((InternetAddress)a).getAddress()).toArray(String[]::new));
+ }
+
+ protected static MessageContent getMessageContent(Part p, List<Header> headers, boolean isTopLevel, boolean preferHTML) throws MessagingException, IOException {
+
+ if (isTopLevel) {
+ headers.addAll(Collections.list(p.getAllHeaders()));
+ }
+
+ if (p.isMimeType("text/*")) {
+ String s = (String)p.getContent();
+ String mimeType = p.getContentType();
+ if (!isTopLevel) {
+ headers.addAll(Collections.list(p.getAllHeaders()));
+ }
+ return new MessageContent(mimeType, s, headers);
+ }
+
+ if (p.isMimeType("multipart/*")) {
+
+ // This mimetype means user agent should pick the most favourable message part
+ // use preferHTML option to determine which part the return if any
+ boolean isAlternative = p.isMimeType("multipart/alternative");
+
+ Multipart mp = (Multipart) p.getContent();
+ MessageContent nonPreferredContent = null;
+
+ for (int i = 0; i < mp.getCount(); i++) {
+ Part bp = mp.getBodyPart(i);
+ List<Header> partHeaders = new ArrayList<>();
+ MessageContent partContent = getMessageContent(bp, partHeaders, false, preferHTML);
+ boolean returnPart;
+
+ if (partContent != null) {
+ returnPart = !isAlternative || (preferHTML && partContent.mimeType.startsWith("text/html")) || (!preferHTML && partContent.mimeType.startsWith("text/plain"));
+
+ if (returnPart) {
+ // Add any top level headers
+ partContent.headers.addAll(headers);
+ return partContent;
+ } else if (nonPreferredContent == null) {
+ nonPreferredContent = partContent;
+ }
+ }
+ }
+ }
+
+ return null;
+ }
+}
diff --git a/gradle.properties b/gradle.properties
index 51a30710a..0d511e8e1 100644
--- a/gradle.properties
+++ b/gradle.properties
@@ -39,7 +39,6 @@ ical4jVersion = 3.2.6
friendlyIdVersion = 1.0.1
geotoolsVersion = 19.1
firebaseAdminVersion = 9.1.1
-simpleJavaMailVersion = 5.0.4
jaywayJsonPathVersion = 2.7.0
typescriptGeneratorVersion = 2.28.785
californiumCoreVersion = 2.1.0
@@ -58,3 +57,4 @@ snapshotsRepoUrl=https://s01.oss.sonatype.org/content/repositories/snapshots/
releasesRepoUrl=https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/
activeMQVersion = 2.26.0
jbossLoggingManagerVersion=2.1.19.Final
+greenmailVersion=1.6.7
diff --git a/manager/build.gradle b/manager/build.gradle
index 7163b2fb6..5a23b4ff3 100644
--- a/manager/build.gradle
+++ b/manager/build.gradle
@@ -20,7 +20,6 @@ dependencies {
implementation "org.geotools:gt-main:$geotoolsVersion"
implementation "com.google.firebase:firebase-admin:$firebaseAdminVersion"
- implementation "org.simplejavamail:simple-java-mail:$simpleJavaMailVersion"
implementation "io.swagger.core.v3:swagger-jaxrs2:$swaggerVersion"
implementation "org.openjdk.nashorn:nashorn-core:$nashornVersion"
diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
index 2be99396e..691806ce3 100644
--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java
+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java
@@ -55,6 +55,7 @@ import org.openremote.model.util.TextUtil;
import javax.persistence.EntityManager;
import java.util.*;
+import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.Consumer;
@@ -96,7 +97,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
protected ClientEventService clientEventService;
protected GatewayService gatewayService;
protected ScheduledExecutorService executorService;
- protected Map<String, Agent<?, ?, ?>> agentMap;
+ protected ConcurrentMap<String, Agent<?, ?, ?>> agentMap;
protected final Map<String, Future<Void>> agentDiscoveryImportFutureMap = new HashMap<>();
protected final Map<String, Protocol<?>> protocolInstanceMap = new HashMap<>();
protected final Map<String, List<Consumer<PersistenceEvent<Asset<?>>>>> childAssetSubscriptions = new HashMap<>();
@@ -377,7 +378,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
LOG.fine("Agent is disabled so not starting: " + agent);
sendAttributeEvent(new AttributeEvent(agent.getId(), Agent.STATUS.getName(), ConnectionStatus.DISABLED));
} else {
- this.startAgent(agent);
+ executorService.execute(() -> this.startAgent(agent));
}
}
@@ -416,8 +417,6 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
.orElse(false)
).forEach((agnt, attributes) -> linkAttributes(agnt, asset.getId(), attributes))
);
-
-
} catch (Exception e) {
if (protocol != null) {
try {
@@ -609,37 +608,30 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse
agent = loadedAgent;
}
- Agent<?, ?, ?> finalAgent = agent;
- withLock(getClass().getSimpleName() + "::addReplaceAgent", () -> getAgents().put(finalAgent.getId(), finalAgent));
+ getAgents().put(agent.getId(), agent);
return agent;
}
- @SuppressWarnings("ConstantConditions")
+ @SuppressWarnings({"BooleanMethodIsAlwaysInverted"})
protected boolean removeAgent(String agentId) {
- return withLockReturning(getClass().getSimpleName() + "::removeAgent", () -> getAgents().remove(agentId) != null);
+ return getAgents().remove(agentId) != null;
}
public Agent<?, ?, ?> getAgent(String agentId) {
return getAgents().get(agentId);
}
- protected Map<String, Agent<?, ?, ?>> getAgents() {
+ protected synchronized Map<String, Agent<?, ?, ?>> getAgents() {
- if (agentMap != null) {
- return agentMap;
+ if (agentMap == null) {
+ agentMap = assetStorageService.findAll(
+ new AssetQuery().types(Agent.class)
+ )
+ .stream()
+ .filter(asset -> gatewayService.getLocallyRegisteredGatewayId(asset.getId(), null) == null)
+ .collect(Collectors.toConcurrentMap(Asset::getId, agent -> (Agent<?, ?, ?>) agent));
}
-
- return withLockReturning(getClass().getSimpleName() + "::getAgents", () -> {
- if (agentMap == null) {
- agentMap = assetStorageService.findAll(
- new AssetQuery().types(Agent.class)
- )
- .stream()
- .filter(asset -> gatewayService.getLocallyRegisteredGatewayId(asset.getId(), null) == null)
- .collect(Collectors.toMap(Asset::getId, agent -> (Agent<?, ?, ?>)agent));
- }
- return agentMap;
- });
+ return agentMap;
}
public Protocol<?> getProtocolInstance(Agent<?, ?, ?> agent) {
diff --git a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
index 0095650ac..42b7210d1 100644
--- a/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
+++ b/manager/src/main/java/org/openremote/manager/notification/EmailNotificationHandler.java
@@ -32,14 +32,11 @@ import org.openremote.model.query.UserQuery;
import org.openremote.model.query.filter.RealmPredicate;
import org.openremote.model.query.filter.UserAssetPredicate;
import org.openremote.model.util.TextUtil;
-import org.simplejavamail.email.Email;
-import org.simplejavamail.email.EmailBuilder;
-import org.simplejavamail.email.EmailPopulatingBuilder;
-import org.simplejavamail.email.Recipient;
-import org.simplejavamail.mailer.Mailer;
-import org.simplejavamail.mailer.MailerBuilder;
-import org.simplejavamail.mailer.config.TransportStrategy;
+import javax.mail.*;
+import javax.mail.internet.InternetAddress;
+import javax.mail.internet.MimeMessage;
+import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -54,7 +51,7 @@ public class EmailNotificationHandler implements NotificationHandler {
private static final Logger LOG = Logger.getLogger(EmailNotificationHandler.class.getName());
protected String defaultFrom;
- protected Mailer mailer;
+ protected Session mailSession;
protected Map<String, String> headers;
protected ManagerIdentityService managerIdentityService;
protected AssetStorageService assetStorageService;
@@ -89,16 +86,33 @@ public class EmailNotificationHandler implements NotificationHandler {
defaultFrom = container.getConfig().getOrDefault(OR_EMAIL_FROM, OR_EMAIL_FROM_DEFAULT);
if (!TextUtil.isNullOrEmpty(host) && !TextUtil.isNullOrEmpty(user) && !TextUtil.isNullOrEmpty(password)) {
- MailerBuilder.MailerRegularBuilder mailerBuilder = MailerBuilder.withSMTPServer(host, port, user, password);
boolean startTls = getBoolean(container.getConfig(), OR_EMAIL_TLS, OR_EMAIL_TLS_DEFAULT);
- mailerBuilder.withTransportStrategy(startTls ? TransportStrategy.SMTP_TLS : TransportStrategy.SMTP);
- mailer = mailerBuilder.buildMailer();
- try {
- mailer.testConnection();
+ Properties props = new Properties();
+ props.put("mail.smtp.auth", true);
+ props.put("mail.smtp.starttls.enable", startTls);
+ props.put("mail.smtp.host", host);
+ props.put("mail.smtp.port", port);
+
+ mailSession = Session.getInstance(props, new Authenticator() {
+ @Override
+ protected PasswordAuthentication getPasswordAuthentication() {
+ return new PasswordAuthentication(user, password);
+ }
+ });
+
+ boolean valid;
+ try (Transport transport = mailSession.getTransport(startTls ? "smtps" : "smtp")) {
+ transport.connect();
+ valid = transport.isConnected();
} catch (Exception e) {
+ valid = false;
LOG.log(Level.SEVERE, "Failed to connect to SMTP server so disabling email notifications", e);
- mailer = null;
+ }
+
+ if (!valid) {
+ mailSession = null;
+ LOG.log(Level.INFO, "SMTP credentials are not valid so email notifications will not function");
}
}
}
@@ -115,7 +129,7 @@ public class EmailNotificationHandler implements NotificationHandler {
@Override
public boolean isValid() {
- return mailer != null;
+ return mailSession != null;
}
@Override
@@ -300,46 +314,53 @@ public class EmailNotificationHandler implements NotificationHandler {
LOG.warning("Target type not supported: " + targetType);
}
- EmailNotificationMessage emailNotificationMessage = (EmailNotificationMessage) message;
- EmailPopulatingBuilder emailBuilder = buildEmailBuilder(id, emailNotificationMessage);
+ try {
+ MimeMessage email = new MimeMessage(mailSession);
- if (!toRecipients.isEmpty()) {
- toRecipients.forEach(recipient -> {
- if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {
- emailBuilder.to(convertRecipient(recipient));
+ if (!toRecipients.isEmpty()) {
+ for (EmailNotificationMessage.Recipient recipient : toRecipients) {
+ if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {
+ email.addRecipient(Message.RecipientType.TO, convertRecipient(recipient));
+ }
}
- });
- }
- if (!ccRecipients.isEmpty()) {
- ccRecipients.forEach(recipient -> {
- if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {
- emailBuilder.cc(convertRecipient(recipient));
+ }
+ if (!ccRecipients.isEmpty()) {
+ for (EmailNotificationMessage.Recipient recipient : ccRecipients) {
+ if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {
+ email.addRecipient(Message.RecipientType.CC, convertRecipient(recipient));
+ }
}
- });
- }
- if (!bccRecipients.isEmpty()) {
- bccRecipients.forEach(recipient -> {
- if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {
- emailBuilder.bcc(convertRecipient(recipient));
+ }
+ if (!bccRecipients.isEmpty()) {
+ for (EmailNotificationMessage.Recipient recipient : bccRecipients) {
+ if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {
+ email.addRecipient(Message.RecipientType.BCC, convertRecipient(recipient));
+ }
}
- });
- }
+ }
- if (emailBuilder.getRecipients().isEmpty()) {
- return NotificationSendResult.failure("No recipients set for " + targetType.name().toLowerCase() + ": " + targetId);
- }
+ buildEmail(id, (EmailNotificationMessage) message, email);
- // Set from based on source if not already set
- if (emailBuilder.getFromRecipient() == null) {
- emailBuilder.from(defaultFrom);
- }
+ Address[] recipients = email.getAllRecipients();
+ if (recipients == null || recipients.length == 0) {
+ return NotificationSendResult.failure("No recipients set for " + targetType.name().toLowerCase() + ": " + targetId);
+ }
- return sendMessage(emailBuilder.buildEmail());
+ // Set from based on source if not already set
+ if (email.getFrom() == null || email.getFrom().length == 0) {
+ email.setFrom(new InternetAddress(defaultFrom));
+ }
+
+ return sendMessage(email);
+ } catch (Exception e) {
+ LOG.log(Level.WARNING, "Email send failed: " + e.getMessage(), e);
+ return NotificationSendResult.failure("Email send failed: " + e.getMessage());
+ }
}
- protected NotificationSendResult sendMessage(Email email) {
+ protected NotificationSendResult sendMessage(Message email) {
try {
- mailer.sendMail(email);
+ Transport.send(email);
return NotificationSendResult.success();
} catch (Exception e) {
LOG.log(Level.WARNING, "Email send failed: " + e.getMessage(), e);
@@ -347,46 +368,55 @@ public class EmailNotificationHandler implements NotificationHandler {
}
}
- protected EmailPopulatingBuilder buildEmailBuilder(long id, EmailNotificationMessage emailNotificationMessage) {
- EmailPopulatingBuilder emailBuilder = EmailBuilder.startingBlank()
- .withReplyTo(convertRecipient(emailNotificationMessage.getReplyTo()))
- .withSubject(emailNotificationMessage.getSubject())
- .withPlainText(emailNotificationMessage.getText())
- .withHTMLText(emailNotificationMessage.getHtml());
-
- if (headers != null) {
- emailBuilder.withHeaders(headers);
+ protected void buildEmail(long id, EmailNotificationMessage emailNotificationMessage, MimeMessage email) throws Exception {
+ if (emailNotificationMessage.getReplyTo() != null) {
+ email.setReplyTo(new Address[]{convertRecipient(emailNotificationMessage.getReplyTo())});
}
-
if (emailNotificationMessage.getFrom() != null) {
- emailBuilder.from(convertRecipient(emailNotificationMessage.getFrom()));
+ email.setFrom(convertRecipient(emailNotificationMessage.getFrom()));
+ }
+ if (emailNotificationMessage.getSubject() != null) {
+ email.setSubject(emailNotificationMessage.getSubject());
+ }
+ if (headers != null) {
+ for (Map.Entry<String, String >entry : headers.entrySet()) {
+ email.addHeader(entry.getKey(), entry.getValue());
+ }
}
-
if (emailNotificationMessage.getTo() != null) {
- emailBuilder.to(
- emailNotificationMessage.getTo().stream()
- .map(this::convertRecipient).collect(Collectors.toList()));
+ for (EmailNotificationMessage.Recipient recipient : emailNotificationMessage.getTo()) {
+ if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {
+ email.addRecipient(Message.RecipientType.TO, convertRecipient(recipient));
+ }
+ }
}
-
if (emailNotificationMessage.getCc() != null) {
- emailBuilder.cc(
- emailNotificationMessage.getCc().stream()
- .map(this::convertRecipient).collect(Collectors.toList()));
+ for (EmailNotificationMessage.Recipient recipient : emailNotificationMessage.getCc()) {
+ if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {
+ email.addRecipient(Message.RecipientType.CC, convertRecipient(recipient));
+ }
+ }
}
-
if (emailNotificationMessage.getBcc() != null) {
- emailBuilder.bcc(
- emailNotificationMessage.getBcc().stream()
- .map(this::convertRecipient).collect(Collectors.toList()));
+ for (EmailNotificationMessage.Recipient recipient : emailNotificationMessage.getBcc()) {
+ if (!TextUtil.isNullOrEmpty(recipient.getAddress())) {
+ email.addRecipient(Message.RecipientType.BCC, convertRecipient(recipient));
+ }
+ }
+ }
+ if (emailNotificationMessage.getText() != null) {
+ email.setText(emailNotificationMessage.getText());
+ } else if (emailNotificationMessage.getHtml() != null) {
+ email.setContent(emailNotificationMessage.getHtml(), "text/html");
}
-
- // Use the notification ID as the message ID
- emailBuilder.fixingMessageId("<" + id + "@openremote.io>");
-
- return emailBuilder;
}
- protected Recipient convertRecipient(EmailNotificationMessage.Recipient recipient) {
- return recipient == null ? null : new Recipient(recipient.getName(), recipient.getAddress(), null);
+ protected InternetAddress convertRecipient(EmailNotificationMessage.Recipient recipient) {
+ try {
+ return recipient == null ? null : new InternetAddress(recipient.getAddress(), recipient.getName());
+ } catch (UnsupportedEncodingException e) {
+ LOG.log(Level.WARNING, "Failed to process email recipient: " + recipient.getAddress(), e);
+ }
+ return null;
}
}
diff --git a/model/src/main/java/org/openremote/model/mail/MailMessage.java b/model/src/main/java/org/openremote/model/mail/MailMessage.java
new file mode 100644
index 000000000..771a3e5bf
--- /dev/null
+++ b/model/src/main/java/org/openremote/model/mail/MailMessage.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2023, 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.model.mail;
+
+import java.util.*;
+
+/**
+ * Represents a mail message; only represents text part with combined headers from the outer message and any mime part
+ * that contained the text content
+ */
+public class MailMessage {
+
+ protected String content;
+ protected String contentType;
+ protected Map<String, List<String>> headers;
+ protected String subject;
+ protected Date sentDate;
+ protected String[] from;
+
+ public MailMessage(String content, String contentType, Map<String, List<String>> headers, String subject, Date sentDate, String[] from) {
+ this.content = content == null ? "" : content.endsWith("\r\n") ? content.substring(0,content.length() - 2) : content;
+ this.contentType = contentType.toLowerCase(Locale.ROOT);
+ this.headers = headers;
+ this.subject = subject;
+ this.sentDate = sentDate;
+ this.from = from;
+ }
+
+ public String getContent() {
+ return content;
+ }
+
+ public String getContentType() {
+ return contentType;
+ }
+
+ public Date getSentDate() {
+ return sentDate;
+ }
+
+ public String[] getFrom() {
+ return from;
+ }
+
+ public String getSubject() {
+ return subject;
+ }
+
+ public String getHeader(String name) {
+ List<String> namedHeaders = getHeaders(name);
+ if (namedHeaders == null || namedHeaders.isEmpty()) {
+ return null;
+ }
+ return namedHeaders.get(0);
+ }
+
+ public List<String> getHeaders(String name) {
+ return headers.get(name);
+ }
+
+ public Map<String, List<String>> getHeaders() {
+ return headers;
+ }
+
+ @Override
+ public String toString() {
+ return "MailMessage{" +
+ "contentType='" + contentType + '\'' +
+ ", subject='" + subject + '\'' +
+ ", sentDate=" + sentDate +
+ ", from=" + Arrays.toString(from) +
+ '}';
+ }
+}
diff --git a/test/build.gradle b/test/build.gradle
index b58ea2627..ad859d39b 100644
--- a/test/build.gradle
+++ b/test/build.gradle
@@ -30,13 +30,13 @@ dependencies {
testImplementation "com.github.calimero:calimero-server:$calimeroVersion"
testImplementation "com.google.firebase:firebase-admin:$firebaseAdminVersion"
- testImplementation "org.simplejavamail:simple-java-mail:$simpleJavaMailVersion"
testImplementation "org.shredzone.commons:commons-suncalc:$suncalcVersion"
+ testImplementation "com.icegreen:greenmail:$greenmailVersion"
+}
+
+tasks.withType(Test) {
+ environment("OR_LOGGING_CONFIG_FILE", "test/src/logging-test.properties")
}
-//
-//tasks.withType(Test) {
-// logging.captureStandardError LogLevel.LIFECYCLE
-//}
test {
outputs.upToDateWhen { false }
diff --git a/test/src/logging-test.properties b/test/src/logging-test.properties
new file mode 100644
index 000000000..f7fef93fd
--- /dev/null
+++ b/test/src/logging-test.properties
@@ -0,0 +1,73 @@
+# Log on STDOUT and internal syslog service (monitoring in Manager UI)
+handlers=java.util.logging.ConsoleHandler, \
+ org.openremote.manager.syslog.SyslogService
+
+###############################################################################
+# Console handler
+###############################################################################
+
+java.util.logging.ConsoleHandler.level=FINE
+
+# Console format, you can colorize this in IntelliJ IDEA "Run Application " console
+# with the GrepConsole plugin and after copying the configuration file of your
+# choice to ~/Library/Preferences/IntelliJ*/options/GrepConsole.xml:
+#
+# - tools/intellijTheme-Default-GrepConsole.xml
+# - tools/intellijTheme-Darcula-GrepConsole.xml
+#
+java.util.logging.ConsoleHandler.formatter=org.openremote.container.util.LogFormatter
+
+###############################################################################
+# Categories and levels
+###############################################################################
+# The following level convention is used
+# FINE = System.Logger.Level.DEBUG - Will log processing times and audit logs
+# FINEST = System.Logger.Level.TRACE - Will log everything
+
+# The root logger category, the default for all categories not configured here
+.level=INFO
+
+# The root logger category of OpenRemote
+org.openremote.level=FINE
+
+# Processing time logging
+#org.openremote.container.concurrent.GlobalLock.level=FINE
+#org.openremote.manager.asset.AssetProcessingService.level=FINE
+#org.openremote.manager.asset.AssetStorageService.level=FINE
+#org.openremote.manager.rules.level=FINE
+
+# Audit logging
+# Set to fine to log MQTT connections
+#org.openremote.manager.mqtt.level=FINE
+# Set to fine to log WS connections
+#org.openremote.container.web.socket.WebsocketAdapter.level=FINE
+# Set to fine to log HTTP requests for servlet deployments
+#org.openremote.container.web.RequestLogger.level=FINE
+# Set to fine to log all HTTP requests (not just deployments)
+#org.openremote.container.web.WebService.level=INFO
+
+# Try to get more info on AbstractNettyIOClient as we get strange intermittent test failures
+org.openremote.agent.protocol.io.AbstractNettyIOClient.level=FINEST
+
+# Third party overrides
+org.jboss.resteasy.level=WARNING
+org.keycloak.level=WARNING
+io.undertow.level=WARNING
+org.hibernate.level=WARNING
+org.hibernate.cfg.beanvalidation.TypeSafeActivator.level=SEVERE
+com.zaxxer.hikari.level=WARNING
+javax.management.level=WARNING
+org.xnio.level=WARNING
+org.apache.camel.management.level=WARNING
+org.apache.camel.impl.converter.DefaultTypeConverter.level=WARNING
+org.apache.camel.impl.DefaultShutdownStrategy.level=SEVERE
+org.jeasy.rules.level=WARNING
+org.apache.activemq.audit.level=WARNING
+com.icegreen.greenmail.level=FINE
+
+# Set this to FINEST to log outgoing HTTP traffic
+#org.apache.http.level=WARNING
+#org.apache.http.client.protocol.level=SEVERE
+
+# Set to SEVERE to log invalid access tokens
+org.keycloak.adapters.BearerTokenRequestAuthenticator.level=OFF
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
index 82a9056d3..8d9420190 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetProcessingTest.groovy
@@ -85,11 +85,6 @@ class AssetProcessingTest extends Specification implements ManagerContainerTrait
def assetProcessingService = container.getService(AssetProcessingService.class)
def keycloakTestSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)
- then: "the container should be running and initialised"
- conditions.eventually {
- assert noEventProcessedIn(assetProcessingService, 500)
- }
-
then: "register mock asset processors"
assetProcessingService.processors.add(0, firstProcessor)
assetProcessingService.processors.add(assetProcessingService.processors.findIndexOf {it instanceof AgentService}+1, afterAgentServiceProcessor)
diff --git a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
index 8ff458365..5cd627d62 100644
--- a/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/notification/NotificationTest.groovy
@@ -2,7 +2,6 @@ package org.openremote.test.notification
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.databind.node.TextNode
-import com.google.firebase.messaging.Message
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.asset.console.ConsoleResourceImpl
@@ -21,13 +20,14 @@ import org.openremote.model.notification.*
import org.openremote.model.query.UserQuery
import org.openremote.model.query.filter.RealmPredicate
import org.openremote.model.util.TextUtil
-import org.openremote.test.ManagerContainerTrait
import org.openremote.setup.integration.KeycloakTestSetup
import org.openremote.setup.integration.ManagerTestSetup
-import org.simplejavamail.email.Email
+import org.openremote.test.ManagerContainerTrait
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
+import javax.mail.Message
+import javax.mail.internet.InternetAddress
import javax.ws.rs.WebApplicationException
import java.time.Instant
import java.time.temporal.ChronoUnit
@@ -62,35 +62,35 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
PushNotificationHandler mockPushNotificationHandler = Spy(pushNotificationHandler)
mockPushNotificationHandler.isValid() >> true
mockPushNotificationHandler.sendMessage(_ as Long, _ as Notification.Source, _ as String, _ as Notification.Target, _ as AbstractNotificationMessage) >> {
- id, source, sourceId, target, message ->
- notificationIds << id
- notificationTargetTypes << target.type
- notificationTargetIds << target.id
- notificationMessages << message
- callRealMethod()
- }
+ id, source, sourceId, target, message ->
+ notificationIds << id
+ notificationTargetTypes << target.type
+ notificationTargetIds << target.id
+ notificationMessages << message
+ callRealMethod()
+ }
// Assume sent to FCM
- mockPushNotificationHandler.sendMessage(_ as Message) >> {
- message -> return NotificationSendResult.success()
- }
+ mockPushNotificationHandler.sendMessage(_ as com.google.firebase.messaging.Message) >> {
+ message -> return NotificationSendResult.success()
+ }
notificationService.notificationHandlerMap.put(pushNotificationHandler.getTypeName(), mockPushNotificationHandler)
and: "an authenticated test user"
def realm = keycloakTestSetup.realmBuilding.name
def testuser1AccessToken = authenticate(
- container,
- MASTER_REALM,
- KEYCLOAK_CLIENT_ID,
- "testuser1",
- "testuser1"
+ container,
+ MASTER_REALM,
+ KEYCLOAK_CLIENT_ID,
+ "testuser1",
+ "testuser1"
).token
def testuser2AccessToken = authenticate(
- container,
- realm,
- KEYCLOAK_CLIENT_ID,
- "testuser2",
- "testuser2"
+ container,
+ realm,
+ KEYCLOAK_CLIENT_ID,
+ "testuser2",
+ "testuser2"
).token
def testuser3AccessToken = authenticate(
container,
@@ -457,8 +457,8 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
when: "a repeat frequency is set and a notification is sent"
// Move clock to beginning of next hour
def advancement = Instant.ofEpochMilli(getClockTimeOf(container))
- .truncatedTo(ChronoUnit.HOURS)
- .plus(59, ChronoUnit.MINUTES)
+ .truncatedTo(ChronoUnit.HOURS)
+ .plus(59, ChronoUnit.MINUTES)
advancePseudoClock(ChronoUnit.MILLIS.between(Instant.ofEpochMilli(getClockTimeOf(container)), advancement), TimeUnit.MILLISECONDS, container)
notification.setRepeatFrequency(RepeatFrequency.HOURLY)
@@ -547,7 +547,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
def "Check email notification functionality"() {
- List<Email> sentEmails = []
+ List<Message> sentEmails = []
given: "the container environment is started with the mock handler"
def conditions = new PollingConditions(timeout: 10, delay: 0.2)
@@ -565,8 +565,8 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
mockEmailNotificationHandler.isValid() >> true
// Log email and assume sent to SMTP Server
- mockEmailNotificationHandler.sendMessage(_ as Email) >> {
- Email email ->
+ mockEmailNotificationHandler.sendMessage(_ as Message) >> {
+ Message email ->
sentEmails << email
return NotificationSendResult.success()
}
@@ -589,12 +589,12 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
then: "the email should have been sent to the realm users"
conditions.eventually {
assert sentEmails.size() == 2
- assert sentEmails.every {it.getPlainText() == "Hello world!"}
- assert sentEmails.every {it.getSubject() == "Test"}
- assert sentEmails.every {it.getHeaders() != null && it.getHeaders().size() == 2 && it.getHeaders().get("Test 1") == "Hello World 1" && it.getHeaders().get("Test2") == "Hello World 2" }
- assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}
- assert !sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}
- assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}
+ assert sentEmails.every {it.content == "Hello world!"}
+ assert sentEmails.every { it.getSubject() == "Test"}
+ assert sentEmails.every { it.allHeaders != null && it.getHeader("Test 1")[0] == "Hello World 1" && it.getHeader("Test2")[0] == "Hello World 2" }
+ assert sentEmails.any { it.allRecipients.length == 1 && (it.allRecipients[0] as InternetAddress).address == "[email protected]"}
+ assert !sentEmails.any { it.allRecipients.length == 1 && (it.allRecipients[0] as InternetAddress).address == "[email protected]"}
+ assert sentEmails.any { it.allRecipients.length == 1 && (it.allRecipients[0] as InternetAddress).address == "[email protected]"}
}
when: "an email attribute is added to an asset"
@@ -612,7 +612,7 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
conditions.eventually {
assert sentEmails.size() == 1
assert sentEmails.any { it.getSubject() == "Test 2"}
- assert sentEmails.any { it.getRecipients().size() == 1 && it.getRecipients().get(0).address == "[email protected]"}
+ assert sentEmails.any { it.allRecipients.length == 1 && (it.allRecipients[0] as InternetAddress).address == "[email protected]"}
}
when: "an email is sent to a custom target"
@@ -624,11 +624,11 @@ class NotificationTest extends Specification implements ManagerContainerTrait {
then: "the email should have been sent to all custom recipients"
conditions.eventually {
assert sentEmails.size() == 1
- assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients().size() == 4}
- assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients().any{it.type == javax.mail.Message.RecipientType.TO && it.address == "[email protected]"}}
- assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients().any{it.type == javax.mail.Message.RecipientType.TO && it.address == "[email protected]"}}
- assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients().any{it.type == javax.mail.Message.RecipientType.CC && it.address == "[email protected]"}}
- assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients().any{it.type == javax.mail.Message.RecipientType.BCC && it.address == "[email protected]"}}
+ assert sentEmails.any { it.getSubject() == "Test Custom" && it.allRecipients.length == 4}
+ assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients(Message.RecipientType.TO).any{(it as InternetAddress).address == "[email protected]"}}
+ assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients(Message.RecipientType.TO).any{(it as InternetAddress).address == "[email protected]"}}
+ assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients(Message.RecipientType.CC).any{(it as InternetAddress).address == "[email protected]"}}
+ assert sentEmails.any { it.getSubject() == "Test Custom" && it.getRecipients(Message.RecipientType.BCC).any{(it as InternetAddress).address == "[email protected]"}}
}
cleanup: "the mock is removed"
diff --git a/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy
new file mode 100644
index 000000000..142f6193e
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy
@@ -0,0 +1,229 @@
+/*
+ * Copyright 2023, 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.test.protocol.mail
+
+import com.icegreen.greenmail.user.GreenMailUser
+import com.icegreen.greenmail.util.GreenMail
+import com.icegreen.greenmail.util.GreenMailUtil
+import com.icegreen.greenmail.util.ServerSetupTest
+import org.openremote.agent.protocol.mail.AbstractMailProtocol
+import org.openremote.agent.protocol.mail.MailAgent
+import org.openremote.agent.protocol.mail.MailAgentLink
+import org.openremote.agent.protocol.mail.MailClientBuilder
+import org.openremote.agent.protocol.mail.MailProtocol
+import org.openremote.manager.agent.AgentService
+import org.openremote.manager.asset.AssetProcessingService
+import org.openremote.manager.asset.AssetStorageService
+import org.openremote.manager.event.ClientEventService
+import org.openremote.model.Constants
+import org.openremote.model.asset.agent.Agent
+import org.openremote.model.asset.agent.ConnectionStatus
+import org.openremote.model.asset.agent.DefaultAgentLink
+import org.openremote.model.asset.impl.ThingAsset
+import org.openremote.model.attribute.Attribute
+import org.openremote.model.attribute.AttributeEvent
+import org.openremote.model.attribute.MetaItem
+import org.openremote.model.auth.UsernamePassword
+import org.openremote.model.query.AssetQuery
+import org.openremote.model.query.filter.StringPredicate
+import org.openremote.model.query.filter.ValueAnyPredicate
+import org.openremote.model.value.SubStringValueFilter
+import org.openremote.model.value.ValueFilter
+import org.openremote.test.ManagerContainerTrait
+import spock.lang.Shared
+import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+import javax.mail.internet.MimeMessage
+
+import static org.openremote.model.value.MetaItemType.AGENT_LINK
+import static org.openremote.model.value.ValueType.EXECUTION_STATUS
+import static org.openremote.model.value.ValueType.TEXT
+
+class MailClientProtocolTest extends Specification implements ManagerContainerTrait {
+
+ @Shared
+ static GreenMail greenMail
+ @Shared
+ static int messageCounter
+ @Shared
+ static GreenMailUser user
+
+
+ def setupSpec() {
+ MailClientBuilder.MIN_CHECK_INTERVAL_MILLIS = 2000
+ AbstractMailProtocol.INITIAL_CHECK_DELAY_MILLIS = 2000
+ greenMail = new GreenMail(ServerSetupTest.ALL)
+ greenMail.start()
+ user = greenMail.setUser("or@localhost", "or", "secret")
+ }
+
+ def cleanupSpec() {
+ if (greenMail != null) {
+ greenMail.stop()
+ }
+ }
+
+ def sendMessage(String fromAddress) {
+ def subject = "Test Message ${++messageCounter}"
+ def body = "Test body ${messageCounter}"
+ MimeMessage message = GreenMailUtil.createTextEmail("to@localhost", fromAddress, subject, body, greenMail.getImap().getServerSetup())
+ message.addHeader("Test-Header", "Test Header Value")
+ user.deliver(message)
+ }
+
+ def sendMessage(String fromAddress, String subject, String body) {
+ MimeMessage message = GreenMailUtil.createTextEmail("to@localhost", fromAddress, subject, body, greenMail.getImap().getServerSetup())
+ message.addHeader("Test-Header", "Test Header Value")
+ user.deliver(message)
+ }
+
+ def "Basic agent and attribute linking"() {
+
+ given: "expected conditions"
+ def conditions = new PollingConditions(timeout: 15, delay: 0.2)
+
+ and: "some mailbox messages"
+ sendMessage("from@localhost")
+ sendMessage("from@localhost")
+ sendMessage("from@localhost")
+
+ and: "the container starts"
+ def container = startContainer(defaultConfig(), defaultServices())
+ def assetStorageService = container.getService(AssetStorageService.class)
+ def assetProcessingService = container.getService(AssetProcessingService.class)
+ def agentService = container.getService(AgentService.class)
+ def clientEventService = container.getService(ClientEventService.class)
+ List<AttributeEvent> attributeEvents = []
+ def eventSessionID = clientEventService.addInternalSubscription(AttributeEvent.class, null, it -> attributeEvents.add(it))
+
+ when: "a mail client agent is created"
+ def agent = new MailAgent("Test agent")
+ agent.setRealm(Constants.MASTER_REALM)
+ .setProtocol("pop3")
+ .setHost("127.0.0.1")
+ .setPort(greenMail.getPop3().getServerSetup().getPort())
+ .setUsernamePassword(new UsernamePassword("or", "secret"))
+ .setCheckIntervalSeconds(2000)
+ .setDisabled(true)
+
+ and: "the agent is added to the asset service"
+ agent = assetStorageService.merge(agent)
+
+ and: "an asset is created with attributes linked to the agent"
+ def asset = new ThingAsset("Test Asset")
+ .setParent(agent)
+ .addOrReplaceAttributes(
+ new Attribute<>("fromMatchUseBody", TEXT)
+ .addMeta(
+ new MetaItem<>(AGENT_LINK, new MailAgentLink(agent.id)
+ .setFromMatchPredicate(new StringPredicate("from@localhost"))
+ )
+ ),
+ new Attribute<>("subjectMatchUseBody", TEXT)
+ .addMeta(
+ new MetaItem<>(AGENT_LINK, new MailAgentLink(agent.id)
+ .setSubjectMatchPredicate(new StringPredicate("Not A Test"))
+ )
+ ),
+ new Attribute<>("subjectAndFromMatchUseBody", TEXT)
+ .addMeta(
+ new MetaItem<>(AGENT_LINK, new MailAgentLink(agent.id)
+ .setFromMatchPredicate(new StringPredicate("fromanother@localhost"))
+ .setSubjectMatchPredicate(new StringPredicate("Not A Test"))
+ )
+ ),
+ new Attribute<>("fromMatchUseSubject", TEXT)
+ .addMeta(
+ new MetaItem<>(AGENT_LINK, new MailAgentLink(agent.id)
+ .setFromMatchPredicate(new StringPredicate("from@localhost"))
+ .setUseSubject(true)
+ )
+ )
+ )
+
+ and: "the asset is merged into the asset service"
+ asset = assetStorageService.merge(asset)
+
+ and: "the agent is enabled"
+ agent.setDisabled(false)
+ agent = assetStorageService.merge(agent)
+
+ then: "the attributes should be linked"
+ conditions.eventually {
+ assert agentService.getProtocolInstance(agent.id).linkedAttributes.size() == 4
+ assert ((MailProtocol)agentService.getProtocolInstance(agent.id)).attributeMessageProcessorMap.size() == 4
+ }
+
+ then: "the agent should become connected"
+ conditions.eventually {
+ assert attributeEvents.any {it.assetId == agent.id && it.attributeName == Agent.STATUS.name && it.value.orElse(null) == ConnectionStatus.CONNECTED}
+ }
+
+ then: "the agent should have finished message checking"
+ conditions.eventually {
+ assert attributeEvents.any {it.assetId == agent.id && it.attributeName == Agent.STATUS.name && it.value.orElse(null) == ConnectionStatus.WAITING}
+ }
+
+ then: "the linked attributes should not have been updated with mailbox messages (as creation time of agent is after message timestamps)"
+ conditions.eventually {
+ assert attributeEvents.count {it.assetId == asset.id} == 0
+ }
+
+ when: "more messages are received in the mailbox"
+ sendMessage("from@localhost")
+ sendMessage("1from@localhost")
+ sendMessage("from@localhost")
+ sendMessage("from@localhost", "Not A Test", "Not a test body 1")
+ sendMessage("fromanother@localhost", "Not A Test", "Not a test body 2")
+
+ then: "the matching linked attributes should have been updated with the new mailbox messages"
+ conditions.eventually {
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Test body")} == 2
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 1
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseSubject" && (it.value.orElse(null) as String).startsWith("Test Message")} == 2
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseSubject" && it.value.orElse(null) == "Not A Test"} == 1
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "subjectMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 2
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "subjectAndFromMatchUseBody" && it.value.orElse(null) == "Not a test body 2"} == 1
+ }
+
+ when: "more messages are received in the mailbox"
+ sendMessage("fromanother@localhost", "Really Not A Test", "Really not a test body 1")
+ sendMessage("fromanotheranother@localhost", "Not A Test", "Not a test body 3")
+
+ then: "the matching linked attributes should have been updated with the new mailbox messages"
+ conditions.eventually {
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Test body")} == 2
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 1
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseSubject" && (it.value.orElse(null) as String).startsWith("Test Message")} == 2
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "fromMatchUseSubject" && it.value.orElse(null) == "Not A Test"} == 1
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "subjectMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 3
+ assert attributeEvents.count {it.assetId == asset.id && it.attributeName == "subjectAndFromMatchUseBody" && (it.value.orElse(null) as String).startsWith("Not a test body")} == 1
+ }
+
+ cleanup: "the event subscription is removed"
+ if (eventSessionID != null) {
+ clientEventService.cancelInternalSubscription(eventSessionID)
+ }
+ if (asset != null) {
+ assetStorageService.delete([agent.id, asset.id])
+ }
+ }
+}
diff --git a/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientTest.groovy
new file mode 100644
index 000000000..448369b69
--- /dev/null
+++ b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientTest.groovy
@@ -0,0 +1,254 @@
+package org.openremote.test.protocol.mail
+
+import com.icegreen.greenmail.user.GreenMailUser
+import com.icegreen.greenmail.util.GreenMail
+import com.icegreen.greenmail.util.GreenMailUtil
+import com.icegreen.greenmail.util.ServerSetupTest
+import org.openremote.agent.protocol.mail.MailClientBuilder
+import org.openremote.container.concurrent.ContainerScheduledExecutor
+import org.openremote.model.asset.agent.ConnectionStatus
+import org.openremote.model.mail.MailMessage
+import org.openremote.test.ManagerContainerTrait
+import spock.lang.Shared
+import spock.lang.Specification
+import spock.util.concurrent.PollingConditions
+
+import javax.mail.Message
+import javax.mail.Multipart
+import javax.mail.internet.InternetAddress
+import javax.mail.internet.MimeBodyPart
+import javax.mail.internet.MimeMessage
+import javax.mail.internet.MimeMultipart
+import java.nio.file.Paths
+import java.util.concurrent.CopyOnWriteArrayList
+
+/*
+ * Copyright 2023, 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/>.
+ */
+
+class MailClientTest extends Specification implements ManagerContainerTrait {
+
+ @Shared
+ static GreenMail greenMail
+ @Shared
+ static int messageCounter
+ @Shared
+ static GreenMailUser user
+
+
+ def setupSpec() {
+ MailClientBuilder.MIN_CHECK_INTERVAL_MILLIS = 2000
+ greenMail = new GreenMail(ServerSetupTest.ALL)
+ greenMail.start()
+ user = greenMail.setUser("or@localhost", "or", "secret")
+ }
+
+ def cleanupSpec() {
+ if (greenMail != null) {
+ greenMail.stop()
+ }
+ }
+
+ def sendMessage() {
+ def subject = "Test Message ${++messageCounter}"
+ def body = "Test body ${messageCounter}"
+ MimeMessage message = GreenMailUtil.createTextEmail("to@localhost", "from@localhost", subject, body, greenMail.getImap().getServerSetup())
+ message.addHeader("Test-Header", "Test Header Value")
+ user.deliver(message)
+ }
+
+ def "POP3 mail receiving test"() {
+
+ given: "an email client with callback handlers"
+ List<ConnectionStatus> connectionEvents = new CopyOnWriteArrayList<>()
+ List<MailMessage> messages = new CopyOnWriteArrayList<>()
+ def conditions = new PollingConditions(delay: 1, initialDelay: 1, timeout: 10)
+ def executor = new ContainerScheduledExecutor("Scheduled task", 1)
+ def mailClient = new MailClientBuilder(
+ executor,
+ "pop3",
+ "localhost",
+ greenMail.getPop3().getServerSetup().getPort(), "or", "secret")
+ .setCheckIntervalMillis(2000)
+ .setPersistenceDir(Paths.get("tmp"))
+ .build()
+ mailClient.addConnectionListener{ connectionEvents.add(it)}
+ mailClient.addMessageListener{messages.add(it)}
+
+ and: "some messages in the mailbox"
+ sendMessage()
+ sendMessage()
+ sendMessage()
+
+ when: "the mail client is connected"
+ mailClient.connect()
+
+ then: "the connection status should be connected and the 3 messages should have been received"
+ conditions.eventually {
+ assert connectionEvents.any {it == ConnectionStatus.CONNECTED}
+ assert messages.size() == 3
+ assert messages.any {it.content == "Test body 1" && it.subject == "Test Message 1" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ assert messages.any {it.content == "Test body 2" && it.subject == "Test Message 2" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ assert messages.any {it.content == "Test body 3" && it.subject == "Test Message 3" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ }
+
+ when: "a multipart mail is sent to the mailbox with text and html parts"
+ def sendMultipartMessage = () -> {
+ def email = new MimeMessage(GreenMailUtil.getSession(greenMail.getImap().getServerSetup()))
+ email.setSubject("Test Multipart Message")
+ MimeBodyPart htmlBodyPart = new MimeBodyPart()
+ htmlBodyPart.setContent("<p>Test html body</p>", "text/html; charset=utf-8")
+ htmlBodyPart.addHeader("HTML-Header", "HTML Header Value")
+ MimeBodyPart textBodyPart = new MimeBodyPart()
+ textBodyPart.setContent("Test text body", "text/plain; charset=utf-8")
+ textBodyPart.addHeader("Text-Header", "Text Header Value")
+ Multipart multipart = new MimeMultipart("alternative")
+ multipart.addBodyPart(htmlBodyPart)
+ multipart.addBodyPart(textBodyPart)
+ email.setContent(multipart)
+ email.setRecipient(Message.RecipientType.TO, new InternetAddress("to@localhost"))
+ email.setFrom("from@localhost")
+ user.deliver(email)
+ }
+ sendMultipartMessage()
+
+ then: "the new mail should be received with the text content"
+ conditions.eventually {
+ assert messages.size() == 4
+ def message = messages.get(messages.size()-1)
+ assert message.subject == "Test Multipart Message"
+ assert message.contentType == "text/plain; charset=utf-8"
+ assert message.content == "Test text body"
+ assert message.from[0] == "from@localhost"
+ assert message.headers.get("Text-Header").get(0) == "Text Header Value"
+ assert message.headers.get("Date") != null
+ assert message.headers.get("HTML-Header") == null
+ }
+
+ when: "the mail client is disconnected"
+ mailClient.disconnect()
+
+ then: "a connection event should have seen sent"
+ conditions.eventually {
+ assert connectionEvents.any {it == ConnectionStatus.DISCONNECTED}
+ }
+
+ when: "new mail is received"
+ sendMessage()
+ sendMessage()
+
+ and: "a new POP3 client is created"
+ mailClient = new MailClientBuilder(
+ executor,
+ "pop3",
+ "localhost",
+ greenMail.getPop3().getServerSetup().getPort(), "or", "secret")
+ .setCheckIntervalMillis(2000)
+ .setPersistenceDir(Paths.get("tmp"))
+ .setPreferHTML(true)
+ .build()
+ mailClient.addConnectionListener{ connectionEvents.add(it)}
+ mailClient.addMessageListener{messages.add(it)}
+
+ and: "the mail client is connected"
+ messages.clear()
+ connectionEvents.clear()
+ mailClient.connect()
+
+ then: "consumers should only be notified of the two new messages"
+ conditions.eventually {
+ assert connectionEvents.any {it == ConnectionStatus.CONNECTED}
+ assert messages.size() == 2
+ assert messages.any {it.content == "Test body 4" && it.subject == "Test Message 4" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ assert messages.any {it.content == "Test body 5" && it.subject == "Test Message 5" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ }
+
+ when: "another multipart mail is sent to the mailbox with text and html parts"
+ sendMultipartMessage()
+
+ then: "the new mail should be received with the text content"
+ conditions.eventually {
+ assert messages.size() == 3
+ def message = messages.get(messages.size()-1)
+ assert message.subject == "Test Multipart Message"
+ assert message.contentType == "text/html; charset=utf-8"
+ assert message.content == "<p>Test html body</p>"
+ assert message.from[0] == "from@localhost"
+ assert message.headers.get("HTML-Header").get(0) == "HTML Header Value"
+ assert message.headers.get("Date") != null
+ assert message.headers.get("Text-Header") == null
+ }
+
+ cleanup: "clean up client"
+ mailClient.disconnect()
+ greenMail.purgeEmailFromAllMailboxes()
+ messageCounter = 0
+ }
+
+ def "IMAP mail receiving test"() {
+
+ given: "an email client with callback handlers"
+ List<ConnectionStatus> connectionEvents = new CopyOnWriteArrayList<>()
+ List<MailMessage> messages = new CopyOnWriteArrayList<>()
+ def conditions = new PollingConditions(delay: 1, initialDelay: 1, timeout: 10)
+ def executor = new ContainerScheduledExecutor("Scheduled task", 1)
+ def mailClient = new MailClientBuilder(
+ executor,
+ "imap",
+ "localhost",
+ greenMail.getImap().getServerSetup().getPort(), "or", "secret")
+ .setCheckIntervalMillis(2000)
+ .build()
+ mailClient.addConnectionListener{ connectionEvents.add(it)}
+ mailClient.addMessageListener{messages.add(it)}
+
+ when: "3 mails are received"
+ sendMessage()
+ sendMessage()
+ sendMessage()
+
+ and: "the mail client is connected"
+ mailClient.connect()
+
+ then: "the connection status should be connected and the 3 messages should have been received"
+ conditions.eventually {
+ assert connectionEvents.any {it == ConnectionStatus.CONNECTED}
+ assert messages.size() == 3
+ assert messages.any {it.content == "Test body 1" && it.subject == "Test Message 1" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ assert messages.any {it.content == "Test body 2" && it.subject == "Test Message 2" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ assert messages.any {it.content == "Test body 3" && it.subject == "Test Message 3" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ }
+
+ when: "new mail is received"
+ sendMessage()
+ sendMessage()
+
+ then: "consumers should be notified of the two new messages"
+ conditions.eventually {
+ assert messages.size() == 5
+ assert messages.any {it.content == "Test body 4" && it.subject == "Test Message 4" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ assert messages.any {it.content == "Test body 5" && it.subject == "Test Message 5" && it.sentDate != null && it.contentType == "text/plain; charset=us-ascii" && it.from[0] == "from@localhost" && it.headers.get("Test-Header").get(0) == "Test Header Value"}
+ }
+
+ cleanup: "clean up client"
+ mailClient.disconnect()
+ greenMail.purgeEmailFromAllMailboxes()
+ messageCounter = 0
+ }
+}
diff --git a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
index 520f809b0..b70cb4346 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/udp/UdpClientProtocolTest.groovy
@@ -63,11 +63,6 @@ class UdpClientProtocolTest extends Specification implements ManagerContainerTra
def assetProcessingService = container.getService(AssetProcessingService.class)
def agentService = container.getService(AgentService.class)
- expect: "the system settles down"
- conditions.eventually {
- assert noEventProcessedIn(assetProcessingService, 300)
- }
-
when: "a simple UDP echo server is started"
def echoServerPort = findEphemeralPort()
def clientPort = findEphemeralPort()
diff --git a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
index fae32f60d..84179a35f 100644
--- a/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/rules/residence/JsonRulesTest.groovy
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode
import com.google.firebase.messaging.Message
import net.fortuna.ical4j.model.Recur
import org.openremote.container.timer.TimerService
+import org.openremote.container.util.MailUtil
import org.openremote.container.util.UniqueIdentifierGenerator
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
@@ -32,7 +33,6 @@ import org.openremote.model.notification.NotificationSendResult
import org.openremote.model.notification.PushNotificationMessage
import org.openremote.model.rules.RealmRuleset
import org.openremote.model.rules.Ruleset
-import org.openremote.model.rules.RulesetStatus
import org.openremote.model.rules.json.JsonRulesetDefinition
import org.openremote.model.util.ValueUtil
import org.openremote.model.value.MetaItemType
@@ -40,11 +40,11 @@ import org.openremote.model.value.ValueType
import org.openremote.setup.integration.KeycloakTestSetup
import org.openremote.setup.integration.ManagerTestSetup
import org.openremote.test.ManagerContainerTrait
-import org.simplejavamail.email.Email
import spock.lang.Shared
import spock.lang.Specification
import spock.util.concurrent.PollingConditions
+import javax.mail.internet.InternetAddress
import javax.ws.rs.client.ClientRequestContext
import javax.ws.rs.client.ClientRequestFilter
import javax.ws.rs.core.MediaType
@@ -57,7 +57,7 @@ import java.util.concurrent.TimeUnit
import static java.util.concurrent.TimeUnit.HOURS
import static java.util.concurrent.TimeUnit.MILLISECONDS
import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID
-import static org.openremote.model.rules.RulesetStatus.DEPLOYED
+import static org.openremote.model.rules.RulesetStatus.*
import static org.openremote.model.util.ValueUtil.parse
import static org.openremote.model.value.ValueType.TEXT
import static org.openremote.setup.integration.ManagerTestSetup.DEMO_RULE_STATES_SMART_BUILDING
@@ -107,7 +107,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
def "Turn all lights off when console exits the residence geofence"() {
List<PushNotificationMessage> pushMessages = []
- List<Email> emailMessages = []
+ List<javax.mail.Message> emailMessages = []
List<Notification.Target> pushTargets = []
List<Notification.Target> emailTargets = []
@@ -155,7 +155,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
}
// Assume sent to FCM
- mockEmailNotificationHandler.sendMessage(_ as Email) >> {
+ mockEmailNotificationHandler.sendMessage(_ as javax.mail.Message) >> {
email ->
emailMessages << email.get(0)
return NotificationSendResult.success()
@@ -164,10 +164,10 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
and: "some rules"
Ruleset ruleset = new RealmRuleset(
- keycloakTestSetup.realmBuilding.name,
- "Demo Apartment - All Lights Off",
- Ruleset.Lang.JSON,
- getClass().getResource("/org/openremote/test/rules/BasicJsonRules.json").text)
+ keycloakTestSetup.realmBuilding.name,
+ "Demo Apartment - All Lights Off",
+ Ruleset.Lang.JSON,
+ getClass().getResource("/org/openremote/test/rules/BasicJsonRules.json").text)
ruleset = rulesetStorageService.merge(ruleset)
expect: "the rule engines to become available and be running with asset states inserted and no longer tracking location rules"
@@ -192,43 +192,43 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
when: "a user authenticates"
def accessToken = authenticate(
- container,
- keycloakTestSetup.realmBuilding.name,
- KEYCLOAK_CLIENT_ID,
- "testuser3",
- "testuser3"
+ container,
+ keycloakTestSetup.realmBuilding.name,
+ KEYCLOAK_CLIENT_ID,
+ "testuser3",
+ "testuser3"
).token
and: "a console is registered by that user"
def authenticatedConsoleResource = getClientApiTarget(serverUri(serverPort), keycloakTestSetup.realmBuilding.name, accessToken).proxy(ConsoleResource.class)
def consoleRegistration = new ConsoleRegistration(null,
- "Test Console",
- "1.0",
- "Android 7.0",
- new HashMap<String, ConsoleProvider>() {
- {
- put("geofence", new ConsoleProvider(
- ORConsoleGeofenceAssetAdapter.NAME,
- true,
- false,
- false,
- false,
- false,
- null
- ))
- put("push", new ConsoleProvider(
- "fcm",
- true,
- true,
- true,
- true,
- false,
- ((ObjectNode) parse("{\"token\": \"23123213ad2313b0897efd\"}").orElse(null)
- )))
- }
- },
- "",
- ["manager"] as String[])
+ "Test Console",
+ "1.0",
+ "Android 7.0",
+ new HashMap<String, ConsoleProvider>() {
+ {
+ put("geofence", new ConsoleProvider(
+ ORConsoleGeofenceAssetAdapter.NAME,
+ true,
+ false,
+ false,
+ false,
+ false,
+ null
+ ))
+ put("push", new ConsoleProvider(
+ "fcm",
+ true,
+ true,
+ true,
+ true,
+ false,
+ ((ObjectNode) parse("{\"token\": \"23123213ad2313b0897efd\"}").orElse(null)
+ )))
+ }
+ },
+ "",
+ ["manager"] as String[])
consoleRegistration = authenticatedConsoleResource.register(null, consoleRegistration)
and: "the console location is marked as RULE_STATE"
@@ -243,7 +243,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
when:"an additional user is linked to this console (to help with testing)"
assetStorageService.storeUserAssetLinks(
- Collections.singletonList(new UserAssetLink(keycloakTestSetup.realmBuilding.getName(), keycloakTestSetup.testuser2Id, consoleRegistration.id))
+ Collections.singletonList(new UserAssetLink(keycloakTestSetup.realmBuilding.getName(), keycloakTestSetup.testuser2Id, consoleRegistration.id))
)
and: "the console location is set to the apartment"
@@ -297,16 +297,16 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
and: "an email notification should have been sent to [email protected] with the triggered asset in the body"
conditions.eventually {
- assert emailMessages.any {it.recipients.size() == 1
- && it.recipients[0].address == "[email protected]"
- && it.HTMLText == "<table cellpadding=\"30\"><tr><th>Asset ID</th><th>Asset Name</th><th>Attribute</th><th>Value</th></tr><tr><td>${consoleRegistration.id}</td><td>Test Console</td><td>location</td><td>" + ValueUtil.asJSON(outsideLocation).orElse("") + "</td></tr></table>"}
+ assert emailMessages.any {it.getRecipients(javax.mail.Message.RecipientType.TO).length == 1
+ && (it.getRecipients(javax.mail.Message.RecipientType.TO)[0] as InternetAddress).address == "[email protected]"
+ && MailUtil.toMailMessage(it, true).content == "<table cellpadding=\"30\"><tr><th>Asset ID</th><th>Asset Name</th><th>Attribute</th><th>Value</th></tr><tr><td>${consoleRegistration.id}</td><td>Test Console</td><td>location</td><td>" + ValueUtil.asJSON(outsideLocation).orElse("") + "</td></tr></table>"}
}
and : "an email notification should have been sent to the asset's linked user(s) (only testuser2 has email notifications enabled)"
conditions.eventually {
- assert emailMessages.any {it.recipients.size() == 1
- && it.recipients[0].address == "[email protected]"
- && it.HTMLText == "<table cellpadding=\"30\"><tr><th>Asset ID</th><th>Asset Name</th><th>Attribute</th><th>Value</th></tr><tr><td>${consoleRegistration.id}</td><td>Test Console</td><td>location</td><td>" + ValueUtil.asJSON(outsideLocation).orElse("") + "</td></tr></table>"}
+ assert emailMessages.any {it.getRecipients(javax.mail.Message.RecipientType.TO).length == 1
+ && (it.getRecipients(javax.mail.Message.RecipientType.TO)[0] as InternetAddress).address == "[email protected]"
+ && MailUtil.toMailMessage(it, true).content == "<table cellpadding=\"30\"><tr><th>Asset ID</th><th>Asset Name</th><th>Attribute</th><th>Value</th></tr><tr><td>${consoleRegistration.id}</td><td>Test Console</td><td>location</td><td>" + ValueUtil.asJSON(outsideLocation).orElse("") + "</td></tr></table>"}
}
and: "after a few seconds the rule should not have fired again"
@@ -351,7 +351,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
then: "the ruleset to be redeployed"
conditions.eventually {
assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.version == version+1
- assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == RulesetStatus.DEPLOYED
+ assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == DEPLOYED
}
and: "another notification should have been sent to the console when rule is redeployed"
@@ -427,16 +427,16 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
def recur = new Recur(Recur.DAILY, 3)
recur.setInterval(2)
def calendarEvent = new CalendarEvent(
- Date.from(validityStart),
- Date.from(validityEnd),
- recur)
+ Date.from(validityStart),
+ Date.from(validityEnd),
+ recur)
ruleset.setValidity(calendarEvent)
ruleset = rulesetStorageService.merge(ruleset)
then: "the ruleset should be redeployed and paused until 1st occurrence"
conditions.eventually {
assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.version == version+1
- assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == RulesetStatus.PAUSED
+ assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == PAUSED
}
when: "the same AttributeEvent is sent"
@@ -458,7 +458,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
then: "eventually the ruleset should be unpaused (1st occurrence)"
conditions.eventually {
- assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == RulesetStatus.DEPLOYED
+ assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == DEPLOYED
}
when: "the same AttributeEvent is sent"
@@ -482,7 +482,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
then: "the ruleset should become paused again (until next occurrence)"
conditions.eventually {
- assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == RulesetStatus.PAUSED
+ assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == PAUSED
}
when: "the same AttributeEvent is sent"
@@ -504,7 +504,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
then: "eventually the ruleset should be unpaused (next occurrence)"
conditions.eventually {
- assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == RulesetStatus.DEPLOYED
+ assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == DEPLOYED
}
when: "the un-pause elapses"
@@ -512,7 +512,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
then: "the ruleset should become paused again (until last occurrence)"
conditions.eventually {
- assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == RulesetStatus.PAUSED
+ assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == PAUSED
}
when: "the pause elapses"
@@ -520,7 +520,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
then: "eventually the ruleset should be unpaused (last occurrence)"
conditions.eventually {
- assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == RulesetStatus.DEPLOYED
+ assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == DEPLOYED
}
when: "the un-pause elapses"
@@ -528,7 +528,7 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
then: "the ruleset should expire"
conditions.eventually {
- assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == RulesetStatus.EXPIRED
+ assert realmBuildingEngine.deployments.find{it.key == ruleset.id}.value.status == EXPIRED
}
cleanup: "static variables are reset and the mock is removed"
@@ -567,14 +567,14 @@ class JsonRulesTest extends Specification implements ManagerContainerTrait {
and: "a thing asset is added to the building realm"
def thingId = UniqueIdentifierGenerator.generateId("TestThing")
def thingAsset = new ThingAsset("TestThing")
- .setId(thingId)
- .setRealm(keycloakTestSetup.realmBuilding.name)
- .setLocation(new GeoJSONPoint(0, 0))
- .addAttributes(
- new Attribute<>("sunset", ValueType.INTEGER, null),
- new Attribute<>("sunriseWithOffset", ValueType.INTEGER, null),
- new Attribute<>("twilightHorizon", ValueType.INTEGER, null),
- )
+ .setId(thingId)
+ .setRealm(keycloakTestSetup.realmBuilding.name)
+ .setLocation(new GeoJSONPoint(0, 0))
+ .addAttributes(
+ new Attribute<>("sunset", ValueType.INTEGER, null),
+ new Attribute<>("sunriseWithOffset", ValueType.INTEGER, null),
+ new Attribute<>("twilightHorizon", ValueType.INTEGER, null),
+ )
thingAsset = assetStorageService.merge(thingAsset)
and: "the pseudo clock is stopped"
diff --git a/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy b/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy
index c464cf3f8..a286a82b5 100644
--- a/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy
+++ b/test/src/testFixtures/groovy/org/openremote/test/ContainerTrait.groovy
@@ -32,6 +32,7 @@ import org.openremote.container.security.IdentityService
import org.openremote.container.security.PasswordAuthForm
import org.openremote.container.security.keycloak.KeycloakIdentityProvider
import org.openremote.container.timer.TimerService
+import org.openremote.container.util.LogUtil
import org.openremote.container.util.MapAccess
import org.openremote.container.web.DefaultWebsocketComponent
import org.openremote.container.web.WebClient
@@ -77,6 +78,10 @@ import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER
trait ContainerTrait {
+ static {
+ LogUtil.initialiseJUL()
+ }
+
Logger LOG = LoggerFactory.getLogger(ContainerTrait.class)
Container startContainer(Map<String, String> config, Iterable<ContainerService> services) {