|
25 | 25 | import com.google.common.io.ByteStreams; |
26 | 26 |
|
27 | 27 | import com.microsoft.graph.core.ClientException; |
28 | | -import com.microsoft.graph.core.Constants; |
29 | 28 | import com.microsoft.graph.http.GraphServiceException; |
30 | 29 | import com.microsoft.graph.http.HttpResponseCode; |
31 | | -import com.microsoft.graph.http.HttpResponseHeadersHelper; |
32 | 30 | import com.microsoft.graph.http.IHttpRequest; |
33 | 31 | import com.microsoft.graph.http.IStatefulResponseHandler; |
34 | 32 | import com.microsoft.graph.logger.ILogger; |
|
37 | 35 | import java.io.ByteArrayInputStream; |
38 | 36 | import java.io.IOException; |
39 | 37 | import java.io.InputStream; |
40 | | -import java.util.Map; |
41 | 38 |
|
42 | 39 | import javax.annotation.Nonnull; |
43 | 40 | import javax.annotation.Nullable; |
44 | 41 |
|
45 | 42 | import okhttp3.Response; |
| 43 | +import okhttp3.ResponseBody; |
46 | 44 |
|
47 | 45 | import static com.microsoft.graph.http.HttpResponseCode.HTTP_OK; |
48 | 46 |
|
|
53 | 51 | */ |
54 | 52 | public class ChunkedUploadResponseHandler<UploadType> |
55 | 53 | implements IStatefulResponseHandler<ChunkedUploadResult<UploadType>, UploadType> { |
56 | | - |
57 | | - private final static HttpResponseHeadersHelper responseHeadersHelper = new HttpResponseHeadersHelper(); |
58 | 54 | /** |
59 | 55 | * The expected deserialized upload type |
60 | 56 | */ |
@@ -107,25 +103,24 @@ public ChunkedUploadResult<UploadType> generateResult( |
107 | 103 | response, logger)); |
108 | 104 | } else if (response.code() >= HTTP_OK |
109 | 105 | && response.code() < HttpResponseCode.HTTP_MULTIPLE_CHOICES) { |
110 | | - final Map<String, String> headers = responseHeadersHelper.getResponseHeadersAsMapStringString(response); |
111 | | - final String contentType = headers.get(Constants.CONTENT_TYPE_HEADER_NAME); |
112 | | - final String location = headers.get("Location"); |
113 | | - if (contentType != null |
114 | | - && contentType.contains(Constants.JSON_CONTENT_TYPE)) { |
115 | | - return parseJsonUploadResult(response, serializer, logger); |
116 | | - } else if (location != null) { |
117 | | - logger.logDebug("Upload session is completed (Outlook), uploaded item returned."); |
118 | | - return new ChunkedUploadResult<>(this.deserializeTypeClass.getDeclaredConstructor().newInstance()); |
119 | | - } else { |
120 | | - logger.logDebug("Upload session returned an unexpected response"); |
121 | | - } |
| 106 | + try(final ResponseBody body = response.body()) { |
| 107 | + final String location = response.headers().get("Location"); |
| 108 | + if (body.contentType() != null && body.contentType().subtype().contains("json")) { |
| 109 | + return parseJsonUploadResult(body, serializer, logger); |
| 110 | + } else if (location != null) { |
| 111 | + logger.logDebug("Upload session is completed (Outlook), uploaded item returned."); |
| 112 | + return new ChunkedUploadResult<>(this.deserializeTypeClass.getDeclaredConstructor().newInstance()); |
| 113 | + } else { |
| 114 | + logger.logDebug("Upload session returned an unexpected response"); |
| 115 | + } |
| 116 | + } |
122 | 117 | } |
123 | 118 | return new ChunkedUploadResult<>(new ClientException("Received an unexpected response from the service, response code: " + response.code(), null)); |
124 | 119 | } |
125 | 120 |
|
126 | 121 | @Nonnull |
127 | | - private ChunkedUploadResult<UploadType> parseJsonUploadResult(@Nonnull Response response, @Nonnull ISerializer serializer, @Nonnull ILogger logger) throws IOException { |
128 | | - try (final InputStream in = response.body().byteStream()) { |
| 122 | + private ChunkedUploadResult<UploadType> parseJsonUploadResult(@Nonnull final ResponseBody responseBody, @Nonnull final ISerializer serializer, @Nonnull final ILogger logger) throws IOException { |
| 123 | + try (final InputStream in = responseBody.byteStream()) { |
129 | 124 | final byte[] responseBytes = ByteStreams.toByteArray(in); |
130 | 125 | final IUploadSession session = serializer.deserializeObject(new ByteArrayInputStream(responseBytes), uploadSessionClass); |
131 | 126 |
|
|
0 commit comments