|
25 | 25 | import java.io.BufferedInputStream; |
26 | 26 | import java.io.IOException; |
27 | 27 | import java.io.InputStream; |
| 28 | +import java.util.Map; |
28 | 29 |
|
| 30 | +import com.microsoft.graph.core.Constants; |
29 | 31 | import com.microsoft.graph.http.DefaultHttpProvider; |
30 | 32 | import com.microsoft.graph.http.GraphServiceException; |
31 | 33 | import com.microsoft.graph.http.HttpResponseCode; |
| 34 | +import com.microsoft.graph.http.HttpResponseHeadersHelper; |
32 | 35 | import com.microsoft.graph.http.IConnection; |
33 | 36 | import com.microsoft.graph.http.IHttpRequest; |
34 | 37 | import com.microsoft.graph.http.IStatefulResponseHandler; |
|
46 | 49 | */ |
47 | 50 | public class ChunkedUploadResponseHandler<UploadType> |
48 | 51 | implements IStatefulResponseHandler<ChunkedUploadResult<UploadType>, UploadType> { |
| 52 | + |
| 53 | + private final static HttpResponseHeadersHelper responseHeadersHelper = new HttpResponseHeadersHelper(); |
49 | 54 | /** |
50 | 55 | * The expected deserialized upload type |
51 | 56 | */ |
@@ -154,30 +159,35 @@ public ChunkedUploadResult<UploadType> generateResult( |
154 | 159 | final ILogger logger) throws Exception { |
155 | 160 | InputStream in = null; |
156 | 161 | try { |
157 | | - if (response.code() == HttpResponseCode.HTTP_ACCEPTED) { |
158 | | - logger.logDebug("Chunk bytes has been accepted by the server."); |
159 | | - in = new BufferedInputStream(response.body().byteStream()); |
160 | | - final UploadSession session = serializer.deserializeObject( |
161 | | - DefaultHttpProvider.streamToString(in), UploadSession.class); |
162 | | - |
163 | | - return new ChunkedUploadResult<UploadType>(session); |
164 | | - |
165 | | - } else if (response.code() == HttpResponseCode.HTTP_CREATED |
166 | | - || response.code() == HttpResponseCode.HTTP_OK) { |
167 | | - logger.logDebug("Upload session is completed, uploaded item returned."); |
168 | | - in = new BufferedInputStream(response.body().byteStream()); |
169 | | - final String rawJson = DefaultHttpProvider.streamToString(in); |
170 | | - final UploadType uploadedItem = serializer.deserializeObject(rawJson, |
171 | | - this.deserializeTypeClass); |
172 | | - |
173 | | - return new ChunkedUploadResult<UploadType>(uploadedItem); |
174 | | - } else if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) { |
| 162 | + if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) { |
175 | 163 | logger.logDebug("Receiving error during upload, see detail on result error"); |
176 | 164 |
|
177 | 165 | return new ChunkedUploadResult<UploadType>( |
178 | 166 | GraphServiceException.createFromConnection(request, null, serializer, |
179 | 167 | response, logger)); |
180 | | - } |
| 168 | + } else if (response.code() >= HttpResponseCode.HTTP_OK |
| 169 | + && response.code() < HttpResponseCode.HTTP_MULTIPLE_CHOICES) { |
| 170 | + final Map<String, String> headers = responseHeadersHelper.getResponseHeadersAsMapStringString(response); |
| 171 | + final String contentType = headers.get(Constants.CONTENT_TYPE_HEADER_NAME); |
| 172 | + final String location = headers.get("Location"); |
| 173 | + if(contentType != null |
| 174 | + && contentType.contains(Constants.JSON_CONTENT_TYPE)) { |
| 175 | + in = new BufferedInputStream(response.body().byteStream()); |
| 176 | + final String rawJson = DefaultHttpProvider.streamToString(in); |
| 177 | + final UploadSession session = serializer.deserializeObject(rawJson, UploadSession.class); |
| 178 | + if(session == null) { |
| 179 | + logger.logDebug("Upload session is completed (ODSP), uploaded item returned."); |
| 180 | + final UploadType uploadedItem = serializer.deserializeObject(rawJson, this.deserializeTypeClass); |
| 181 | + return new ChunkedUploadResult<UploadType>(uploadedItem); |
| 182 | + } else { |
| 183 | + logger.logDebug("Chunk bytes has been accepted by the server."); |
| 184 | + return new ChunkedUploadResult<UploadType>(session); |
| 185 | + } |
| 186 | + } else if(location != null) { |
| 187 | + logger.logDebug("Upload session is completed (Outlook), uploaded item returned."); |
| 188 | + return new ChunkedUploadResult<UploadType>(this.deserializeTypeClass.getDeclaredConstructor().newInstance()); |
| 189 | + } |
| 190 | + } |
181 | 191 | } finally { |
182 | 192 | if (in != null) { |
183 | 193 | try{ |
|
0 commit comments