2525import com .google .common .annotations .VisibleForTesting ;
2626
2727import com .microsoft .graph .core .ClientException ;
28- import com .microsoft .graph .core .Constants ;
2928import com .microsoft .graph .httpcore .middlewareoption .RedirectOptions ;
3029import com .microsoft .graph .httpcore .middlewareoption .RetryOptions ;
3130import com .microsoft .graph .logger .ILogger ;
6160 * HTTP provider based off of OkHttp and msgraph-sdk-java-core library
6261 */
6362public class CoreHttpProvider implements IHttpProvider {
64- /**
63+ /**
64+ * The content type header
65+ */
66+ private static final String CONTENT_TYPE_HEADER_NAME = "Content-Type" ;
67+ /**
68+ * The encoding type for getBytes
69+ */
70+ private static final String JSON_ENCODING = "UTF-8" ;
71+ /**
72+ * The content type for JSON responses
73+ */
74+ private static final String JSON_CONTENT_TYPE = "application/json" ;
75+ /**
76+ * The binary content type header's value
77+ */
78+ private static final String BINARY_CONTENT_TYPE = "application/octet-stream" ;
79+ /**
6580 * The serializer
6681 */
6782 private final ISerializer serializer ;
@@ -234,7 +249,7 @@ public <Result, Body> Request getHttpRequest(@Nonnull final IHttpRequest request
234249 final List <HeaderOption > requestHeaders = request .getHeaders ();
235250
236251 for (HeaderOption headerOption : requestHeaders ) {
237- if (headerOption .getName ().equalsIgnoreCase (Constants . CONTENT_TYPE_HEADER_NAME )) {
252+ if (headerOption .getName ().equalsIgnoreCase (CONTENT_TYPE_HEADER_NAME )) {
238253 contenttype = headerOption .getValue ().toString ();
239254 break ;
240255 }
@@ -248,7 +263,7 @@ public <Result, Body> Request getHttpRequest(@Nonnull final IHttpRequest request
248263 if (request .getHttpMethod () == HttpMethod .POST ) {
249264 bytesToWrite = new byte [0 ];
250265 if (contenttype == null ) {
251- contenttype = Constants . BINARY_CONTENT_TYPE ;
266+ contenttype = BINARY_CONTENT_TYPE ;
252267 }
253268 }
254269 else {
@@ -259,15 +274,15 @@ public <Result, Body> Request getHttpRequest(@Nonnull final IHttpRequest request
259274 bytesToWrite = (byte []) serializable ;
260275
261276 // If the user hasn't specified a Content-Type for the request
262- if (!hasHeader (requestHeaders , Constants . CONTENT_TYPE_HEADER_NAME )) {
263- corehttpRequestBuilder .addHeader (Constants . CONTENT_TYPE_HEADER_NAME , Constants . BINARY_CONTENT_TYPE );
264- contenttype = Constants . BINARY_CONTENT_TYPE ;
277+ if (!hasHeader (requestHeaders , CONTENT_TYPE_HEADER_NAME )) {
278+ corehttpRequestBuilder .addHeader (CONTENT_TYPE_HEADER_NAME , BINARY_CONTENT_TYPE );
279+ contenttype = BINARY_CONTENT_TYPE ;
265280 }
266281 } else {
267282 logger .logDebug ("Sending " + serializable .getClass ().getName () + " as request body" );
268283 final String serializeObject = serializer .serializeObject (serializable );
269284 try {
270- bytesToWrite = serializeObject .getBytes (Constants . JSON_ENCODING );
285+ bytesToWrite = serializeObject .getBytes (JSON_ENCODING );
271286 } catch (final UnsupportedEncodingException ex ) {
272287 final ClientException clientException = new ClientException ("Unsupported encoding problem: " ,
273288 ex );
@@ -276,9 +291,9 @@ public <Result, Body> Request getHttpRequest(@Nonnull final IHttpRequest request
276291 }
277292
278293 // If the user hasn't specified a Content-Type for the request
279- if (!hasHeader (requestHeaders , Constants . CONTENT_TYPE_HEADER_NAME )) {
280- corehttpRequestBuilder .addHeader (Constants . CONTENT_TYPE_HEADER_NAME , Constants . JSON_CONTENT_TYPE );
281- contenttype = Constants . JSON_CONTENT_TYPE ;
294+ if (!hasHeader (requestHeaders , CONTENT_TYPE_HEADER_NAME )) {
295+ corehttpRequestBuilder .addHeader (CONTENT_TYPE_HEADER_NAME , JSON_CONTENT_TYPE );
296+ contenttype = JSON_CONTENT_TYPE ;
282297 }
283298 }
284299
@@ -534,7 +549,7 @@ private <Result> Result handleEmptyResponse(Map<String, List<String>> responseHe
534549 throws UnsupportedEncodingException {
535550 //Create an empty object to attach the response headers to
536551 Result result = null ;
537- try (final InputStream in = new ByteArrayInputStream ("{}" .getBytes (Constants . JSON_ENCODING ))) {
552+ try (final InputStream in = new ByteArrayInputStream ("{}" .getBytes (JSON_ENCODING ))) {
538553 result = handleJsonResponse (in , responseHeaders , clazz );
539554 } catch (IOException ex ) {
540555 //noop we couldnt close the byte array stream we just opened and its ok
0 commit comments