100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Added WebsocketClientProtocol with tests
Rich Turner307304c70b59
diff --git a/agent/build.gradle b/agent/build.gradleindex eda82de28..b1f546c1d 100644--- a/agent/build.gradle+++ b/agent/build.gradle@@ -7,6 +7,7 @@ dependencies {compile "io.netty:netty-transport-rxtx:$nettyVersion"compile "io.netty:netty-codec:$nettyVersion"+ compile "io.netty:netty-codec-http:$nettyVersion"compile "com.neuronrobotics:nrjavaserial:$nrjavaserialVersion"compile "com.luckycatlabs:SunriseSunsetCalculator:$sunriseSunsetCalculatorVersion"diff --git a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.javaindex 62ea10763..4980e8515 100644--- a/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java+++ b/agent/src/main/java/org/openremote/agent/protocol/http/OAuthFilter.java@@ -64,7 +64,7 @@ public class OAuthFilter implements ClientRequestFilter {return null;}- protected synchronized String getAccessToken() {+ public synchronized String getAccessToken() {LocalDateTime expiryDateTime = authServerResponse == null ? null : authServerResponse.getExpiryDateTime();boolean updateRequired = expiryDateTime == null || expiryDateTime.minusSeconds(10).isBefore(LocalDateTime.now());diff --git a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.javaindex 1a6382076..03fefd071 100644--- a/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java+++ b/agent/src/main/java/org/openremote/agent/protocol/io/AbstractNettyIoClient.java@@ -234,7 +234,7 @@ public abstract class AbstractNettyIoClient<T, U extends SocketAddress> implemen@Overridepublic void sendMessage(T message) {if (connectionStatus != ConnectionStatus.CONNECTED) {- LOG.fine("Cannot send message: Status = " + connectionStatus + ": " + getSocketAddressString());+ LOG.warning("Cannot send message: Status = " + connectionStatus + ": " + getSocketAddressString());return;}diff --git a/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.javaindex f13c811f7..0641e54ba 100644--- a/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java+++ b/agent/src/main/java/org/openremote/agent/protocol/udp/UdpClientProtocol.java@@ -24,6 +24,7 @@ import io.netty.util.CharsetUtil;import org.openremote.agent.protocol.AbstractProtocol;import org.openremote.agent.protocol.Protocol;import org.openremote.agent.protocol.io.IoClient;+import org.openremote.container.Container;import org.openremote.model.asset.AssetAttribute;import org.openremote.model.asset.agent.ConnectionStatus;import org.openremote.model.asset.agent.ProtocolConfiguration;@@ -289,6 +290,11 @@ public class UdpClientProtocol extends AbstractProtocol {META_SERVER_ALWAYS_RESPONDS);+ @Override+ protected void doStop(Container container) {+ clientMap.forEach((ref, clientAndQueue) -> clientAndQueue.disconnect());+ }+@Overridepublic String getProtocolName() {return PROTOCOL_NAME;diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClient.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClient.javanew file mode 100644index 000000000..185aa3815--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClient.java@@ -0,0 +1,293 @@+/*+ * Copyright 2019, 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.websocket;++import io.netty.buffer.ByteBuf;+import io.netty.channel.*;+import io.netty.channel.nio.NioEventLoopGroup;+import io.netty.channel.socket.nio.NioSocketChannel;+import io.netty.handler.codec.MessageToMessageEncoder;+import io.netty.handler.codec.http.*;+import io.netty.handler.codec.http.websocketx.*;+import io.netty.handler.codec.http.websocketx.extensions.compression.WebSocketClientCompressionHandler;+import io.netty.handler.ssl.SslContext;+import io.netty.handler.ssl.SslContextBuilder;+import io.netty.handler.ssl.util.InsecureTrustManagerFactory;+import io.netty.util.CharsetUtil;+import org.apache.http.client.utils.URIBuilder;+import org.jboss.resteasy.client.jaxrs.ResteasyClient;+import org.openremote.agent.protocol.ProtocolExecutorService;+import org.openremote.agent.protocol.http.OAuthFilter;+import org.openremote.agent.protocol.http.OAuthGrant;+import org.openremote.agent.protocol.io.AbstractNettyIoClient;+import org.openremote.model.asset.agent.ConnectionStatus;+import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.TextUtil;++import javax.ws.rs.client.WebTarget;+import javax.ws.rs.core.MultivaluedMap;+import java.net.InetSocketAddress;+import java.net.URI;+import java.util.List;+import java.util.logging.Level;+import java.util.logging.Logger;++import static org.openremote.agent.protocol.http.WebTargetBuilder.CONNECTION_TIMEOUT_MILLISECONDS;+import static org.openremote.agent.protocol.http.WebTargetBuilder.createClient;+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;++public class WebsocketClient extends AbstractNettyIoClient<String, InetSocketAddress> {++ public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {++ private final WebSocketClientHandshaker handshaker;+ private ChannelPromise handshakeFuture;++ public WebSocketClientHandler(WebSocketClientHandshaker handshaker) {+ this.handshaker = handshaker;+ }++ public ChannelFuture handshakeFuture() {+ return handshakeFuture;+ }++ @Override+ public void handlerAdded(ChannelHandlerContext ctx) {+ handshakeFuture = ctx.newPromise();+ }++ @Override+ public void channelActive(ChannelHandlerContext ctx) throws Exception {+ handshaker.handshake(ctx.channel());+ }++ @Override+ public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {+ Channel ch = ctx.channel();+ if (!handshaker.isHandshakeComplete()) {+ try {+ handshaker.finishHandshake(ch, (FullHttpResponse) msg);+ synchronized (WebsocketClient.this) {+ LOG.fine("Connected: " + getSocketAddressString());+ onConnectionStatusChanged(ConnectionStatus.CONNECTED);+ }++ handshakeFuture.setSuccess();+ } catch (WebSocketHandshakeException e) {+ LOG.log(Level.SEVERE, "Connection failed: " + getSocketAddressString(), e);+ setPermanentError("Connection failed: " + e.getMessage());+ handshakeFuture.setFailure(e);+ }+ return;+ }++ if (msg instanceof FullHttpResponse) {+ FullHttpResponse response = (FullHttpResponse) msg;+ LOG.severe("Websocket client unexpected FullHttpResponse (getStatus=" + response.status() ++ ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');+ }++ WebSocketFrame frame = (WebSocketFrame) msg;+ if (frame instanceof TextWebSocketFrame) {+ TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;+ WebsocketClient.this.onMessageReceived((InetSocketAddress)ch.remoteAddress(), textFrame.text());+ } else if (frame instanceof PongWebSocketFrame) {+ LOG.finest("Websocket client pong received");+ } else if (frame instanceof CloseWebSocketFrame) {+ System.out.println("WebSocket Client received closing");+ ch.close();+ }+ }++ @Override+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {+ LOG.log(Level.SEVERE, "Websocket client exception caught", cause);+ if (!handshakeFuture.isDone()) {+ handshakeFuture.setFailure(cause);+ }+ ctx.close();+ }+ }++ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, WebsocketClient.class);+ protected boolean useSsl;+ protected URI uri;+ protected SslContext sslCtx;+ protected WebSocketClientHandler handler;+ protected MultivaluedMap<String, String> headers;+ protected OAuthGrant oAuthGrant;+ protected String authHeaderValue;+ protected String host;+ protected int port;++ public WebsocketClient(URI uri, ProtocolExecutorService executorService) {+ this(uri, null, null, executorService);+ }++ public WebsocketClient(URI uri, MultivaluedMap<String, String> headers, OAuthGrant oAuthGrant, ProtocolExecutorService executorService) {+ super(executorService);++ this.uri = uri;+ this.headers = headers;+ this.oAuthGrant = oAuthGrant;++ String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();+ host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();++ if (uri.getPort() == -1) {+ if ("ws".equalsIgnoreCase(scheme)) {+ port = 80;+ } else if ("wss".equalsIgnoreCase(scheme)) {+ port = 443;+ } else {+ port = -1;+ }+ } else {+ port = uri.getPort();+ }++ if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {+ LOG.warning("Only WS(S) is supported: " + getSocketAddressString());+ setPermanentError("Only WS(S) is supported");+ return;+ }++ useSsl = "wss".equalsIgnoreCase(scheme);+ }++ @Override+ protected Class<? extends Channel> getChannelClass() {+ return NioSocketChannel.class;+ }++ @Override+ protected String getSocketAddressString() {+ return uri.toString();+ }++ @Override+ protected EventLoopGroup getWorkerGroup() {+ return new NioEventLoopGroup(1);+ }++ @Override+ protected ChannelFuture startChannel() {+ return bootstrap.connect(new InetSocketAddress(host, port));+ }++ @Override+ protected void initChannel(Channel channel) {+ try {+ if (useSsl) {+ sslCtx = SslContextBuilder.forClient()+ .trustManager(InsecureTrustManagerFactory.INSTANCE).build();+ } else {+ sslCtx = null;+ }++ HttpHeaders hdrs = new DefaultHttpHeaders();++ if (this.headers != null) {+ this.headers.forEach(hdrs::add);+ }++ if (authHeaderValue != null) {+ hdrs.set(HttpHeaderNames.AUTHORIZATION, authHeaderValue);+ }++ // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00.+ // If you change it to V00, ping is not supported and remember to change+ // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline.+ handler =+ new WebSocketClientHandler(+ WebSocketClientHandshakerFactory.newHandshaker(+ uri, WebSocketVersion.V13, null, true, hdrs));++ } catch (Exception e) {+ LOG.log(Level.SEVERE, "Failed to initialise channel: " + getSocketAddressString(), e);+ setPermanentError(e.getMessage());+ return;+ }++ super.initChannel(channel);+ }++ @Override+ protected void addDecoders(Channel channel) {+ if (sslCtx != null) {+ channel.pipeline().addLast(sslCtx.newHandler(channel.alloc(), host, port));+ }+ channel.pipeline().addLast(+ new HttpClientCodec(),+ new HttpObjectAggregator(8192),+ WebSocketClientCompressionHandler.INSTANCE,+ handler);+ }++ @Override+ protected void addEncoders(Channel channel) {+ channel.pipeline().addLast(new MessageToMessageEncoder<String>() {+ @Override+ protected void encode(ChannelHandlerContext ctx, String msg, List<Object> out) throws Exception {+ out.add(new TextWebSocketFrame(msg));+ }+ });+ }++ @Override+ protected void decode(ByteBuf buf, List<String> messages) {+ // Not used+ }++ @Override+ protected void encode(String message, ByteBuf buf) {+ // Not used+ }++ @Override+ public synchronized void connect() {+ if (oAuthGrant != null) {+ LOG.fine("Retrieving OAuth access token: " + getSocketAddressString());++ ResteasyClient client = createClient(executorService, 1, CONNECTION_TIMEOUT_MILLISECONDS);++ try {+ WebTarget authTarget = client.target(oAuthGrant.getTokenEndpointUri());+ OAuthFilter oAuthFilter = new OAuthFilter(authTarget, oAuthGrant);+ authHeaderValue = oAuthFilter.getAuthHeader();+ if (TextUtil.isNullOrEmpty(authHeaderValue)) {+ throw new Exception("Returned access token is null");+ }+ LOG.fine("Retrieved access token via OAuth: " + getSocketAddressString());++ } catch (Exception e) {+ LOG.log(Level.SEVERE, "Failed to retrieve OAuth access token: " + getSocketAddressString(), e);+ setPermanentError(e.getMessage());+ return;+ } finally {+ if (client != null) {+ client.close();+ }+ }+ }++ super.connect();+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.javanew file mode 100644index 000000000..fcb7b4f4a--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketClientProtocol.java@@ -0,0 +1,648 @@+/*+ * Copyright 2017, 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.websocket;++import org.apache.http.HttpHeaders;+import org.jboss.resteasy.client.jaxrs.ResteasyClient;+import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;+import org.jboss.resteasy.util.BasicAuthHelper;+import org.openremote.agent.protocol.AbstractProtocol;+import org.openremote.agent.protocol.Protocol;+import org.openremote.agent.protocol.http.HttpClientProtocol;+import org.openremote.agent.protocol.http.OAuthGrant;+import org.openremote.agent.protocol.http.WebTargetBuilder;+import org.openremote.container.Container;+import org.openremote.model.AbstractValueHolder;+import org.openremote.model.ValidationFailure;+import org.openremote.model.ValueHolder;+import org.openremote.model.asset.AssetAttribute;+import org.openremote.model.asset.agent.ConnectionStatus;+import org.openremote.model.asset.agent.ProtocolConfiguration;+import org.openremote.model.attribute.*;+import org.openremote.model.query.BaseAssetQuery;+import org.openremote.model.query.filter.StringPredicate;+import org.openremote.model.syslog.SyslogCategory;+import org.openremote.model.util.Pair;+import org.openremote.model.util.TextUtil;+import org.openremote.model.value.*;++import javax.ws.rs.client.Entity;+import javax.ws.rs.client.Invocation;+import javax.ws.rs.core.MultivaluedHashMap;+import javax.ws.rs.core.MultivaluedMap;+import javax.ws.rs.core.Response;+import java.io.IOException;+import java.net.URI;+import java.net.URISyntaxException;+import java.util.*;+import java.util.function.Consumer;+import java.util.logging.Level;+import java.util.logging.Logger;++import static org.openremote.agent.protocol.http.HttpClientProtocol.*;+import static org.openremote.agent.protocol.http.WebTargetBuilder.createClient;+import static org.openremote.model.Constants.PROTOCOL_NAMESPACE;+import static org.openremote.model.attribute.MetaItemDescriptor.Access.ACCESS_PRIVATE;+import static org.openremote.model.attribute.MetaItemDescriptorImpl.*;+import static org.openremote.model.syslog.SyslogCategory.PROTOCOL;+import static org.openremote.model.util.TextUtil.REGEXP_PATTERN_STRING_NON_EMPTY;++/**+ * <p>+ * This is a Websocket client protocol for communicating with Websocket servers; it uses the {@link WebsocketClient}+ * to handle the communication, but also uses the {@link WebTargetBuilder} factory to send+ * {@link WebsocketHttpSubscription}s when the connection is initially established.+ * <h1>Protocol Configurations</h1>+ * <p>+ * {@link Attribute}s that are configured as {@link ProtocolConfiguration}s for this protocol support the meta+ * items defined in {@link #PROTOCOL_META_ITEM_DESCRIPTORS}.+ * <h1>Linked Attributes</h1>+ * <p>+ * {@link Attribute}s that are linked to this protocol using an {@link MetaItemType#AGENT_LINK} {@link MetaItem} support+ * the meta items defined in {@link #ATTRIBUTE_META_ITEM_DESCRIPTORS}.+ * <h1>Protocol -> Attribute</h1>+ * <p>+ * When a new value comes from the protocol destined for a linked {@link Attribute} the actual value written to the+ * attribute can be filtered in the standard way using {@link ValueFilter}s via the{@link MetaItemType#VALUE_FILTERS}+ * {@link MetaItem}.+ * <h1>Attribute -> Protocol</h1>+ * <p>+ * When a linked {@link Attribute} is written to, the actual value written to the protocol can either be the exact value+ * written to the linked {@link Attribute} or the {@link Protocol#META_ATTRIBUTE_WRITE_VALUE} {@link MetaItem} can be+ * used to inject the written value into a bigger payload using the {@link Protocol#DYNAMIC_VALUE_PLACEHOLDER} and then+ * this bigger payload will be written to the protocol.+ * <h1>Executable Attributes</h1>+ * When a linked {@link Attribute} that has an {@link MetaItemType#EXECUTABLE} {@link MetaItem} is executed the+ * {@link Value} stored in the {@link Protocol#META_ATTRIBUTE_WRITE_VALUE} {@link MetaItem} is actually written to the+ * protocol (note dynamic value injection doesn't work in this scenario as there is no dynamic value to inject).+ * <p>+ * <h1>Protocol Specifics</h1>+ * When the websocket connection is established it is possible to subscribe to events by specifying the+ * {@link #META_SUBSCRIPTIONS} {@link MetaItem}, a subscription can be either a message sent over the websocket+ * or a HTTP REST API call.+ * <p>+ * For a linked {@link Attribute} to display a value from the protocol the {@link #META_ATTRIBUTE_MESSAGE_MATCH_FILTERS}+ * and {@link #META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE} {@link MetaItem}s must be specified these allow incoming messages to+ * be matched to the attribute.+ * <p>+ *+ */+public class WebsocketClientProtocol extends AbstractProtocol {++ public static final String PROTOCOL_NAME = PROTOCOL_NAMESPACE + ":websocketClient";+ public static final String PROTOCOL_DISPLAY_NAME = "Websocket Client";+ public static final String PROTOCOL_VERSION = "1.0";+ private static final Logger LOG = SyslogCategory.getLogger(PROTOCOL, WebsocketClientProtocol.class.getName());++ /*--------------- META ITEMS TO BE USED ON PROTOCOL CONFIGURATIONS ---------------*/++ /**+ * Websocket connect endpoint URI+ */+ public static final MetaItemDescriptor META_PROTOCOL_CONNECT_URI = metaItemString(+ PROTOCOL_NAME + ":uri",+ ACCESS_PRIVATE,+ true,+ REGEXP_PATTERN_STRING_NON_EMPTY,+ PatternFailure.STRING_EMPTY+ );++ /**+ * Basic authentication username (string)+ */+ public static final MetaItemDescriptor META_PROTOCOL_USERNAME = metaItemString(+ PROTOCOL_NAME + ":username",+ ACCESS_PRIVATE,+ false,+ REGEXP_PATTERN_STRING_NON_EMPTY,+ PatternFailure.STRING_EMPTY);++ /**+ * Basic authentication password (string)+ */+ public static final MetaItemDescriptor META_PROTOCOL_PASSWORD = metaItemString(+ PROTOCOL_NAME + ":password",+ ACCESS_PRIVATE,+ false,+ REGEXP_PATTERN_STRING_NON_EMPTY,+ PatternFailure.STRING_EMPTY);++ /**+ * Headers for websocket connect call (see {@link HttpClientProtocol#META_HEADERS} for details)+ */+ public static final MetaItemDescriptor META_PROTOCOL_CONNECT_HEADERS = metaItemObject(+ PROTOCOL_NAME + ":headers",+ ACCESS_PRIVATE,+ false,+ null);++ /**+ * Array of {@link WebsocketSubscription}s that should be executed either once the websocket connection is+ * established (when used on a linked {@link ProtocolConfiguration}) or when a linked attribute is linked (when+ * used on a linked {@link AssetAttribute}); the subscriptions are executed in the order specified in the array+ */+ public static final MetaItemDescriptor META_SUBSCRIPTIONS = metaItemArray(+ PROTOCOL_NAME + ":subscriptions",+ ACCESS_PRIVATE,+ false,+ null);++ /*--------------- META ITEMS TO BE USED ON LINKED ATTRIBUTES ---------------*/++ /**+ * {@link ValueFilter}s to apply to incoming messages with the final result being compared to the+ * {@link #META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE}, if they are equal (e.g. {@link String#equals}) then the message+ * is intended for this linked attribute and the message can be once again filtered using the+ * {@link MetaItemType#VALUE_FILTERS} {@link MetaItem}. The {@link MetaItem} value should be an {@link ArrayValue}+ * of {@link ObjectValue}s where each {@link ObjectValue} represents a serialised {@link ValueFilter}. The message+ * will pass through the filters in array order+ */+ public static final MetaItemDescriptor META_ATTRIBUTE_MESSAGE_MATCH_FILTERS = metaItemArray(+ PROTOCOL_NAME + ":messageMatchFilters",+ ACCESS_PRIVATE,+ false,+ null);++ /**+ * The predicate to use on filtered message to determine if the message is intended for this+ * linked attribute+ */+ public static final MetaItemDescriptor META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE = metaItemObject(+ PROTOCOL_NAME + ":messageMatchPredicate",+ ACCESS_PRIVATE,+ false,+ new StringPredicate(BaseAssetQuery.Match.EXACT, false, "").toModelValue());++ public static final List<MetaItemDescriptor> ATTRIBUTE_META_ITEM_DESCRIPTORS = Arrays.asList(+ META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,+ META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,+ META_ATTRIBUTE_WRITE_VALUE);++ public static final List<MetaItemDescriptor> PROTOCOL_META_ITEM_DESCRIPTORS = Arrays.asList(+ META_PROTOCOL_CONNECT_URI,+ META_PROTOCOL_CONNECT_HEADERS,+ META_PROTOCOL_USERNAME,+ META_PROTOCOL_PASSWORD,+ META_PROTOCOL_OAUTH_GRANT,+ META_SUBSCRIPTIONS);++ protected ResteasyClient client;+ protected Map<AttributeRef, WebsocketClient> websocketClients = new HashMap<>();+ protected Map<AttributeRef, List<Pair<AttributeRef, Consumer<String>>>> protocolMessageConsumers = new HashMap<>();+ protected final Map<AttributeRef, List<Runnable>> protocolConnectedTasks = new HashMap<>();+ protected Map<AttributeRef, MultivaluedMap<String, String>> clientHeaders = new HashMap<>();+ protected Map<AttributeRef, Consumer<Value>> attributeWriteConsumers = new HashMap<>();++ @Override+ public void init(Container container) throws Exception {+ super.init(container);+ client = createClient(executorService);+ }++ @Override+ protected void doStop(Container container) {+ websocketClients.forEach((ref, client) -> client.disconnect());+ }++ @Override+ protected void doLinkProtocolConfiguration(AssetAttribute protocolConfiguration) {+ final AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();++ if (!protocolConfiguration.isEnabled()) {+ updateStatus(protocolRef, ConnectionStatus.DISABLED);+ return;+ }++ String uriStr = protocolConfiguration.getMetaItem(META_PROTOCOL_CONNECT_URI)+ .flatMap(AbstractValueHolder::getValueAsString).orElseThrow(() ->+ new IllegalArgumentException("Missing or invalid require meta item: " + META_PROTOCOL_CONNECT_URI));++ URI uri;++ try {+ uri = new URI(uriStr);+ } catch (URISyntaxException e) {+ LOG.log(Level.WARNING, "META_PROTOCOL_CONNECT_URI value is not a valid URI");+ updateStatus(protocolRef, ConnectionStatus.ERROR_CONFIGURATION);+ return;+ }++ /* We're going to fail hard and fast if optional meta items are incorrectly configured */++ Optional<OAuthGrant> oAuthGrant = Protocol.getOAuthGrant(protocolConfiguration);+ Optional<Pair<StringValue, StringValue>> usernameAndPassword = getUsernameAndPassword(protocolConfiguration);++ MultivaluedMap<String, String> headers = Values.getMetaItemValueOrThrow(+ protocolConfiguration,+ META_PROTOCOL_CONNECT_HEADERS,+ false,+ true)+ .flatMap(Values::getObject)+ .flatMap(objectValue -> getMultivaluedMap(objectValue, true))+ .orElse(null);++ Optional<WebsocketSubscription[]> subscriptions = Values.getMetaItemValueOrThrow(+ protocolConfiguration,+ META_SUBSCRIPTIONS,+ false,+ true)+ .flatMap(Values::getArray)+ .map(arrValue -> {+ try {+ return Container.JSON.readValue(arrValue.toJson(), WebsocketSubscription[].class);+ } catch (IOException e) {+ LOG.log(Level.WARNING, "Failed to deserialize WebsocketSubscription[]", e);+ return null;+ }+ });++ if (!oAuthGrant.isPresent() && usernameAndPassword.isPresent()) {+ String authValue = BasicAuthHelper.createHeader(usernameAndPassword.get().key.getString(), usernameAndPassword.get().value.getString());+ if (headers == null) {+ headers = new MultivaluedHashMap<>();+ headers.add(HttpHeaders.AUTHORIZATION, authValue);+ } else {+ headers.remove(HttpHeaders.AUTHORIZATION);+ headers.replace(HttpHeaders.AUTHORIZATION, Collections.singletonList(authValue));+ }+ }++ try {+ WebsocketClient websocketClient = new WebsocketClient(uri, headers, oAuthGrant.orElse(null), executorService);+ websocketClient.addConnectionStatusConsumer(connectionStatus -> this.onClientConnectionStatusChanged(protocolRef, connectionStatus));+ websocketClient.addMessageConsumer(msg -> this.onClientMessage(protocolRef, msg));+ websocketClients.put(protocolRef, websocketClient);++ MultivaluedMap<String, String> finalHeaders = headers;+ clientHeaders.put(protocolRef, headers);+ subscriptions.ifPresent(websocketSubscriptions ->+ protocolConnectedTasks.put(protocolRef, new ArrayList<>(Collections.singleton(() -> doSubscriptions(websocketClient, finalHeaders, websocketSubscriptions))))+ );++ websocketClient.connect();+ } catch (Exception e) {+ LOG.log(Level.WARNING, "Failed to establish websocket connection, protocol is unusable", e);+ updateStatus(protocolRef, ConnectionStatus.ERROR);+ }+ }++ @Override+ protected void doUnlinkProtocolConfiguration(AssetAttribute protocolConfiguration) {+ AttributeRef protocolConfigurationRef = protocolConfiguration.getReferenceOrThrow();+ WebsocketClient client = websocketClients.remove(protocolConfigurationRef);+ if (client != null) {+ client.removeAllMessageConsumers();+ client.removeAllConnectionStatusConsumers();+ client.disconnect();+ }+ clientHeaders.remove(protocolConfigurationRef);+ protocolMessageConsumers.remove(protocolConfigurationRef);+ protocolConnectedTasks.remove(protocolConfigurationRef);+ }++ @Override+ protected void doLinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {++ AttributeRef protocolRef = protocolConfiguration.getReferenceOrThrow();+ WebsocketClient client = websocketClients.get(protocolRef);++ if (client == null) {+ LOG.warning("Linked attribute protocol configuration is not valid or disabled so cannot link attribute");+ return;+ }++ ValueFilter[] filters = Values.getMetaItemValueOrThrow(+ attribute,+ META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,+ false,+ true)+ .map(Value::toJson)+ .map(json -> {+ try {+ return Container.JSON.readValue(json, ValueFilter[].class);+ } catch (IOException e) {+ LOG.log(Level.WARNING, "Failed to deserialize ValueFilter[]", e);+ return null;+ }+ }).orElse(null);++ StringPredicate matchPredicate = Values.getMetaItemValueOrThrow(+ attribute,+ META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,+ false,+ true)+ .map(Value::toJson)+ .map(s -> {+ try {+ return Container.JSON.readValue(s, StringPredicate.class);+ } catch (IOException e) {+ LOG.log(Level.WARNING, "Failed to deserialise StringPredicate", e);+ return null;+ }+ })+ .orElse(null);++ Optional<WebsocketSubscription[]> subscriptions = Values.getMetaItemValueOrThrow(+ attribute,+ META_SUBSCRIPTIONS,+ false,+ true)+ .flatMap(Values::getArray)+ .map(arrValue -> {+ try {+ return Container.JSON.readValue(arrValue.toJson(), WebsocketSubscription[].class);+ } catch (IOException e) {+ LOG.log(Level.WARNING, "Failed to deserialize WebsocketSubscription[]", e);+ return null;+ }+ });++ boolean hasFilters = filters != null || matchPredicate != null;++ if (!attribute.isReadOnly()) {+ Consumer<Value> writeConsumer = Protocol.createDynamicAttributeWriteConsumer(attribute, str -> {+ LOG.fine("Sending message to websocket client: " + client.getSocketAddressString());+ client.sendMessage(str);+ });+ attributeWriteConsumers.put(attribute.getReferenceOrThrow(), writeConsumer);+ } else if (!hasFilters) {+ LOG.warning("Readonly attribute doesn't have any message filters and will therefore display nothing: " + attribute.getReferenceOrThrow());+ }++ if (hasFilters && (filters == null || matchPredicate == null)) {+ LOG.warning("Invalid or missing '" + META_ATTRIBUTE_MESSAGE_MATCH_FILTERS.getUrn() + "' and/or '" + META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE.getUrn() + "': " + attribute.getReferenceOrThrow());+ return;+ }++ if (hasFilters) {+ protocolMessageConsumers.compute(protocolRef, (ref, consumers) -> {+ if (consumers == null) {+ consumers = new ArrayList<>();+ }+ consumers.add(new Pair<>(+ attribute.getReferenceOrThrow(),+ createAttributeWebsocketMessageConsumer(attribute.getReferenceOrThrow(), filters, matchPredicate)));+ return consumers;+ });+ }++ subscriptions.ifPresent(websocketSubscriptions -> {+ MultivaluedMap<String, String> headers = clientHeaders.get(protocolRef);+ Runnable task = () -> doSubscriptions(client, headers, websocketSubscriptions);+ if (client.getConnectionStatus() == ConnectionStatus.CONNECTED) {+ executorService.schedule(task, 1000);+ } else {+ synchronized (protocolConnectedTasks) {+ protocolConnectedTasks.compute(protocolRef, (ref, tasks) -> {+ if (tasks == null) {+ tasks = new ArrayList<>();+ }+ tasks.add(task);+ return tasks;+ });+ }+ }+ });+ }++ @Override+ protected void doUnlinkAttribute(AssetAttribute attribute, AssetAttribute protocolConfiguration) {+ AttributeRef attributeRef = attribute.getReferenceOrThrow();+ attributeWriteConsumers.remove(attributeRef);+ protocolMessageConsumers.compute(protocolConfiguration.getReferenceOrThrow(), (ref, consumers) -> {+ if (consumers != null) {+ consumers.removeIf((attrRefConsumer) -> attrRefConsumer.key.equals(attributeRef));+ }+ return consumers;+ });+ }++ @Override+ protected void processLinkedAttributeWrite(AttributeEvent event, AssetAttribute protocolConfiguration) {++ Consumer<Value> writeConsumer = attributeWriteConsumers.get(event.getAttributeRef());++ if (writeConsumer == null) {+ LOG.fine("Attempt to write to an attribute that doesn't support writes: " + event.getAttributeRef());+ return;+ }++ AssetAttribute attribute = getLinkedAttribute(event.getAttributeRef());+ AttributeExecuteStatus status = null;++ if (attribute.isExecutable()) {+ status = event.getValue()+ .flatMap(Values::getString)+ .flatMap(AttributeExecuteStatus::fromString)+ .orElse(null);++ if (status != null && status != AttributeExecuteStatus.REQUEST_START) {+ LOG.fine("Unsupported execution status: " + status);+ return;+ }+ }++ Value value = status != null ? null : event.getValue().orElse(null);+ LOG.fine("Attribute write being sent to the websocket client: " + event.getAttributeRef());+ writeConsumer.accept(value);++ if (status != null) {+ updateLinkedAttribute(new AttributeState(event.getAttributeRef(), AttributeExecuteStatus.COMPLETED.asValue()));+ }+ }++ @Override+ public String getProtocolName() {+ return PROTOCOL_NAME;+ }++ @Override+ public String getProtocolDisplayName() {+ return PROTOCOL_DISPLAY_NAME;+ }++ @Override+ public String getVersion() {+ return PROTOCOL_VERSION;+ }++ @Override+ protected List<MetaItemDescriptor> getProtocolConfigurationMetaItemDescriptors() {+ return new ArrayList<>(PROTOCOL_META_ITEM_DESCRIPTORS);+ }++ @Override+ protected List<MetaItemDescriptor> getLinkedAttributeMetaItemDescriptors() {+ List<MetaItemDescriptor> descriptors = new ArrayList<>(super.getLinkedAttributeMetaItemDescriptors());+ descriptors.addAll(ATTRIBUTE_META_ITEM_DESCRIPTORS);+ return descriptors;+ }++ @Override+ public AssetAttribute getProtocolConfigurationTemplate() {+ return super.getProtocolConfigurationTemplate()+ .addMeta(+ new MetaItem(META_PROTOCOL_CONNECT_URI, null)+ );+ }++ @Override+ public AttributeValidationResult validateProtocolConfiguration(AssetAttribute protocolConfiguration) {+ AttributeValidationResult result = super.validateProtocolConfiguration(protocolConfiguration);+ if (result.isValid()) {+ try {+ Protocol.getOAuthGrant(protocolConfiguration);+ getUsernameAndPassword(protocolConfiguration);+ } catch (IllegalArgumentException e) {+ result.addAttributeFailure(+ new ValidationFailure(ValueHolder.ValueFailureReason.VALUE_MISMATCH, PROTOCOL_NAME)+ );+ }+ }+ return result;+ }++ private void onClientMessage(AttributeRef protocolRef, String msg) {+ List<Pair<AttributeRef, Consumer<String>>> consumers = protocolMessageConsumers.get(protocolRef);+ if (consumers != null) {+ consumers.forEach(c -> {+ if (c.value != null) {+ c.value.accept(msg);+ }+ });+ }+ }++ protected void onClientConnectionStatusChanged(AttributeRef protocolRef, ConnectionStatus connectionStatus) {+ updateStatus(protocolRef, connectionStatus);++ if (connectionStatus == ConnectionStatus.CONNECTED) {+ // Look for any subscriptions that need to be processed+ List<Runnable> connectedTasks;++ synchronized (protocolConnectedTasks) {+ connectedTasks = protocolConnectedTasks.remove(protocolRef);+ }+ if (connectedTasks != null) {+ // Execute after a delay to ensure connection is properly initialised+ executorService.schedule(() -> connectedTasks.forEach(Runnable::run), 500);+ }+ }+ }++ protected void doSubscriptions(WebsocketClient websocketClient, MultivaluedMap<String, String> headers, WebsocketSubscription[] subscriptions) {+ LOG.info("Executing subscriptions for websocket: " + websocketClient.getSocketAddressString());++ // Inject OAuth header+ if (!TextUtil.isNullOrEmpty(websocketClient.authHeaderValue)) {+ if (headers == null) {+ headers = new MultivaluedHashMap<>();+ }+ headers.remove(HttpHeaders.AUTHORIZATION);+ headers.add(HttpHeaders.AUTHORIZATION, websocketClient.authHeaderValue);+ }++ MultivaluedMap<String, String> finalHeaders = headers;+ Arrays.stream(subscriptions).forEach(+ subscription -> processSubscription(websocketClient, finalHeaders, subscription)+ );+ }++ protected void processSubscription(WebsocketClient websocketClient, MultivaluedMap<String, String> headers, WebsocketSubscription subscription) {+ if (subscription instanceof WebsocketHttpSubscription) {+ WebsocketHttpSubscription httpSubscription = (WebsocketHttpSubscription)subscription;++ if (TextUtil.isNullOrEmpty(httpSubscription.uri)) {+ LOG.warning("Websocket subscription missing or empty URI so skipping: " + subscription);+ return;+ }++ URI uri;++ try {+ uri = new URI(httpSubscription.uri);+ } catch (URISyntaxException e) {+ LOG.warning("Websocket subscription invalid URI so skipping: " + subscription);+ return;+ }++ if (httpSubscription.method == null) {+ httpSubscription.method = WebsocketHttpSubscription.Method.valueOf(DEFAULT_HTTP_METHOD);+ }++ if (TextUtil.isNullOrEmpty(httpSubscription.contentType)) {+ httpSubscription.contentType = DEFAULT_CONTENT_TYPE;+ }++ if (httpSubscription.headers != null) {+ headers = headers != null ? new MultivaluedHashMap<String, String>(headers) : new MultivaluedHashMap<>();+ @SuppressWarnings("OptionalGetWithoutIsPresent")+ MultivaluedMap<String, String> subscriptionHeaders = getMultivaluedMap(httpSubscription.headers, false).get();+ MultivaluedMap<String, String> finalHeaders = headers;+ subscriptionHeaders.forEach((header, values) -> {+ if (values == null || values.isEmpty()) {+ finalHeaders.remove(header);+ } else {+ finalHeaders.addAll(header, values);+ }+ });+ }++ WebTargetBuilder webTargetBuilder = new WebTargetBuilder(client, uri);++ if (headers != null) {+ webTargetBuilder.setInjectHeaders(headers);+ }++ LOG.fine("Creating web target client for subscription '" + uri + "'");+ ResteasyWebTarget target = webTargetBuilder.build();++ Invocation invocation;++ if (httpSubscription.body == null) {+ invocation = target.request().build(httpSubscription.method.toString());+ } else {+ invocation = target.request().build(httpSubscription.method.toString(), Entity.entity(httpSubscription.body, httpSubscription.contentType));+ }+ Response response = invocation.invoke();+ if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) {+ LOG.warning("WebsocketHttpSubscription returned an un-successful response code: " + response.getStatus());+ }+ } else {+ websocketClient.sendMessage(subscription.body);+ }+ }++ protected Consumer<String> createAttributeWebsocketMessageConsumer(AttributeRef attributeRef, ValueFilter[] filters, StringPredicate matchPredicate) {++ return message -> {+ if (!TextUtil.isNullOrEmpty(message)) {+ StringValue stringValue = Values.create(message);+ Value val = assetService.applyValueFilters(stringValue, filters);+ if (val != null) {+ if (StringPredicate.asPredicate(matchPredicate).test(val.toString())) {+ LOG.fine("Websocket message matches attribute so writing message to the attribute: " + attributeRef);+ updateLinkedAttribute(new AttributeState(attributeRef, stringValue));+ }+ }+ }+ };+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketHttpSubscription.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketHttpSubscription.javanew file mode 100644index 000000000..55af6b25c--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketHttpSubscription.java@@ -0,0 +1,73 @@+/*+ * Copyright 2019, 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.websocket;++import org.openremote.model.value.ObjectValue;++public class WebsocketHttpSubscription extends WebsocketSubscription {++ public enum Method {+ GET,+ PUT,+ POST+ }++ public static final String TYPE = "http";++ public Method method;+ public String contentType;+ public ObjectValue headers;+ public String uri;++ public WebsocketHttpSubscription() {+ super(TYPE);+ }++ public WebsocketHttpSubscription method(Method method) {+ this.method = method;+ return this;+ }++ public WebsocketHttpSubscription contentType(String contentType) {+ this.contentType = contentType;+ return this;+ }++ public WebsocketHttpSubscription headers(ObjectValue headers) {+ this.headers = headers;+ return this;+ }++ public WebsocketHttpSubscription uri(String uri) {+ this.uri = uri;+ return this;+ }++ @Override+ public String toString() {+ return WebsocketHttpSubscription.class.getSimpleName() + "{" ++ "method=" + method ++ ", uri='" + uri + '\'' ++ ", contentType='" + contentType + '\'' ++ ", headers=" + headers ++ ", body='" + body + '\'' ++ '}';+ }+}diff --git a/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketSubscription.java b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketSubscription.javanew file mode 100644index 000000000..a1fd595fb--- /dev/null+++ b/agent/src/main/java/org/openremote/agent/protocol/websocket/WebsocketSubscription.java@@ -0,0 +1,52 @@+/*+ * Copyright 2019, 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.websocket;++import com.fasterxml.jackson.annotation.JsonSubTypes;+import com.fasterxml.jackson.annotation.JsonTypeInfo;++@JsonSubTypes({+ @JsonSubTypes.Type(value = WebsocketSubscription.class, name = WebsocketSubscription.TYPE),+ @JsonSubTypes.Type(value = WebsocketHttpSubscription.class, name = WebsocketHttpSubscription.TYPE)+})+@JsonTypeInfo(+ use = JsonTypeInfo.Id.NAME,+ property = "type",+ defaultImpl = WebsocketSubscription.class+)+public class WebsocketSubscription {++ public static final String TYPE = "websocket";++ public String type = TYPE;+ public String body;++ public WebsocketSubscription() {+ }++ protected WebsocketSubscription(String type) {+ this.type = type;+ }++ public WebsocketSubscription body(String body) {+ this.body = body;+ return this;+ }+}diff --git a/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService b/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerServiceindex c1d8da283..fb9268cae 100644--- a/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService+++ b/agent/src/main/resources/META-INF/services/org.openremote.container.ContainerService@@ -6,4 +6,5 @@ org.openremote.agent.protocol.velbus.VelbusTcpProtocolorg.openremote.agent.protocol.velbus.VelbusSerialProtocolorg.openremote.agent.protocol.http.HttpClientProtocolorg.openremote.agent.protocol.controller.ControllerProtocol-org.openremote.agent.protocol.udp.UdpClientProtocol\ No newline at end of file+org.openremote.agent.protocol.udp.UdpClientProtocol+org.openremote.agent.protocol.websocket.WebsocketClientProtocol\ No newline at end of filediff --git a/client/src/main/java/org/openremote/app/client/assets/AgentStatusEventMapper.java b/client/src/main/java/org/openremote/app/client/assets/AgentStatusEventMapper.javaindex 785ce0bef..23c1ee527 100644--- a/client/src/main/java/org/openremote/app/client/assets/AgentStatusEventMapper.java+++ b/client/src/main/java/org/openremote/app/client/assets/AgentStatusEventMapper.java@@ -29,6 +29,6 @@ import java.util.List;@JsonMixIns({@JsonMixIns.JsonMixIn(target = AgentStatusEvent.class, mixIn = DefaultJsonMixin.class)})public interface AgentStatusEventMapper- extends ObjectMapper<List<AgentStatusEvent>>,- EntityReader<List<AgentStatusEvent>> {+ extends ObjectMapper<AgentStatusEvent[]>,+ EntityReader<AgentStatusEvent[]> {}diff --git a/manager/src/main/java/org/openremote/manager/agent/AgentService.java b/manager/src/main/java/org/openremote/manager/agent/AgentService.javaindex 26505175d..3e3ad5779 100644--- a/manager/src/main/java/org/openremote/manager/agent/AgentService.java+++ b/manager/src/main/java/org/openremote/manager/agent/AgentService.java@@ -809,7 +809,7 @@ public class AgentService extends RouteBuilder implements ContainerService, Asse} else if (filter instanceof SubStringValueFilter) {value = applySubstringFilter((StringValue)value, (SubStringValueFilter)filter);} else if (filter instanceof JsonPathFilter) {- value = applyJsonPathFilter(value, (JsonPathFilter)filter);+ value = applyJsonPathFilter(value, (JsonPathFilter) filter);} else {throw new UnsupportedOperationException("Unsupported filter: " + filter);}@@ -878,8 +878,21 @@ public class AgentService extends RouteBuilder implements ContainerService, Assereturn null;}- JsonNode node = jsonPathParser.parse(value.toJson()).read(filter.path);- String pathJson = node != null ? node.toString() : null;+ if (value.getType() == ValueType.STRING) {+ try {+ // Assume value is actually a JSON payload+ value = Values.parse(((StringValue) value).getString()).orElse(null);+ } catch (Exception e) {+ value = null;+ }+ }++ if (value == null) {+ return null;+ }++ Object obj = jsonPathParser.parse(value.toJson()).read(filter.path);+ String pathJson = obj != null ? obj.toString() : null;return TextUtil.isNullOrEmpty(pathJson) ? null : Values.parse(pathJson).orElse(null);}}\ No newline at end of filediff --git a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java b/manager/src/main/java/org/openremote/manager/event/ClientEventService.javaindex f4115081f..37bf46490 100644--- a/manager/src/main/java/org/openremote/manager/event/ClientEventService.java+++ b/manager/src/main/java/org/openremote/manager/event/ClientEventService.java@@ -48,7 +48,7 @@ import static org.apache.camel.builder.PredicateBuilder.or;* <p>* The following messages can be sent by a client:* <dl>- * <dt><code>SUBSCRIBE{...}</code><dt>+ * <dt><code>SUBSCRIBE:{...}</code><dt>* <dd><p>* The payload is a serialized representation of {@link EventSubscription} with an optional* {@link org.openremote.model.event.shared.EventFilter}. Clients can subscribe to receive {@link SharedEvent}s@@ -59,12 +59,12 @@ import static org.apache.camel.builder.PredicateBuilder.or;* time is {@link EventSubscription#RENEWAL_PERIOD_SECONDS}; it is recommended clients renew the subscription* in shorter periods to allow for processing time of the renewal.* </p></dd>- * <dt><code>UNSUBSCRIBE{...}</code></dt>+ * <dt><code>UNSUBSCRIBE:{...}</code></dt>* <dd><p>* The payload is a serialized representation of {@link CancelEventSubscription}. If a client* does not want to wait for expiration of its subscriptions, it can cancel a subscription.* </p></dd>- * <dt><code>EVENT{...}</code></dt>+ * <dt><code>EVENT:{...}</code></dt>* <dd><p>* The payload is a serialized representation of a subtype of {@link SharedEvent}. If the server* does not recognize the event, it is silently ignored.@@ -73,15 +73,15 @@ import static org.apache.camel.builder.PredicateBuilder.or;* <p>* The following messages can be published/returned by the server:* <dl>- * <dt><code>UNAUTHORIZED{...}</code></dt>+ * <dt><code>UNAUTHORIZED:{...}</code></dt>* <dd><p>* The payload is a serialized representation of {@link UnauthorizedEventSubscription}.* </p></dd>- * <dt><code>EVENT{...}</code></dt>+ * <dt><code>EVENT:{...}</code></dt>* <dd><p>* The payload is a serialized representation of a subtype of {@link SharedEvent}.* </p></dd>- * <dt><code>EVENT[...]</code></dt>+ * <dt><code>EVENT:[...]</code></dt>* <dd><p>* The payload is an array of {@link SharedEvent}s.* </p></dd>diff --git a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.javaindex a69aedd19..ac99ffdfe 100644--- a/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java+++ b/manager/src/main/java/org/openremote/manager/event/EventTypeConverters.java@@ -45,7 +45,6 @@ import org.apache.camel.TypeConverters;import org.openremote.container.Container;import org.openremote.model.event.TriggeredEventSubscription;import org.openremote.model.event.shared.*;-import org.openremote.model.util.TextUtil;import java.util.logging.Logger;diff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovynew file mode 100644index 000000000..6db7c8b61--- /dev/null+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientProtocolTest.groovy@@ -0,0 +1,319 @@+/*+ * Copyright 2017, 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.websocket++import io.netty.buffer.Unpooled+import io.netty.channel.ChannelHandlerContext+import io.netty.channel.socket.DatagramChannel+import io.netty.handler.codec.FixedLengthFrameDecoder+import io.netty.handler.codec.MessageToMessageEncoder+import io.netty.handler.codec.bytes.ByteArrayDecoder+import org.jboss.resteasy.spi.ResteasyUriInfo+import org.jboss.resteasy.util.BasicAuthHelper+import org.openremote.agent.protocol.Protocol+import org.openremote.agent.protocol.ProtocolExecutorService+import org.openremote.agent.protocol.http.OAuthGrant+import org.openremote.agent.protocol.http.OAuthPasswordGrant+import org.openremote.agent.protocol.http.OAuthRefreshTokenGrant+import org.openremote.agent.protocol.http.OAuthServerResponse+import org.openremote.agent.protocol.simulator.SimulatorProtocol+import org.openremote.agent.protocol.udp.AbstractUdpServer+import org.openremote.agent.protocol.udp.UdpClientProtocol+import org.openremote.agent.protocol.websocket.WebsocketClientProtocol+import org.openremote.agent.protocol.websocket.WebsocketHttpSubscription+import org.openremote.agent.protocol.websocket.WebsocketSubscription+import org.openremote.container.Container+import org.openremote.manager.agent.AgentService+import org.openremote.manager.asset.AssetProcessingService+import org.openremote.manager.asset.AssetStorageService+import org.openremote.manager.setup.SetupService+import org.openremote.manager.setup.builtin.ManagerDemoSetup+import org.openremote.model.Constants+import org.openremote.model.asset.Asset+import org.openremote.model.asset.AssetAttribute+import org.openremote.model.asset.AssetEvent+import org.openremote.model.asset.AssetType+import org.openremote.model.asset.ReadAssetAttributesEvent+import org.openremote.model.asset.agent.ConnectionStatus+import org.openremote.model.attribute.*+import org.openremote.model.event.shared.EventSubscription+import org.openremote.model.event.shared.SharedEvent+import org.openremote.model.query.AssetQuery+import org.openremote.model.query.BaseAssetQuery+import org.openremote.model.query.filter.StringPredicate+import org.openremote.model.value.JsonPathFilter+import org.openremote.model.value.ObjectValue+import org.openremote.model.value.SubStringValueFilter+import org.openremote.model.value.ValueFilter+import org.openremote.model.value.Values+import org.openremote.test.ManagerContainerTrait+import spock.lang.Shared+import spock.lang.Specification+import spock.util.concurrent.PollingConditions++import javax.ws.rs.HttpMethod+import javax.ws.rs.client.ClientRequestContext+import javax.ws.rs.client.ClientRequestFilter+import javax.ws.rs.core.Form+import javax.ws.rs.core.HttpHeaders+import javax.ws.rs.core.MediaType+import javax.ws.rs.core.Response+import javax.ws.rs.core.UriInfo++import static org.openremote.container.util.MapAccess.getString+import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_ADMIN_PASSWORD+import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_ADMIN_PASSWORD_DEFAULT+import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID+import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER+import static org.openremote.model.asset.agent.ProtocolConfiguration.initProtocolConfiguration++class WebsocketClientProtocolTest extends Specification implements ManagerContainerTrait {++ @Shared+ def mockServer = new ClientRequestFilter() {++ private boolean subscriptionDone = false++ @Override+ void filter(ClientRequestContext requestContext) throws IOException {+ def requestUri = requestContext.uri+ def requestPath = requestUri.scheme + "://" + requestUri.host + requestUri.path++ // Check auth header is present+ def authHeader = requestContext.getHeaderString(HttpHeaders.AUTHORIZATION)+ if (authHeader == null || authHeader.length() < 8) {+ requestContext.abortWith(Response.status(Response.Status.UNAUTHORIZED).build())+ return+ }++ switch (requestPath) {++ case "https://mockapi/assets":+ if (requestContext.method == HttpMethod.POST+ && requestContext.getHeaderString("header1") == "header1Value1"+ && requestContext.getHeaderString("header2") == "header2Value1,header2Value2"+ && requestContext.getHeaderString("Content-type") == MediaType.APPLICATION_JSON) {++ String bodyStr = (String)requestContext.getEntity()+ AssetQuery assetQuery = Container.JSON.readValue(bodyStr, AssetQuery.class)+ if (assetQuery != null && assetQuery.ids != null && assetQuery.ids.size() == 1) {+ subscriptionDone = true+ requestContext.abortWith(Response.ok().build())+ return+ }+ }+ break+ }++ requestContext.abortWith(Response.serverError().build())+ }+ }++ def "Check websocket client protocol configuration and linked attribute deployment"() {++ given: "expected conditions"+ def conditions = new PollingConditions(timeout: 10, delay: 1)++ when: "the container starts"+ def serverPort = findEphemeralPort()+ def container = startContainer(defaultConfig(serverPort), defaultServices())+ def protocolExecutorService = container.getService(ProtocolExecutorService.class)+ def websocketClientProtocol = container.getService(WebsocketClientProtocol.class)+ def simulatorProtocol = container.getService(SimulatorProtocol.class)+ def assetStorageService = container.getService(AssetStorageService.class)+ def assetProcessingService = container.getService(AssetProcessingService.class)+ def agentService = container.getService(AgentService.class)+ def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)++ then: "the container should be running"+ conditions.eventually {+ assert container.isRunning()+ }++ when: "the web target builder is configured to use the mock server"+ if (!websocketClientProtocol.client.configuration.isRegistered(mockServer)) {+ websocketClientProtocol.client.register(mockServer, Integer.MAX_VALUE)+ }++ and: "an agent with a websocket client protocol configuration is created to connect to this tests manager"+ def agent = new Asset()+ agent.setRealm(Constants.MASTER_REALM)+ agent.setName("Test Agent")+ agent.setType(AssetType.AGENT)+ agent.setAttributes(+ initProtocolConfiguration(new AssetAttribute("protocolConfig"), WebsocketClientProtocol.PROTOCOL_NAME)+ .addMeta(+ new MetaItem(+ WebsocketClientProtocol.META_PROTOCOL_CONNECT_URI,+ Values.create("ws://localhost:$serverPort/websocket/events?Auth-Realm=master")+ ),+ new MetaItem(+ Protocol.META_PROTOCOL_OAUTH_GRANT,+ new OAuthPasswordGrant("http://localhost:$serverPort/auth/realms/master/protocol/openid-connect/token",+ KEYCLOAK_CLIENT_ID,+ null,+ null,+ MASTER_REALM_ADMIN_USER,+ getString(container.getConfig(), SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT)).toObjectValue()+ ),+ new MetaItem(+ WebsocketClientProtocol.META_SUBSCRIPTIONS,+ Values.parse(+ Container.JSON.writeValueAsString(Arrays.asList(+ new WebsocketSubscription().body(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX + Container.JSON.writeValueAsString(+ new EventSubscription(+ AttributeEvent.class,+ new AssetEvent.AssetIdFilter<AttributeEvent>(managerDemoSetup.apartment1LivingroomId),+ "1",+ null))),+ new WebsocketHttpSubscription()+ .contentType(MediaType.APPLICATION_JSON)+ .method(WebsocketHttpSubscription.Method.POST)+ .headers(Values.<ObjectValue>parse(/{"header1": "header1Value1", "header2": ["header2Value1","header2Value2"]}/).get())+ .uri("https://mockapi/assets")+ .body(+ Container.JSON.writeValueAsString(new AssetQuery().id(managerDemoSetup.apartment1LivingroomId))+ )+ ))+ ).orElse(null)+ )+ )+ )++ and: "the agent is added to the asset service"+ agent = assetStorageService.merge(agent)++ then: "the protocol should authenticate and the connection status should become CONNECTED"+ conditions.eventually {+ def status = agentService.getProtocolConnectionStatus(new AttributeRef(agent.id, "protocolConfig"))+ assert status == ConnectionStatus.CONNECTED+ }++ and: "the subscriptions should have been executed"+ conditions.eventually {+ assert mockServer.subscriptionDone+ }++ when: "an asset is created with attributes linked to the protocol configuration"+ def asset = new Asset("Test Asset", AssetType.THING, agent)+ asset.setAttributes(+ // write attribute value+ new AssetAttribute("readWriteTargetTemp", AttributeValueType.NUMBER)+ .addMeta(+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),+ new MetaItem(Protocol.META_ATTRIBUTE_WRITE_VALUE, Values.create(SharedEvent.MESSAGE_PREFIX ++ Container.JSON.writeValueAsString(new AttributeEvent(+ managerDemoSetup.apartment1LivingroomId,+ "targetTemperature",+ Values.create(0.12345))).replace("0.12345", Protocol.DYNAMIC_VALUE_PLACEHOLDER))),+ new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,+ Values.parse(Container.JSON.writeValueAsString(+ [+ new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),+ new JsonPathFilter("\$..attributeState.attributeRef")+ ] as ValueFilter[]+ )).orElse(null)),+ new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,+ new StringPredicate(BaseAssetQuery.Match.CONTAINS, true, "targetTemperature").toModelValue()),+ new MetaItem(MetaItemType.VALUE_FILTERS,+ Values.parse(Container.JSON.writeValueAsString(+ [+ new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),+ new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"targetTemperature\")].attributeState.value"),+ new JsonPathFilter("\$.min()")+ ] as ValueFilter[]+ )).orElse(null)),+ new MetaItem(WebsocketClientProtocol.META_SUBSCRIPTIONS,+ Values.parse(+ Container.JSON.writeValueAsString(Arrays.asList(+ new WebsocketSubscription().body(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(+ new ReadAssetAttributesEvent(managerDemoSetup.apartment1LivingroomId, "targetTemperature")+ )))+ )).orElse(null))+ ),+ new AssetAttribute("readCo2Level", AttributeValueType.NUMBER)+ .addMeta(+ new MetaItem(MetaItemType.AGENT_LINK, new AttributeRef(agent.id, "protocolConfig").toArrayValue()),+ new MetaItem(MetaItemType.READ_ONLY),+ new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_FILTERS,+ Values.parse(Container.JSON.writeValueAsString(+ [+ new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),+ new JsonPathFilter("\$..attributeState.attributeRef")+ ] as ValueFilter[]+ )).orElse(null)),+ new MetaItem(WebsocketClientProtocol.META_ATTRIBUTE_MESSAGE_MATCH_PREDICATE,+ new StringPredicate(BaseAssetQuery.Match.CONTAINS, "co2Level").toModelValue()),+ new MetaItem(MetaItemType.VALUE_FILTERS,+ Values.parse(Container.JSON.writeValueAsString(+ [+ new SubStringValueFilter(SharedEvent.MESSAGE_PREFIX.length()),+ new JsonPathFilter("\$..events[?(@.attributeState.attributeRef.attributeName == \"co2Level\")].attributeState.value"),+ new JsonPathFilter("\$.min()")+ ] as ValueFilter[]+ )).orElse(null)),+ new MetaItem(WebsocketClientProtocol.META_SUBSCRIPTIONS,+ Values.parse(+ Container.JSON.writeValueAsString(Arrays.asList(+ new WebsocketSubscription().body(SharedEvent.MESSAGE_PREFIX + Container.JSON.writeValueAsString(+ new ReadAssetAttributesEvent(managerDemoSetup.apartment1LivingroomId, "co2Level")+ )))+ )).orElse(null))+ )+ )++ and: "the asset is merged into the asset service"+ asset = assetStorageService.merge(asset)++ then: "the linked attributes should have no initial values"+ conditions.eventually {+ asset = assetStorageService.find(asset.getId(), true)+ assert !asset.getAttribute("readCo2Level").get().value.isPresent()+ assert !asset.getAttribute("readWriteTargetTemp").get().value.isPresent()+ }++ when: "a linked attribute value is updated"+ def attributeEvent = new AttributeEvent(asset.id,+ "readWriteTargetTemp",+ Values.create(19.5))+ assetProcessingService.sendAttributeEvent(attributeEvent)++ then: "the linked targetTemperature attribute should contain this written value"+ conditions.eventually {+ asset = assetStorageService.find(asset.getId(), true)+ assert asset.getAttribute("readWriteTargetTemp").flatMap{it.getValueAsNumber()}.orElse(null) == 19.5d+ }++ when: "the co2level changes"+ def co2LevelIncrement = new AttributeEvent(+ managerDemoSetup.apartment1LivingroomId, "co2Level", Values.create(600)+ )+ simulatorProtocol.putValue(co2LevelIncrement)++ then: "the linked co2Level attribute should get the new value"+ conditions.eventually {+ asset = assetStorageService.find(asset.getId(), true)+ assert asset.getAttribute("readCo2Level").flatMap{it.getValueAsNumber()}.orElse(null) == 600d+ }++ cleanup: "the server should be stopped"+ stopContainer(container)+ }+}\ No newline at end of filediff --git a/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovynew file mode 100644index 000000000..e7cbb1819--- /dev/null+++ b/test/src/test/groovy/org/openremote/test/protocol/websocket/WebsocketClientTest.groovy@@ -0,0 +1,133 @@+/*+ * Copyright 2017, 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.websocket+++import org.apache.http.client.utils.URIBuilder+import org.openremote.agent.protocol.http.OAuthPasswordGrant+import org.openremote.agent.protocol.websocket.WebsocketClient+import org.openremote.container.Container+import org.openremote.manager.asset.AssetProcessingService+import org.openremote.manager.concurrent.ManagerExecutorService+import org.openremote.manager.setup.SetupService+import org.openremote.manager.setup.builtin.ManagerDemoSetup+import org.openremote.model.asset.AssetEvent+import org.openremote.model.asset.agent.ConnectionStatus+import org.openremote.model.attribute.AttributeEvent+import org.openremote.model.event.TriggeredEventSubscription+import org.openremote.model.event.shared.EventSubscription+import org.openremote.model.event.shared.SharedEvent+import org.openremote.model.value.Values+import org.openremote.test.ManagerContainerTrait+import spock.lang.Specification+import spock.util.concurrent.PollingConditions++import static org.openremote.container.util.MapAccess.getString+import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_ADMIN_PASSWORD+import static org.openremote.manager.setup.AbstractKeycloakSetup.SETUP_ADMIN_PASSWORD_DEFAULT+import static org.openremote.model.Constants.KEYCLOAK_CLIENT_ID+import static org.openremote.model.Constants.MASTER_REALM_ADMIN_USER++/**+ * This tests the {@link WebsocketClient} by connecting to the manager web socket+ */+class WebsocketClientTest extends Specification implements ManagerContainerTrait {++ def "Check client"() {++ given: "expected conditions"+ def conditions = new PollingConditions(timeout: 10, delay: 1)++ and: "the container is started"+ def serverPort = findEphemeralPort()+ def container = startContainer(defaultConfig(serverPort), defaultServices())+ def protocolExecutorService = container.getService(ManagerExecutorService.class)+ def assetProcessingService = container.getService(AssetProcessingService.class)+ def managerDemoSetup = container.getService(SetupService.class).getTaskOfType(ManagerDemoSetup.class)++ and: "a simple Websocket client"+ def client = new WebsocketClient(+ new URIBuilder("ws://localhost:$serverPort/websocket/events?Auth-Realm=master").build(),+ null,+ new OAuthPasswordGrant("http://localhost:$serverPort/auth/realms/master/protocol/openid-connect/token",+ KEYCLOAK_CLIENT_ID,+ null,+ null,+ MASTER_REALM_ADMIN_USER,+ getString(container.getConfig(), SETUP_ADMIN_PASSWORD, SETUP_ADMIN_PASSWORD_DEFAULT)),+ protocolExecutorService)++ and: "we add callback consumers to the client"+ def connectionStatus = client.getConnectionStatus()+ String lastMessage+ client.addMessageConsumer({+ message -> lastMessage = message+ })+ client.addConnectionStatusConsumer({+ status -> connectionStatus = status+ })++ when: "we call connect on the client"+ client.connect()++ then: "the client status should become CONNECTED"+ conditions.eventually {+ assert client.connectionStatus == ConnectionStatus.CONNECTED+ assert connectionStatus == ConnectionStatus.CONNECTED+ }++ when: "we subscribe to events produced by the server"+ client.sendMessage(EventSubscription.SUBSCRIBE_MESSAGE_PREFIX + Container.JSON.writeValueAsString(+ new EventSubscription(+ AttributeEvent.class,+ new AssetEvent.AssetIdFilter<AttributeEvent>(managerDemoSetup.apartment1LivingroomId),+ "1",+ null)))++ then: "the server should return a subscribed event"+ conditions.eventually {+ assert lastMessage != null+ assert lastMessage.indexOf(EventSubscription.SUBSCRIBED_MESSAGE_PREFIX) == 0+ def subscription = Container.JSON.readValue(lastMessage.substring(EventSubscription.SUBSCRIBED_MESSAGE_PREFIX.length()), EventSubscription.class)+ assert subscription.subscriptionId == "1"+ }++ when: "when an event occurs that should be sent to the client"+ assetProcessingService.sendAttributeEvent(new AttributeEvent(managerDemoSetup.apartment1LivingroomId, "targetTemperature", Values.create(5)))++ then: "the client receives the event"+ conditions.eventually {+ assert lastMessage != null+ assert lastMessage.indexOf(SharedEvent.MESSAGE_PREFIX) == 0+ def triggeredEvent = Container.JSON.readValue(lastMessage.substring(SharedEvent.MESSAGE_PREFIX.length()), TriggeredEventSubscription.class)+ assert triggeredEvent.subscriptionId == "1"+ assert triggeredEvent.events.length == 1+ assert ((AttributeEvent)triggeredEvent.events[0]).entityId == managerDemoSetup.apartment1LivingroomId+ assert ((AttributeEvent)triggeredEvent.events[0]).attributeName == "targetTemperature"+ assert ((AttributeEvent)triggeredEvent.events[0]).value.flatMap{Values.getNumber(it)}.orElse(0) == 5+ }++ cleanup: "the server should be stopped"+ if (client != null) {+ client.disconnect()+ }+ stopContainer(container)+ }+}