4343import com .microsoft .graph .concurrency .IExecutors ;
4444import com .microsoft .graph .concurrency .IProgressCallback ;
4545import com .microsoft .graph .core .ClientException ;
46+ import com .microsoft .graph .core .Constants ;
4647import com .microsoft .graph .core .DefaultConnectionConfig ;
4748import com .microsoft .graph .core .IConnectionConfig ;
48- import com .microsoft .graph .httpcore .AuthenticationHandler ;
4949import com .microsoft .graph .httpcore .HttpClients ;
5050import com .microsoft .graph .httpcore .ICoreAuthenticationProvider ;
51- import com .microsoft .graph .httpcore .RedirectHandler ;
52- import com .microsoft .graph .httpcore .RetryHandler ;
5351import com .microsoft .graph .httpcore .middlewareoption .RedirectOptions ;
5452import com .microsoft .graph .httpcore .middlewareoption .RetryOptions ;
5553import com .microsoft .graph .logger .ILogger ;
5856import com .microsoft .graph .serializer .ISerializer ;
5957
6058import okhttp3 .Headers ;
61- import okhttp3 .Interceptor ;
6259import okhttp3 .MediaType ;
6360import okhttp3 .OkHttpClient ;
6461import okhttp3 .Request ;
6764import okio .BufferedSink ;
6865
6966/**
70- * HTTP provider based off of URLConnection
67+ * HTTP provider based off of OkHttp and msgraph-sdk-java-core library
7168 */
7269public class CoreHttpProvider implements IHttpProvider {
7370
74- /**
75- * The content type header
76- */
77- static final String CONTENT_TYPE_HEADER_NAME = "Content-Type" ;
78-
79- /**
80- * The content type for JSON responses
81- */
82- static final String JSON_CONTENT_TYPE = "application/json" ;
83-
84- /**
85- * The encoding type for getBytes
86- */
87- static final String JSON_ENCODING = "UTF-8" ;
88-
89- /**
90- * The binary content type header's value
91- */
92- static final String BINARY_CONTENT_TYPE = "application/octet-stream" ;
93-
9471 /**
9572 * The serializer
9673 */
@@ -298,7 +275,7 @@ public Request authenticateRequest(Request request) {
298275 List <HeaderOption > requestHeaders = request .getHeaders ();
299276
300277 for (HeaderOption headerOption : requestHeaders ) {
301- if (headerOption .getName ().equalsIgnoreCase (CONTENT_TYPE_HEADER_NAME )) {
278+ if (headerOption .getName ().equalsIgnoreCase (Constants . CONTENT_TYPE_HEADER_NAME )) {
302279 contenttype = headerOption .getValue ().toString ();
303280 break ;
304281 }
@@ -320,19 +297,19 @@ public Request authenticateRequest(Request request) {
320297 bytesToWrite = (byte []) serializable ;
321298
322299 // If the user hasn't specified a Content-Type for the request
323- if (!hasHeader (requestHeaders , CONTENT_TYPE_HEADER_NAME )) {
324- corehttpRequestBuilder .addHeader (CONTENT_TYPE_HEADER_NAME , BINARY_CONTENT_TYPE );
325- contenttype = BINARY_CONTENT_TYPE ;
300+ if (!hasHeader (requestHeaders , Constants . CONTENT_TYPE_HEADER_NAME )) {
301+ corehttpRequestBuilder .addHeader (Constants . CONTENT_TYPE_HEADER_NAME , Constants . BINARY_CONTENT_TYPE );
302+ contenttype = Constants . BINARY_CONTENT_TYPE ;
326303 }
327304 } else {
328305 logger .logDebug ("Sending " + serializable .getClass ().getName () + " as request body" );
329306 final String serializeObject = serializer .serializeObject (serializable );
330- bytesToWrite = serializeObject .getBytes (JSON_ENCODING );
307+ bytesToWrite = serializeObject .getBytes (Constants . JSON_ENCODING );
331308
332309 // If the user hasn't specified a Content-Type for the request
333- if (!hasHeader (requestHeaders , CONTENT_TYPE_HEADER_NAME )) {
334- corehttpRequestBuilder .addHeader (CONTENT_TYPE_HEADER_NAME , JSON_CONTENT_TYPE );
335- contenttype = JSON_CONTENT_TYPE ;
310+ if (!hasHeader (requestHeaders , Constants . CONTENT_TYPE_HEADER_NAME )) {
311+ corehttpRequestBuilder .addHeader (Constants . CONTENT_TYPE_HEADER_NAME , Constants . JSON_CONTENT_TYPE );
312+ contenttype = Constants . JSON_CONTENT_TYPE ;
336313 }
337314 }
338315
@@ -412,8 +389,8 @@ public MediaType contentType() {
412389
413390 final Map <String , String > headers = CoreHttpProvider .getResponseHeadersAsMapStringString (response );
414391
415- final String contentType = headers .get (CONTENT_TYPE_HEADER_NAME );
416- if (contentType .contains (JSON_CONTENT_TYPE )) {
392+ final String contentType = headers .get (Constants . CONTENT_TYPE_HEADER_NAME );
393+ if (contentType .contains (Constants . JSON_CONTENT_TYPE )) {
417394 logger .logDebug ("Response json" );
418395 return handleJsonResponse (in , CoreHttpProvider .getResponseHeadersAsMapOfStringList (response ), resultClass );
419396 } else {
@@ -542,7 +519,7 @@ private <Result> Result handleJsonResponse(final InputStream in, Map<String, Lis
542519 private <Result > Result handleEmptyResponse (Map <String , List <String >> responseHeaders , final Class <Result > clazz )
543520 throws UnsupportedEncodingException {
544521 //Create an empty object to attach the response headers to
545- InputStream in = new ByteArrayInputStream ("{}" .getBytes (JSON_ENCODING ));
522+ InputStream in = new ByteArrayInputStream ("{}" .getBytes (Constants . JSON_ENCODING ));
546523 return handleJsonResponse (in , responseHeaders , clazz );
547524 }
548525
0 commit comments