Skip to content
14 changes: 6 additions & 8 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,17 @@ android {
targetCompatibility JavaVersion.VERSION_1_8
}

lint {
baseline = file("lint-baseline.xml")
}

lintOptions {
textOutput "stdout"
checkAllWarnings true
warningsAsErrors true
disable "UnusedResources" // Unused will be removed on release
disable "IconExpectedSize" // Using the material icons provided from Google
disable "GoogleAppIndexingApiWarning" // We might want to index our app later
disable "InvalidPackage" // Butterknife, Okio and Realm
disable "ResourceType" // Annotation binding
disable "GradleDependency"
disable "NewerVersionAvailable"
disable "DuplicatePlatformClasses" // xpp3 added by azure-identity
lintConfig file("lint.xml")
}

sourceSets {
main {
java.srcDirs = ['../src/main/java']
Expand Down
11 changes: 11 additions & 0 deletions android/lint-baseline.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">

<issue
id="InvalidPackage"
message="Invalid package reference in com.azure:azure-xml; not included in Android: `javax.xml.stream`. Referenced from `com.azure.xml.XmlReader`.">
<location
file="$GRADLE_USER_HOME/caches/modules-2/files-2.1/com.azure/azure-xml/1.1.0/8218a00c07f9f66d5dc7ae2ba613da6890867497/azure-xml-1.1.0.jar"/>
</issue>

</issues>
6 changes: 6 additions & 0 deletions android/lint.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="LambdaLast">
<ignore path="../src/main/java/com/microsoft/graph/core/requests/GraphClientFactory.java" />
</issue>
</lint>
3 changes: 2 additions & 1 deletion spotBugsExcludeFilter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu
<Class name="com.microsoft.graph.core.content.BatchResponseContent" />
<Class name="com.microsoft.graph.core.requests.ResponseBodyHandler" />
<Class name="com.microsoft.graph.core.requests.upload.UploadResponseHandler" />
<Class name="com.microsoft.graph.core.requests.middleware.GraphTelemetryHandlerTest" />
</Or>
</Match>
<Match>
Expand Down Expand Up @@ -111,4 +112,4 @@ xsi:schemaLocation="https://github.com/spotbugs/filter/3.0.0 https://raw.githubu
<Bug pattern="DCN_NULLPOINTER_EXCEPTION" />
<Class name="com.microsoft.graph.core.content.BatchResponseContentTest" />
</Match>
</FindBugsFilter>
</FindBugsFilter>
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.microsoft.graph.core.requests;

import com.azure.core.credential.TokenCredential;
import com.microsoft.graph.core.CoreConstants;
import com.microsoft.graph.core.authentication.AzureIdentityAccessTokenProvider;
import com.microsoft.graph.core.requests.middleware.GraphTelemetryHandler;
import com.microsoft.graph.core.requests.options.GraphClientOption;
import com.microsoft.kiota.RequestOption;
import com.microsoft.kiota.authentication.BaseBearerTokenAuthenticationProvider;
import com.microsoft.kiota.http.KiotaClientFactory;
import com.microsoft.kiota.http.middleware.AuthorizationHandler;
Expand Down Expand Up @@ -36,7 +39,7 @@ public static OkHttpClient.Builder create() {
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull Interceptor... interceptors) {
public static OkHttpClient.Builder create(@Nonnull final Interceptor... interceptors) {
return create(new GraphClientOption(), interceptors);
}

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

/**
* OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential.
* @param tokenCredential the TokenCredential to use for authentication.
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull final TokenCredential tokenCredential) {
return create(tokenCredential, new RequestOption[0]);
}

/**
* OkHttpClient Builder for Graph with authentication middleware that uses the specified TokenCredential and RequestOptions to override default graph interceptors.
* @param tokenCredential the TokenCredential to use for authentication.
* @param requestOptions custom request options to override default graph interceptors
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull final TokenCredential tokenCredential, @Nonnull final RequestOption[] requestOptions) {
return create(new BaseBearerTokenAuthenticationProvider(new AzureIdentityAccessTokenProvider(tokenCredential)), requestOptions);
}

/**
* OkHttpClient Builder for Graph with specified AuthenticationProvider.
* Adds an AuthorizationHandler to the OkHttpClient Builder.
* @param authenticationProvider the AuthenticationProvider to use for requests.
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthenticationProvider authenticationProvider) {
public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider) {
return create(authenticationProvider, new RequestOption[0]);
}

/**
* OkHttpClient Builder for Graph with specified AuthenticationProvider and RequestOptions to override default graph interceptors
* @param authenticationProvider the AuthenticationProvider to use for requests.
* @param requestOptions custom request options to override default graph interceptors
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull final BaseBearerTokenAuthenticationProvider authenticationProvider, @Nonnull final RequestOption[] requestOptions) {
final GraphClientOption graphClientOption = new GraphClientOption();
final Interceptor[] interceptors = createDefaultGraphInterceptors(graphClientOption);
final List<RequestOption> requestOptionsList = new ArrayList<>(Arrays.asList(requestOptions));
requestOptionsList.add(graphClientOption);
final Interceptor[] interceptors = createDefaultGraphInterceptors(requestOptionsList.toArray(new RequestOption[0]));
final ArrayList<Interceptor> interceptorList = new ArrayList<>(Arrays.asList(interceptors));
interceptorList.add(new AuthorizationHandler(authenticationProvider));
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.AUTH_HANDLER_FLAG);
Expand All @@ -74,18 +111,12 @@ public static OkHttpClient.Builder create(@Nonnull BaseBearerTokenAuthentication
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull Interceptor... interceptors) {
final OkHttpClient.Builder builder = create(graphClientOption);
//Skip adding interceptor if that class of interceptor already exist.
final List<String> appliedInterceptors = new ArrayList<>();
for(Interceptor interceptor: builder.interceptors()) {
appliedInterceptors.add(interceptor.getClass().toString());
}
for (Interceptor interceptor:interceptors){
if(appliedInterceptors.contains(interceptor.getClass().toString())) {
continue;
}
builder.addInterceptor(interceptor);
public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graphClientOption, @Nonnull final Interceptor... interceptors) {
final OkHttpClient.Builder builder = KiotaClientFactory.create(interceptors);
final List<Interceptor> customInterceptors = builder.interceptors();
final boolean telemetryHandlerExists = customInterceptors.stream().anyMatch(x -> x instanceof GraphTelemetryHandler);
if (!telemetryHandlerExists) {
customInterceptors.add(new GraphTelemetryHandler(graphClientOption));
}
return builder;
}
Expand All @@ -97,7 +128,7 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClientOption, @Nonnull List<Interceptor> interceptors) {
public static OkHttpClient.Builder create(@Nonnull final GraphClientOption graphClientOption, @Nonnull final List<Interceptor> interceptors) {
return create(graphClientOption, interceptors.toArray(new Interceptor[0]));
}

Expand All @@ -108,26 +139,68 @@ public static OkHttpClient.Builder create(@Nonnull GraphClientOption graphClient
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nullable GraphClientOption graphClientOption) {
GraphClientOption options = graphClientOption != null ? graphClientOption : new GraphClientOption();
return KiotaClientFactory.create(createDefaultGraphInterceptors(options));
public static OkHttpClient.Builder create(@Nullable final GraphClientOption graphClientOption) {
GraphClientOption option = graphClientOption == null ? new GraphClientOption() : graphClientOption;
return KiotaClientFactory.create(createDefaultGraphInterceptors(option));
}

/**
* The OkHttpClient Builder with optional GraphClientOption and RequestOptions to override default graph interceptors
* @param requestOptions custom request options to override default graph interceptors
* @return an OkHttpClient Builder instance.
*/
@Nonnull
public static OkHttpClient.Builder create(@Nonnull final RequestOption[] requestOptions) {
return KiotaClientFactory.create(createDefaultGraphInterceptors(requestOptions));
}

/**
* Creates the default Interceptors for use with Graph.
*
* @param graphClientOption the GraphClientOption used to create the GraphTelemetryHandler with.
* @return an array of interceptors.
*/
@Nonnull
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull GraphClientOption graphClientOption) {
List<Interceptor> handlers = new ArrayList<>();
addDefaultFeatureUsages(graphClientOption);
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final GraphClientOption graphClientOption) {
return getDefaultGraphInterceptors(new RequestOption[]{ graphClientOption }).toArray(new Interceptor[0]);
}

handlers.add(new UrlReplaceHandler(new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs())));
/**
* Creates the default Interceptors for use with Graph configured with the provided RequestOptions.
* @param requestOptions custom request options to override default graph interceptors
* @return an array of interceptors.
*/
@Nonnull
public static Interceptor[] createDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) {
Objects.requireNonNull(requestOptions, "parameter requestOptions cannot be null");
return getDefaultGraphInterceptors(requestOptions).toArray(new Interceptor[0]);
}

