Skip to content

armgit / core

Public Imported from GitHub
0

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

Added OAuth2.0 to mail client and agent (#1010)

Rich Turner91185f2e9bf9
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
index 2e72c80ac..27e795dcf 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/AbstractMailProtocol.java
@@ -28,11 +28,13 @@ 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.OAuthGrant;
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.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
@@ -46,7 +48,7 @@ public abstract class AbstractMailProtocol<T extends AbstractMailAgent<T, U, 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;
+ protected static int INITIAL_CHECK_DELAY_SECONDS = 10;
public AbstractMailProtocol(T agent) {
super(agent);
@@ -55,21 +57,20 @@ public abstract class AbstractMailProtocol<T extends AbstractMailAgent<T, U, V>,
@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");
+ Optional<OAuthGrant> oAuthGrant = getAgent().getOAuthGrant();
+ UsernamePassword userPassword = getAgent().getUsernamePassword().orElseThrow();
- mailClient = new MailClientBuilder(
+ MailClientBuilder clientBuilder = new MailClientBuilder(
container.getExecutorService(),
getAgent().getProtocol().orElseThrow(),
getAgent().getHost().orElseThrow(),
- getAgent().getPort().orElseThrow(),
- usernamePassword.getUsername(),
- usernamePassword.getPassword()
+ getAgent().getPort().orElseThrow()
)
- .setCheckIntervalMillis(
- getAgent().getCheckIntervalSeconds().orElse(MailClientBuilder.DEFAULT_CHECK_INTERVAL_MILLIS)
+ .setCheckIntervalSeconds(
+ getAgent().getCheckIntervalSeconds().orElse(MailClientBuilder.DEFAULT_CHECK_INTERVAL_SECONDS)
)
.setDeleteMessageOnceProcessed(
getAgent().getDeleteProcessedMail().orElse(false)
@@ -77,11 +78,14 @@ public abstract class AbstractMailProtocol<T extends AbstractMailAgent<T, U, V>,
.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)
+ .setCheckInitialDelaySeconds(INITIAL_CHECK_DELAY_SECONDS)
.setPreferHTML(getAgent().getPreferHTML().orElse(false))
- .setEarliestMessageDate(agent.getCreatedOn())
- .build();
+ .setEarliestMessageDate(agent.getCreatedOn());
+ oAuthGrant.map(oAuth -> clientBuilder.setOAuth(userPassword.getUsername(), oAuth)).orElseGet(() ->
+ clientBuilder.setBasicAuth(userPassword.getUsername(), userPassword.getPassword()));
+
+ mailClient = clientBuilder.build();
mailClient.addConnectionListener(this::onConnectionEvent);
mailClient.addMessageListener(this::onMailMessage);
mailClient.connect();
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
index 24a43bcb2..ca523f062 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mail/MailClient.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/MailClient.java
@@ -22,6 +22,7 @@ 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.auth.UsernamePassword;
import org.openremote.model.mail.MailMessage;
import org.openremote.model.syslog.SyslogCategory;
@@ -38,7 +39,6 @@ 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 {
@@ -93,7 +93,7 @@ public class MailClient {
try {
withFolder((folder) -> {
connected.set(true);
- mailChecker = config.getScheduledExecutorService().scheduleWithFixedDelay(this::checkForMessages, config.getCheckInitialDelayMillis(), config.getCheckIntervalMillis(), TimeUnit.MILLISECONDS);
+ mailChecker = config.getScheduledExecutorService().scheduleWithFixedDelay(this::checkForMessages, config.getCheckInitialDelaySeconds(), config.getCheckIntervalSeconds(), TimeUnit.SECONDS);
});
updateConnectionStatus(ConnectionStatus.CONNECTED);
@@ -140,7 +140,8 @@ public class MailClient {
LOG.log(System.Logger.Level.INFO, "Connecting to mail server: " + config.getHost());
try (Store mailStore = session.getStore()) {
- mailStore.connect(config.getUser(), config.getPassword());
+ UsernamePassword usernamePassword = config.getAuth();
+ mailStore.connect(usernamePassword.getUsername(), usernamePassword.getPassword());
Folder mailFolder = mailStore.getFolder(config.getFolder());
try {
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
index 898ea8a01..abc30351d 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mail/MailClientBuilder.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/MailClientBuilder.java
@@ -19,50 +19,76 @@
*/
package org.openremote.agent.protocol.mail;
+import org.openremote.container.web.OAuthFilter;
+import org.openremote.container.web.WebTargetBuilder;
+import org.openremote.model.auth.OAuthGrant;
+import org.openremote.model.auth.UsernamePassword;
+
+import javax.ws.rs.client.Client;
+import java.net.SocketException;
import java.nio.file.Path;
import java.util.Date;
+import java.util.Objects;
import java.util.Properties;
import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.atomic.AtomicReference;
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;
+ public static final int DEFAULT_CHECK_INTERVAL_SECONDS = 5 * 60;
+ protected static int MIN_CHECK_INTERVAL_SECONDS = 10;
+ protected static final AtomicReference<Client> jaxrsClient = new AtomicReference<>();
protected ScheduledExecutorService scheduledExecutorService;
- protected int checkInitialDelayMillis = 0;
- protected int checkIntervalMillis = DEFAULT_CHECK_INTERVAL_MILLIS;
+ protected int checkInitialDelaySeconds = 0;
+ protected int checkIntervalSeconds = DEFAULT_CHECK_INTERVAL_SECONDS;
protected String folder;
protected boolean preferHTML;
protected Date earliestMessageDate;
protected Path persistenceDir;
protected String protocol;
protected String host;
+ protected OAuthGrant oAuthGrant;
+ protected OAuthFilter oAuthFilter;
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) {
+ public MailClientBuilder(ScheduledExecutorService scheduledExecutorService, String protocol, String host, int port) {
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);
+ public MailClientBuilder setBasicAuth(String user, String password) {
+ this.user = user;
+ this.password = password;
+ return this;
+ }
+
+ public MailClientBuilder setOAuth(String user, OAuthGrant oAuthGrant) {
+ this.user = user;
+ this.oAuthGrant = oAuthGrant;
+ setProperty("auth.mechanisms", "XOAUTH2");
+ setProperty("mail.imap.sasl.enable", "true");
+ setProperty("auth.login.disable", "true");
+ setProperty("auth.plain.disable", "true");
return this;
}
- public MailClientBuilder setCheckInitialDelayMillis(int checkInitialDelayMillis) {
- this.checkInitialDelayMillis = Math.max(checkInitialDelayMillis, 0);
+ public MailClientBuilder setCheckIntervalSeconds(int checkIntervalSeconds) {
+ this.checkIntervalSeconds = Math.max(checkIntervalSeconds, MIN_CHECK_INTERVAL_SECONDS);
+ return this;
+ }
+
+ public MailClientBuilder setCheckInitialDelaySeconds(int checkInitialDelaySeconds) {
+ this.checkInitialDelaySeconds = Math.max(checkInitialDelaySeconds, 0);
return this;
}
@@ -90,6 +116,9 @@ public class MailClientBuilder {
}
public MailClientBuilder setProperty(String property, Object value) {
+ if (!property.startsWith("mail." + protocol + ".")) {
+ property = "mail." + protocol + "." + property;
+ }
properties.put(property, value);
return this;
}
@@ -107,12 +136,12 @@ public class MailClientBuilder {
return scheduledExecutorService;
}
- public int getCheckIntervalMillis() {
- return checkIntervalMillis;
+ public int getCheckIntervalSeconds() {
+ return checkIntervalSeconds;
}
- public int getCheckInitialDelayMillis() {
- return checkInitialDelayMillis;
+ public int getCheckInitialDelaySeconds() {
+ return checkInitialDelaySeconds;
}
public Date getEarliestMessageDate() {
@@ -143,6 +172,10 @@ public class MailClientBuilder {
return password;
}
+ public OAuthGrant getOAuthGrant() {
+ return oAuthGrant;
+ }
+
public boolean isDeleteMessageOnceProcessed() {
return deleteMessageOnceProcessed;
}
@@ -158,4 +191,26 @@ public class MailClientBuilder {
public MailClient build() {
return new MailClient(this);
}
+
+ UsernamePassword getAuth() throws SocketException, NullPointerException {
+ Objects.requireNonNull(user, "User must be supplied");
+
+ if (oAuthGrant != null) {
+ synchronized (jaxrsClient) {
+ if (jaxrsClient.get() == null) {
+ jaxrsClient.set(WebTargetBuilder.createClient(scheduledExecutorService));
+ }
+ }
+ synchronized (this) {
+ if (oAuthFilter == null) {
+ oAuthFilter = new OAuthFilter(jaxrsClient.get(), oAuthGrant);
+ }
+ }
+ return new UsernamePassword(user, oAuthFilter.getAccessToken());
+ }
+
+ Objects.requireNonNull(password, "Password must be supplied");
+
+ return new UsernamePassword(user, password);
+ }
}
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
index 86687bff2..ed6eead00 100644
--- a/agent/src/main/java/org/openremote/agent/protocol/mail/MailProtocol.java
+++ b/agent/src/main/java/org/openremote/agent/protocol/mail/MailProtocol.java
@@ -19,6 +19,7 @@
*/
package org.openremote.agent.protocol.mail;
+import org.openremote.container.timer.TimerService;
import org.openremote.model.attribute.Attribute;
import org.openremote.model.auth.UsernamePassword;
import org.openremote.model.mail.MailMessage;
@@ -35,7 +36,6 @@ public class MailProtocol extends AbstractMailProtocol<MailAgent, MailProtocol,
super(agent);
}
-
@Override
public String getProtocolName() {
return PROTOCOL_DISPLAY_NAME;
@@ -54,7 +54,10 @@ public class MailProtocol extends AbstractMailProtocol<MailAgent, MailProtocol,
@Override
protected Predicate<MailMessage> getAttributeMailMessageFilter(String assetId, Attribute<?> attribute, MailAgentLink agentLink) {
+ return getAttributeMailMessageFilter(timerService, agentLink);
+ }
+ public static <T extends MailAgentLink> Predicate<MailMessage> getAttributeMailMessageFilter(TimerService timerService, T agentLink) {
StringPredicate fromPredicate = agentLink.getFromMatchPredicate();
StringPredicate subjectPredicate = agentLink.getSubjectMatchPredicate();
diff --git a/container/src/main/java/org/openremote/container/web/ExtensibleResteasyClientBuilder.java b/container/src/main/java/org/openremote/container/web/ExtensibleResteasyClientBuilder.java
deleted file mode 100644
index cec07cda9..000000000
--- a/container/src/main/java/org/openremote/container/web/ExtensibleResteasyClientBuilder.java
+++ /dev/null
@@ -1,176 +0,0 @@
-package org.openremote.container.web;
-
-import org.apache.http.client.HttpClient;
-import org.apache.http.client.config.RequestConfig;
-import org.apache.http.config.Registry;
-import org.apache.http.config.RegistryBuilder;
-import org.apache.http.conn.HttpClientConnectionManager;
-import org.apache.http.conn.socket.ConnectionSocketFactory;
-import org.apache.http.conn.socket.PlainConnectionSocketFactory;
-import org.apache.http.conn.ssl.DefaultHostnameVerifier;
-import org.apache.http.conn.ssl.NoopHostnameVerifier;
-import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
-import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
-import org.apache.http.impl.client.HttpClientBuilder;
-import org.apache.http.impl.conn.BasicHttpClientConnectionManager;
-import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
-import org.apache.http.ssl.SSLContexts;
-import org.jboss.resteasy.client.jaxrs.ResteasyClient;
-import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
-import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient43Engine;
-import org.jboss.resteasy.client.jaxrs.engines.PassthroughTrustManager;
-import org.jboss.resteasy.client.jaxrs.engines.factory.ApacheHttpClient4EngineFactory;
-
-import javax.net.ssl.*;
-import java.io.IOException;
-import java.security.SecureRandom;
-
-/**
- * This tremendous code was copied from Resteasy. Make the private static SPI public again. Sad.
- * <p>
- * TODO https://issues.jboss.org/browse/RESTEASY-1599
- */
-public class ExtensibleResteasyClientBuilder extends ResteasyClientBuilder {
-
- @Override
- public ResteasyClient build() {
- httpEngine(initDefaultEngine43(this));
- return super.build();
- }
-
- @SuppressWarnings("deprecation")
- @Override
- public ResteasyClient buildOld() {
- throw new UnsupportedOperationException("Just don't...");
- }
-
- /**
- * You want to override this sometimes. You really do.
- */
- public HttpClientBuilder configure(HttpClientBuilder httpClientBuilder) {
- return httpClientBuilder;
- }
-
- // The rest is copy/paste pretty much
-
- static public ApacheHttpClient43Engine initDefaultEngine43(ExtensibleResteasyClientBuilder that) {
-
- HttpClient httpClient = null;
-
- HostnameVerifier verifier = null;
- if (that.verifier != null) {
- verifier = new ExtensibleResteasyClientBuilder.VerifierWrapper(that.verifier);
- } else {
- switch (that.policy) {
- case ANY:
- verifier = new NoopHostnameVerifier();
- break;
- case WILDCARD:
- verifier = new DefaultHostnameVerifier();
- break;
- case STRICT:
- verifier = new DefaultHostnameVerifier();
- break;
- }
- }
- try {
- SSLConnectionSocketFactory sslsf = null;
- SSLContext theContext = that.sslContext;
- if (that.disableTrustManager) {
- theContext = SSLContext.getInstance("SSL");
- theContext.init(null, new TrustManager[]{new PassthroughTrustManager()},
- new SecureRandom());
- verifier = new NoopHostnameVerifier();
- sslsf = new SSLConnectionSocketFactory(theContext, verifier);
- } else if (theContext != null) {
- sslsf = new SSLConnectionSocketFactory(theContext, verifier) {
- @Override
- protected void prepareSocket(SSLSocket socket) throws IOException {
- that.prepareSocketForSni(socket);
- }
- };
- } else if (that.clientKeyStore != null || that.truststore != null) {
- SSLContext ctx = SSLContexts.custom()
- .setProtocol(SSLConnectionSocketFactory.TLS)
- .setSecureRandom(null)
- .loadKeyMaterial(that.clientKeyStore,
- that.clientPrivateKeyPassword != null ? that.clientPrivateKeyPassword.toCharArray() : null)
- .loadTrustMaterial(that.truststore, TrustSelfSignedStrategy.INSTANCE)
- .build();
- sslsf = new SSLConnectionSocketFactory(ctx, verifier) {
- @Override
- protected void prepareSocket(SSLSocket socket) throws IOException {
- that.prepareSocketForSni(socket);
- }
- };
- } else {
- final SSLContext tlsContext = SSLContext.getInstance(SSLConnectionSocketFactory.TLS);
- tlsContext.init(null, null, null);
- sslsf = new SSLConnectionSocketFactory(tlsContext, verifier);
- }
-
- final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
- .register("http", PlainConnectionSocketFactory.getSocketFactory())
- .register("https", sslsf)
- .build();
-
- HttpClientConnectionManager cm = null;
- if (that.connectionPoolSize > 0) {
- PoolingHttpClientConnectionManager tcm = new PoolingHttpClientConnectionManager(
- registry, null, null, null, that.connectionTTL, that.connectionTTLUnit);
- tcm.setMaxTotal(that.connectionPoolSize);
- if (that.maxPooledPerRoute == 0) {
- that.maxPooledPerRoute = that.connectionPoolSize;
- }
- tcm.setDefaultMaxPerRoute(that.maxPooledPerRoute);
- cm = tcm;
-
- } else {
- cm = new BasicHttpClientConnectionManager(registry);
- }
-
- RequestConfig.Builder rcBuilder = RequestConfig.custom();
- if (that.socketTimeout > -1) {
- rcBuilder.setSocketTimeout((int) that.socketTimeoutUnits.toMillis(that.socketTimeout));
- }
- if (that.establishConnectionTimeout > -1) {
- rcBuilder.setConnectTimeout((int) that.establishConnectionTimeoutUnits.toMillis(that.establishConnectionTimeout));
- }
- if (that.connectionCheckoutTimeoutMs > -1) {
- rcBuilder.setConnectionRequestTimeout(that.connectionCheckoutTimeoutMs);
- }
-
- // The magic configure()
- httpClient = that.configure(
- HttpClientBuilder.create()
- .setConnectionManager(cm)
- .setDefaultRequestConfig(rcBuilder.build())
- .setProxy(that.defaultProxy)
- .disableContentCompression()
- ).build();
-
- ApacheHttpClient43Engine engine =
- (ApacheHttpClient43Engine) ApacheHttpClient4EngineFactory.create(httpClient, true);
- engine.setResponseBufferSize(that.responseBufferSize);
- engine.setHostnameVerifier(verifier);
- // this may be null. We can't really support this with Apache Client.
- engine.setSslContext(theContext);
- return engine;
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public static class VerifierWrapper implements HostnameVerifier {
- protected HostnameVerifier verifier;
-
- VerifierWrapper(HostnameVerifier verifier) {
- this.verifier = verifier;
- }
-
- @Override
- public boolean verify(String s, SSLSession sslSession) {
- return verifier.verify(s, sslSession);
- }
- }
-}
diff --git a/container/src/main/java/org/openremote/container/web/OAuthFilter.java b/container/src/main/java/org/openremote/container/web/OAuthFilter.java
index bc330e7e1..153736fc7 100644
--- a/container/src/main/java/org/openremote/container/web/OAuthFilter.java
+++ b/container/src/main/java/org/openremote/container/web/OAuthFilter.java
@@ -19,7 +19,6 @@
*/
package org.openremote.container.web;
-import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.util.BasicAuthHelper;
import org.openremote.model.auth.OAuthGrant;
import org.openremote.model.auth.OAuthRefreshTokenGrant;
@@ -47,11 +46,11 @@ public class OAuthFilter implements ClientRequestFilter {
private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, OAuthFilter.class);
public static final String BEARER_AUTH = "Bearer";
protected OAuthServerResponse authServerResponse;
- protected ResteasyClient client;
+ protected Client client;
protected WebTarget authTarget;
protected OAuthGrant oAuthGrant;
- public OAuthFilter(ResteasyClient client, OAuthGrant oAuthGrant) {
+ public OAuthFilter(Client client, OAuthGrant oAuthGrant) {
Objects.requireNonNull(client);
Objects.requireNonNull(oAuthGrant);
this.client = client;
diff --git a/container/src/main/java/org/openremote/container/web/ProxyWebClientBuilder.java b/container/src/main/java/org/openremote/container/web/ProxyWebClientBuilder.java
deleted file mode 100644
index 52361b267..000000000
--- a/container/src/main/java/org/openremote/container/web/ProxyWebClientBuilder.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Copyright 2016, OpenRemote Inc.
- *
- * See the CONTRIBUTORS.txt file in the distribution for a
- * full listing of individual contributors.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.openremote.container.web;
-
-import org.apache.http.HttpHost;
-import org.apache.http.HttpRequestInterceptor;
-import org.apache.http.impl.client.HttpClientBuilder;
-import org.apache.http.protocol.HTTP;
-
-import javax.ws.rs.core.HttpHeaders;
-
-/**
- * This client will always set the configured Host header on all outgoing requests.
- * We use this to emulate a reverse proxy that "preserves" the Host header. Using a
- * request interceptor is the only reliable method for setting the Host header
- * in Apache HttpClient.
- */
-public class ProxyWebClientBuilder extends ExtensibleResteasyClientBuilder {
-
- final protected String proxyHost;
- final protected Integer proxyPort;
-
- public ProxyWebClientBuilder(String proxyHost, Integer proxyPort) {
- this.proxyHost = proxyHost;
- this.proxyPort = proxyPort;
- }
-
- @Override
- public HttpClientBuilder configure(HttpClientBuilder httpClientBuilder) {
- httpClientBuilder = super.configure(httpClientBuilder);
- httpClientBuilder.addInterceptorLast((HttpRequestInterceptor) (request, context) ->
- request.setHeader(HTTP.TARGET_HOST, new HttpHost(proxyHost, proxyPort).toHostString())
- );
- return httpClientBuilder;
- }
-}
diff --git a/container/src/main/java/org/openremote/container/web/WebTargetBuilder.java b/container/src/main/java/org/openremote/container/web/WebTargetBuilder.java
index afaaa9708..62398739e 100644
--- a/container/src/main/java/org/openremote/container/web/WebTargetBuilder.java
+++ b/container/src/main/java/org/openremote/container/web/WebTargetBuilder.java
@@ -52,7 +52,8 @@ import java.util.stream.Collectors;
* <li>{@link org.openremote.container.json.JacksonConfig}.</li>
* </ul>
*/
-// TODO: This should probably be amalgamated with WebClient somehow to provide a unified JAX-RS Client API
+// TODO: This should probably be amalgamated with WebClient somehow to provide a unified JAX-RS Client API and a default
+// client should be made available on the Container
public class WebTargetBuilder {
public static final int CONNECTION_POOL_SIZE = 10;
diff --git a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointService.java b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointService.java
index e2c6957e6..414059ad2 100644
--- a/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointService.java
+++ b/manager/src/main/java/org/openremote/manager/datapoint/AssetPredictedDatapointService.java
@@ -84,6 +84,12 @@ public class AssetPredictedDatapointService extends AbstractDatapointService<Ass
persistenceService.doTransaction(em -> upsertValues(assetId, attributeName, valuesAndTimestamps));
}
+ public void purgeValues(String assetId, String attributeName) {
+ persistenceService.doTransaction(em -> em.createQuery(
+ "delete from " + getDatapointClass().getSimpleName() + " dp where dp.assetId=?1 and dp.attributeName=?2"
+ ).setParameter(1, assetId).setParameter(2, attributeName).executeUpdate());
+ }
+
@Override
protected Class<AssetPredictedDatapoint> getDatapointClass() {
return AssetPredictedDatapoint.class;
diff --git a/model/build.gradle b/model/build.gradle
index d6bffe0fb..88da3dcb6 100644
--- a/model/build.gradle
+++ b/model/build.gradle
@@ -2,9 +2,6 @@ apply plugin: "java-library"
apply plugin: "maven-publish"
apply plugin: "signing"
-compileJava.options.compilerArgs.add '-parameters'
-compileTestJava.options.compilerArgs.add '-parameters'
-
dependencies {
api "com.fasterxml.jackson.core:jackson-databind:$jacksonVersion"
api "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:$jacksonVersion"
diff --git a/project.gradle b/project.gradle
index 038e97b6b..16cc1e3cd 100644
--- a/project.gradle
+++ b/project.gradle
@@ -307,7 +307,7 @@ plugins.withType(JavaPlugin).whenPluginAdded {
warnLogFile << output
}
})
- options.compilerArgs += ["-Xlint:unchecked", "-Xlint:deprecation"]
+ options.compilerArgs += ["-Xlint:unchecked", "-Xlint:deprecation", "-parameters"]
options.encoding = 'UTF-8'
}
diff --git a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
index a9540e3a7..9cfcb5b0c 100644
--- a/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/assets/AssetDatapointTest.groovy
@@ -5,9 +5,11 @@ import org.openremote.manager.agent.AgentService
import org.openremote.manager.asset.AssetProcessingService
import org.openremote.manager.asset.AssetStorageService
import org.openremote.manager.datapoint.AssetDatapointService
+import org.openremote.manager.datapoint.AssetPredictedDatapointService
import org.openremote.manager.setup.SetupService
import org.openremote.model.attribute.AttributeRef
import org.openremote.model.datapoint.query.AssetDatapointIntervalQuery
+import org.openremote.model.util.Pair
import org.openremote.model.util.ValueUtil
import org.openremote.setup.integration.ManagerTestSetup
import org.openremote.test.ManagerContainerTrait
@@ -40,6 +42,7 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
def assetStorageService = container.getService(AssetStorageService.class)
def assetProcessingService = container.getService(AssetProcessingService.class)
def assetDatapointService = container.getService(AssetDatapointService.class)
+ def assetPredictedDatapointService = container.getService(AssetPredictedDatapointService.class)
and: "the clock is stopped for testing purposes and advanced to the next hour"
stopPseudoClock()
@@ -343,5 +346,44 @@ class AssetDatapointTest extends Specification implements ManagerContainerTrait
def datapoints = assetDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))
assert datapoints.isEmpty()
}
+
+ when: "predicted data points are added"
+ assetPredictedDatapointService.upsertValues(managerTestSetup.thingId, "light1PowerConsumption",
+ [
+ new Pair<>(10d, LocalDateTime.now().plusSeconds(60)),
+ new Pair<>(20d, LocalDateTime.now().plusSeconds(120)),
+ new Pair<>(30d, LocalDateTime.now().plusSeconds(180)),
+ new Pair<>(40d, LocalDateTime.now().plusSeconds(240)),
+ new Pair<>(50d, LocalDateTime.now().plusSeconds(300))
+ ]
+ )
+ assetPredictedDatapointService.upsertValues(managerTestSetup.thingId, thingLightToggleAttributeName,
+ [
+ new Pair<>(true, LocalDateTime.now().plusSeconds(60)),
+ new Pair<>(true, LocalDateTime.now().plusSeconds(120)),
+ new Pair<>(true, LocalDateTime.now().plusSeconds(180)),
+ new Pair<>(false, LocalDateTime.now().plusSeconds(240)),
+ new Pair<>(false, LocalDateTime.now().plusSeconds(300))
+ ]
+ )
+
+ then: "the predicted data should be available"
+ def predictedData = assetPredictedDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))
+ assert predictedData.size() == 5
+ assert predictedData.any {it.value == 20d}
+
+ when: "the predicted data is purged and then retrieved"
+ assetPredictedDatapointService.purgeValues(managerTestSetup.thingId, "light1PowerConsumption")
+ predictedData = assetPredictedDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, "light1PowerConsumption"))
+
+ then: "no predicted data should remain for this attribute"
+ assert predictedData.isEmpty()
+
+ when: "other predicted data is retrieved"
+ predictedData = assetPredictedDatapointService.getDatapoints(new AttributeRef(managerTestSetup.thingId, thingLightToggleAttributeName))
+
+ then: "predicted data should remain for this attribute"
+ assert predictedData.size() == 5
+ assert predictedData.count {it.value == false} == 2
}
}
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
index 142f6193e..ac71066bc 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientProtocolTest.groovy
@@ -35,17 +35,12 @@ 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
@@ -54,7 +49,6 @@ 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 {
@@ -68,8 +62,8 @@ class MailClientProtocolTest extends Specification implements ManagerContainerTr
def setupSpec() {
- MailClientBuilder.MIN_CHECK_INTERVAL_MILLIS = 2000
- AbstractMailProtocol.INITIAL_CHECK_DELAY_MILLIS = 2000
+ MailClientBuilder.MIN_CHECK_INTERVAL_SECONDS = 2
+ AbstractMailProtocol.INITIAL_CHECK_DELAY_SECONDS = 2
greenMail = new GreenMail(ServerSetupTest.ALL)
greenMail.start()
user = greenMail.setUser("or@localhost", "or", "secret")
@@ -121,7 +115,7 @@ class MailClientProtocolTest extends Specification implements ManagerContainerTr
.setHost("127.0.0.1")
.setPort(greenMail.getPop3().getServerSetup().getPort())
.setUsernamePassword(new UsernamePassword("or", "secret"))
- .setCheckIntervalSeconds(2000)
+ .setCheckIntervalSeconds(2)
.setDisabled(true)
and: "the agent is added to the asset service"
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
index 448369b69..35cfc7bf8 100644
--- a/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientTest.groovy
+++ b/test/src/test/groovy/org/openremote/test/protocol/mail/MailClientTest.groovy
@@ -53,7 +53,7 @@ class MailClientTest extends Specification implements ManagerContainerTrait {
def setupSpec() {
- MailClientBuilder.MIN_CHECK_INTERVAL_MILLIS = 2000
+ MailClientBuilder.MIN_CHECK_INTERVAL_SECONDS = 2
greenMail = new GreenMail(ServerSetupTest.ALL)
greenMail.start()
user = greenMail.setUser("or@localhost", "or", "secret")
@@ -84,8 +84,9 @@ class MailClientTest extends Specification implements ManagerContainerTrait {
executor,
"pop3",
"localhost",
- greenMail.getPop3().getServerSetup().getPort(), "or", "secret")
- .setCheckIntervalMillis(2000)
+ greenMail.getPop3().getServerSetup().getPort())
+ .setBasicAuth("or", "secret")
+ .setCheckIntervalSeconds(2)
.setPersistenceDir(Paths.get("tmp"))
.build()
mailClient.addConnectionListener{ connectionEvents.add(it)}
@@ -158,8 +159,9 @@ class MailClientTest extends Specification implements ManagerContainerTrait {
executor,
"pop3",
"localhost",
- greenMail.getPop3().getServerSetup().getPort(), "or", "secret")
- .setCheckIntervalMillis(2000)
+ greenMail.getPop3().getServerSetup().getPort())
+ .setBasicAuth("or", "secret")
+ .setCheckIntervalSeconds(2)
.setPersistenceDir(Paths.get("tmp"))
.setPreferHTML(true)
.build()
@@ -212,8 +214,9 @@ class MailClientTest extends Specification implements ManagerContainerTrait {
executor,
"imap",
"localhost",
- greenMail.getImap().getServerSetup().getPort(), "or", "secret")
- .setCheckIntervalMillis(2000)
+ greenMail.getImap().getServerSetup().getPort())
+ .setBasicAuth("or", "secret")
+ .setCheckIntervalSeconds(2)
.build()
mailClient.addConnectionListener{ connectionEvents.add(it)}
mailClient.addMessageListener{messages.add(it)}