Skip to content

Commit cd70c24

Browse files
authored
Merge pull request #6 from microsoftgraph/middlewareoption
Add middleware options classes
2 parents 81bcacf + ae5dfeb commit cd70c24

File tree

7 files changed

+137
-15
lines changed

7 files changed

+137
-15
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+
}

src/test/java/com/microsoft/graph/httpcore/RetryHandlerTest.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,40 +14,66 @@
1414
import org.apache.http.client.protocol.HttpClientContext;
1515
import org.apache.http.entity.StringEntity;
1616
import org.apache.http.message.BasicHttpResponse;
17+
import org.apache.http.protocol.HttpContext;
1718
import org.apache.http.protocol.HttpCoreContext;
1819
import org.junit.Test;
1920

21+
import com.microsoft.graph.httpcore.middlewareoption.IShouldRetry;
22+
import com.microsoft.graph.httpcore.middlewareoption.RetryOptions;
23+
2024
public class RetryHandlerTest {
2125

2226
int maxRetries = 2;
23-
int retryInterval = 2000;
27+
int retryInterval = 1000;
2428
String testurl = "https://graph.microsoft.com/v1.0/";
2529

2630
@Test
2731
public void testRetryHandlerCreation() {
28-
RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval);
32+
RetryHandler retryhandler = new RetryHandler();
2933
assertTrue(retryhandler.getRetryInterval() == retryInterval);
3034
}
3135

36+
@Test
37+
public void testRetryHandlerWithRetryOptions() {
38+
RetryOptions option = new RetryOptions();
39+
RetryHandler retryhandler = new RetryHandler(option);
40+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout");
41+
HttpClientContext localContext = HttpClientContext.create();
42+
assertTrue(retryhandler.retryRequest(response, 1, localContext));
43+
}
44+
45+
@Test
46+
public void testRetryHandlerWithCustomRetryOptions() {
47+
RetryOptions option = new RetryOptions(new IShouldRetry() {
48+
public boolean shouldRetry(HttpResponse response, int executionCount, HttpContext context) {
49+
return false;
50+
}
51+
});
52+
RetryHandler retryhandler = new RetryHandler(option);
53+
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout");
54+
HttpClientContext localContext = HttpClientContext.create();
55+
assertTrue(!retryhandler.retryRequest(response, 1, localContext));
56+
}
57+
3258
@Test
3359
public void testRetryRequestWithMaxRetryAttempts() {
34-
RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval);
60+
RetryHandler retryhandler = new RetryHandler();
3561
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Gateway Timeout");
3662
HttpClientContext localContext = HttpClientContext.create();
3763
assertFalse(retryhandler.retryRequest(response, 3, localContext));
3864
}
3965

4066
@Test
4167
public void testRetryRequestForStatusCode() {
42-
RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval);
68+
RetryHandler retryhandler = new RetryHandler();
4369
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_INTERNAL_SERVER_ERROR, "Internal Server Error");
4470
HttpClientContext localContext = HttpClientContext.create();
4571
assertFalse(retryhandler.retryRequest(response, 1, localContext));
4672
}
4773

4874
@Test
4975
public void testRetryRequestWithTransferEncoding() {
50-
RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval);
76+
RetryHandler retryhandler = new RetryHandler();
5177
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error");
5278
response.setHeader("Transfer-Encoding", "chunked");
5379
HttpPost httppost = new HttpPost(testurl);
@@ -67,7 +93,7 @@ public void testRetryRequestWithTransferEncoding() {
6793

6894
@Test
6995
public void testRetryRequestWithExponentialBackOff() {
70-
RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval);
96+
RetryHandler retryhandler = new RetryHandler();
7197
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error");
7298
HttpPost httppost = new HttpPost(testurl);
7399

@@ -87,7 +113,7 @@ public void testRetryRequestWithExponentialBackOff() {
87113

88114
@Test
89115
public void testRetryHandlerRetryRequestWithRetryAfterHeader() {
90-
RetryHandler retryhandler = new RetryHandler(maxRetries, retryInterval);
116+
RetryHandler retryhandler = new RetryHandler();
91117
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_GATEWAY_TIMEOUT, "Internal Server Error");
92118
response.setHeader("Retry-After", "100");
93119
HttpPost httppost = new HttpPost(testurl);

0 commit comments

Comments
 (0)