Skip to content

Commit 00b01c7

Browse files
committed
Add middleware options classes to enable middleware custom configurations
1 parent 81bcacf commit 00b01c7

File tree

6 files changed

+104
-8
lines changed

6 files changed

+104
-8
lines changed

src/main/java/com/microsoft/graph/httpcore/RetryHandler.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,51 @@
1313
import org.apache.http.protocol.HttpCoreContext;
1414
import org.apache.http.util.Args;
1515

16+
import com.microsoft.graph.httpcore.middlewareoption.MiddlewareType;
17+
import com.microsoft.graph.httpcore.middlewareoption.RetryOptions;
18+
1619
public class RetryHandler implements ServiceUnavailableRetryStrategy{
1720

1821
/**
1922
* Maximum number of allowed retries if the server responds with a HTTP code
2023
* in our retry code list. Default value is 1.
2124
*/
22-
private final int maxRetries;
25+
private final int maxRetries = 2;
2326

2427
/**
2528
* Retry interval between subsequent requests, in milliseconds. Default
2629
* value is 1 second.
2730
*/
28-
private long retryInterval;
31+
private long retryInterval = 1000;
2932
private final int DELAY_MILLISECONDS = 1000;
3033
private final String RETRY_AFTER = "Retry-After";
3134
private final String TRANSFER_ENCODING = "Transfer-Encoding";
3235

3336
private final int MSClientErrorCodeTooManyRequests = 429;
3437
private final int MSClientErrorCodeServiceUnavailable = 503;
3538
private final int MSClientErrorCodeGatewayTimeout = 504;
39+
private final RetryOptions mRetryOption;
3640

37-
public RetryHandler(final int maxRetries, final int retryInterval) {
41+
public RetryHandler(RetryOptions option) {
3842
super();
39-
Args.positive(maxRetries, "Max retries");
40-
Args.positive(retryInterval, "Retry interval");
41-
this.maxRetries = maxRetries;
42-
this.retryInterval = retryInterval;
43+
this.mRetryOption = option;
4344
}
4445

4546
public RetryHandler() {
46-
this(2, 1000);
47+
this(null);
4748
}
4849

4950
@Override
5051
public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
52+
53+
RetryOptions retryOption = (RetryOptions)context.getAttribute(MiddlewareType.RETRY.toString());
54+
if(retryOption != null) {
55+
return retryOption.shouldRetry().shouldRetry(response, executionCount, context);
56+
}
57+
if(mRetryOption != null) {
58+
return mRetryOption.shouldRetry().shouldRetry(response, executionCount, context);
59+
}
60+
5161
boolean shouldRetry = false;
5262
int statusCode = response.getStatusLine().getStatusCode();
5363
shouldRetry = (executionCount < maxRetries) && checkStatus(statusCode) && isBuffered(response, context);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.microsoft.graph.httpcore.middlewareoption;
2+
3+
import org.apache.http.client.config.RequestConfig;
4+
import org.apache.http.client.protocol.HttpClientContext;
5+
6+
public class HttpContextBuilder {
7+
8+
private RetryOptions retryoptions;
9+
private int maxRedirect = -1;
10+
11+
public static HttpContextBuilder create() {
12+
return new HttpContextBuilder();
13+
}
14+
15+
public void setRetryOption(IShouldRetry shouldRetry) {
16+
retryoptions = new RetryOptions(shouldRetry);
17+
}
18+
19+
public void setRedirectOption(int maxRedirect) {
20+
this.maxRedirect = maxRedirect;
21+
}
22+
23+
public HttpClientContext build() {
24+
HttpClientContext context = HttpClientContext.create();
25+
if(retryoptions != null)
26+
context.setAttribute(MiddlewareType.RETRY.toString(), retryoptions);
27+
if(maxRedirect != -1) {
28+
RequestConfig config = RequestConfig.custom().setMaxRedirects(maxRedirect).build();
29+
context.setRequestConfig(config);
30+
}
31+
return context;
32+
}
33+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.microsoft.graph.httpcore.middlewareoption;
2+
3+
public interface IMiddlewareControl {
4+
5+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.microsoft.graph.httpcore.middlewareoption;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.protocol.HttpContext;
5+
6+
public interface IShouldRetry {
7+
boolean shouldRetry(HttpResponse response, int executionCount, HttpContext context);
8+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.microsoft.graph.httpcore.middlewareoption;
2+
3+
public enum MiddlewareType {
4+
5+
//Authentication Middleware
6+
AUTHENTICATION,
7+
8+
//Redirect Middleware
9+
REDIRECT,
10+
11+
//Retry Middleware
12+
RETRY,
13+
14+
//Not supported
15+
NOT_SUPPORTED
16+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.microsoft.graph.httpcore.middlewareoption;
2+
3+
import org.apache.http.HttpResponse;
4+
import org.apache.http.protocol.HttpContext;
5+
6+
public class RetryOptions implements IMiddlewareControl {
7+
private IShouldRetry shouldretry;
8+
9+
public RetryOptions(){
10+
this(new IShouldRetry() {
11+
public boolean shouldRetry(HttpResponse response, int executionCount, HttpContext context) {
12+
return true;
13+
}
14+
});
15+
}
16+
17+
public RetryOptions(IShouldRetry shouldretry){
18+
this.shouldretry = shouldretry;
19+
}
20+
21+
public IShouldRetry shouldRetry() {
22+
return shouldretry;
23+
}
24+
}

0 commit comments

Comments
 (0)