Skip to content

Commit 14e9dd6

Browse files
Nullity inference and documentation update
Also, it seems that "Transport" isn't a legal name for an inner enum
1 parent de27e32 commit 14e9dd6

File tree

7 files changed

+66
-35
lines changed

7 files changed

+66
-35
lines changed

README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class SocketController {
7777
}
7878
```
7979

80-
Check out the [full example server](https://github.com/NaikSoftware/stomp-protocol-example-server)
80+
Check out the [upstream example server](https://github.com/NaikSoftware/stomp-protocol-example-server)
8181

8282
## Example library usage
8383

@@ -91,7 +91,7 @@ Check out the [full example server](https://github.com/NaikSoftware/stomp-protoc
9191

9292
// ...
9393

94-
StompClient client = Stomp.over(WebSocket.class, "http://localhost/example-endpoint");
94+
StompClient client = Stomp.over(Stomp.ConnectionProvider.OKHTTP, "http://localhost/example-endpoint");
9595
client.connect();
9696

9797
client.topic("/topic/greetings").subscribe(message -> {
@@ -110,13 +110,13 @@ Check out the [full example server](https://github.com/NaikSoftware/stomp-protoc
110110

111111
```
112112

113-
See the [full example](https://github.com/NaikSoftware/StompProtocolAndroid/tree/master/example-client)
113+
See the [upstream example](https://github.com/NaikSoftware/StompProtocolAndroid/tree/master/example-client)
114114

115-
Method `Stomp.over` consume class for create connection as first parameter.
116-
You must provide dependency for lib and pass class.
117-
At now supported connection providers:
118-
- `org.java_websocket.WebSocket.class` ('org.java-websocket:Java-WebSocket:1.3.2')
119-
- `okhttp3.WebSocket.class` ('com.squareup.okhttp3:okhttp:3.8.1')
115+
Method `Stomp.over` uses an enum to know what connection provider to use.
116+
117+
Currently supported connection providers:
118+
- `Stomp.ConnectionProvider.JWS` ('org.java-websocket:Java-WebSocket:1.3.2')
119+
- `Stomp.ConnectionProvider.OKHTTP` ('com.squareup.okhttp3:okhttp:3.8.1')
120120

121121
You can add own connection provider. Just implement interface `ConnectionProvider`.
122122
If you implement new provider, please create pull request :)
@@ -143,7 +143,7 @@ client.lifecycle().subscribe(lifecycleEvent -> {
143143
You can use a custom OkHttpClient (for example, [if you want to allow untrusted HTTPS](https://gist.github.com/grow2014/b6969d8f0cfc0f0a1b2bf12f84973dec)) using the four-argument overload of Stomp.over, like so:
144144

145145
``` java
146-
client = Stomp.over(WebSocket.class, address, null, getUnsafeOkHttpClient());
146+
client = Stomp.over(Stomp.ConnectionProvider.OKHTTP, address, null, getUnsafeOkHttpClient());
147147
```
148148

149149
Yes, it's safe to pass `null` for either (or both) of the last two arguments. That's exactly what the shorter overloads do.
@@ -222,6 +222,9 @@ These are the possible changes you need to make to your code for this branch, if
222222
- Passing null as the topic path now throws an exception
223223
- Previously, it was supposed to silently fail, although it would probably hit a NPE first (untested)
224224
- Now it throws an IllegalArgumentException
225+
- Connection Provider is selected by an enum
226+
- It used to be done by passing it a WebSocket class from either JWS or OkHttp
227+
- New way of using it can be seen in the examples above
225228
226229
## Additional Reading
227230

lib/src/main/java/ua/naiksoftware/stomp/AbstractConnectionProvider.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ua.naiksoftware.stomp;
22

3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
35
import android.util.Log;
46

57
import rx.Completable;
@@ -16,14 +18,17 @@ abstract class AbstractConnectionProvider implements ConnectionProvider {
1618

1719
private static final String TAG = AbstractConnectionProvider.class.getSimpleName();
1820

21+
@NonNull
1922
private final PublishSubject<LifecycleEvent> mLifecycleStream;
23+
@NonNull
2024
private final PublishSubject<String> mMessagesStream;
2125

2226
AbstractConnectionProvider() {
2327
mLifecycleStream = PublishSubject.create();
2428
mMessagesStream = PublishSubject.create();
2529
}
2630

31+
@NonNull
2732
@Override
2833
public Observable<String> messages() {
2934
createWebSocketConnection();
@@ -48,6 +53,7 @@ public Observable<String> messages() {
4853
*/
4954
abstract void createWebSocketConnection();
5055

56+
@NonNull
5157
@Override
5258
public Completable send(String stompMessage) {
5359
return Completable.fromCallable(() -> {
@@ -82,9 +88,10 @@ public Completable send(String stompMessage) {
8288
* return webSocket;
8389
* </pre>
8490
*/
91+
@Nullable
8592
abstract Object getSocket();
8693

87-
void emitLifecycleEvent(LifecycleEvent lifecycleEvent) {
94+
void emitLifecycleEvent(@NonNull LifecycleEvent lifecycleEvent) {
8895
Log.d(TAG, "Emit lifecycle event: " + lifecycleEvent.getType().name());
8996
mLifecycleStream.onNext(lifecycleEvent);
9097
}
@@ -94,6 +101,7 @@ void emitMessage(String stompMessage) {
94101
mMessagesStream.onNext(stompMessage);
95102
}
96103

104+
@NonNull
97105
@Override
98106
public Observable<LifecycleEvent> getLifecycleReceiver() {
99107
return mLifecycleStream;

lib/src/main/java/ua/naiksoftware/stomp/OkHttpConnectionProvider.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package ua.naiksoftware.stomp;
22

3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
36
import java.util.HashMap;
47
import java.util.Map;
58
import java.util.TreeMap;
@@ -16,18 +19,21 @@
1619
class OkHttpConnectionProvider extends AbstractConnectionProvider {
1720

1821
private final String mUri;
22+
@NonNull
1923
private final Map<String, String> mConnectHttpHeaders;
2024
private final OkHttpClient mOkHttpClient;
2125

26+
@Nullable
2227
private WebSocket openedSocked;
2328

24-
OkHttpConnectionProvider(String uri, Map<String, String> connectHttpHeaders, OkHttpClient okHttpClient) {
29+
OkHttpConnectionProvider(String uri, @Nullable Map<String, String> connectHttpHeaders, OkHttpClient okHttpClient) {
2530
super();
2631
mUri = uri;
2732
mConnectHttpHeaders = connectHttpHeaders != null ? connectHttpHeaders : new HashMap<>();
2833
mOkHttpClient = okHttpClient;
2934
}
3035

36+
@NonNull
3137
@Override
3238
public Completable disconnect() {
3339
return Completable.fromAction(() -> openedSocked.close(1000, ""));
@@ -48,7 +54,7 @@ void createWebSocketConnection() {
4854
openedSocked = mOkHttpClient.newWebSocket(requestBuilder.build(),
4955
new WebSocketListener() {
5056
@Override
51-
public void onOpen(WebSocket webSocket, Response response) {
57+
public void onOpen(WebSocket webSocket, @NonNull Response response) {
5258
LifecycleEvent openEvent = new LifecycleEvent(LifecycleEvent.Type.OPENED);
5359

5460
TreeMap<String, String> headersAsMap = headersAsMap(response);
@@ -63,7 +69,7 @@ public void onMessage(WebSocket webSocket, String text) {
6369
}
6470

6571
@Override
66-
public void onMessage(WebSocket webSocket, ByteString bytes) {
72+
public void onMessage(WebSocket webSocket, @NonNull ByteString bytes) {
6773
emitMessage(bytes.utf8());
6874
}
6975

@@ -87,12 +93,14 @@ void bareSend(String stompMessage) {
8793
openedSocked.send(stompMessage);
8894
}
8995

96+
@Nullable
9097
@Override
9198
Object getSocket() {
9299
return openedSocked;
93100
}
94101

95-
private TreeMap<String, String> headersAsMap(Response response) {
102+
@NonNull
103+
private TreeMap<String, String> headersAsMap(@NonNull Response response) {
96104
TreeMap<String, String> headersAsMap = new TreeMap<>();
97105
Headers headers = response.headers();
98106
for (String key : headers.names()) {
@@ -101,7 +109,7 @@ private TreeMap<String, String> headersAsMap(Response response) {
101109
return headersAsMap;
102110
}
103111

104-
private void addConnectionHeadersToBuilder(Request.Builder requestBuilder, Map<String, String> mConnectHttpHeaders) {
112+
private void addConnectionHeadersToBuilder(@NonNull Request.Builder requestBuilder, @NonNull Map<String, String> mConnectHttpHeaders) {
105113
for (Map.Entry<String, String> headerEntry : mConnectHttpHeaders.entrySet()) {
106114
requestBuilder.addHeader(headerEntry.getKey(), headerEntry.getValue());
107115
}

lib/src/main/java/ua/naiksoftware/stomp/Stomp.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package ua.naiksoftware.stomp;
22

3+
import android.support.annotation.NonNull;
34
import android.support.annotation.Nullable;
45

56
import java.util.Map;
@@ -19,19 +20,19 @@
1920
*/
2021
public class Stomp {
2122

22-
public static StompClient over(Transport transport, String uri) {
23-
return over(transport, uri, null, null);
23+
public static StompClient over(@NonNull ConnectionProvider connectionProvider, String uri) {
24+
return over(connectionProvider, uri, null, null);
2425
}
2526

2627
/**
2728
*
28-
* @param transport transport method
29+
* @param connectionProvider connectionProvider method
2930
* @param uri URI to connect
3031
* @param connectHttpHeaders HTTP headers, will be passed with handshake query, may be null
3132
* @return StompClient for receiving and sending messages. Call #StompClient.connect
3233
*/
33-
public static StompClient over(Transport transport, String uri, Map<String, String> connectHttpHeaders) {
34-
return over(transport, uri, connectHttpHeaders, null);
34+
public static StompClient over(@NonNull ConnectionProvider connectionProvider, String uri, Map<String, String> connectHttpHeaders) {
35+
return over(connectionProvider, uri, connectHttpHeaders, null);
3536
}
3637

3738
/**
@@ -40,32 +41,32 @@ public static StompClient over(Transport transport, String uri, Map<String, Stri
4041
* <li>{@code org.java_websocket.WebSocket}: cannot accept an existing client</li>
4142
* <li>{@code okhttp3.WebSocket}: can accept a non-null instance of {@code okhttp3.OkHttpClient}</li>
4243
* </ul>
43-
* @param transport transport method
44+
* @param connectionProvider connectionProvider method
4445
* @param uri URI to connect
4546
* @param connectHttpHeaders HTTP headers, will be passed with handshake query, may be null
4647
* @param okHttpClient Existing client that will be used to open the WebSocket connection, may be null to use default client
4748
* @return StompClient for receiving and sending messages. Call #StompClient.connect
4849
*/
49-
public static StompClient over(Transport transport, String uri, @Nullable Map<String, String> connectHttpHeaders, @Nullable OkHttpClient okHttpClient) {
50-
if (transport == Transport.JWS) {
50+
public static StompClient over(@NonNull ConnectionProvider connectionProvider, String uri, @Nullable Map<String, String> connectHttpHeaders, @Nullable OkHttpClient okHttpClient) {
51+
if (connectionProvider == ConnectionProvider.JWS) {
5152
if (okHttpClient != null) {
5253
throw new IllegalArgumentException("You cannot pass a webSocketClient with 'org.java_websocket.WebSocket'. use null instead.");
5354
}
5455
return createStompClient(new WebSocketsConnectionProvider(uri, connectHttpHeaders));
5556
}
5657

57-
if (transport == Transport.OKHTTP) {
58+
if (connectionProvider == ConnectionProvider.OKHTTP) {
5859
return createStompClient(new OkHttpConnectionProvider(uri, connectHttpHeaders, (okHttpClient == null) ? new OkHttpClient() : okHttpClient));
5960
}
6061

61-
throw new IllegalArgumentException("Transport type not supported: " + transport.toString());
62+
throw new IllegalArgumentException("ConnectionProvider type not supported: " + connectionProvider.toString());
6263
}
6364

64-
private static StompClient createStompClient(ConnectionProvider connectionProvider) {
65+
private static StompClient createStompClient(ua.naiksoftware.stomp.ConnectionProvider connectionProvider) {
6566
return new StompClient(connectionProvider);
6667
}
6768

68-
public enum Transport {
69+
public enum ConnectionProvider {
6970
OKHTTP, JWS
7071
}
7172
}

lib/src/main/java/ua/naiksoftware/stomp/WebSocketsConnectionProvider.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ua.naiksoftware.stomp;
22

3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
35
import android.util.Log;
46

57
import org.java_websocket.WebSocket;
@@ -29,6 +31,7 @@ class WebSocketsConnectionProvider extends AbstractConnectionProvider {
2931
private static final String TAG = WebSocketsConnectionProvider.class.getSimpleName();
3032

3133
private final String mUri;
34+
@NonNull
3235
private final Map<String, String> mConnectHttpHeaders;
3336

3437
private WebSocketClient mWebSocketClient;
@@ -39,11 +42,12 @@ class WebSocketsConnectionProvider extends AbstractConnectionProvider {
3942
* Support UIR scheme ws://host:port/path
4043
* @param connectHttpHeaders may be null
4144
*/
42-
WebSocketsConnectionProvider(String uri, Map<String, String> connectHttpHeaders) {
45+
WebSocketsConnectionProvider(String uri, @Nullable Map<String, String> connectHttpHeaders) {
4346
mUri = uri;
4447
mConnectHttpHeaders = connectHttpHeaders != null ? connectHttpHeaders : new HashMap<>();
4548
}
4649

50+
@NonNull
4751
@Override
4852
public Completable disconnect() {
4953
return Completable.fromAction(() -> mWebSocketClient.close());
@@ -57,7 +61,7 @@ void createWebSocketConnection() {
5761
mWebSocketClient = new WebSocketClient(URI.create(mUri), new Draft_17(), mConnectHttpHeaders, 0) {
5862

5963
@Override
60-
public void onWebsocketHandshakeReceivedAsClient(WebSocket conn, ClientHandshake request, ServerHandshake response) throws InvalidDataException {
64+
public void onWebsocketHandshakeReceivedAsClient(WebSocket conn, ClientHandshake request, @NonNull ServerHandshake response) throws InvalidDataException {
6165
Log.d(TAG, "onWebsocketHandshakeReceivedAsClient with response: " + response.getHttpStatus() + " " + response.getHttpStatusMessage());
6266
mServerHandshakeHeaders = new TreeMap<>();
6367
Iterator<String> keys = response.iterateHttpFields();
@@ -68,7 +72,7 @@ public void onWebsocketHandshakeReceivedAsClient(WebSocket conn, ClientHandshake
6872
}
6973

7074
@Override
71-
public void onOpen(ServerHandshake handshakeData) {
75+
public void onOpen(@NonNull ServerHandshake handshakeData) {
7276
Log.d(TAG, "onOpen with handshakeData: " + handshakeData.getHttpStatus() + " " + handshakeData.getHttpStatusMessage());
7377
LifecycleEvent openEvent = new LifecycleEvent(LifecycleEvent.Type.OPENED);
7478
openEvent.setHandshakeResponseHeaders(mServerHandshakeHeaders);

lib/src/main/java/ua/naiksoftware/stomp/client/StompClient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ua.naiksoftware.stomp.client;
22

3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
35
import android.util.Log;
46

57
import java.util.ArrayList;
@@ -70,7 +72,7 @@ public void connect(List<StompHeader> _headers) {
7072
*
7173
* @param _headers might be null
7274
*/
73-
public void connect(List<StompHeader> _headers, boolean reconnect) {
75+
public void connect(@Nullable List<StompHeader> _headers, boolean reconnect) {
7476
if (reconnect) disconnect();
7577
if (mConnected) return;
7678
lifecycle()
@@ -119,7 +121,7 @@ public Completable send(String destination, String data) {
119121
data));
120122
}
121123

122-
public Completable send(StompMessage stompMessage) {
124+
public Completable send(@NonNull StompMessage stompMessage) {
123125
Completable completable = mConnectionProvider.send(stompMessage.compile());
124126
mConnectionComplete.subscribe();
125127
return completable.startWith(mConnectionComplete);
@@ -141,7 +143,7 @@ public Observable<StompMessage> topic(String destinationPath) {
141143
return topic(destinationPath, null);
142144
}
143145

144-
public Observable<StompMessage> topic(String destPath, List<StompHeader> headerList) {
146+
public Observable<StompMessage> topic(@Nullable String destPath, List<StompHeader> headerList) {
145147
if (destPath == null)
146148
return Observable.error(new IllegalArgumentException("Topic path cannot be null"));
147149
else if (!mStreamMap.containsKey(destPath))
@@ -155,7 +157,7 @@ else if (!mStreamMap.containsKey(destPath))
155157
return mStreamMap.get(destPath);
156158
}
157159

158-
private Completable subscribePath(String destinationPath, List<StompHeader> headerList) {
160+
private Completable subscribePath(String destinationPath, @Nullable List<StompHeader> headerList) {
159161
String topicId = UUID.randomUUID().toString();
160162

161163
if (mTopics == null) mTopics = new HashMap<>();

lib/src/main/java/ua/naiksoftware/stomp/client/StompMessage.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package ua.naiksoftware.stomp.client;
22

3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
36
import java.io.StringReader;
47
import java.util.ArrayList;
58
import java.util.List;
@@ -40,6 +43,7 @@ public String getStompCommand() {
4043
return mStompCommand;
4144
}
4245

46+
@Nullable
4347
public String findHeader(String key) {
4448
if (mStompHeaders == null) return null;
4549
for (StompHeader header : mStompHeaders) {
@@ -48,6 +52,7 @@ public String findHeader(String key) {
4852
return null;
4953
}
5054

55+
@NonNull
5156
public String compile() {
5257
StringBuilder builder = new StringBuilder();
5358
builder.append(mStompCommand).append('\n');
@@ -62,7 +67,7 @@ public String compile() {
6267
return builder.toString();
6368
}
6469

65-
public static StompMessage from(String data) {
70+
public static StompMessage from(@Nullable String data) {
6671
if (data == null || data.trim().isEmpty()) {
6772
return new StompMessage(StompCommand.UNKNOWN, null, data);
6873
}

0 commit comments

Comments
 (0)