diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index ec4300d..3469cc7 100644 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,5 +1,5 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 -org.eclipse.jdt.core.compiler.compliance=1.7 +org.eclipse.jdt.core.compiler.compliance=1.6 org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.7 +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/README.md b/README.md index e22333a..e08ee92 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ This library offers MQTT client functionality over WebSocket transport with [Pah # Supported Paho MQTT library version and Jetty WebSocket Client version 1. [Paho org.eclipse.paho.mqtt.java 1.0.0](http://git.eclipse.org/c/paho/org.eclipse.paho.mqtt.java.git/tag/?id=v1.0.0) -1. [Jetty websocket-client 9.2.1.v20140609](http://www.eclipse.org/jetty/documentation/9.2.1.v20140609/jetty-websocket-client-api.html) +1. [Jetty websocket-client 9.2.5.v20141112](http://www.eclipse.org/jetty/documentation/9.2.3.v20140905/jetty-websocket-client-api.html) # Supported JDK/JRE Version @@ -22,10 +22,32 @@ The following libraries are requried as well. | GroupId | ArtifactId | Version | |---------------------------|----------------|---------------| -|org.eclipse.jetty |jetty-io |9.2.1.v20140609| -|org.eclipse.jetty |jetty-util |9.2.1.v20140609| -|org.eclipse.jetty.websocket|websocket-api |9.2.1.v20140609| -|org.eclipse.jetty.websocket|websocket-common|9.2.1.v20140609| +|org.eclipse.jetty |jetty-io |9.2.5.v20141112| +|org.eclipse.jetty |jetty-util |9.2.5.v20141112| +|org.eclipse.jetty.websocket|websocket-api |9.2.5.v20141112| +|org.eclipse.jetty.websocket|websocket-common|9.2.5.v20141112| + +## maven pom.xml settings + +Adds the following elements to your pom.xml if you're using maven. + +``` + + io.inventit.dev + mqtt-websocket-java + 1.0.1 + + + org.eclipse.jetty.websocket + websocket-client + 9.2.5.v20141112 + + + org.eclipse.paho + org.eclipse.paho.client.mqttv3 + 1.0.0 + +``` # How to build @@ -35,13 +57,19 @@ Note that Paho Java library is included in this project as the binary isn't uplo $ mvn clean package -Then you'll get `mqtt-websocket-java-1.0.0.jar` under the `target` directory. - +Then you'll get `mqtt-websocket-java-.jar` under the `target` directory. # How to use -Here is a sample code to use `MqttWebSocketAsyncClient`. +You can use this library as the same manner as Paho's library but use `MqttWebSocketAsyncClient` instead of Paho's classes such as `MqttClient` and `MqttAsyncClient`. + +The `MqttWebSocketAsyncClient` supports the following URI schimes: -Do NOT use `MqttClient` as it always uses Paho's default async client `MqttAsyncClient`. +1. `ws://:` ... for a plain WebSocket +1. `wss://:` ... for a WebSocket with SSL/TLS +1. `tcp://:` ... for a plain TCP MQTT socket +1. `ssl://:` ... for a secure SSL/TLS MQTT socket + +Here is sample code to use `MqttWebSocketAsyncClient`. // Plain MQTT // final String uriString = "tcp://your-mqtt-broker:1883"; @@ -90,3 +118,16 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +# Change History + +[1.0.1 : November 29, 2014](https://github.com/inventit/mqtt-websocket-java/releases/tag/1.0.1) + +* Upgrades Jetty 9 library +* Adds a new factory method for WebSocketNetworkModule instance +* Adds new constructors with a new paramter for specifying the logger name to MqttWebSocketAsyncClient +* Releases a JDK1.6 class version (50) jar as well in order for Android app to include this library (**Note that Jetty 9 itself doesn't support Android and JDK 1.6**) + +[1.0.0 : July 30, 2014](https://github.com/inventit/mqtt-websocket-java/releases/tag/1.0.0) + +* Initial diff --git a/pom.xml b/pom.xml index ef5efd3..b26d187 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.inventit.dev mqtt-websocket-java - 1.0.0 + 1.0.1 MQTT over WebScoket for Java with Paho and Jetty https://github.com/inventit/mqtt-websocket-java @@ -34,6 +34,8 @@ + + 1.7 UTF-8 @@ -53,8 +55,8 @@ 3.1 UTF-8 - 1.7 - 1.7 + 1.6 + ${target.jdk} @@ -107,7 +109,6 @@ - org.apache.maven.plugins maven-javadoc-plugin 2.9 @@ -121,9 +122,9 @@ - org.apache.maven.plugins maven-jar-plugin + ${jdk.classifier} false @@ -143,14 +144,13 @@ org.eclipse.jetty.websocket websocket-client - 9.2.1.v20140609 - compile + 9.2.5.v20141112 + true org.eclipse.paho org.eclipse.paho.client.mqttv3 1.0.0 - compile junit @@ -177,4 +177,16 @@ test + + + jdk16 + + 1.6 + + + jdk16 + 1.6 + + + diff --git a/src/main/java/io/inventit/dev/mqtt/paho/MqttWebSocketAsyncClient.java b/src/main/java/io/inventit/dev/mqtt/paho/MqttWebSocketAsyncClient.java index 985d19f..1ffd9a6 100644 --- a/src/main/java/io/inventit/dev/mqtt/paho/MqttWebSocketAsyncClient.java +++ b/src/main/java/io/inventit/dev/mqtt/paho/MqttWebSocketAsyncClient.java @@ -21,8 +21,7 @@ public class MqttWebSocketAsyncClient extends MqttAsyncClient { private static final String CLASS_NAME = MqttWebSocketAsyncClient.class .getName(); - private static final Logger log = LoggerFactory.getLogger( - LoggerFactory.MQTT_CLIENT_MSG_CAT, CLASS_NAME); + private final Logger log; private final String serverURI; @@ -33,7 +32,7 @@ public class MqttWebSocketAsyncClient extends MqttAsyncClient { * @param original * @return */ - static String createDummyURI(String original) { + protected static String createDummyURI(String original) { if (!original.startsWith("ws:") && !original.startsWith("wss:")) { return original; } @@ -42,19 +41,23 @@ static String createDummyURI(String original) { + (uri.getPort() > 0 ? uri.getPort() : 80); } - static boolean isDummyURI(String uri) { + protected static boolean isDummyURI(String uri) { return uri.startsWith("tcp://DUMMY-"); } public MqttWebSocketAsyncClient(String serverURI, String clientId, - MqttClientPersistence persistence, MqttPingSender pingSender) - throws MqttException { + MqttClientPersistence persistence, MqttPingSender pingSender, + String loggerName) throws MqttException { super(createDummyURI(serverURI), clientId, persistence, pingSender); this.serverURI = serverURI; final String methodName = "MqttWebSocketAsyncClient"; + this.log = LoggerFactory.getLogger(LoggerFactory.MQTT_CLIENT_MSG_CAT, + (loggerName == null || loggerName.length() == 0) ? CLASS_NAME + : loggerName); + // @TRACE 101= ClientID={0} ServerURI={1} PersistenceType={2} if (log.isLoggable(Logger.FINE)) { log.fine(CLASS_NAME, methodName, "101", new Object[] { clientId, @@ -62,14 +65,26 @@ public MqttWebSocketAsyncClient(String serverURI, String clientId, } } + public MqttWebSocketAsyncClient(String serverURI, String clientId, + MqttClientPersistence persistence, String loggerName) + throws MqttException { + this(serverURI, clientId, persistence, new TimerPingSender(), + loggerName); + } + public MqttWebSocketAsyncClient(String serverURI, String clientId, MqttClientPersistence persistence) throws MqttException { - this(serverURI, clientId, persistence, new TimerPingSender()); + this(serverURI, clientId, persistence, null); + } + + public MqttWebSocketAsyncClient(String serverURI, String clientId, + String loggerName) throws MqttException { + this(serverURI, clientId, new MqttDefaultFilePersistence(), loggerName); } public MqttWebSocketAsyncClient(String serverURI, String clientId) throws MqttException { - this(serverURI, clientId, new MqttDefaultFilePersistence()); + this(serverURI, clientId, (String) null); } /** @@ -136,9 +151,25 @@ protected NetworkModule createNetworkModule(String input, // http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/cs01/mqtt-v3.1.1-cs01.html#_Toc388534418 subProtocol = "mqtt"; } + return newWebSocketNetworkModule(URI.create(address), subProtocol, + options); + } + /** + * A factory method for instantiating a {@link NetworkModule} with websocket + * support. Subclasses is able to extend this method in order to create an + * arbitrary {@link NetworkModule} class instance. + * + * @param uri + * @param subProtocol + * Either `mqtt` for MQTT v3 or `mqttv3.1` for MQTT v3.1 + * @param options + * @return + */ + protected NetworkModule newWebSocketNetworkModule(URI uri, + String subProtocol, MqttConnectOptions options) { final WebSocketNetworkModule netModule = new WebSocketNetworkModule( - URI.create(address), subProtocol, getClientId()); + uri, subProtocol, getClientId()); netModule.setConnectTimeout(options.getConnectionTimeout()); return netModule; } diff --git a/src/main/java/io/inventit/dev/mqtt/paho/WebSocketNetworkModule.java b/src/main/java/io/inventit/dev/mqtt/paho/WebSocketNetworkModule.java index 0a6674f..f25491e 100644 --- a/src/main/java/io/inventit/dev/mqtt/paho/WebSocketNetworkModule.java +++ b/src/main/java/io/inventit/dev/mqtt/paho/WebSocketNetworkModule.java @@ -25,7 +25,7 @@ import org.eclipse.paho.client.mqttv3.logging.LoggerFactory; /** - * A network module for connecting over WebScoket with Jetty. + * A network module for connecting over WebScoket with Jetty 9. */ public class WebSocketNetworkModule extends WebSocketAdapter implements NetworkModule { @@ -45,7 +45,7 @@ public class WebSocketNetworkModule extends WebSocketAdapter implements private final String subProtocol; /** - * A stream for outgonig data + * A stream for outgoing data */ private final ByteArrayOutputStream outputStream = new ByteArrayOutputStream() { @Override