Skip to content

Commit a49aff9

Browse files
cyrushineLin Wei
andauthored
Support customize subscribe.options && compatible with null value for type int (#514)
* support customize subscribe.options * enable empty SubscribeOptions * compatible with null value for type int Co-authored-by: Lin Wei <linwei@ifanr.com>
1 parent b374b47 commit a49aff9

File tree

3 files changed

+69
-36
lines changed

3 files changed

+69
-36
lines changed

autobahn/src/main/java/io/crossbar/autobahn/wamp/messages/Subscribe.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ public class Subscribe implements IMessage {
3030

3131
private final long request;
3232
private final String topic;
33-
private final String match;
34-
private final boolean getRetained;
33+
private final SubscribeOptions options;
3534

3635
private static final String MATCH_EXACT = "exact";
3736
private static final String MATCH_PREFIX = "prefix";
@@ -41,54 +40,48 @@ public Subscribe(long request, SubscribeOptions options, String topic) {
4140
this.request = request;
4241
this.topic = topic;
4342
if (options != null) {
44-
if (options.match != null) {
45-
if (!options.match.equals(MATCH_EXACT) && !options.match.equals(MATCH_PREFIX) &&
46-
!options.match.equals(MATCH_WILDCARD)) {
43+
String match = options.getMatch();
44+
if (match != null) {
45+
if (!match.equals(MATCH_EXACT) && !match.equals(MATCH_PREFIX) &&
46+
!match.equals(MATCH_WILDCARD)) {
4747
throw new IllegalArgumentException("match must be one of exact, prefix or wildcard.");
4848
}
4949
}
50-
this.match = options.match;
51-
this.getRetained = options.getRetained;
50+
this.options = options;
5251
} else {
53-
this.match = MATCH_EXACT;
54-
this.getRetained = false;
52+
this.options = new SubscribeOptions(MATCH_EXACT, false);
5553
}
5654
}
5755

5856
public static Subscribe parse(List<Object> wmsg) {
5957
MessageUtil.validateMessage(wmsg, MESSAGE_TYPE, "SUBSCRIBE", 4);
6058

6159
long request = MessageUtil.parseLong(wmsg.get(1));
62-
Map<String, Object> options = (Map<String, Object>) wmsg.get(2);
60+
SubscribeOptions options = new SubscribeOptions((Map<String, Object>) wmsg.get(2));
6361

64-
String match = null;
65-
if (options.containsKey("match")) {
66-
match = (String) options.get("match");
67-
if (!match.equals(MATCH_EXACT) && !match.equals(MATCH_PREFIX) &&
68-
!match.equals(MATCH_WILDCARD)) {
69-
throw new ProtocolError("match must be one of exact, prefix or wildcard.");
70-
}
62+
String match = options.getMatch();
63+
if (match != null && !match.equals(MATCH_EXACT) && !match.equals(MATCH_PREFIX) &&
64+
!match.equals(MATCH_WILDCARD)) {
65+
throw new ProtocolError("match must be one of exact, prefix or wildcard.");
7166
}
72-
boolean getRetained = getOrDefault(options, "get_retained", false);
7367

7468
String topic = (String) wmsg.get(3);
75-
SubscribeOptions opt = new SubscribeOptions(match, true, getRetained);
76-
return new Subscribe(request, opt, topic);
69+
return new Subscribe(request, options, topic);
7770
}
7871

7972
@Override
8073
public List<Object> marshal() {
8174
List<Object> marshaled = new ArrayList<>();
8275
marshaled.add(MESSAGE_TYPE);
8376
marshaled.add(request);
84-
Map<String, Object> extra = new HashMap<>();
85-
if (match != null && !match.equals(MATCH_EXACT)) {
86-
extra.put("match", match);
87-
}
88-
if (getRetained) {
89-
extra.put("get_retained", getRetained);
77+
78+
SubscribeOptions options = new SubscribeOptions(this.options);
79+
String match = options.getMatch();
80+
if (match != null && match.equals(MATCH_EXACT)) {
81+
options.removeMatch();
9082
}
91-
marshaled.add(extra);
83+
84+
marshaled.add(options);
9285
marshaled.add(topic);
9386
return marshaled;
9487
}

autobahn/src/main/java/io/crossbar/autobahn/wamp/types/SubscribeOptions.java

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,51 @@
1111

1212
package io.crossbar.autobahn.wamp.types;
1313

14-
public class SubscribeOptions {
15-
public String match;
16-
public boolean details;
17-
public boolean getRetained;
14+
import java.util.HashMap;
15+
import java.util.Map;
1816

19-
public SubscribeOptions(String match, boolean details, boolean getRetained) {
20-
this.match = match;
21-
this.details = details;
22-
this.getRetained = getRetained;
17+
public class SubscribeOptions extends HashMap<String, Object> {
18+
19+
public static final String KEY_MATCH = "match";
20+
public static final String KEY_GET_RETAINED = "get_retained";
21+
22+
public SubscribeOptions(String match, boolean getRetained) {
23+
putMatch(match);
24+
putGetRetained(getRetained);
25+
}
26+
27+
public SubscribeOptions(Map<String, Object> origin) {
28+
super(origin);
29+
}
30+
31+
public SubscribeOptions() {
32+
super();
33+
}
34+
35+
public void putMatch(String match) {
36+
put(KEY_MATCH, match);
37+
}
38+
39+
public void putGetRetained(boolean getRetained) {
40+
put(KEY_GET_RETAINED, getRetained);
2341
}
42+
43+
public String getMatch() {
44+
Object value = get(KEY_MATCH);
45+
return value instanceof String ? (String) value : null;
46+
}
47+
48+
public Boolean getRetained() {
49+
Object value = get(KEY_GET_RETAINED);
50+
return value instanceof Boolean ? (Boolean) value : null;
51+
}
52+
53+
public void removeMatch() {
54+
remove(KEY_MATCH);
55+
}
56+
57+
public void removeGetRetained() {
58+
remove(KEY_GET_RETAINED);
59+
}
60+
2461
}

autobahn/src/main/java/io/crossbar/autobahn/wamp/utils/MessageUtil.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ public static long parseLong(Object object) {
6767
if (object instanceof Integer) {
6868
return (int) object;
6969
}
70-
return (long) object;
70+
if (object instanceof Long) {
71+
return (Long) object;
72+
}
73+
return 0;
7174
}
7275
}

0 commit comments

Comments
 (0)