Skip to content

Commit 7284951

Browse files
author
Roman Sutormin
authored
Merge pull request #14 from kbase/develop
develop -> master sync for version 0.0.17
2 parents 7cfad4c + 0111c73 commit 7284951

File tree

2 files changed

+97
-15
lines changed

2 files changed

+97
-15
lines changed

RELEASE_NOTES.txt

+16
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ OVERVIEW
44
-----------------------------------------
55
Repo for code shared between Java services.
66

7+
8+
VERSION: 0.0.17 (Released 6/14/2016)
9+
--------------------------------------
10+
11+
NEW FEATURES:
12+
- None
13+
14+
UPDATED FEATURES / MAJOR BUG FIXES:
15+
- Support for dynamic Java client URL lookup is added to JsonClientCaller
16+
17+
ANTICIPATED FUTURE DEVELOPMENTS:
18+
- None
19+
20+
721
VERSION: 0.0.16 (Released 6/13/2016)
822
--------------------------------------
923

@@ -19,6 +33,7 @@ UPDATED FEATURES / MAJOR BUG FIXES:
1933
ANTICIPATED FUTURE DEVELOPMENTS:
2034
- None
2135

36+
2237
VERSION: 0.0.15 (Released 1/24/2015)
2338
--------------------------------------
2439

@@ -32,6 +47,7 @@ UPDATED FEATURES / MAJOR BUG FIXES:
3247
ANTICIPATED FUTURE DEVELOPMENTS:
3348
- None
3449

50+
3551
VERSION: 0.0.14 (Released 1/4/2015)
3652
--------------------------------------
3753

src/us/kbase/common/service/JsonClientCaller.java

+81-15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.security.SecureRandom;
1616
import java.security.cert.X509Certificate;
1717
import java.util.*;
18+
import java.util.regex.Pattern;
1819

