|
| 1 | +package com.microsoft.graph.httpcore; |
| 2 | + |
| 3 | +import org.apache.http.Header; |
| 4 | +import org.apache.http.HttpEntity; |
| 5 | +import org.apache.http.HttpRequest; |
| 6 | +import org.apache.http.HttpResponse; |
| 7 | +import org.apache.http.client.ServiceUnavailableRetryStrategy; |
| 8 | +import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; |
| 9 | +import org.apache.http.client.methods.HttpPatch; |
| 10 | +import org.apache.http.client.methods.HttpPost; |
| 11 | +import org.apache.http.client.methods.HttpPut; |
| 12 | +import org.apache.http.protocol.HttpContext; |
| 13 | +import org.apache.http.protocol.HttpCoreContext; |
| 14 | +import org.apache.http.util.Args; |
| 15 | + |
| 16 | +public class RetryHandler implements ServiceUnavailableRetryStrategy{ |
| 17 | + |
| 18 | + /** |
| 19 | + * Maximum number of allowed retries if the server responds with a HTTP code |
| 20 | + * in our retry code list. Default value is 1. |
| 21 | + */ |
| 22 | + private final int maxRetries; |
| 23 | + |
| 24 | + /** |
| 25 | + * Retry interval between subsequent requests, in milliseconds. Default |
| 26 | + * value is 1 second. |
| 27 | + */ |
| 28 | + private long retryInterval; |
| 29 | + private final int DELAY_SECONDS = 10; |
| 30 | + private final String RETRY_AFTER = "Retry-After"; |
| 31 | + private final String TRANSFER_ENCODING = "Transfer-Encoding"; |
| 32 | + |
| 33 | + private final int MSClientErrorCodeTooManyRequests = 429; |
| 34 | + private final int MSClientErrorCodeServiceUnavailable = 503; |
| 35 | + private final int MSClientErrorCodeGatewayTimeout = 504; |
| 36 | + |
| 37 | + public RetryHandler(final int maxRetries, final int retryInterval) { |
| 38 | + super(); |
| 39 | + Args.positive(maxRetries, "Max retries"); |
| 40 | + Args.positive(retryInterval, "Retry interval"); |
| 41 | + this.maxRetries = maxRetries; |
| 42 | + this.retryInterval = retryInterval; |
| 43 | + } |
| 44 | + |
| 45 | + public RetryHandler() { |
| 46 | + this(1, 1000); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) { |
| 51 | + boolean shouldRetry = false; |
| 52 | + int statusCode = response.getStatusLine().getStatusCode(); |
| 53 | + shouldRetry = (executionCount < maxRetries) && checkStatus(statusCode) && isBuffered(response, context); |
| 54 | + |
| 55 | + if(shouldRetry) { |
| 56 | + Header header = response.getFirstHeader(RETRY_AFTER); |
| 57 | + if(header != null) |
| 58 | + retryInterval = Long.parseLong(header.getValue()); |
| 59 | + else |
| 60 | + retryInterval = (long)Math.pow(2.0, (double)executionCount) * DELAY_SECONDS; |
| 61 | + } |
| 62 | + return shouldRetry; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public long getRetryInterval() { |
| 67 | + // TODO Auto-generated method stub |
| 68 | + return retryInterval; |
| 69 | + } |
| 70 | + |
| 71 | + private boolean checkStatus(int statusCode) { |
| 72 | + if (statusCode == MSClientErrorCodeTooManyRequests || statusCode == MSClientErrorCodeServiceUnavailable |
| 73 | + || statusCode == MSClientErrorCodeGatewayTimeout) |
| 74 | + return true; |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + private boolean isBuffered(HttpResponse response, HttpContext context) { |
| 79 | + HttpRequest request = (HttpRequest)context.getAttribute( HttpCoreContext.HTTP_REQUEST); |
| 80 | + String methodName = request.getRequestLine().getMethod(); |
| 81 | + |
| 82 | + boolean isHTTPMethodPutPatchOrPost = methodName.equalsIgnoreCase(HttpPost.METHOD_NAME) || |
| 83 | + methodName.equalsIgnoreCase(HttpPut.METHOD_NAME) || |
| 84 | + methodName.equalsIgnoreCase(HttpPatch.METHOD_NAME); |
| 85 | + |
| 86 | + Header transferEncoding = response.getFirstHeader(TRANSFER_ENCODING); |
| 87 | + boolean isTransferEncodingChunked = (transferEncoding != null) && |
| 88 | + transferEncoding.getValue().equalsIgnoreCase("chunked"); |
| 89 | + |
| 90 | + HttpEntity entity = null; |
| 91 | + if(request instanceof HttpEntityEnclosingRequestBase) { |
| 92 | + HttpEntityEnclosingRequestBase httprequest = (HttpEntityEnclosingRequestBase)request; |
| 93 | + entity = httprequest.getEntity(); |
| 94 | + } |
| 95 | + |
| 96 | + if(entity != null && isHTTPMethodPutPatchOrPost && isTransferEncodingChunked) |
| 97 | + return false; |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | +} |
0 commit comments