Skip to content

Commit fedf358

Browse files
author
Nakul Sabharwal
committed
Moved common strings to Constants, try catch for response body stream closing
1 parent 896f0a1 commit fedf358

File tree

3 files changed

+34
-37
lines changed

3 files changed

+34
-37
lines changed

src/main/java/com/microsoft/graph/concurrency/ChunkedUploadResponseHandler.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ public ChunkedUploadResult<UploadType> generateResult(
125125
}
126126
} finally {
127127
if (in != null) {
128-
in.close();
128+
try{
129+
in.close();
130+
} catch(IOException e) {
131+
logger.logError(e.getMessage(), e);
132+
}
129133
}
130134
}
131135

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ private Constants() {
55
}
66

77
public static final String APPROOT = "approot";
8+
/**
9+
* The content type header
10+
*/
11+
public static final String CONTENT_TYPE_HEADER_NAME = "Content-Type";
12+
/**
13+
* The encoding type for getBytes
14+
*/
15+
public static final String JSON_ENCODING = "UTF-8";
16+
/**
17+
* The content type for JSON responses
18+
*/
19+
public static final String JSON_CONTENT_TYPE = "application/json";
20+
/**
21+
* The binary content type header's value
22+
*/
23+
public static final String BINARY_CONTENT_TYPE = "application/octet-stream";
824

925
// Constants for functional tests
1026
// TO-DO: document how to register an application for functional

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

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,11 @@
4343
import com.microsoft.graph.concurrency.IExecutors;
4444
import com.microsoft.graph.concurrency.IProgressCallback;
4545
import com.microsoft.graph.core.ClientException;
46+
import com.microsoft.graph.core.Constants;
4647
import com.microsoft.graph.core.DefaultConnectionConfig;
4748
import com.microsoft.graph.core.IConnectionConfig;
48-
import com.microsoft.graph.httpcore.AuthenticationHandler;
4949
import com.microsoft.graph.httpcore.HttpClients;
5050
import com.microsoft.graph.httpcore.ICoreAuthenticationProvider;
51-
import com.microsoft.graph.httpcore.RedirectHandler;
52-
import com.microsoft.graph.httpcore.RetryHandler;
5351
import com.microsoft.graph.httpcore.middlewareoption.RedirectOptions;
5452
import com.microsoft.graph.httpcore.middlewareoption.RetryOptions;
5553
import com.microsoft.graph.logger.ILogger;
@@ -58,7 +56,6 @@
5856
import com.microsoft.graph.serializer.ISerializer;
5957

6058
import okhttp3.Headers;
61-
import okhttp3.Interceptor;
6259
import okhttp3.MediaType;
6360
import okhttp3.OkHttpClient;
6461
import okhttp3.Request;
@@ -67,30 +64,10 @@
6764
import 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
*/
7269
public 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

Comments
 (0)