Skip to content

Commit 76a447d

Browse files
Added redirect options
1 parent d644fda commit 76a447d

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.microsoft.graph.httpcore.middlewareoption;
2+
3+
import okhttp3.Response;
4+
5+
public interface IShouldRedirect {
6+
boolean shouldRedirect(final Response response);
7+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.microsoft.graph.httpcore.middlewareoption;
2+
3+
import okhttp3.Response;
4+
5+
public class RedirectOptions implements IMiddlewareControl{
6+
private int maxRedirects;
7+
public static final int DEFAULT_MAX_REDIRECTS = 5;
8+
public static final int MAX_REDIRECTS = 20;
9+
10+
private IShouldRedirect shouldRedirect;
11+
public static final IShouldRedirect DEFAULT_SHOULD_REDIRECT = new IShouldRedirect() {
12+
@Override
13+
public boolean shouldRedirect(Response response) {
14+
return true;
15+
}
16+
};
17+
18+
/*
19+
* Create default instance of redirect options, with default values of max redirects and should redirect
20+
*/
21+
public RedirectOptions() {
22+
this(DEFAULT_MAX_REDIRECTS, DEFAULT_SHOULD_REDIRECT);
23+
}
24+
25+
/*
26+
* @param maxRedirects Max redirects to occur
27+
* @param shouldRedirect Should redirect callback called before every redirect
28+
*/
29+
public RedirectOptions(int maxRedirects, IShouldRedirect shouldRedirect) {
30+
if(maxRedirects < 0)
31+
throw new IllegalArgumentException("Max redirects cannot be negative");
32+
if(maxRedirects > MAX_REDIRECTS)
33+
throw new IllegalArgumentException("Max redirect cannot exceed " + MAX_REDIRECTS);
34+
35+
this.maxRedirects = maxRedirects;
36+
this.shouldRedirect = shouldRedirect != null ? shouldRedirect : DEFAULT_SHOULD_REDIRECT;
37+
}
38+
39+
/*
40+
* @return max redirects
41+
*/
42+
public int maxRedirects() {
43+
return this.maxRedirects;
44+
}
45+
46+
/*
47+
* @return should redirect
48+
*/
49+
public IShouldRedirect shouldRedirect() {
50+
return this.shouldRedirect;
51+
}
52+
}

0 commit comments

Comments
 (0)