Skip to content

Commit ea73913

Browse files
authored
Merge pull request #1795 from microsoftgraph/fix/handler-overrides
Fixes overriding default interceptors and customizing their configuration
2 parents 85729b5 + 0615547 commit ea73913

File tree

7 files changed

+306
-42
lines changed

7 files changed

+306
-42
lines changed

android/build.gradle

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,17 @@ android {
4343
targetCompatibility JavaVersion.VERSION_1_8
4444
}
4545

46+
lint {
47+
baseline = file("lint-baseline.xml")
48+
}
49+
4650
lintOptions {
4751
textOutput "stdout"
4852
checkAllWarnings true
4953
warningsAsErrors true
50-
disable "UnusedResources" // Unused will be removed on release
51-
disable "IconExpectedSize" // Using the material icons provided from Google
52-
disable "GoogleAppIndexingApiWarning" // We might want to index our app later
53-
disable "InvalidPackage" // Butterknife, Okio and Realm
54-
disable "ResourceType" // Annotation binding
55-
disable "GradleDependency"
56-
disable "NewerVersionAvailable"
57-
disable "DuplicatePlatformClasses" // xpp3 added by azure-identity
54+
lintConfig file("lint.xml")
5855
}
56+
5957
sourceSets {
6058
main {
6159
java.srcDirs = ['../src/main/java']

android/lint-baseline.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<issues format="6" by="lint 8.7.2" type="baseline" client="gradle" dependencies="false" name="AGP (8.7.2)" variant="all" version="8.7.2">
3+
4+
<issue
5+
id="InvalidPackage"
6+
message="Invalid package reference in com.azure:azure-xml; not included in Android: `javax.xml.stream`. Referenced from `com.azure.xml.XmlReader`.">
7+
<location
8+
file="$GRADLE_USER_HOME/caches/modules-2/files-2.1/com.azure/azure-xml/1.1.0/8218a00c07f9f66d5dc7ae2ba613da6890867497/azure-xml-1.1.0.jar"/>
9+
</issue>
10+
11+
</issues>

android/lint.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<lint>
3+
<issue id="LambdaLast">
4+
<ignore path="../src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java" />
5+
</issue>
6+
</lint>

spotBugsExcludeFilter.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu
6464
<Class name="com.microsoft.graph.core.content.BatchResponseContent" />
6565
<Class name="com.microsoft.graph.core.requests.ResponseBodyHandler" />
6666
<Class name="com.microsoft.graph.core.requests.upload.UploadResponseHandler" />
67+
<Class name="com.microsoft.graph.core.requests.middleware.GraphTelemetryHandlerTest" />
6768
</Or>
6869
</Match>
6970
<Match>
@@ -111,4 +112,4 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu
111112
<Bug pattern="DCN_NULLPOINTER_EXCEPTION" />
112113
<Class name="com.microsoft.graph.core.content.BatchResponseContentTest" />
113114
</Match>
114-
</FindBugsFilter>
115+
</FindBugsFilter>

src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java

Lines changed: 99 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.microsoft.graph.core.requests;
22

3+
import com.azure.core.credential.TokenCredential;
34
import com.microsoft.graph.core.CoreConstants;
5+
import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider;
46
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
57
import com.microsoft.graph.core.requests.options.GraphClientOption;
8+
import com.microsoft.kiota.RequestOption;
69
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
710
import com.microsoft.kiota.http.KiotaClientFactory;
811
import com.microsoft.kiota.http.middleware.AuthorizationHandler;
@@ -36,7 +39,7 @@ public static OkHttpClient.Builder create() {
3639
* @return an OkHttpClient Builder instance.
3740
*/
3841
@Nonnull
39-
public static OkHttpClient.Builder create(@Nonnull Interceptor... interceptors) {
42+
public static OkHttpClient.Builder create(@Nonnull final Interceptor... interceptors) {
4043
return create(new GraphClientOption(), interceptors);
4144
}
4245

@@ -46,20 +49,54 @@ public static OkHttpClient.Builder create(@Nonnull Interceptor... interceptors)
4649
* @return an OkHttpClient Builder instance.
4750
*/
4851
@Nonnull
49-
public static OkHttpClient.Builder create(@Nonnull List<Interceptor> interceptors) {
52+
public static OkHttpClient.Builder create(@Nonnull final List<Interceptor> interceptors) {
5053
return create(new GraphClientOption(), interceptors.toArray(new Interceptor[0]));
5154
}
5255

56+
/**
57+
* OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential.
58+
* @param tokenCredential the TokenCredential to use for authentication.
59+
* @return an OkHttpClient Builder instance.
60+
*/
61+
@Nonnull
62+
public static OkHttpClient.Builder create(@Nonnull final TokenCredential tokenCredential) {
63+
return create(tokenCredential, new RequestOption[0]);
64+
}
65+
66+
/**
67+
* OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential and RequestOptions to override default graph interceptors.
68+
* @param tokenCredential the TokenCredential to use for authentication.
69+
* @param requestOptions custom request options to override default graph interceptors
70+
* @return an OkHttpClient Builder instance.
71+
*/
72+
@Nonnull
73+
public static OkHttpClient.Builder create(@Nonnull final TokenCredential tokenCredential, @Nonnull final RequestOption[] requestOptions) {
74+
return create(new BaseBearerTokenAuthenticationProvider(new AzureIdentityAccessTokenProvider(tokenCredential)), requestOptions);
75+
}
76+
5377
/**
5478
* OkHttpClient Builder for Graph with specified AuthenticationProvider.
5579
* Adds an AuthorizationHandler to the OkHttpClient Builder.
5680
* @param authenticationProvider the AuthenticationProvider to use for requests.
5781
* @return an OkHttpClient Builder instance.
5882
*/
5983
@Nonnull
60-
public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider) {
84+
public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider) {
85+
return create(authenticationProvider, new RequestOption[0]);
86+
}
87+
88+
/**
89+
* OkHttpClient Builder for Graph with specified AuthenticationProvider and RequestOptions to override default graph interceptors
90+
* @param authenticationProvider the AuthenticationProvider to use for requests.
91+
* @param requestOptions custom request options to override default graph interceptors
92+
* @return an OkHttpClient Builder instance.
93+
*/
94+
@Nonnull
95+
public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull final RequestOption[] requestOptions) {
6196
final GraphClientOption graphClientOption = new GraphClientOption();
62-
final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption);
97+
final List<RequestOption> requestOptionsList = new ArrayList<>(Arrays.asList(requestOptions));
98+
requestOptionsList.add(graphClientOption);
99+
final Interceptor[] interceptors = createDefaultGraphInterceptors(requestOptionsList.toArray(new RequestOption[0]));
63100
final ArrayList<Interceptor> interceptorList = new ArrayList<>(Arrays.asList(interceptors));
64101
interceptorList.add(new AuthorizationHandler(authenticationProvider));
65102
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.AUTH_HANDLER_FLAG);
@@ -74,18 +111,12 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication
74111
* @return an OkHttpClient Builder instance.
75112
*/
76113
@Nonnull
77-
public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull Interceptor... interceptors) {
78-
final OkHttpClient.Builder builder = create(graphClientOption);
79-
//Skip adding interceptor if that class of interceptor already exist.
80-
final List<String> appliedInterceptors = new ArrayList<>();
81-
for(Interceptor interceptor: builder.interceptors()) {
82-
appliedInterceptors.add(interceptor.getClass().toString());
83-
}
84-
for (Interceptor interceptor:interceptors){
85-
if(appliedInterceptors.contains(interceptor.getClass().toString())) {
86-
continue;
87-
}
88-
builder.addInterceptor(interceptor);
114+
public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graphClientOption, @Nonnull final Interceptor... interceptors) {
115+
final OkHttpClient.Builder builder = KiotaClientFactory.create(interceptors);
116+
final List<Interceptor> customInterceptors = builder.interceptors();
117+
final boolean telemetryHandlerExists = customInterceptors.stream().anyMatch(x -> x instanceof GraphTelemetryHandler);
118+
if (!telemetryHandlerExists) {
119+
customInterceptors.add(new GraphTelemetryHandler(graphClientOption));
89120
}
90121
return builder;
91122
}
@@ -97,7 +128,7 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient
97128
* @return an OkHttpClient Builder instance.
98129
*/
99130
@Nonnull
100-
public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull List<Interceptor> interceptors) {
131+
public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graphClientOption, @Nonnull final List<Interceptor> interceptors) {
101132
return create(graphClientOption, interceptors.toArray(new Interceptor[0]));
102133
}
103134

@@ -108,26 +139,68 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient
108139
* @return an OkHttpClient Builder instance.
109140
*/
110141
@Nonnull
111-
public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClientOption) {
112-
GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption();
113-
return KiotaClientFactory.create(createDefaultGraphInterceptors(options));
142+
public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption) {
143+
GraphClientOption option = graphClientOption == null ? new GraphClientOption() : graphClientOption;
144+
return KiotaClientFactory.create(createDefaultGraphInterceptors(option));
114145
}
146+
147+
/**
148+
* The OkHttpClient Builder with optional GraphClientOption and RequestOptions to override default graph interceptors
149+
* @param requestOptions custom request options to override default graph interceptors
150+
* @return an OkHttpClient Builder instance.
151+
*/
152+
@Nonnull
153+
public static OkHttpClient.Builder create(@Nonnull final RequestOption[] requestOptions) {
154+
return KiotaClientFactory.create(createDefaultGraphInterceptors(requestOptions));
155+
}
156+
115157
/**
116158
* Creates the default Interceptors for use with Graph.
117159
*
118160
* @param graphClientOption the GraphClientOption used to create the GraphTelemetryHandler with.
119161
* @return an array of interceptors.
120162
*/
121163
@Nonnull
122-
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientOption graphClientOption) {
123-
List<Interceptor> handlers = new ArrayList<>();
124-
addDefaultFeatureUsages(graphClientOption);
164+
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphClientOption graphClientOption) {
165+
return getDefaultGraphInterceptors(new RequestOption[]{ graphClientOption }).toArray(new Interceptor[0]);
166+
}
125167

