Skip to content

Commit 3b1681e

Browse files
ramsessanchezNdiritu
authored andcommitted
add 'canBeParsed' check
1 parent f328241 commit 3b1681e

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

src/main/java/com/microsoft/graph/core/requests/upload/UploadResponseHandler.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,27 +64,39 @@ public <T extends Parsable> UploadResult<T> handleResponse(@Nonnull final Respon
6464
throw new ApiException(ErrorConstants.Messages.NO_RESPONSE_FOR_UPLOAD);
6565
}
6666
try(final InputStream in = body.byteStream()){
67-
final String contentType = body.contentType().toString().split(";")[0]; //contentType.toString() returns in format <mediaType>;<charset>, we only want the mediaType.
6867
if(!response.isSuccessful()) {
6968
throw new ApiExceptionBuilder()
7069
.withMessage(ErrorConstants.Codes.GENERAL_EXCEPTION)
7170
.withResponseStatusCode(response.code())
7271
.withResponseHeaders(HeadersCompatibility.getResponseHeaders(response.headers()))
7372
.build();
7473
}
74+
boolean canBeParsed = ((!Objects.isNull(body.contentType())) && (body.contentLength() > 0));
75+
String contentType = canBeParsed ? body.contentType().toString().split(";")[0] : null; //contentType.toString() returns in format <mediaType>;<charset>, we only want the mediaType.
7576
if (response.code() == HttpURLConnection.HTTP_CREATED) {
76-
if (body.contentLength() > 0) {
77+
if(canBeParsed) {
7778
final ParseNode uploadTypeParseNode = parseNodeFactory.getParseNode(contentType, in);
7879
uploadResult.itemResponse = uploadTypeParseNode.getObjectValue(factory);
7980
}
81+
final String location = response.headers().get("location");
82+
if(!Objects.isNull(location) && !location.isEmpty()) {
83+
uploadResult.location = new URI(location);
84+
}
8085
} else {
81-
final ParseNode parseNode = parseNodeFactory.getParseNode(contentType, in);
82-
final UploadSession uploadSession = parseNode.getObjectValue(UploadSession::createFromDiscriminatorValue);
83-
final List<String> nextExpectedRanges = uploadSession.getNextExpectedRanges();
84-
if (!(nextExpectedRanges == null || nextExpectedRanges.isEmpty())) {
85-
uploadResult.uploadSession = uploadSession;
86+
if(canBeParsed) {
87+
final ParseNode parseNode = parseNodeFactory.getParseNode(contentType, in);
88+
final UploadSession uploadSession = parseNode.getObjectValue(UploadSession::createFromDiscriminatorValue);
89+
final List<String> nextExpectedRanges = uploadSession.getNextExpectedRanges();
90+
if (!(nextExpectedRanges == null || nextExpectedRanges.isEmpty())) {
91+
uploadResult.uploadSession = uploadSession;
92+
} else {
93+
uploadResult.itemResponse = parseNode.getObjectValue(factory);
94+
}
8695
} else {
87-
uploadResult.itemResponse = parseNode.getObjectValue(factory);
96+
final String location = response.headers().get("location");
97+
if (!Objects.isNull(location) && !location.isEmpty()) {
98+
uploadResult.location = new URI(location);
99+
}
88100
}
89101
}
90102
return uploadResult;
@@ -94,6 +106,7 @@ public <T extends Parsable> UploadResult<T> handleResponse(@Nonnull final Respon
94106
throw new RuntimeException(ex);
95107
}
96108
}
109+
97110
}
98111

99112

0 commit comments

Comments
 (0)