Skip to content

Commit d8c4078

Browse files
authored
Merge pull request #501 from microsoftgraph/feature/http-client-configuration
feature/http client configuration
2 parents 7490725 + 54fa900 commit d8c4078

File tree

8 files changed

+57
-18
lines changed

8 files changed

+57
-18
lines changed

src/main/java/com/microsoft/graph/authentication/IAuthenticationProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626

2727
/**
2828
* Provides authentication for a requests before it is sent by an HTTP provider
29+
* @deprecated use ICoreAuthenticationProvider instead
2930
*/
31+
@Deprecated
3032
public interface IAuthenticationProvider {
3133

3234
/**

src/main/java/com/microsoft/graph/core/DefaultClientConfig.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import com.microsoft.graph.serializer.DefaultSerializer;
3333
import com.microsoft.graph.serializer.ISerializer;
3434

35+
import okhttp3.OkHttpClient;
36+
3537
/**
3638
* The default configuration for a service client
3739
*/
@@ -55,7 +57,7 @@ public abstract class DefaultClientConfig implements IClientConfig {
5557
/**
5658
* The serializer instance
5759
*/
58-
private DefaultSerializer serializer;
60+
private ISerializer serializer;
5961

6062
/**
6163
* Creates an instance of this configuration with an authentication provider
@@ -97,11 +99,22 @@ public IAuthenticationProvider getAuthenticationProvider() {
9799
*/
98100
@Override
99101
public IHttpProvider getHttpProvider() {
102+
return this.getHttpProvider(null);
103+
}
104+
105+
/**
106+
* Gets the HTTP provider
107+
*
108+
* @param httpClient the http client to pass to the http provider when building it
109+
* @param <T1> the http client type
110+
* @return the HTTP provider
111+
*/
112+
@Override
113+
public <T1> IHttpProvider getHttpProvider(final T1 httpClient) {
100114
if (httpProvider == null) {
101-
httpProvider = new CoreHttpProvider(getSerializer(),
102-
getAuthenticationProvider(),
103-
getExecutors(),
104-
getLogger());
115+
httpProvider = (httpClient instanceof OkHttpClient) ?
116+
new CoreHttpProvider(this, (OkHttpClient) httpClient) :
117+
new CoreHttpProvider(this, null);
105118
getLogger().logDebug("Created CoreHttpProvider");
106119
}
107120
return httpProvider;

src/main/java/com/microsoft/graph/core/IClientConfig.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ public interface IClientConfig {
5353
*/
5454
IHttpProvider getHttpProvider();
5555

56+
/**
57+
* Gets the HTTP provider
58+
*
59+
* @param httpClient the http client to pass to the http provider when building it
60+
* @param <T1> the http client type
61+
* @return the HTTP provider
62+
*/
63+
<T1> IHttpProvider getHttpProvider(final T1 httpClient);
64+
5665
/**
5766
* Gets the logger
5867
*

src/main/java/com/microsoft/graph/http/CoreHttpProvider.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.microsoft.graph.core.ClientException;
4747
import com.microsoft.graph.core.Constants;
4848
import com.microsoft.graph.core.DefaultConnectionConfig;
49+
import com.microsoft.graph.core.IClientConfig;
4950
import com.microsoft.graph.core.IConnectionConfig;
5051
import com.microsoft.graph.httpcore.HttpClients;
5152
import com.microsoft.graph.httpcore.ICoreAuthenticationProvider;
@@ -119,6 +120,18 @@ public CoreHttpProvider(final ISerializer serializer,
119120
this.logger = logger;
120121
}
121122

123+
/**
124+
* Creates the DefaultHttpProvider
125+
*
126+
* @param clientConfig the client configuration to use for the provider
127+
* @param httpClient the http client to execute the requests with
128+
*/
129+
public CoreHttpProvider(final IClientConfig clientConfig,
130+
final OkHttpClient httpClient) {
131+
this(clientConfig.getSerializer(), clientConfig.getAuthenticationProvider(), clientConfig.getExecutors(), clientConfig.getLogger());
132+
this.corehttpClient = httpClient;
133+
}
134+
122135
/**
123136
* Gets the serializer for this HTTP provider
124137
*
@@ -144,9 +157,9 @@ public <Result, Body> void send(final IHttpRequest request,
144157
final ICallback<? super Result> callback,
145158
final Class<Result> resultClass,
146159
final Body serializable) {
147-
final IProgressCallback<Result> progressCallback;
160+
final IProgressCallback<? super Result> progressCallback;
148161
if (callback instanceof IProgressCallback) {
149-
progressCallback = (IProgressCallback<Result>) callback;
162+
progressCallback = (IProgressCallback<? super Result>) callback;
150163
} else {
151164
progressCallback = null;
152165
}
@@ -221,7 +234,7 @@ public <Result, Body, DeserializeType> Result send(final IHttpRequest request,
221234
public <Result, Body> Request getHttpRequest(final IHttpRequest request,
222235
final Class<Result> resultClass,
223236
final Body serializable,
224-
final IProgressCallback<Result> progress) throws ClientException {
237+
final IProgressCallback<? super Result> progress) throws ClientException {
225238
final int defaultBufferSize = 4096;
226239

227240
final URL requestUrl = request.getRequestUrl();
@@ -354,7 +367,7 @@ public MediaType contentType() {
354367
private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRequest request,
355368
final Class<Result> resultClass,
356369
final Body serializable,
357-
final IProgressCallback<Result> progress,
370+
final IProgressCallback<? super Result> progress,
358371
final IStatefulResponseHandler<Result, DeserializeType> handler)
359372
throws ClientException {
360373

@@ -374,11 +387,11 @@ public Request authenticateRequest(Request request) {
374387
.newBuilder()
375388
.connectTimeout(connectionConfig.getConnectTimeout(), TimeUnit.MILLISECONDS)
376389
.readTimeout(connectionConfig.getReadTimeout(), TimeUnit.MILLISECONDS)
377-
.followRedirects(false)
390+
.followRedirects(false) //TODO https://github.com/microsoftgraph/msgraph-sdk-java/issues/516
378391
.protocols(Arrays.asList(Protocol.HTTP_1_1)) //https://stackoverflow.com/questions/62031298/sockettimeout-on-java-11-but-not-on-java-8
379392
.build();
380393
}
381-
if (authenticationProvider != null) {
394+
if (authenticationProvider != null) { //TODO https://github.com/microsoftgraph/msgraph-sdk-java/issues/517
382395
authenticationProvider.authenticateRequest(request);
383396
}
384397
Request coreHttpRequest = getHttpRequest(request, resultClass, serializable, progress);

src/main/java/com/microsoft/graph/http/DefaultHttpProvider.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@
5151

5252
/**
5353
* HTTP provider based off of URLConnection
54+
* @deprecated user CoreHttpProvider instead
5455
*/
56+
@Deprecated
5557
public class DefaultHttpProvider implements IHttpProvider {
5658

5759
/**
@@ -160,9 +162,9 @@ public <Result, Body> void send(final IHttpRequest request,
160162
final ICallback<? super Result> callback,
161163
final Class<Result> resultClass,
162164
final Body serializable) {
163-
final IProgressCallback<Result> progressCallback;
165+
final IProgressCallback<? super Result> progressCallback;
164166
if (callback instanceof IProgressCallback) {
165-
progressCallback = (IProgressCallback<Result>) callback;
167+
progressCallback = (IProgressCallback<? super Result>) callback;
166168
} else {
167169
progressCallback = null;
168170
}
@@ -241,7 +243,7 @@ public <Result, Body, DeserializeType> Result send(final IHttpRequest request,
241243
private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRequest request,
242244
final Class<Result> resultClass,
243245
final Body serializable,
244-
final IProgressCallback<Result> progress,
246+
final IProgressCallback<? super Result> progress,
245247
final IStatefulResponseHandler<Result, DeserializeType> handler)
246248
throws ClientException {
247249
final int defaultBufferSize = 4096;
@@ -535,7 +537,7 @@ public void setConnectionConfig(IConnectionConfig connectionConfig) {
535537

536538
@Override
537539
public <Result, BodyType> Request getHttpRequest(IHttpRequest request, Class<Result> resultClass,
538-
BodyType serializable, IProgressCallback<Result> progress) throws ClientException {
540+
BodyType serializable, IProgressCallback<? super Result> progress) throws ClientException {
539541
return null;
540542
}
541543
}

src/main/java/com/microsoft/graph/http/IHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,6 @@ <Result, BodyType, DeserializeType> Result send(final IHttpRequest request,
123123
<Result, BodyType> Request getHttpRequest(final IHttpRequest request,
124124
final Class<Result> resultClass,
125125
final BodyType serializable,
126-
final IProgressCallback<Result> progress)
126+
final IProgressCallback<? super Result> progress)
127127
throws ClientException;
128128
}

src/test/java/com/microsoft/graph/http/MockHttpProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void setConnectionConfig(IConnectionConfig connectionConfig) {
129129

130130
@Override
131131
public <Result, BodyType> Request getHttpRequest(IHttpRequest request, Class<Result> resultClass,
132-
BodyType serializable, IProgressCallback<Result> progress) throws ClientException {
132+
BodyType serializable, IProgressCallback<? super Result> progress) throws ClientException {
133133
return null;
134134
}
135135
}

src/test/java/com/microsoft/graph/requests/extensions/GraphServiceClientTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public void setConnectionConfig(IConnectionConfig connectionConfig) {
207207

208208
@Override
209209
public <Result, BodyType> Request getHttpRequest(IHttpRequest request, Class<Result> resultClass,
210-
BodyType serializable, IProgressCallback<Result> progress) throws ClientException {
210+
BodyType serializable, IProgressCallback<? super Result> progress) throws ClientException {
211211
return null;
212212
}
213213
};

0 commit comments

Comments
 (0)