1920
import javax.net.ssl.HostnameVerifier;
2021
import javax.net.ssl.HttpsURLConnection;
@@ -38,7 +39,8 @@
3839
public class JsonClientCaller {
3940

4041
public final URL serviceUrl;
41-
private ObjectMapper mapper;
42+
private static final ObjectMapper mapper = new ObjectMapper().registerModule(
43+
new JacksonTupleModule());;
4244
private String user = null;
4345
private char[] password = null;
4446
private AuthToken accessToken = null;
@@ -47,6 +49,8 @@ public class JsonClientCaller {
4749
private boolean streamRequest = false;
4850
private Integer connectionReadTimeOut = 30 * 60 * 1000;
4951
private File fileForNextRpcResponse = null;
52+
private boolean isDynamic = false;
53+
5054
private final URL authServiceUrl;
5155

5256
private static TrustManager[] GULLIBLE_TRUST_MGR = new TrustManager[] {
@@ -74,7 +78,6 @@ public JsonClientCaller(URL url) {
7478

7579
public JsonClientCaller(URL url, URL authServiceUrl) {
7680
serviceUrl = url;
77-
mapper = new ObjectMapper().registerModule(new JacksonTupleModule());
7881
this.authServiceUrl = authServiceUrl;
7982
}
8083

@@ -187,33 +190,44 @@ public void setConnectionReadTimeOut(Integer connectionReadTimeOut) {
187190
this.connectionReadTimeOut = connectionReadTimeOut;
188191
}
189192

190-
private HttpURLConnection setupCall(boolean authRequired) throws IOException, JsonClientException {
193+
public boolean isDynamic() {
194+
return isDynamic;
195+
}
196+
197+
public void setDynamic(boolean isDynamic) {
198+
this.isDynamic = isDynamic;
199+
}
200+
201+
private static HttpURLConnection setupCall(URL serviceUrl, boolean authRequired,
202+
Integer connectionReadTimeOut, JsonClientCaller accessTokenHolder,
203+
boolean allowInsecureHttp, String user, char[] password, URL authServiceUrl,
204+
boolean trustAllCerts) throws IOException, JsonClientException {
191205
HttpURLConnection conn = (HttpURLConnection) serviceUrl.openConnection();
192206
conn.setConnectTimeout(10000);
193207
if (connectionReadTimeOut != null) {
194208
conn.setReadTimeout(connectionReadTimeOut);
195209
}
196210
conn.setDoOutput(true);
197211
conn.setRequestMethod("POST");
198-
if (authRequired || accessToken != null) {
212+
if (authRequired || accessTokenHolder.accessToken != null) {
199213
if (!(conn instanceof HttpsURLConnection || allowInsecureHttp)) {
200214
throw new UnauthorizedException("RPC method required authentication shouldn't " +
201215
"be called through unsecured http, use https instead or call " +
202216
"setAuthAllowedForHttp(true) for your client");
203217
}
204-
if (accessToken == null || accessToken.isExpired()) {
218+
if (accessTokenHolder.accessToken == null || accessTokenHolder.accessToken.isExpired()) {
205219
if (user == null) {
206-
if (accessToken == null) {
220+
if (accessTokenHolder.accessToken == null) {
207221
throw new UnauthorizedException("RPC method requires authentication but neither " +
208222
"user nor token was set");
209223
} else {
210224
throw new UnauthorizedException("Token is expired and can not be reloaded " +
211225
"because user wasn't set");
212226
}
213227
}
214-
accessToken = requestTokenFromKBase(user, password, authServiceUrl);
228+
accessTokenHolder.accessToken = requestTokenFromKBase(user, password, authServiceUrl);
215229
}
216-
conn.setRequestProperty("Authorization", accessToken.toString());
230+
conn.setRequestProperty("Authorization", accessTokenHolder.accessToken.toString());
217231
}
218232
if (conn instanceof HttpsURLConnection && trustAllCerts) {
219233
final HttpsURLConnection hc = (HttpsURLConnection) conn;
@@ -270,14 +284,62 @@ public <ARG, RET> RET jsonrpcCall(String method, ARG arg, TypeReference<RET> cls
270284
public <ARG, RET> RET jsonrpcCall(String method, ARG arg, TypeReference<RET> cls,
271285
boolean ret, boolean authRequired, RpcContext[] context)
272286
throws IOException, JsonClientException {
287+
return jsonrpcCall(method, arg, cls, ret, authRequired, context, null);
288+
}
289+
290+
public <ARG, RET> RET jsonrpcCall(String method, ARG arg, TypeReference<RET> cls,
291+
boolean ret, boolean authRequired, RpcContext[] context, String serviceVersion)
292+
throws IOException, JsonClientException {
273293
return jsonrpcCall(method, arg, cls, ret, authRequired,
274-
context != null && context.length == 1 ? context[0] : null);
294+
context != null && context.length == 1 ? context[0] : null, serviceVersion);
295+
}
296+
297+
public <ARG, RET> RET jsonrpcCall(String method, ARG arg, TypeReference<RET> cls,
298+
boolean ret, boolean authRequired, RpcContext context)
299+
throws IOException, JsonClientException {
300+
return jsonrpcCall(method, arg, cls, ret, authRequired, context, null);
275301
}
276302

277303
public <ARG, RET> RET jsonrpcCall(String method, ARG arg, TypeReference<RET> cls,
278-
boolean ret, boolean authRequired, RpcContext context)
304+
boolean ret, boolean authRequired, RpcContext context, String serviceVersion)
279305
throws IOException, JsonClientException {
280-
HttpURLConnection conn = setupCall(authRequired);
306+
URL url;
307+
if (isDynamic) {
308+
String serviceModuleName = method.split(Pattern.quote("."))[0];
309+
List<Object> serviceStatusArgs = new ArrayList<Object>();
310+
Map<String, String> serviceStruct = new LinkedHashMap<String, String>();
311+
serviceStruct.put("module_name", serviceModuleName);
312+
serviceStruct.put("version", serviceVersion);
313+
serviceStatusArgs.add(serviceStruct);
314+
List<Map<String, Object>> serviceState = jsonrpcCallStatic(serviceUrl,
315+
"ServiceWizard.get_service_status", serviceStatusArgs,
316+
new TypeReference<List<Map<String, Object>>>() {}, ret,
317+
false, null, streamRequest, connectionReadTimeOut,
318+
this, allowInsecureHttp, user, password, authServiceUrl,
319+
trustAllCerts, null);
320+
url = new URL((String)serviceState.get(0).get("url"));
321+
} else {
322+
url = serviceUrl;
323+
}
324+
try {
325+
return jsonrpcCallStatic(url, method, arg, cls, ret, authRequired,
326+
context, streamRequest, connectionReadTimeOut,
327+
this, allowInsecureHttp, user, password, authServiceUrl,
328+
trustAllCerts, fileForNextRpcResponse);
329+
} finally {
330+
fileForNextRpcResponse = null;
331+
}
332+
}
333+
334+
private static <ARG, RET> RET jsonrpcCallStatic(URL serviceUrl, String method, ARG arg,
335+
TypeReference<RET> cls, boolean ret, boolean authRequired, RpcContext context,
336+
boolean streamRequest, Integer connectionReadTimeOut,
337+
JsonClientCaller accessTokenHolder, boolean allowInsecureHttp, String user,
338+
char[] password, URL authServiceUrl, boolean trustAllCerts,
339+
File fileForNextRpcResponse) throws IOException, JsonClientException {
340+
HttpURLConnection conn = setupCall(serviceUrl, authRequired, connectionReadTimeOut,
341+
accessTokenHolder, allowInsecureHttp, user, password, authServiceUrl,
342+
trustAllCerts);
281343
String id = ("" + Math.random()).replace(".", "");
282344
if (streamRequest) {
283345
// Calculate content-length before
@@ -286,7 +348,7 @@ public <ARG, RET> RET jsonrpcCall(String method, ARG arg, TypeReference<RET> cls
286348
conn.setFixedLengthStreamingMode(size);
287349
}
288350
// Write real data into http output stream
289-
writeRequestData(method, arg, conn.getOutputStream(), id, context);
351+
writeRequestDataStatic(method, arg, conn.getOutputStream(), id, context);
290352
// Read response
291353
int code = conn.getResponseCode();
292354
conn.getResponseMessage();
@@ -380,7 +442,6 @@ public <ARG, RET> RET jsonrpcCall(String method, ARG arg, TypeReference<RET> cls
380442
throw new ServerException("An unknown server error occured", 0, "Unknown", null);
381443
}
382444
} finally {
383-
fileForNextRpcResponse = null;
384445
if (fos != null)
385446
try {
386447
fos.close();
@@ -389,7 +450,7 @@ public <ARG, RET> RET jsonrpcCall(String method, ARG arg, TypeReference<RET> cls
389450
}
390451
}
391452

392-
private <ARG> long calculateResponseLength(String method, ARG arg,
453+
private static <ARG> long calculateResponseLength(String method, ARG arg,
393454
String id, RpcContext context) throws IOException {
394455
final long[] sizeWrapper = new long[] {0};
395456
OutputStream os = new OutputStream() {
@@ -400,7 +461,7 @@ private <ARG> long calculateResponseLength(String method, ARG arg,
400461
@Override
401462
public void write(byte[] b, int o, int l) {sizeWrapper[0] += l;}
402463
};
403-
writeRequestData(method, arg, os, id, context);
464+
writeRequestDataStatic(method, arg, os, id, context);
404465
return sizeWrapper[0];
405466
}
406467

@@ -422,6 +483,11 @@ private static void checkToken(JsonToken expected, JsonToken actual) throws Json
422483

423484
public void writeRequestData(String method, Object arg, OutputStream os, String id, RpcContext context)
424485
throws IOException {
486+
writeRequestDataStatic(method, arg, os, id, context);
487+
}
488+
489+
private static void writeRequestDataStatic(String method, Object arg, OutputStream os, String id,
490+
RpcContext context) throws IOException {
425491
JsonGenerator g = mapper.getFactory().createGenerator(os, JsonEncoding.UTF8);
426492
g.writeStartObject();
427493
g.writeObjectField("params", arg);

0 commit comments

Comments
 (0)