|
54 | 54 | import okhttp3.Request; |
55 | 55 | import okhttp3.RequestBody; |
56 | 56 | import okhttp3.Response; |
| 57 | +import okhttp3.ResponseBody; |
57 | 58 | import okio.BufferedSink; |
58 | 59 |
|
59 | | -import static com.microsoft.graph.Util.closeQuietly; |
60 | | - |
61 | 60 | /** |
62 | 61 | * HTTP provider based off of OkHttp and msgraph-sdk-java-core library |
63 | 62 | */ |
64 | 63 | public class CoreHttpProvider implements IHttpProvider { |
65 | | - private final static HttpResponseHeadersHelper responseHeadersHelper = new HttpResponseHeadersHelper(); |
66 | 64 | /** |
67 | 65 | * The serializer |
68 | 66 | */ |
@@ -376,86 +374,88 @@ private <Result, Body, DeserializeType> Result processResponse(final Response re |
376 | 374 | final Class<Result> resultClass, |
377 | 375 | final Body serializable, |
378 | 376 | final IStatefulResponseHandler<Result, DeserializeType> handler) { |
379 | | - try { |
380 | | - InputStream in = null; |
381 | | - boolean isBinaryStreamInput = false; |
| 377 | + try(final ResponseBody body = response.body()) { |
382 | 378 | try { |
| 379 | + InputStream in = null; |
| 380 | + boolean isBinaryStreamInput = false; |
| 381 | + try { |
383 | 382 |
|
384 | | - // Call being executed |
| 383 | + // Call being executed |
385 | 384 |
|
386 | 385 |
|
387 | | - if (handler != null) { |
388 | | - handler.configConnection(response); |
389 | | - } |
| 386 | + if (handler != null) { |
| 387 | + handler.configConnection(response); |
| 388 | + } |
390 | 389 |
|
391 | | - logger.logDebug(String.format(Locale.ROOT, "Response code %d, %s", |
392 | | - response.code(), |
393 | | - response.message())); |
| 390 | + logger.logDebug(String.format(Locale.ROOT, "Response code %d, %s", |
| 391 | + response.code(), |
| 392 | + response.message())); |
394 | 393 |
|
395 | | - if (handler != null) { |
396 | | - logger.logDebug("StatefulResponse is handling the HTTP response."); |
397 | | - return handler.generateResult( |
398 | | - request, response, this.serializer, this.logger); |
399 | | - } |
| 394 | + if (handler != null) { |
| 395 | + logger.logDebug("StatefulResponse is handling the HTTP response."); |
| 396 | + return handler.generateResult( |
| 397 | + request, response, this.serializer, this.logger); |
| 398 | + } |
400 | 399 |
|
401 | | - if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) { |
402 | | - logger.logDebug("Handling error response"); |
403 | | - in = response.body().byteStream(); |
404 | | - handleErrorResponse(request, serializable, response); |
405 | | - } |
| 400 | + if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) { |
| 401 | + logger.logDebug("Handling error response"); |
| 402 | + in = body.byteStream(); |
| 403 | + handleErrorResponse(request, serializable, response); |
| 404 | + } |
406 | 405 |
|
407 | | - if (response.code() == HttpResponseCode.HTTP_NOBODY |
408 | | - || response.code() == HttpResponseCode.HTTP_NOT_MODIFIED) { |
409 | | - logger.logDebug("Handling response with no body"); |
410 | | - return handleEmptyResponse(responseHeadersHelper.getResponseHeadersAsMapOfStringList(response), resultClass); |
411 | | - } |
| 406 | + final Map<String, List<String>> responseHeaders = response.headers().toMultimap(); |
412 | 407 |
|
413 | | - if (response.code() == HttpResponseCode.HTTP_ACCEPTED) { |
414 | | - logger.logDebug("Handling accepted response"); |
415 | | - return handleEmptyResponse(responseHeadersHelper.getResponseHeadersAsMapOfStringList(response), resultClass); |
416 | | - } |
| 408 | + if (response.code() == HttpResponseCode.HTTP_NOBODY |
| 409 | + || response.code() == HttpResponseCode.HTTP_NOT_MODIFIED) { |
| 410 | + logger.logDebug("Handling response with no body"); |
| 411 | + return handleEmptyResponse(responseHeaders, resultClass); |
| 412 | + } |
417 | 413 |
|
418 | | - in = new BufferedInputStream(response.body().byteStream()); |
419 | | - |
420 | | - final Map<String, String> headers = responseHeadersHelper.getResponseHeadersAsMapStringString(response); |
421 | | - |
422 | | - if (response.body() == null || response.body().contentLength() == 0) |
423 | | - return null; |
424 | | - |
425 | | - final String contentType = headers.get(Constants.CONTENT_TYPE_HEADER_NAME); |
426 | | - if (contentType != null && resultClass != InputStream.class && |
427 | | - contentType.contains(Constants.JSON_CONTENT_TYPE)) { |
428 | | - logger.logDebug("Response json"); |
429 | | - return handleJsonResponse(in, responseHeadersHelper.getResponseHeadersAsMapOfStringList(response), resultClass); |
430 | | - } else if (resultClass == InputStream.class) { |
431 | | - logger.logDebug("Response binary"); |
432 | | - isBinaryStreamInput = true; |
433 | | - return (Result) handleBinaryStream(in); |
434 | | - } else if (contentType != null && resultClass != InputStream.class && |
435 | | - contentType.contains(Constants.TEXT_CONTENT_TYPE)) { |
436 | | - return handleRawResponse(in, resultClass); |
437 | | - } else { |
438 | | - return null; |
439 | | - } |
440 | | - } finally { |
441 | | - if (!isBinaryStreamInput) { |
442 | | - try{ |
443 | | - if (in != null) in.close(); |
444 | | - }catch(IOException e) { |
445 | | - logger.logError(e.getMessage(), e); |
| 414 | + if (response.code() == HttpResponseCode.HTTP_ACCEPTED) { |
| 415 | + logger.logDebug("Handling accepted response"); |
| 416 | + return handleEmptyResponse(responseHeaders, resultClass); |
| 417 | + } |
| 418 | + |
| 419 | + in = new BufferedInputStream(body.byteStream()); |
| 420 | + |
| 421 | + if (body == null || body.contentLength() == 0) |
| 422 | + return null; |
| 423 | + |
| 424 | + if (body.contentType() != null && body.contentType().subtype().contains("json") |
| 425 | + && resultClass != InputStream.class) { |
| 426 | + logger.logDebug("Response json"); |
| 427 | + return handleJsonResponse(in, responseHeaders, resultClass); |
| 428 | + } else if (resultClass == InputStream.class) { |
| 429 | + logger.logDebug("Response binary"); |
| 430 | + isBinaryStreamInput = true; |
| 431 | + return (Result) handleBinaryStream(in); |
| 432 | + } else if (body.contentType() != null && resultClass != InputStream.class && |
| 433 | + body.contentType().type().contains("text") && |
| 434 | + body.contentType().subtype().contains("plain")) { |
| 435 | + return handleRawResponse(in, resultClass); |
| 436 | + } else { |
| 437 | + return null; |
| 438 | + } |
| 439 | + } finally { |
| 440 | + if (!isBinaryStreamInput) { |
| 441 | + try{ |
| 442 | + if (in != null) in.close(); |
| 443 | + }catch(IOException e) { |
| 444 | + logger.logError(e.getMessage(), e); |
| 445 | + } |
| 446 | + if (response != null) response.close(); |
446 | 447 | } |
447 | | - if (response != null) response.close(); |
448 | 448 | } |
| 449 | + } catch (final GraphServiceException ex) { |
| 450 | + final boolean shouldLogVerbosely = logger.getLoggingLevel() == LoggerLevel.DEBUG; |
| 451 | + logger.logError("Graph service exception " + ex.getMessage(shouldLogVerbosely), ex); |
| 452 | + throw ex; |
| 453 | + } catch (final Exception ex) { |
| 454 | + final ClientException clientException = new ClientException("Error during http request", |
| 455 | + ex); |
| 456 | + logger.logError("Error during http request", clientException); |
| 457 | + throw clientException; |
449 | 458 | } |
450 | | - } catch (final GraphServiceException ex) { |
451 | | - final boolean shouldLogVerbosely = logger.getLoggingLevel() == LoggerLevel.DEBUG; |
452 | | - logger.logError("Graph service exception " + ex.getMessage(shouldLogVerbosely), ex); |
453 | | - throw ex; |
454 | | - } catch (final Exception ex) { |
455 | | - final ClientException clientException = new ClientException("Error during http request", |
456 | | - ex); |
457 | | - logger.logError("Error during http request", clientException); |
458 | | - throw clientException; |
459 | 459 | } |
460 | 460 | } |
461 | 461 | /** |
@@ -496,12 +496,7 @@ private <Result> Result handleJsonResponse(final InputStream in, Map<String, Lis |
496 | 496 | return null; |
497 | 497 | } |
498 | 498 |
|
499 | | - try { |
500 | | - return serializer.deserializeObject(in, clazz, responseHeaders); |
501 | | - } finally { |
502 | | - closeQuietly(in); |
503 | | - } |
504 | | - |
| 499 | + return serializer.deserializeObject(in, clazz, responseHeaders); |
505 | 500 | } |
506 | 501 | /** |
507 | 502 | * Handles the cause where the response is a Text object |
@@ -538,8 +533,13 @@ private <Result> Result handleRawResponse(final InputStream in, final Class<Resu |
538 | 533 | private <Result> Result handleEmptyResponse(Map<String, List<String>> responseHeaders, final Class<Result> clazz) |
539 | 534 | throws UnsupportedEncodingException{ |
540 | 535 | //Create an empty object to attach the response headers to |
541 | | - InputStream in = new ByteArrayInputStream("{}".getBytes(Constants.JSON_ENCODING)); |
542 | | - return handleJsonResponse(in, responseHeaders, clazz); |
| 536 | + Result result = null; |
| 537 | + try(final InputStream in = new ByteArrayInputStream("{}".getBytes(Constants.JSON_ENCODING))) { |
| 538 | + result = handleJsonResponse(in, responseHeaders, clazz); |
| 539 | + } catch (IOException ex) { |
| 540 | + //noop we couldnt close the byte array stream we just opened and its ok |
| 541 | + } |
| 542 | + return result; |
543 | 543 | } |
544 | 544 |
|
545 | 545 | private Request convertIHttpRequestToOkHttpRequest(IHttpRequest request) { |
|
0 commit comments