5353import com .microsoft .graph .http .IHttpProvider ;
5454import com .microsoft .graph .http .IHttpRequest ;
5555import com .microsoft .graph .http .IStatefulResponseHandler ;
56+ import com .microsoft .graph .httpcore .AuthenticationHandler ;
5657import com .microsoft .graph .httpcore .HttpClients ;
5758import com .microsoft .graph .httpcore .ICoreAuthenticationProvider ;
5859import com .microsoft .graph .httpcore .RedirectHandler ;
@@ -258,7 +259,6 @@ private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRe
258259 authenticationProvider .authenticateRequest (request );
259260 }
260261
261- OutputStream out = null ;
262262 InputStream in = null ;
263263 boolean isBinaryStreamInput = false ;
264264 final URL requestUrl = request .getRequestUrl ();
@@ -269,16 +269,24 @@ private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRe
269269 }
270270
271271 if (okhttpClient == null ) {
272- OkHttpClient .Builder okBuilder = HttpClients
273- .createDefault (new ICoreAuthenticationProvider () {
274- @ Override
275- public Request authenticateRequest (Request request ) {
276- return request ;
277- }
278- }).newBuilder ();
279-
272+ RedirectOptions redirectOptions = new RedirectOptions (this .connectionConfig .getMaxRedirects (), this .connectionConfig .getShouldRedirect ());
273+ RedirectHandler redirectHandler = new RedirectHandler (redirectOptions );
274+
275+ RetryOptions retryOptions = new RetryOptions (this .connectionConfig .getShouldRetry (), this .connectionConfig .getMaxRetries (), this .connectionConfig .getDelay ());
276+ RetryHandler retryHandler = new RetryHandler (retryOptions );
277+
278+ AuthenticationHandler authenticationHandler = new AuthenticationHandler (new ICoreAuthenticationProvider () {
279+ @ Override
280+ public Request authenticateRequest (Request request ) {
281+ return request ;
282+ }
283+ });
284+
285+ Interceptor [] interceptors = {redirectHandler , retryHandler , authenticationHandler };
286+ OkHttpClient .Builder okBuilder = HttpClients .createFromInterceptors (interceptors ).newBuilder ();
280287 okBuilder .connectTimeout (connectionConfig .getConnectTimeout (), TimeUnit .MILLISECONDS );
281288 okBuilder .readTimeout (connectionConfig .getReadTimeout (), TimeUnit .MILLISECONDS );
289+ okBuilder .followRedirects (false );
282290 okhttpClient = okBuilder .build ();
283291 }
284292
@@ -355,6 +363,7 @@ public void writeTo(BufferedSink sink) throws IOException {
355363 }
356364 } while (toWrite > 0 );
357365 bos .close ();
366+ out .close ();
358367 }
359368
360369 @ Override
@@ -415,9 +424,6 @@ public MediaType contentType() {
415424 return (Result ) handleBinaryStream (in );
416425 }
417426 } finally {
418- if (out != null ) {
419- out .close ();
420- }
421427 if (!isBinaryStreamInput && in != null ) {
422428 in .close ();
423429 }
0 commit comments