Skip to content

Commit e4628ca

Browse files
committed
instanceId and requestId
1 parent 4923b78 commit e4628ca

File tree

6 files changed

+124
-25
lines changed

6 files changed

+124
-25
lines changed

src/main/java/com/pubnub/api/PNConfiguration.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,27 @@
1515
@Getter
1616
@Setter
1717
@Accessors(chain = true)
18+
1819
public class PNConfiguration {
1920
private static final int PRESENCE_TIMEOUT = 300;
2021
private static final int NON_SUBSCRIBE_REQUEST_TIMEOUT = 10;
2122
private static final int SUBSCRIBE_TIMEOUT = 310;
2223
private static final int CONNECT_TIMEOUT = 5;
2324

25+
/**
26+
* Set to true to send a UUID for PubNub instance
27+
*/
28+
@Getter
29+
@Setter(AccessLevel.NONE)
30+
private boolean includeInstanceIdentifier;
31+
32+
/**
33+
* Set to true to send a UUID on each request
34+
*/
35+
@Getter
36+
@Setter(AccessLevel.NONE)
37+
private boolean includeRequestIdentifier;
38+
2439
/**
2540
* By default, the origin is pointing directly to PubNub servers. If a proxy origin is needed, set a custom
2641
* origin using this parameter.
@@ -122,6 +137,11 @@ public PNConfiguration() {
122137
reconnectionPolicy = PNReconnectionPolicy.NONE;
123138

124139
secure = true;
140+
141+
includeInstanceIdentifier = true;
142+
143+
includeRequestIdentifier = true;
144+
125145
}
126146

127147
/**

src/main/java/com/pubnub/api/PubNub.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,16 @@
3333

3434
import java.util.Date;
3535
import java.util.List;
36+
import java.util.UUID;
3637

3738

3839
@Getter
3940
@Slf4j
4041
public class PubNub {
4142

4243
private PNConfiguration configuration;
43-
44+
@Getter(AccessLevel.NONE)
45+
private String instanceId;
4446
@Getter(AccessLevel.NONE)
4547
private SubscriptionManager subscriptionManager;
4648
@Getter(AccessLevel.NONE)
@@ -62,6 +64,7 @@ public PubNub(final PNConfiguration initialConfig) {
6264
this.retrofitManager = new RetrofitManager(this);
6365
this.subscriptionManager = new SubscriptionManager(this, retrofitManager);
6466
this.publishSequenceManager = new PublishSequenceManager(MAX_SEQUENCE);
67+
instanceId = UUID.randomUUID().toString();
6568
}
6669

6770
public String getBaseUrl() {
@@ -229,6 +232,20 @@ public int getTimestamp() {
229232
return (int) ((new Date().getTime()) / TIMESTAMP_DIVIDER);
230233
}
231234

235+
/**
236+
* @return instance uuid.
237+
*/
238+
public String getInstanceId() {
239+
return instanceId;
240+
}
241+
242+
/**
243+
* @return request uuid.
244+
*/
245+
public String getRequestId() {
246+
return UUID.randomUUID().toString();
247+
}
248+
232249
/**
233250
* @return version of the SDK.
234251
*/

src/main/java/com/pubnub/api/endpoints/Endpoint.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ protected final Map<String, String> createBaseParams() {
283283
params.put("pnsdk", "PubNub-Java-Unified/".concat(this.pubnub.getVersion()));
284284
params.put("uuid", this.pubnub.getConfiguration().getUuid());
285285

286+
if (this.pubnub.getConfiguration().isIncludeInstanceIdentifier()) {
287+
params.put("instanceId", pubnub.getInstanceId());
288+
}
289+
290+
if (this.pubnub.getConfiguration().isIncludeRequestIdentifier()) {
291+
params.put("requestId", pubnub.getRequestId());
292+
}
293+
286294
// add the auth key for publish and subscribe.
287295
if (this.pubnub.getConfiguration().getAuthKey() != null && isAuthRequired()) {
288296
params.put("auth", pubnub.getConfiguration().getAuthKey());

src/test/java/com/pubnub/api/endpoints/TestHarness.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public String getVersion() {
3131
return "suchJava";
3232
}
3333

34+
@Override
35+
public String getInstanceId() { return "PubNubInstanceId"; }
36+
37+
@Override
38+
public String getRequestId() { return "PubNubRequestId"; }
39+
3440
}
3541

3642
return new MockedTimePubNub(pnConfiguration);

src/test/java/com/pubnub/api/endpoints/access/AuditEndpointTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ public void testSuccessChannelGroupSync() throws PubNubException {
4848

4949
stubFor(get(urlPathEqualTo("/v1/auth/audit/sub-key/mySubscribeKey"))
5050
.withQueryParam("pnsdk", matching("PubNub-Java-Unified/suchJava"))
51+
.withQueryParam("instanceId", matching("PubNubInstanceId"))
52+
.withQueryParam("requestId", matching("PubNubRequestId"))
53+
.withQueryParam("signature", matching("dVle3aE_grdzKypoEegdNSZs_CXpJQJEcnqcM0nMH0Q%3D%0A"))
5154
.withQueryParam("channel-group", matching("cg1"))
5255
.withQueryParam("auth", matching("key1"))
53-
.withQueryParam("signature", matching("rXy69MNT1vceNs3Ob6HnjShUAzCV5x4OumSG1lSPL6s%3D%0A"))
5456
.withQueryParam("uuid", matching("myUUID"))
5557
.withQueryParam("timestamp", matching("1337"))
5658
.willReturn(aResponse().withBody("{\"message\":\"Success\",\"payload\":{\"level\":\"channel-group+auth\",\"subscribe_key\":\"sub-c-82ab2196-b64f-11e5-8622-0619f8945a4f\",\"channel-group\":\"cg2\",\"auths\":{\"key1\":{\"r\":1,\"m\":1,\"w\":1}}},\"service\":\"Access Manager\",\"status\":200}")));
@@ -76,7 +78,9 @@ public void testSuccessChannelSync() throws PubNubException {
7678
.withQueryParam("pnsdk", matching("PubNub-Java-Unified/suchJava"))
7779
.withQueryParam("channel", matching("ch1"))
7880
.withQueryParam("auth", matching("key1"))
79-
.withQueryParam("signature", matching("oICb_S5OubabiqrEA9vDqQ-Ri4PdztWLs6__fQQP_Gs%3D%0A"))
81+
.withQueryParam("instanceId", matching("PubNubInstanceId"))
82+
.withQueryParam("requestId", matching("PubNubRequestId"))
83+
.withQueryParam("signature", matching("4WIVMIc007EqwYGrqyJuMy5qf-gvtdopDjfaXEa1zOs%3D%0A"))
8084
.withQueryParam("uuid", matching("myUUID"))
8185
.withQueryParam("timestamp", matching("1337"))
8286
.willReturn(aResponse().withBody("{\"message\":\"Success\",\"payload\":{\"level\":\"user\",\"subscribe_key\":\"sub-c-82ab2196-b64f-11e5-8622-0619f8945a4f\",\"channel\":\"ch1\",\"auths\":{\"key1\":{\"r\":1,\"m\":1,\"w\":1}}},\"service\":\"Access Manager\",\"status\":200}")));
@@ -143,7 +147,9 @@ public void testIsAuthRequiredSuccessSync() throws IOException, PubNubException,
143147
.withQueryParam("pnsdk", matching("PubNub-Java-Unified/suchJava"))
144148
.withQueryParam("channel-group", matching("cg1"))
145149
.withQueryParam("auth", matching("key1"))
146-
.withQueryParam("signature", matching("rXy69MNT1vceNs3Ob6HnjShUAzCV5x4OumSG1lSPL6s%3D%0A"))
150+
.withQueryParam("instanceId", matching("PubNubInstanceId"))
151+
.withQueryParam("requestId", matching("PubNubRequestId"))
152+
.withQueryParam("signature", matching("dVle3aE_grdzKypoEegdNSZs_CXpJQJEcnqcM0nMH0Q%3D%0A"))
147153
.withQueryParam("uuid", matching("myUUID"))
148154
.withQueryParam("timestamp", matching("1337"))
149155
.willReturn(aResponse().withBody("{\"message\":\"Success\",\"payload\":{\"level\":\"channel-group+auth\",\"subscribe_key\":\"sub-c-82ab2196-b64f-11e5-8622-0619f8945a4f\",\"channel-group\":\"cg2\",\"auths\":{\"key1\":{\"r\":1,\"m\":1,\"w\":1}}},\"service\":\"Access Manager\",\"status\":200}")));

0 commit comments

Comments
 (0)