Skip to content

Commit 66f3553

Browse files
matt rubinMax Presman
authored andcommitted
Fix Google App Engine support
1 parent 86c1e8d commit 66f3553

2 files changed

Lines changed: 103 additions & 26 deletions

File tree

‎src/main/java/com/pubnub/api/endpoints/vendor/AppEngineFactory.java‎

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
11
package com.pubnub.api.endpoints.vendor;
22

3+
import com.pubnub.api.PubNub;
4+
import com.pubnub.api.PubNubException;
5+
import com.pubnub.api.PubNubUtil;
6+
7+
import java.io.IOException;
8+
import java.net.HttpURLConnection;
9+
import java.net.URL;
10+
import java.util.HashMap;
11+
import java.util.Map;
12+
13+
import lombok.extern.java.Log;
314
import okhttp3.Call;
415
import okhttp3.Callback;
516
import okhttp3.Headers;
17+
import okhttp3.HttpUrl;
618
import okhttp3.MediaType;
719
import okhttp3.Protocol;
820
import okhttp3.Request;
@@ -12,16 +24,14 @@
1224
import okio.BufferedSource;
1325
import okio.Okio;
1426

15-
import java.io.IOException;
16-
import java.net.HttpURLConnection;
17-
import java.net.URL;
18-
19-
27+
@Log
2028
public class AppEngineFactory implements Call {
2129
private Request request;
30+
private PubNub pubNub;
2231

23-
AppEngineFactory(Request request) {
32+
AppEngineFactory(Request request, PubNub pubNub) {
2433
this.request = request;
34+
this.pubNub = pubNub;
2535
}
2636

2737
@Override
@@ -31,6 +41,8 @@ public Request request() {
3141

3242
@Override
3343
public Response execute() throws IOException {
44+
request = signIfNecessary(request); // We need this since we bypass the signature interceptor
45+
3446
URL url = request.url().url();
3547
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
3648
connection.setUseCaches(false);
@@ -83,6 +95,53 @@ public BufferedSource source() {
8395
return response;
8496
}
8597

98+
// TODO: This should probably go in a util file so we don't have copied code from SignatureInterceptor
99+
public Request signIfNecessary(Request request) {
100+
// only sign if we have a secret key in place.
101+
if (this.pubNub.getConfiguration().getSecretKey() == null) {
102+
return request;
103+
}
104+
105+
HttpUrl url = request.url();
106+
String requestURL = url.encodedPath();
107+
int timestamp = this.pubNub.getTimestamp();
108+
Map<String, String> queryParams = new HashMap<>();
109+
String signature = "";
110+
111+
for (String queryKey: url.queryParameterNames()) {
112+
queryParams.put(queryKey, url.queryParameter(queryKey));
113+
}
114+
115+
queryParams.put("timestamp", String.valueOf(timestamp));
116+
117+
String signInput = pubNub.getConfiguration().getSubscribeKey() + "\n"
118+
+ pubNub.getConfiguration().getPublishKey() + "\n";
119+
120+
if (requestURL.startsWith("/v1/auth/audit")) {
121+
signInput += "audit" + "\n";
122+
} else if (requestURL.startsWith("/v1/auth/grant")) {
123+
signInput += "grant" + "\n";
124+
} else {
125+
signInput += requestURL + "\n";
126+
}
127+
128+
signInput += PubNubUtil.preparePamArguments(queryParams);
129+
130+
try {
131+
signature = PubNubUtil.signSHA256(pubNub.getConfiguration().getSecretKey(), signInput);
132+
} catch (PubNubException e) {
133+
log.warning("signature failed on SignatureInterceptor: " + e.toString());
134+
}
135+
136+
HttpUrl rebuiltUrl = url.newBuilder()
137+
.addQueryParameter("timestamp", String.valueOf(timestamp))
138+
.addQueryParameter("signature", signature)
139+
.build();
140+
141+
request = request.newBuilder().url(rebuiltUrl).build();
142+
return request;
143+
}
144+
86145
@Override
87146
public void enqueue(Callback responseCallback) {
88147

@@ -113,9 +172,15 @@ public Call clone() {
113172
}
114173

115174
public static class Factory implements Call.Factory {
175+
private PubNub pubNub;
176+
177+
public Factory(PubNub pubNub) {
178+
this.pubNub = pubNub;
179+
}
180+
116181
@Override
117182
public Call newCall(Request request) {
118-
return new AppEngineFactory(request);
183+
return new AppEngineFactory(request, pubNub);
119184
}
120185
}
121186
}

‎src/main/java/com/pubnub/api/managers/RetrofitManager.java‎

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@
66
import com.pubnub.api.endpoints.vendor.AppEngineFactory;
77
import com.pubnub.api.enums.PNLogVerbosity;
88
import com.pubnub.api.interceptors.SignatureInterceptor;
9-
import lombok.Getter;
10-
import okhttp3.OkHttpClient;
11-
import okhttp3.logging.HttpLoggingInterceptor;
12-
import retrofit2.Retrofit;
139

1410
import java.util.Collections;
1511
import java.util.concurrent.ExecutorService;
1612
import java.util.concurrent.TimeUnit;
1713

14+
import lombok.Getter;
15+
import okhttp3.OkHttpClient;
16+
import okhttp3.logging.HttpLoggingInterceptor;
17+
import retrofit2.Retrofit;
18+
1819
public class RetrofitManager {
1920

2021
private PubNub pubnub;
@@ -32,15 +33,17 @@ public RetrofitManager(PubNub pubNubInstance) {
3233

3334
this.signatureInterceptor = new SignatureInterceptor(pubNubInstance);
3435

35-
this.transactionClientInstance = createOkHttpClient(
36-
this.pubnub.getConfiguration().getNonSubscribeRequestTimeout(),
37-
this.pubnub.getConfiguration().getConnectTimeout()
38-
);
36+
if (!pubNubInstance.getConfiguration().isGoogleAppEngineNetworking()) {
37+
this.transactionClientInstance = createOkHttpClient(
38+
this.pubnub.getConfiguration().getNonSubscribeRequestTimeout(),
39+
this.pubnub.getConfiguration().getConnectTimeout()
40+
);
3941

40-
this.subscriptionClientInstance = createOkHttpClient(
41-
this.pubnub.getConfiguration().getSubscribeTimeout(),
42-
this.pubnub.getConfiguration().getConnectTimeout()
43-
);
42+
this.subscriptionClientInstance = createOkHttpClient(
43+
this.pubnub.getConfiguration().getSubscribeTimeout(),
44+
this.pubnub.getConfiguration().getConnectTimeout()
45+
);
46+
}
4447

4548
this.transactionInstance = createRetrofit(this.transactionClientInstance);
4649
this.subscriptionInstance = createRetrofit(this.subscriptionClientInstance);
@@ -99,19 +102,28 @@ private Retrofit createRetrofit(OkHttpClient client) {
99102
Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
100103

101104
if (pubnub.getConfiguration().isGoogleAppEngineNetworking()) {
102-
retrofitBuilder.callFactory(new AppEngineFactory.Factory());
105+
retrofitBuilder.callFactory(new AppEngineFactory.Factory(pubnub));
103106
}
104107

105-
return retrofitBuilder
108+
retrofitBuilder = retrofitBuilder
106109
.baseUrl(pubnub.getBaseUrl())
107-
.addConverterFactory(this.pubnub.getMapper().getConverterFactory())
108-
.client(client)
109-
.build();
110+
.addConverterFactory(this.pubnub.getMapper().getConverterFactory());
111+
112+
if (!pubnub.getConfiguration().isGoogleAppEngineNetworking()) {
113+
retrofitBuilder = retrofitBuilder
114+
.client(client);
115+
}
116+
117+
return retrofitBuilder.build();
110118
}
111119

112120
public void destroy() {
113-
closeExecutor(this.transactionClientInstance);
114-
closeExecutor(this.subscriptionClientInstance);
121+
if (this.transactionClientInstance != null) {
122+
closeExecutor(this.transactionClientInstance);
123+
}
124+
if (this.subscriptionClientInstance != null) {
125+
closeExecutor(this.subscriptionClientInstance);
126+
}
115127
}
116128

117129
private void closeExecutor(OkHttpClient client) {

0 commit comments

Comments
 (0)