Skip to content

Commit 7c62250

Browse files
committed
Copy latest ServiceClient into android project
1 parent 016fd35 commit 7c62250

File tree

4 files changed

+224
-96
lines changed

4 files changed

+224
-96
lines changed

src/AndroidClient/android/src/main/java/net/servicestack/client/HttpHeaders.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
public class HttpHeaders {
77
public static final String Accept = "Accept";
8-
public static final String ContentType = "Content-Type";
8+
public static final String Authorization = "Authorization";
99
public static final String ContentLength = "Content-Length";
10+
public static final String ContentType = "Content-Type";
1011
}

src/AndroidClient/android/src/main/java/net/servicestack/client/JsonServiceClient.java

Lines changed: 107 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public class JsonServiceClient implements ServiceClient {
3333
String baseUrl;
3434
String replyUrl;
3535

36+
boolean alwaysSendBasicAuthHeaders;
37+
String userName;
38+
String password;
39+
3640
Integer timeoutMs;
3741
public ConnectionFilter RequestFilter;
3842
public ConnectionFilter ResponseFilter;
@@ -123,26 +127,6 @@ public String createUrl(Object requestDto, Map<String,String> query){
123127
return requestUrl + sb.toString();
124128
}
125129

126-
public HttpURLConnection createRequest(String url, String httpMethod) {
127-
return createRequest(url, httpMethod, null, null);
128-
}
129-
130-
public HttpURLConnection createRequest(String url, String httpMethod, Object request) {
131-
String contentType = null;
132-
byte[] requestBody = null;
133-
134-
if (request != null) {
135-
contentType = MimeTypes.Json;
136-
String json = getGson().toJson(request);
137-
if (Log.isDebugEnabled()){
138-
Log.d(json);
139-
}
140-
requestBody = json.getBytes(UTF8);
141-
}
142-
143-
return createRequest(url, httpMethod, requestBody, contentType);
144-
}
145-
146130
public HttpURLConnection createRequest(String requestUrl, String httpMethod, byte[] requestBody, String requestType) {
147131
try {
148132
URL url = new URL(requestUrl);
@@ -161,6 +145,10 @@ public HttpURLConnection createRequest(String requestUrl, String httpMethod, byt
161145
req.setRequestProperty(HttpHeaders.ContentType, requestType);
162146
}
163147

148+
if (alwaysSendBasicAuthHeaders) {
149+
addBasicAuth(req, userName, password);
150+
}
151+
164152
if (RequestFilter != null) {
165153
RequestFilter.exec(req);
166154
}
@@ -185,6 +173,21 @@ public HttpURLConnection createRequest(String requestUrl, String httpMethod, byt
185173
}
186174
}
187175

176+
private static void addBasicAuth(HttpURLConnection req, String userName, String password) {
177+
req.setRequestProperty(HttpHeaders.Authorization,
178+
"Basic " + Utils.toBase64String(userName + ":" + password));
179+
}
180+
181+
private static boolean shouldAuthenticate(HttpURLConnection req, String userName, String password){
182+
try {
183+
return req.getResponseCode() == 401
184+
&& userName != null
185+
&& password != null;
186+
} catch (IOException e) {
187+
return false;
188+
}
189+
}
190+
188191
public static RuntimeException createException(HttpURLConnection res, int responseCode){
189192

190193
WebServiceException webEx = null;
@@ -249,48 +252,90 @@ public static boolean hasRequestBody(String httpMethod)
249252
return true;
250253
}
251254

252-
public HttpURLConnection createSendRequest(Object request) {
255+
@Override
256+
public void setAlwaysSendBasicAuthHeaders(boolean value) {
257+
this.alwaysSendBasicAuthHeaders = value;
258+
}
259+
260+
@Override
261+
public void setCredentials(String userName, String password) {
262+
this.userName = userName;
263+
this.password = password;
264+
}
265+
266+
public <TResponse> TResponse sendRequest(Object request, Object responseClass) {
253267
String httpMethod = GetSendMethod(request);
254268
if (hasRequestBody(httpMethod)){
255-
return createRequest(Utils.combinePath(replyUrl, typeName(request)), httpMethod, request);
269+
return send(Utils.combinePath(replyUrl, typeName(request)), httpMethod, request, responseClass);
256270
} else {
257271
String url = createUrl(request);
258-
return createRequest(url, httpMethod, null, null);
272+
return send(url, httpMethod, null, null, responseClass);
259273
}
260274
}
261275

262276
@Override
263277
public <TResponse> TResponse send(IReturn<TResponse> request) {
264-
return send(
265-
createSendRequest(request),
266-
request.getResponseType());
278+
return sendRequest(request, request.getResponseType());
267279
}
268280

269281
@Override
270282
public void send(IReturnVoid request) {
271283
String httpMethod = GetSendMethod(request);
272-
send(createRequest(Utils.combinePath(replyUrl, typeName(request)), httpMethod, request),
284+
send(Utils.combinePath(replyUrl, typeName(request)), httpMethod, request,
273285
IReturnVoid.class);
274286
}
275287

276-
public <TResponse> TResponse send(HttpURLConnection req, Object responseClass) {
288+
public <TResponse> TResponse send(String url, String httpMethod, Object responseClass) {
289+
return send(url, httpMethod, null, null, responseClass);
290+
}
291+
292+
public <TResponse> TResponse send(String url, String httpMethod, Object request, Object responseClass) {
293+
String contentType = null;
294+
byte[] requestBody = null;
295+
296+
if (request != null) {
297+
contentType = MimeTypes.Json;
298+
String json = getGson().toJson(request);
299+
if (Log.isDebugEnabled()){
300+
Log.d(json);
301+
}
302+
requestBody = json.getBytes(UTF8);
303+
}
304+
305+
return send(url, httpMethod, requestBody, contentType, responseClass);
306+
}
307+
308+
public <TResponse> TResponse send(String requestUrl, String httpMethod, byte[] requestBody, String requestType, Object responseClass) {
309+
HttpURLConnection req = null;
277310
try {
311+
req = createRequest(requestUrl, httpMethod, requestBody, requestType);
278312
Class resClass = responseClass instanceof Class ? (Class)responseClass : null;
279313
Type resType = responseClass instanceof Type ? (Type)responseClass : null;
280314
if (resClass == null && resType == null)
281315
throw new RuntimeException("responseClass '" + responseClass.getClass().getSimpleName() + "' must be a Class or Type");
282316

283317
int responseCode = req.getResponseCode();
284318
if (responseCode >= 400){
285-
RuntimeException ex = createException(req, responseCode);
319+
boolean success = false;
286320

287-
if (ExceptionFilter != null)
288-
ExceptionFilter.exec(req, ex);
321+
if (shouldAuthenticate(req, userName, password)){
322+
req.disconnect();
323+
req = createRequest(requestUrl, httpMethod, requestBody, requestType);
324+
addBasicAuth(req, userName, password);
325+
success = req.getResponseCode() < 400;
326+
}
289327

290-
if (GlobalExceptionFilter != null)
291-
GlobalExceptionFilter.exec(req, ex);
328+
if (!success){
329+
RuntimeException ex = createException(req, responseCode);
292330

293-
throw ex;
331+
if (ExceptionFilter != null)
332+
ExceptionFilter.exec(req, ex);
333+
334+
if (GlobalExceptionFilter != null)
335+
GlobalExceptionFilter.exec(req, ex);
336+
337+
throw ex;
338+
}
294339
}
295340

296341
InputStream is = req.getInputStream();
@@ -323,8 +368,8 @@ public <TResponse> TResponse send(HttpURLConnection req, Object responseClass) {
323368
else {
324369
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
325370
TResponse response = resClass != null
326-
? (TResponse) getGson().fromJson(reader, resClass)
327-
: (TResponse) getGson().fromJson(reader, resType);
371+
? (TResponse) getGson().fromJson(reader, resClass)
372+
: (TResponse) getGson().fromJson(reader, resType);
328373

329374
reader.close();
330375
return response;
@@ -333,7 +378,8 @@ public <TResponse> TResponse send(HttpURLConnection req, Object responseClass) {
333378
throw new RuntimeException(e);
334379
}
335380
finally {
336-
req.disconnect();
381+
if (req != null)
382+
req.disconnect();
337383
}
338384
}
339385

@@ -350,81 +396,64 @@ private String resolveUrl(String relativeOrAbsoluteUrl) {
350396

351397
@Override
352398
public <TResponse> TResponse get(IReturn<TResponse> request) {
353-
return send(
354-
createRequest(createUrl(request), HttpMethods.Get),
355-
request.getResponseType());
399+
return send(createUrl(request), HttpMethods.Get, request.getResponseType());
356400
}
357401

358402
@Override
359403
public void get(IReturnVoid request) {
360-
send(createRequest(createUrl(request), HttpMethods.Get), IReturnVoid.class);
404+
send(createUrl(request), HttpMethods.Get, IReturnVoid.class);
361405
}
362406

363407
@Override
364408
public <TResponse> TResponse get(IReturn<TResponse> request, Map<String, String> queryParams) {
365-
return send(
366-
createRequest(createUrl(request, queryParams), HttpMethods.Get),
367-
request.getResponseType());
409+
return send(createUrl(request, queryParams), HttpMethods.Get, request.getResponseType());
368410
}
369411

370412
@Override
371413
public <TResponse> TResponse get(String path, Class responseType) {
372-
return send(
373-
createRequest(resolveUrl(path), HttpMethods.Get),
374-
responseType);
414+
return send(resolveUrl(path), HttpMethods.Get, responseType);
375415
}
376416

377417
@Override
378418
public <TResponse> TResponse get(String path, Type responseType) {
379-
return send(
380-
createRequest(resolveUrl(path), HttpMethods.Get),
381-
responseType);
419+
return send(resolveUrl(path), HttpMethods.Get, responseType);
382420
}
383421

384422
@Override
385423
public HttpURLConnection get(String path) {
386-
return createRequest(resolveUrl(path), HttpMethods.Get);
424+
return createRequest(resolveUrl(path), HttpMethods.Get, null, null);
387425
}
388426

389427
@Override
390428
public <TResponse> TResponse post(IReturn<TResponse> request) {
391429
return send(
392-
createRequest(Utils.combinePath(replyUrl, typeName(request)), HttpMethods.Post, request),
430+
Utils.combinePath(replyUrl, typeName(request)), HttpMethods.Post, request,
393431
request.getResponseType());
394432
}
395433

396434
@Override
397435
public void post(IReturnVoid request) {
398-
send(createRequest(Utils.combinePath(replyUrl, typeName(request)), HttpMethods.Post, request),
399-
IReturnVoid.class);
436+
send(Utils.combinePath(replyUrl, typeName(request)), HttpMethods.Post, request, IReturnVoid.class);
400437
}
401438

402439
@Override
403440
public <TResponse> TResponse post(String path, Object request, Class responseType) {
404-
return send(
405-
createRequest(resolveUrl(path), HttpMethods.Post, request),
406-
responseType);
441+
return send(resolveUrl(path), HttpMethods.Post, request, responseType);
407442
}
408443

409444
@Override
410445
public <TResponse> TResponse post(String path, Object request, Type responseType) {
411-
return send(
412-
createRequest(resolveUrl(path), HttpMethods.Post, request),
413-
responseType);
446+
return send(resolveUrl(path), HttpMethods.Post, request, responseType);
414447
}
415448

416449
@Override
417450
public <TResponse> TResponse post(String path, byte[] requestBody, String contentType, Class responseType) {
418-
return send(
419-
createRequest(resolveUrl(path), HttpMethods.Post, requestBody, contentType),
420-
responseType);
451+
return send(resolveUrl(path), HttpMethods.Post, requestBody, contentType, responseType);
421452
}
422453

423454
@Override
424455
public <TResponse> TResponse post(String path, byte[] requestBody, String contentType, Type responseType) {
425-
return send(
426-
createRequest(resolveUrl(path), HttpMethods.Post, requestBody, contentType),
427-
responseType);
456+
return send(resolveUrl(path), HttpMethods.Post, requestBody, contentType, responseType);
428457
}
429458

430459
@Override
@@ -435,42 +464,34 @@ public HttpURLConnection post(String path, byte[] requestBody, String contentTyp
435464
@Override
436465
public <TResponse> TResponse put(IReturn<TResponse> request) {
437466
return send(
438-
createRequest(Utils.combinePath(replyUrl, typeName(request)), HttpMethods.Put, request),
467+
Utils.combinePath(replyUrl, typeName(request)), HttpMethods.Put, request,
439468
request.getResponseType());
440469
}
441470

442471
@Override
443472
public void put(IReturnVoid request) {
444-
send(createRequest(Utils.combinePath(replyUrl, typeName(request)), HttpMethods.Put, request),
473+
send(Utils.combinePath(replyUrl, typeName(request)), HttpMethods.Put, request,
445474
IReturnVoid.class);
446475
}
447476

448477
@Override
449478
public <TResponse> TResponse put(String path, Object request, Class responseType) {
450-
return send(
451-
createRequest(resolveUrl(path), HttpMethods.Put, request),
452-
responseType);
479+
return send(resolveUrl(path), HttpMethods.Put, request, responseType);
453480
}
454481

455482
@Override
456483
public <TResponse> TResponse put(String path, Object request, Type responseType) {
457-
return send(
458-
createRequest(resolveUrl(path), HttpMethods.Put, request),
459-
responseType);
484+
return send(resolveUrl(path), HttpMethods.Put, request, responseType);
460485
}
461486

462487
@Override
463488
public <TResponse> TResponse put(String path, byte[] requestBody, String contentType, Class responseType) {
464-
return send(
465-
createRequest(resolveUrl(path), HttpMethods.Put, requestBody, contentType),
466-
responseType);
489+
return send(resolveUrl(path), HttpMethods.Put, requestBody, contentType, responseType);
467490
}
468491

469492
@Override
470493
public <TResponse> TResponse put(String path, byte[] requestBody, String contentType, Type responseType) {
471-
return send(
472-
createRequest(resolveUrl(path), HttpMethods.Put, requestBody, contentType),
473-
responseType);
494+
return send(resolveUrl(path), HttpMethods.Put, requestBody, contentType, responseType);
474495
}
475496

476497
@Override
@@ -480,40 +501,32 @@ public HttpURLConnection put(String path, byte[] requestBody, String contentType
480501

481502
@Override
482503
public <TResponse> TResponse delete(IReturn<TResponse> request) {
483-
return send(
484-
createRequest(createUrl(request), HttpMethods.Delete),
485-
request.getResponseType());
504+
return send(createUrl(request), HttpMethods.Delete, request.getResponseType());
486505
}
487506

488507
@Override
489508
public void delete(IReturnVoid request) {
490-
send(createRequest(createUrl(request), HttpMethods.Delete), IReturnVoid.class);
509+
send(createUrl(request), HttpMethods.Delete, IReturnVoid.class);
491510
}
492511

493512
@Override
494513
public <TResponse> TResponse delete(IReturn<TResponse> request, Map<String, String> queryParams) {
495-
return send(
496-
createRequest(createUrl(request, queryParams), HttpMethods.Delete),
497-
request.getResponseType());
514+
return send(createUrl(request, queryParams), HttpMethods.Delete, request.getResponseType());
498515
}
499516

500517
@Override
501518
public <TResponse> TResponse delete(String path, Class responseType) {
502-
return send(
503-
createRequest(resolveUrl(path), HttpMethods.Delete),
504-
responseType);
519+
return send(resolveUrl(path), HttpMethods.Delete, responseType);
505520
}
506521

507522
@Override
508523
public <TResponse> TResponse delete(String path, Type responseType) {
509-
return send(
510-
createRequest(resolveUrl(path), HttpMethods.Delete),
511-
responseType);
524+
return send(resolveUrl(path), HttpMethods.Delete, responseType);
512525
}
513526

514527
@Override
515528
public HttpURLConnection delete(String path) {
516-
return createRequest(resolveUrl(path), HttpMethods.Delete);
529+
return createRequest(resolveUrl(path), HttpMethods.Delete, null, null);
517530
}
518531

519532
}

0 commit comments

Comments
 (0)