Skip to content

Commit 63407b0

Browse files
author
Max Presman
committed
clean up here now, get state and set state
1 parent 3624365 commit 63407b0

13 files changed

Lines changed: 139 additions & 130 deletions

File tree

‎build.gradle‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ repositories {
2626

2727
dependencies {
2828
compile group: 'com.squareup.retrofit2', name: 'retrofit', version:'2.1.0'
29+
compile group: 'com.squareup.okhttp3', name: 'logging-interceptor', version:'3.4.1'
30+
compile group: 'org.slf4j', name: 'slf4j-nop', version:'1.7.21'
31+
32+
// jackson
2933
compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version:'2.7.3'
3034
compile group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version:'2.7.3'
3135
compile group: 'com.squareup.retrofit2', name: 'converter-jackson', version:'2.1.0'
32-
compile group: 'com.squareup.okhttp3', name: 'logging-interceptor', version:'3.4.1'
33-
compile group: 'org.slf4j', name: 'slf4j-nop', version:'1.7.21'
36+
//
37+
38+
// gson
39+
// compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
40+
// compile group: 'com.squareup.retrofit2', name: 'converter-gson', version:'2.1.0'
41+
//
3442

3543
testCompile group: 'ch.qos.logback', name: 'logback-classic', version:'1.1.7'
3644
testCompile group: 'ch.qos.logback', name: 'logback-core', version:'1.1.7'
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Fri Nov 25 18:18:27 PST 2016
1+
#Fri Nov 25 21:51:45 PST 2016
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-all.zip

‎src/main/java/com/pubnub/api/endpoints/presence/GetState.java‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pubnub.api.endpoints.presence;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import com.pubnub.api.PubNub;
45
import com.pubnub.api.PubNubException;
56
import com.pubnub.api.PubNubUtil;
@@ -16,11 +17,12 @@
1617

1718
import java.util.ArrayList;
1819
import java.util.HashMap;
20+
import java.util.Iterator;
1921
import java.util.List;
2022
import java.util.Map;
2123

2224
@Accessors(chain = true, fluent = true)
23-
public class GetState extends Endpoint<Envelope<Object>, PNGetStateResult> {
25+
public class GetState extends Endpoint<Envelope<JsonNode>, PNGetStateResult> {
2426

2527
@Setter
2628
private List<String> channels;
@@ -46,7 +48,7 @@ protected void validateParams() throws PubNubException {
4648
}
4749

4850
@Override
49-
protected Call<Envelope<Object>> doWork(Map<String, String> params) {
51+
protected Call<Envelope<JsonNode>> doWork(Map<String, String> params) {
5052
PresenceService service = this.getRetrofit().create(PresenceService.class);
5153

5254
if (channelGroups.size() > 0) {
@@ -61,14 +63,16 @@ protected Call<Envelope<Object>> doWork(Map<String, String> params) {
6163
}
6264

6365
@Override
64-
protected PNGetStateResult createResponse(final Response<Envelope<Object>> input) throws PubNubException {
65-
Map<String, Object> stateMappings;
66+
protected PNGetStateResult createResponse(final Response<Envelope<JsonNode>> input) throws PubNubException {
67+
Map<String, JsonNode> stateMappings = new HashMap<>();
6668

6769
if (channels.size() == 1 && channelGroups.size() == 0) {
68-
stateMappings = new HashMap<>();
6970
stateMappings.put(channels.get(0), input.body().getPayload());
7071
} else {
71-
stateMappings = (Map<String, Object>) input.body().getPayload();
72+
for (Iterator<Map.Entry<String, JsonNode>> it = input.body().getPayload().fields(); it.hasNext();) {
73+
Map.Entry<String, JsonNode> stateMapping = it.next();
74+
stateMappings.put(stateMapping.getKey(), stateMapping.getValue());
75+
}
7276
}
7377

7478
return PNGetStateResult.builder().stateByUUID(stateMappings).build();

‎src/main/java/com/pubnub/api/endpoints/presence/HereNow.java‎

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pubnub.api.endpoints.presence;
22

33

4+
import com.fasterxml.jackson.databind.JsonNode;
45
import com.pubnub.api.PubNub;
56
import com.pubnub.api.PubNubException;
67
import com.pubnub.api.PubNubUtil;
@@ -19,11 +20,12 @@
1920

2021
import java.util.ArrayList;
2122
import java.util.HashMap;
23+
import java.util.Iterator;
2224
import java.util.List;
2325
import java.util.Map;
2426

2527
@Accessors(chain = true, fluent = true)
26-
public class HereNow extends Endpoint<Envelope<Object>, PNHereNowResult> {
28+
public class HereNow extends Endpoint<Envelope<JsonNode>, PNHereNowResult> {
2729
@Setter
2830
private List<String> channels;
2931
@Setter
@@ -48,7 +50,7 @@ protected void validateParams() throws PubNubException {
4850
}
4951

5052
@Override
51-
protected Call<Envelope<Object>> doWork(Map<String, String> params) {
53+
protected Call<Envelope<JsonNode>> doWork(Map<String, String> params) {
5254

5355
if (includeState == null) {
5456
includeState = false;
@@ -86,7 +88,7 @@ protected Call<Envelope<Object>> doWork(Map<String, String> params) {
8688
}
8789

8890
@Override
89-
protected PNHereNowResult createResponse(Response<Envelope<Object>> input) {
91+
protected PNHereNowResult createResponse(Response<Envelope<JsonNode>> input) {
9092
PNHereNowResult herenowData;
9193

9294
if (channels.size() > 1 || channelGroups.size() > 0) {
@@ -98,7 +100,7 @@ protected PNHereNowResult createResponse(Response<Envelope<Object>> input) {
98100
return herenowData;
99101
}
100102

101-
private PNHereNowResult parseSingleChannelResponse(Envelope<Object> input) {
103+
private PNHereNowResult parseSingleChannelResponse(Envelope<JsonNode> input) {
102104
PNHereNowResult hereNowData = PNHereNowResult.builder()
103105
.totalChannels(1)
104106
.channels(new HashMap<String, PNHereNowChannelData>())
@@ -117,72 +119,55 @@ private PNHereNowResult parseSingleChannelResponse(Envelope<Object> input) {
117119
return hereNowData;
118120
}
119121

120-
private PNHereNowResult parseMultipleChannelResponse(Object input) {
121-
Map<String, Object> parsedInput = (Map<String, Object>) input;
122-
122+
private PNHereNowResult parseMultipleChannelResponse(JsonNode input) {
123123
PNHereNowResult hereNowData = PNHereNowResult.builder()
124124
.channels(new HashMap<String, PNHereNowChannelData>())
125-
.totalChannels((Integer) parsedInput.get("total_channels"))
126-
.totalOccupancy((Integer) parsedInput.get("total_occupancy"))
125+
.totalChannels(input.get("total_channels").asInt())
126+
.totalOccupancy(input.get("total_occupancy").asInt())
127127
.build();
128128

129-
Map<String, Object> channelsParam = (HashMap<String, Object>) parsedInput.get("channels");
130-
131-
for (Map.Entry<String, Object> entry : channelsParam.entrySet()) {
132-
Map<String, Object> channel = (Map<String, Object>) channelsParam.get(entry.getKey());
129+
for (Iterator<Map.Entry<String, JsonNode>> it = input.get("channels").fields(); it.hasNext();) {
130+
Map.Entry<String, JsonNode> entry = it.next();
133131

134132
PNHereNowChannelData.PNHereNowChannelDataBuilder hereNowChannelData = PNHereNowChannelData.builder()
135133
.channelName(entry.getKey())
136-
.occupancy((Integer) channel.get("occupancy"));
134+
.occupancy(entry.getValue().get("occupancy").asInt());
137135

138136
if (includeUUIDs) {
139-
hereNowChannelData.occupants(prepareOccupantData(channel.get("uuids")));
137+
hereNowChannelData.occupants(prepareOccupantData(entry.getValue().get("uuids")));
140138
} else {
141139
hereNowChannelData.occupants(null);
142140
}
143141

144142
hereNowData.getChannels().put(entry.getKey(), hereNowChannelData.build());
145143
}
146144

147-
// for (String channelName : channelsParam.keySet()) {
148-
// Map<String, Object> channel = (Map<String, Object>) channelsParam.get(channelName);
149-
//
150-
// PNHereNowChannelData.PNHereNowChannelDataBuilder hereNowChannelData = PNHereNowChannelData.builder()
151-
// .channelName(channelName)
152-
// .occupancy((Integer) channel.get("occupancy"));
153-
//
154-
// if (includeUUIDs) {
155-
// hereNowChannelData.occupants(prepareOccupantData(channel.get("uuids")));
156-
// } else {
157-
// hereNowChannelData.occupants(null);
158-
// }
159-
//
160-
// hereNowData.getChannels().put(channelName, hereNowChannelData.build());
161-
// }
162-
163145
return hereNowData;
164146
}
165147

166-
private List<PNHereNowOccupantData> prepareOccupantData(Object input) {
167-
List<PNHereNowOccupantData> occupantsResults = new ArrayList<PNHereNowOccupantData>();
148+
private List<PNHereNowOccupantData> prepareOccupantData(JsonNode input) {
149+
List<PNHereNowOccupantData> occupantsResults = new ArrayList<>();
168150

169151
if (includeState != null && includeState) {
170-
for (Map<String, Object> occupant : (List<Map<String, Object>>) input) {
152+
for (Iterator<JsonNode> it = input.elements(); it.hasNext();) {
153+
JsonNode occupant = it.next();
171154
PNHereNowOccupantData.PNHereNowOccupantDataBuilder hereNowOccupantData = PNHereNowOccupantData.builder();
172-
hereNowOccupantData.uuid((String) occupant.get("uuid"));
155+
hereNowOccupantData.uuid(occupant.get("uuid").asText());
173156
hereNowOccupantData.state(occupant.get("state"));
174157

175158
occupantsResults.add(hereNowOccupantData.build());
176159
}
177160
} else {
178-
for (String occupant : (List<String>) input) {
161+
for (Iterator<JsonNode> it = input.elements(); it.hasNext();) {
162+
JsonNode occupant = it.next();
179163
PNHereNowOccupantData.PNHereNowOccupantDataBuilder hereNowOccupantData = PNHereNowOccupantData.builder();
180-
hereNowOccupantData.uuid(occupant);
164+
hereNowOccupantData.uuid(occupant.asText());
181165
hereNowOccupantData.state(null);
182166

183167
occupantsResults.add(hereNowOccupantData.build());
184168
}
185169
}
170+
186171
return occupantsResults;
187172
}
188173

‎src/main/java/com/pubnub/api/endpoints/presence/PresenceService.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pubnub.api.endpoints.presence;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import com.pubnub.api.models.server.Envelope;
45
import com.pubnub.api.models.server.presence.WhereNowPayload;
56
import retrofit2.Call;
@@ -27,22 +28,22 @@ Call<Envelope<WhereNowPayload>> whereNow(@Path("subKey") String subKey,
2728
@QueryMap Map<String, String> options);
2829

2930
@GET("v2/presence/sub_key/{subKey}")
30-
Call<Envelope<Object>> globalHereNow(@Path("subKey") String subKey,
31-
@QueryMap Map<String, String> options);
31+
Call<Envelope<JsonNode>> globalHereNow(@Path("subKey") String subKey,
32+
@QueryMap Map<String, String> options);
3233

3334
@GET("v2/presence/sub_key/{subKey}/channel/{channel}")
34-
Call<Envelope<Object>> hereNow(@Path("subKey") String subKey,
35+
Call<Envelope<JsonNode>> hereNow(@Path("subKey") String subKey,
3536
@Path("channel") String channel,
3637
@QueryMap Map<String, String> options);
3738

3839
@GET("v2/presence/sub-key/{subKey}/channel/{channel}/uuid/{uuid}")
39-
Call<Envelope<Object>> getState(@Path("subKey") String subKey,
40+
Call<Envelope<JsonNode>> getState(@Path("subKey") String subKey,
4041
@Path("channel") String channel,
4142
@Path("uuid") String uuid,
4243
@QueryMap Map<String, String> options);
4344

4445
@GET("v2/presence/sub-key/{subKey}/channel/{channel}/uuid/{uuid}/data")
45-
Call<Envelope<Map<String, Object>>> setState(@Path("subKey") String subKey,
46+
Call<Envelope<JsonNode>> setState(@Path("subKey") String subKey,
4647
@Path("channel") String channel,
4748
@Path("uuid") String uuid,
4849
@QueryMap(encoded = true) Map<String, String> options);

‎src/main/java/com/pubnub/api/endpoints/presence/SetState.java‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pubnub.api.endpoints.presence;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import com.pubnub.api.PubNub;
45
import com.pubnub.api.PubNubException;
56
import com.pubnub.api.PubNubUtil;
@@ -24,7 +25,7 @@
2425

2526

2627
@Accessors(chain = true, fluent = true)
27-
public class SetState extends Endpoint<Envelope<Map<String, Object>>, PNSetStateResult> {
28+
public class SetState extends Endpoint<Envelope<JsonNode>, PNSetStateResult> {
2829

2930
@Getter(AccessLevel.NONE)
3031
private SubscriptionManager subscriptionManager;
@@ -59,7 +60,7 @@ protected void validateParams() throws PubNubException {
5960
}
6061

6162
@Override
62-
protected Call<Envelope<Map<String, Object>>> doWork(Map<String, String> params) throws PubNubException {
63+
protected Call<Envelope<JsonNode>> doWork(Map<String, String> params) throws PubNubException {
6364
String selectedUUID = uuid != null ? uuid : this.getPubnub().getConfiguration().getUuid();
6465
String stringifiedState;
6566

@@ -90,7 +91,7 @@ protected Call<Envelope<Map<String, Object>>> doWork(Map<String, String> params)
9091
}
9192

9293
@Override
93-
protected PNSetStateResult createResponse(Response<Envelope<Map<String, Object>>> input) throws PubNubException {
94+
protected PNSetStateResult createResponse(Response<Envelope<JsonNode>> input) throws PubNubException {
9495

9596
if (input.body() == null || input.body().getPayload() == null) {
9697
throw PubNubException.builder().pubnubError(PubNubErrorBuilder.PNERROBJ_PARSING_ERROR).build();

‎src/main/java/com/pubnub/api/models/consumer/presence/PNGetStateResult.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pubnub.api.models.consumer.presence;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import lombok.Builder;
45
import lombok.Getter;
56

@@ -9,6 +10,6 @@
910
@Getter
1011
public class PNGetStateResult {
1112

12-
private Map<String, Object> stateByUUID;
13+
private Map<String, JsonNode> stateByUUID;
1314

1415
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.pubnub.api.models.consumer.presence;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import lombok.Builder;
45
import lombok.Getter;
56

67
@Getter
78
@Builder
89
public class PNHereNowOccupantData {
910
private String uuid;
10-
private Object state;
11+
private JsonNode state;
1112
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
package com.pubnub.api.models.consumer.presence;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import lombok.Builder;
45
import lombok.Getter;
56

6-
import java.util.Map;
7-
87
@Builder
98
@Getter
109
public class PNSetStateResult {
1110

12-
private Map<String, Object> state;
11+
private JsonNode state;
1312

1413
}

‎src/main/java/com/pubnub/api/models/server/Envelope.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.pubnub.api.models.server;
22

3+
import com.fasterxml.jackson.databind.JsonNode;
34
import lombok.Getter;
45
import lombok.Setter;
56

@@ -11,7 +12,7 @@ public class Envelope<T> {
1112
private String service;
1213
private T payload;
1314
private int occupancy;
14-
private Object uuids;
15+
private JsonNode uuids;
1516
private String action;
1617
private boolean error;
1718
}

0 commit comments

Comments
 (0)