Skip to content

Commit 160ccb3

Browse files
Merge pull request #109 from microsoftgraph/utf-issue-fix
Utf issue fix
2 parents c138b34 + d3100e9 commit 160ccb3

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.io.IOException;
4040
import java.io.InputStream;
4141
import java.io.OutputStream;
42+
import java.io.UnsupportedEncodingException;
4243
import java.net.URL;
4344
import java.util.List;
4445
import java.util.Map;
@@ -58,6 +59,11 @@ public class DefaultHttpProvider implements IHttpProvider {
5859
* The content type for JSON responses
5960
*/
6061
static final String JSON_CONTENT_TYPE = "application/json";
62+
63+
/**
64+
* The encoding type for getBytes
65+
*/
66+
static final String JSON_ENCODING = "UTF-8";
6167

6268
/**
6369
* The serializer
@@ -254,7 +260,7 @@ private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRe
254260
} else {
255261
logger.logDebug("Sending " + serializable.getClass().getName() + " as request body");
256262
final String serializeObject = serializer.serializeObject(serializable);
257-
bytesToWrite = serializeObject.getBytes();
263+
bytesToWrite = serializeObject.getBytes(JSON_ENCODING);
258264

259265
// If the user hasn't specified a Content-Type for the request
260266
if (!hasHeader(requestHeaders, CONTENT_TYPE_HEADER_NAME)) {
@@ -341,6 +347,11 @@ private <Result, Body, DeserializeType> Result sendRequestInternal(final IHttpRe
341347
final boolean shouldLogVerbosely = logger.getLoggingLevel() == LoggerLevel.DEBUG;
342348
logger.logError("Graph service exception " + ex.getMessage(shouldLogVerbosely), ex);
343349
throw ex;
350+
} catch (final UnsupportedEncodingException ex) {
351+
final ClientException clientException = new ClientException("Unsupported encoding problem: ",
352+
ex);
353+
logger.logError("Unsupported encoding problem: " + ex.getMessage(), ex);
354+
throw clientException;
344355
} catch (final Exception ex) {
345356
final ClientException clientException = new ClientException("Error during http request",
346357
ex);
@@ -401,10 +412,10 @@ private <Result> Result handleJsonResponse(final InputStream in, Map<String, Lis
401412
* @param clazz the type of the response object
402413
* @return the JSON object
403414
*/
404-
private <Result> Result handleEmptyResponse(Map<String, List<String>> responseHeaders, final Class<Result> clazz) {
415+
private <Result> Result handleEmptyResponse(Map<String, List<String>> responseHeaders, final Class<Result> clazz)
416+
throws UnsupportedEncodingException{
405417
//Create an empty object to attach the response headers to
406-
InputStream in = new ByteArrayInputStream("{}".getBytes());
407-
418+
InputStream in = new ByteArrayInputStream("{}".getBytes(JSON_ENCODING));
408419
return handleJsonResponse(in, responseHeaders, clazz);
409420
}
410421

0 commit comments

Comments
 (0)