Skip to content

Commit 1f3e2fd

Browse files
authored
Merge pull request #470 from microsoftgraph/bugfix/odsp-json-file-download
- fixes a bug where downloading json files from onedrive would cause a cast exception
2 parents 3d4a4bb + d32a129 commit 1f3e2fd

File tree

2 files changed

+72
-29
lines changed

2 files changed

+72
-29
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,20 +419,20 @@ public Request authenticateRequest(Request request) {
419419

420420
final Map<String, String> headers = CoreHttpProvider.getResponseHeadersAsMapStringString(response);
421421

422+
if(response.body() == null || response.body().contentLength() == 0)
423+
return (Result) null;
424+
422425
final String contentType = headers.get(Constants.CONTENT_TYPE_HEADER_NAME);
423-
if (contentType != null && contentType.contains(Constants.JSON_CONTENT_TYPE)) {
426+
if (contentType != null && resultClass != InputStream.class &&
427+
contentType.contains(Constants.JSON_CONTENT_TYPE)) {
424428
logger.logDebug("Response json");
425429
return handleJsonResponse(in, CoreHttpProvider.getResponseHeadersAsMapOfStringList(response), resultClass);
426-
} else {
430+
} else if (resultClass == InputStream.class) {
427431
logger.logDebug("Response binary");
428432
isBinaryStreamInput = true;
429-
if (resultClass == InputStream.class) {
430-
return (Result) handleBinaryStream(in);
431-
} else if(response.body() != null && response.body().contentLength() > 0) { // some services reply in text/plain with a JSON representation...
432-
return handleJsonResponse(in, CoreHttpProvider.getResponseHeadersAsMapOfStringList(response), resultClass);
433-
} else {
434-
return (Result) null;
435-
}
433+
return (Result) handleBinaryStream(in);
434+
} else {
435+
return (Result) null;
436436
}
437437
} finally {
438438
if (!isBinaryStreamInput) {

src/test/java/com/microsoft/graph/functional/OneDriveTests.java

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
package com.microsoft.graph.functional;
22

33
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
45

6+
import java.io.ByteArrayInputStream;
57
import java.io.IOException;
68
import java.io.InputStream;
9+
import java.nio.charset.StandardCharsets;
710

811
import org.junit.Assert;
912
import org.junit.Before;
1013
import org.junit.Ignore;
1114
import org.junit.Test;
1215

16+
import com.google.gson.JsonPrimitive;
1317
import com.microsoft.graph.concurrency.ChunkedUploadProvider;
1418
import com.microsoft.graph.concurrency.IProgressCallback;
1519
import com.microsoft.graph.core.ClientException;
20+
import com.microsoft.graph.http.CoreHttpProvider;
1621
import com.microsoft.graph.models.extensions.DriveItem;
1722
import com.microsoft.graph.models.extensions.DriveItemUploadableProperties;
1823
import com.microsoft.graph.models.extensions.UploadSession;
@@ -25,7 +30,25 @@ public class OneDriveTests {
2530
public void setUp() {
2631
testBase = new TestBase();
2732
}
28-
33+
34+
IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem> () {
35+
@Override
36+
public void progress(final long current, final long max) {
37+
//Check progress
38+
}
39+
@Override
40+
public void success(final DriveItem result) {
41+
//Handle the successful response
42+
String finishedItemId = result.id;
43+
Assert.assertNotNull(finishedItemId);
44+
}
45+
46+
@Override
47+
public void failure(final ClientException ex) {
48+
//Handle the failed upload
49+
Assert.fail("Upload session failed");
50+
}
51+
};
2952
/**
3053
* Test large file upload.
3154
* https://github.com/OneDrive/onedrive-sdk-csharp/blob/master/docs/chunked-uploads.md
@@ -41,25 +64,6 @@ public void testLargeFileUpload() throws IOException, InterruptedException {
4164
InputStream uploadFile = OneDriveTests.class.getClassLoader().getResourceAsStream("hamilton.jpg");
4265
long fileSize = (long) uploadFile.available();
4366

44-
IProgressCallback<DriveItem> callback = new IProgressCallback<DriveItem> () {
45-
@Override
46-
public void progress(final long current, final long max) {
47-
//Check progress
48-
}
49-
@Override
50-
public void success(final DriveItem result) {
51-
//Handle the successful response
52-
String finishedItemId = result.id;
53-
Assert.assertNotNull(finishedItemId);
54-
}
55-
56-
@Override
57-
public void failure(final ClientException ex) {
58-
//Handle the failed upload
59-
Assert.fail("Upload session failed");
60-
}
61-
};
62-
6367
UploadSession uploadSession = testBase
6468
.graphClient
6569
.me()
@@ -85,4 +89,43 @@ public void testDownloadWithCustomRequest() throws IOException {
8589
assertFalse("stream should not be empty", stream.read() == -1);
8690
}
8791
}
92+
@Test
93+
public void downloadJsonFileFromOneDrive() throws Exception {
94+
final DriveItemUploadableProperties item = new DriveItemUploadableProperties();
95+
item.name = "test.json";
96+
item.additionalDataManager().put("@microsoft.graph.conflictBehavior", new JsonPrimitive("replace"));
97+
98+
final InputStream uploadFile = new ByteArrayInputStream("{\"hehe\":\"haha\"}".getBytes(StandardCharsets.UTF_8));
99+
100+
final long fileSize = (long) uploadFile.available();
101+
102+
final UploadSession session = testBase.graphClient.me()
103+
.drive()
104+
.root()
105+
.itemWithPath(item.name)
106+
.createUploadSession(item)
107+
.buildRequest()
108+
.post();
109+
110+
ChunkedUploadProvider<DriveItem> chunkedUploadProvider = new ChunkedUploadProvider<DriveItem>(
111+
session,
112+
testBase.graphClient,
113+
uploadFile,
114+
fileSize,
115+
DriveItem.class);
116+
117+
chunkedUploadProvider.upload(callback);
118+
119+
final InputStream stream = testBase.graphClient.me()
120+
.drive()
121+
.root()
122+
.itemWithPath(item.name)
123+
.content()
124+
.buildRequest()
125+
.get();
126+
127+
final String fileContent = CoreHttpProvider.streamToString(stream);
128+
129+
assertTrue(fileContent.length() > 0);
130+
}
88131
}

0 commit comments

Comments
 (0)