126-
handlers.add(new UrlReplaceHandler(new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs())));
168+
/**
169+
* Creates the default Interceptors for use with Graph configured with the provided RequestOptions.
170+
* @param requestOptions custom request options to override default graph interceptors
171+
* @return an array of interceptors.
172+
*/
173+
@Nonnull
174+
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) {
175+
Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null");
176+
return getDefaultGraphInterceptors(requestOptions).toArray(new Interceptor[0]);
177+
}
178+
179+
/**
180+
* Creates the default Interceptors for use with Graph.
181+
* @param requestOptions custom request options to override default graph interceptors
182+
* @return a list of interceptors.
183+
*/
184+
private static List<Interceptor> getDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) {
185+
GraphClientOption graphClientOption = new GraphClientOption();
186+
UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs());
187+
for (RequestOption option : requestOptions) {
188+
if (option instanceof UrlReplaceHandlerOption) {
189+
urlReplaceHandlerOption = (UrlReplaceHandlerOption) option;
190+
}
191+
if (option instanceof GraphClientOption) {
192+
graphClientOption = (GraphClientOption) option;
193+
}
194+
}
195+
196+
List<Interceptor> handlers = new ArrayList<>();
197+
handlers.add(new UrlReplaceHandler(urlReplaceHandlerOption));
127198
handlers.add(new GraphTelemetryHandler(graphClientOption));
128-
handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors()));
129-
return handlers.toArray(new Interceptor[0]);
199+
handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors(requestOptions)));
200+
addDefaultFeatureUsages(graphClientOption);
201+
return handlers;
130202
}
203+
131204
//These are the default features used by the Graph Client
132205
private static void addDefaultFeatureUsages(GraphClientOption graphClientOption) {
133206
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.RETRY_HANDLER_FLAG);

0 commit comments

Comments
 (0)