1616import okhttp3 .Interceptor ;
1717import okhttp3 .Request ;
1818import okhttp3 .Response ;
19+ import okhttp3 .ResponseBody ;
1920
2021/**
2122 * The middleware responsible for retrying requests when they fail because of transient issues
@@ -32,27 +33,27 @@ public class RetryHandler implements Interceptor{
3233 /**
3334 * Header name to track the retry attempt number
3435 */
35- private final String RETRY_ATTEMPT_HEADER = "Retry-Attempt" ;
36+ private static final String RETRY_ATTEMPT_HEADER = "Retry-Attempt" ;
3637 /**
3738 * Header name for the retry after information
3839 */
39- private final String RETRY_AFTER = "Retry-After" ;
40+ private static final String RETRY_AFTER = "Retry-After" ;
4041 /**
4142 * Header name for the transfer information
4243 */
43- private final String TRANSFER_ENCODING = "Transfer-Encoding" ;
44+ private static final String TRANSFER_ENCODING = "Transfer-Encoding" ;
4445 /**
4546 * Chunked encoding header value
4647 */
47- private final String TRANSFER_ENCODING_CHUNKED = "chunked" ;
48+ private static final String TRANSFER_ENCODING_CHUNKED = "chunked" ;
4849 /**
4950 * Binary content type header value
5051 */
51- private final String APPLICATION_OCTET_STREAM = "application/octet-stream" ;
52+ private static final String APPLICATION_OCTET_STREAM = "application/octet-stream" ;
5253 /**
5354 * Header name for the content type
5455 */
55- private final String CONTENT_TYPE = "Content-Type" ;
56+ private static final String CONTENT_TYPE = "Content-Type" ;
5657
5758 /**
5859 * Too many requests status code
@@ -70,7 +71,7 @@ public class RetryHandler implements Interceptor{
7071 /**
7172 * One second as milliseconds
7273 */
73- private final long DELAY_MILLISECONDS = 1000 ;
74+ private static final long DELAY_MILLISECONDS = 1000 ;
7475
7576 private final ILogger logger ;
7677
@@ -114,7 +115,8 @@ boolean retryRequest(Response response, int executionCount, Request request, Ret
114115 // Payloads with forward only streams will be have the responses returned
115116 // without any retry attempt.
116117 shouldRetry =
117- (executionCount <= retryOptions .maxRetries ())
118+ retryOptions != null
119+ && executionCount <= retryOptions .maxRetries ()
118120 && checkStatus (statusCode ) && isBuffered (response , request )
119121 && shouldRetryCallback != null
120122 && shouldRetryCallback .shouldRetry (retryOptions .delay (), executionCount , request , response );
@@ -155,7 +157,7 @@ boolean checkStatus(int statusCode) {
155157 || statusCode == MSClientErrorCodeGatewayTimeout );
156158 }
157159
158- boolean isBuffered (Response response , Request request ) {
160+ boolean isBuffered (final Response response , final Request request ) {
159161 String methodName = request .method ();
160162 if (methodName .equalsIgnoreCase ("GET" ) || methodName .equalsIgnoreCase ("DELETE" ) || methodName .equalsIgnoreCase ("HEAD" ) || methodName .equalsIgnoreCase ("OPTIONS" ))
161163 return true ;
@@ -164,11 +166,12 @@ boolean isBuffered(Response response, Request request) {
164166 methodName .equalsIgnoreCase ("PUT" ) ||
165167 methodName .equalsIgnoreCase ("PATCH" );
166168
167- if (isHTTPMethodPutPatchOrPost ) {
168- boolean isStream = response .header (CONTENT_TYPE )!=null && response .header (CONTENT_TYPE ).equalsIgnoreCase (APPLICATION_OCTET_STREAM );
169+ if (isHTTPMethodPutPatchOrPost && response != null ) {
170+ final String contentTypeHeaderValue = response .header (CONTENT_TYPE );
171+ final boolean isStream = contentTypeHeaderValue !=null && contentTypeHeaderValue .equalsIgnoreCase (APPLICATION_OCTET_STREAM );
169172 if (!isStream ) {
170- String transferEncoding = response .header (TRANSFER_ENCODING );
171- boolean isTransferEncodingChunked = (transferEncoding != null ) &&
173+ final String transferEncoding = response .header (TRANSFER_ENCODING );
174+ final boolean isTransferEncodingChunked = (transferEncoding != null ) &&
172175 transferEncoding .equalsIgnoreCase (TRANSFER_ENCODING_CHUNKED );
173176
174177 if (request .body () != null && isTransferEncodingChunked )
@@ -179,13 +182,16 @@ boolean isBuffered(Response response, Request request) {
179182 }
180183
181184 @ Override
182- @ Nullable
185+ @ Nonnull
183186 public Response intercept (@ Nonnull final Chain chain ) throws IOException {
184187 Request request = chain .request ();
185188
186- if (request .tag (TelemetryOptions .class ) == null )
187- request = request .newBuilder ().tag (TelemetryOptions .class , new TelemetryOptions ()).build ();
188- request .tag (TelemetryOptions .class ).setFeatureUsage (TelemetryOptions .RETRY_HANDLER_ENABLED_FLAG );
189+ TelemetryOptions telemetryOptions = request .tag (TelemetryOptions .class );
190+ if (telemetryOptions == null ) {
191+ telemetryOptions = new TelemetryOptions ();
192+ request = request .newBuilder ().tag (TelemetryOptions .class , telemetryOptions ).build ();
193+ }
194+ telemetryOptions .setFeatureUsage (TelemetryOptions .RETRY_HANDLER_ENABLED_FLAG );
189195
190196 Response response = chain .proceed (request );
191197
@@ -198,8 +204,9 @@ public Response intercept(@Nonnull final Chain chain) throws IOException {
198204 request = request .newBuilder ().addHeader (RETRY_ATTEMPT_HEADER , String .valueOf (executionCount )).build ();
199205 executionCount ++;
200206 if (response != null ) {
201- if (response .body () != null )
202- response .body ().close ();
207+ final ResponseBody body = response .body ();
208+ if (body != null )
209+ body .close ();
203210 response .close ();
204211 }
205212 response = chain .proceed (request );
0 commit comments