/**
* Creates the default Interceptors for use with Graph.
* @param requestOptions custom request options to override default graph interceptors
* @return a list of interceptors.
*/
private static List<Interceptor> getDefaultGraphInterceptors(@Nonnull final RequestOption[] requestOptions) {
GraphClientOption graphClientOption = new GraphClientOption();
UrlReplaceHandlerOption urlReplaceHandlerOption = new UrlReplaceHandlerOption(CoreConstants.ReplacementConstants.getDefaultReplacementPairs());
for (RequestOption option : requestOptions) {
if (option instanceof UrlReplaceHandlerOption) {
urlReplaceHandlerOption = (UrlReplaceHandlerOption) option;
}
if (option instanceof GraphClientOption) {
graphClientOption = (GraphClientOption) option;
}
}

List<Interceptor> handlers = new ArrayList<>();
handlers.add(new UrlReplaceHandler(urlReplaceHandlerOption));
handlers.add(new GraphTelemetryHandler(graphClientOption));
handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors()));
return handlers.toArray(new Interceptor[0]);
handlers.addAll(Arrays.asList(KiotaClientFactory.createDefaultInterceptors(requestOptions)));
addDefaultFeatureUsages(graphClientOption);
return handlers;
}

//These are the default features used by the Graph Client
private static void addDefaultFeatureUsages(GraphClientOption graphClientOption) {
graphClientOption.featureTracker.setFeatureUsage(FeatureFlag.RETRY_HANDLER_FLAG);
Expand Down
Loading
Loading