From 53eeb0c04914c04046760aa1d41293adaafed740 Mon Sep 17 00:00:00 2001 From: Dennis Frommknecht Date: Thu, 8 Mar 2018 15:00:22 +0100 Subject: [PATCH] fix connection leak on retrofit OAuth token renewal (#7750) --- .../Java/libraries/retrofit/auth/OAuth.mustache | 10 ++++++++-- .../Java/libraries/retrofit2/auth/OAuth.mustache | 10 ++++++++-- .../src/main/java/io/swagger/client/auth/OAuth.java | 10 ++++++++-- .../src/main/java/io/swagger/client/auth/OAuth.java | 10 ++++++++-- .../src/main/java/io/swagger/client/auth/OAuth.java | 10 ++++++++-- .../src/main/java/io/swagger/client/auth/OAuth.java | 10 ++++++++-- 6 files changed, 48 insertions(+), 12 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/auth/OAuth.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/auth/OAuth.mustache index a88378f3e2e6..4c39f4e629e5 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/auth/OAuth.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit/auth/OAuth.mustache @@ -113,8 +113,14 @@ public class OAuth implements Interceptor { // 401/403 most likely indicates that access token has expired. Unless it happens two times in a row. if ( response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure ) { - if (updateAccessToken(requestAccessToken)) { - return retryingIntercept( chain, false ); + try { + if (updateAccessToken(requestAccessToken)) { + response.body().close(); + return retryingIntercept( chain, false ); + } + } catch (Exception e) { + response.body().close(); + throw e; } } return response; diff --git a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/auth/OAuth.mustache b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/auth/OAuth.mustache index e3b3727cd831..80ec7b3946cb 100644 --- a/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/auth/OAuth.mustache +++ b/modules/swagger-codegen/src/main/resources/Java/libraries/retrofit2/auth/OAuth.mustache @@ -113,8 +113,14 @@ public class OAuth implements Interceptor { // 401/403 most likely indicates that access token has expired. Unless it happens two times in a row. if ( response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure ) { - if (updateAccessToken(requestAccessToken)) { - return retryingIntercept( chain, false ); + try { + if (updateAccessToken(requestAccessToken)) { + response.body().close(); + return retryingIntercept( chain, false ); + } + } catch (Exception e) { + response.body().close(); + throw e; } } return response; diff --git a/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/auth/OAuth.java b/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/auth/OAuth.java index 9d587d43d60a..7d53df918800 100644 --- a/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/auth/OAuth.java +++ b/samples/client/petstore/java/retrofit/src/main/java/io/swagger/client/auth/OAuth.java @@ -113,8 +113,14 @@ private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAut // 401/403 most likely indicates that access token has expired. Unless it happens two times in a row. if ( response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure ) { - if (updateAccessToken(requestAccessToken)) { - return retryingIntercept( chain, false ); + try { + if (updateAccessToken(requestAccessToken)) { + response.body().close(); + return retryingIntercept( chain, false ); + } + } catch (Exception e) { + response.body().close(); + throw e; } } return response; diff --git a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/auth/OAuth.java b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/auth/OAuth.java index 7d1634db5104..7141ceb8710a 100644 --- a/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/auth/OAuth.java +++ b/samples/client/petstore/java/retrofit2/src/main/java/io/swagger/client/auth/OAuth.java @@ -113,8 +113,14 @@ private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAut // 401/403 most likely indicates that access token has expired. Unless it happens two times in a row. if ( response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure ) { - if (updateAccessToken(requestAccessToken)) { - return retryingIntercept( chain, false ); + try { + if (updateAccessToken(requestAccessToken)) { + response.body().close(); + return retryingIntercept( chain, false ); + } + } catch (Exception e) { + response.body().close(); + throw e; } } return response; diff --git a/samples/client/petstore/java/retrofit2rx/src/main/java/io/swagger/client/auth/OAuth.java b/samples/client/petstore/java/retrofit2rx/src/main/java/io/swagger/client/auth/OAuth.java index 7d1634db5104..7141ceb8710a 100644 --- a/samples/client/petstore/java/retrofit2rx/src/main/java/io/swagger/client/auth/OAuth.java +++ b/samples/client/petstore/java/retrofit2rx/src/main/java/io/swagger/client/auth/OAuth.java @@ -113,8 +113,14 @@ private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAut // 401/403 most likely indicates that access token has expired. Unless it happens two times in a row. if ( response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure ) { - if (updateAccessToken(requestAccessToken)) { - return retryingIntercept( chain, false ); + try { + if (updateAccessToken(requestAccessToken)) { + response.body().close(); + return retryingIntercept( chain, false ); + } + } catch (Exception e) { + response.body().close(); + throw e; } } return response; diff --git a/samples/client/petstore/java/retrofit2rx2/src/main/java/io/swagger/client/auth/OAuth.java b/samples/client/petstore/java/retrofit2rx2/src/main/java/io/swagger/client/auth/OAuth.java index 7d1634db5104..7141ceb8710a 100644 --- a/samples/client/petstore/java/retrofit2rx2/src/main/java/io/swagger/client/auth/OAuth.java +++ b/samples/client/petstore/java/retrofit2rx2/src/main/java/io/swagger/client/auth/OAuth.java @@ -113,8 +113,14 @@ private Response retryingIntercept(Chain chain, boolean updateTokenAndRetryOnAut // 401/403 most likely indicates that access token has expired. Unless it happens two times in a row. if ( response != null && (response.code() == HTTP_UNAUTHORIZED || response.code() == HTTP_FORBIDDEN) && updateTokenAndRetryOnAuthorizationFailure ) { - if (updateAccessToken(requestAccessToken)) { - return retryingIntercept( chain, false ); + try { + if (updateAccessToken(requestAccessToken)) { + response.body().close(); + return retryingIntercept( chain, false ); + } + } catch (Exception e) { + response.body().close(); + throw e; } } return response;