|
22 | 22 |
|
23 | 23 | package com.microsoft.graph.concurrency; |
24 | 24 |
|
25 | | -import java.io.BufferedInputStream; |
26 | | -import java.io.InputStream; |
27 | | -import java.util.Map; |
28 | | - |
29 | | -import javax.annotation.Nullable; |
30 | | -import javax.annotation.Nonnull; |
31 | | - |
32 | | -import com.microsoft.graph.http.CoreHttpProvider; |
| 25 | +import com.microsoft.graph.core.ClientException; |
33 | 26 | import com.microsoft.graph.core.Constants; |
34 | 27 | import com.microsoft.graph.http.GraphServiceException; |
35 | | -import com.microsoft.graph.core.ClientException; |
36 | 28 | import com.microsoft.graph.http.HttpResponseCode; |
37 | 29 | import com.microsoft.graph.http.HttpResponseHeadersHelper; |
38 | 30 | import com.microsoft.graph.http.IHttpRequest; |
39 | 31 | import com.microsoft.graph.http.IStatefulResponseHandler; |
40 | 32 | import com.microsoft.graph.logger.ILogger; |
41 | 33 | import com.microsoft.graph.serializer.ISerializer; |
42 | 34 |
|
| 35 | +import java.io.BufferedInputStream; |
| 36 | +import java.io.InputStream; |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +import javax.annotation.Nonnull; |
| 40 | +import javax.annotation.Nullable; |
| 41 | + |
43 | 42 | import okhttp3.Response; |
44 | 43 |
|
| 44 | +import static com.microsoft.graph.http.HttpResponseCode.HTTP_CREATED; |
| 45 | +import static com.microsoft.graph.http.HttpResponseCode.HTTP_OK; |
| 46 | + |
45 | 47 | /** |
46 | 48 | * Handles the stateful response from the OneDrive upload session |
47 | 49 | * |
@@ -98,35 +100,38 @@ public ChunkedUploadResult<UploadType> generateResult( |
98 | 100 | if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) { |
99 | 101 | logger.logDebug("Receiving error during upload, see detail on result error"); |
100 | 102 |
|
101 | | - return new ChunkedUploadResult<UploadType>( |
| 103 | + return new ChunkedUploadResult<>( |
102 | 104 | GraphServiceException.createFromResponse(request, null, serializer, |
103 | | - response, logger)); |
104 | | - } else if (response.code() >= HttpResponseCode.HTTP_OK |
| 105 | + response, logger)); |
| 106 | + } else if (response.code() >= HTTP_OK |
105 | 107 | && response.code() < HttpResponseCode.HTTP_MULTIPLE_CHOICES) { |
106 | 108 | final Map<String, String> headers = responseHeadersHelper.getResponseHeadersAsMapStringString(response); |
107 | 109 | final String contentType = headers.get(Constants.CONTENT_TYPE_HEADER_NAME); |
108 | 110 | final String location = headers.get("Location"); |
109 | | - if(contentType != null |
110 | | - && contentType.contains(Constants.JSON_CONTENT_TYPE)) { |
| 111 | + if (contentType != null |
| 112 | + && contentType.contains(Constants.JSON_CONTENT_TYPE)) { |
111 | 113 | try (final InputStream in = new BufferedInputStream(response.body().byteStream())) { |
112 | | - final String rawJson = CoreHttpProvider.streamToString(in); |
113 | | - final IUploadSession session = serializer.deserializeObject(rawJson, uploadSessionClass); |
114 | | - if(session == null || session.getNextExpectedRanges() == null) { |
| 114 | + if (isUploadSessionCompleted(response)) { |
115 | 115 | logger.logDebug("Upload session is completed (ODSP), uploaded item returned."); |
116 | | - final UploadType uploadedItem = serializer.deserializeObject(rawJson, this.deserializeTypeClass); |
117 | | - return new ChunkedUploadResult<UploadType>(uploadedItem); |
| 116 | + final UploadType uploadedItem = serializer.deserializeObject(in, this.deserializeTypeClass); |
| 117 | + return new ChunkedUploadResult<>(uploadedItem); |
118 | 118 | } else { |
| 119 | + final IUploadSession session = serializer.deserializeObject(in, uploadSessionClass); |
119 | 120 | logger.logDebug("Chunk bytes has been accepted by the server."); |
120 | | - return new ChunkedUploadResult<UploadType>(session); |
| 121 | + return new ChunkedUploadResult<>(session); |
121 | 122 | } |
122 | 123 | } |
123 | | - } else if(location != null) { |
| 124 | + } else if (location != null) { |
124 | 125 | logger.logDebug("Upload session is completed (Outlook), uploaded item returned."); |
125 | | - return new ChunkedUploadResult<UploadType>(this.deserializeTypeClass.getDeclaredConstructor().newInstance()); |
| 126 | + return new ChunkedUploadResult<>(this.deserializeTypeClass.getDeclaredConstructor().newInstance()); |
126 | 127 | } else { |
127 | 128 | logger.logDebug("Upload session returned an unexpected response"); |
128 | 129 | } |
129 | 130 | } |
130 | | - return new ChunkedUploadResult<UploadType>(new ClientException("Received an unexpected response from the service, response code: " + response.code(), null)); |
| 131 | + return new ChunkedUploadResult<>(new ClientException("Received an unexpected response from the service, response code: " + response.code(), null)); |
| 132 | + } |
| 133 | + |
| 134 | + private boolean isUploadSessionCompleted(Response response) { |
| 135 | + return response.code() == HTTP_OK || response.code() == HTTP_CREATED; |
131 | 136 | } |
132 | 137 | } |
0 commit comments