|
30 | 30 | import com.microsoft.graph.core.Constants; |
31 | 31 | import com.microsoft.graph.http.DefaultHttpProvider; |
32 | 32 | import com.microsoft.graph.http.GraphServiceException; |
| 33 | +import com.microsoft.graph.core.ClientException; |
33 | 34 | import com.microsoft.graph.http.HttpResponseCode; |
34 | 35 | import com.microsoft.graph.http.HttpResponseHeadersHelper; |
35 | 36 | import com.microsoft.graph.http.IConnection; |
|
48 | 49 | * @param <UploadType> the expected uploaded item |
49 | 50 | */ |
50 | 51 | public class ChunkedUploadResponseHandler<UploadType> |
51 | | - implements IStatefulResponseHandler<ChunkedUploadResult<UploadType>, UploadType> { |
52 | | - |
53 | | - private final static HttpResponseHeadersHelper responseHeadersHelper = new HttpResponseHeadersHelper(); |
54 | | - /** |
55 | | - * The expected deserialized upload type |
56 | | - */ |
57 | | - private final Class<UploadType> deserializeTypeClass; |
58 | | - |
59 | | - /** |
60 | | - * Creates a chunked upload response handler |
61 | | - * |
62 | | - * @param uploadType the expected upload item type |
63 | | - */ |
64 | | - public ChunkedUploadResponseHandler(final Class<UploadType> uploadType) { |
65 | | - this.deserializeTypeClass = uploadType; |
66 | | - } |
67 | | - |
68 | | - /** |
69 | | - * Do nothing before getting the response |
70 | | - * |
71 | | - * @param connection the connection |
72 | | - */ |
73 | | - @Override |
74 | | - public void configConnection(final IConnection connection) { |
75 | | - return; |
76 | | - } |
77 | | - |
78 | | - /** |
79 | | - * Do nothing before getting the response |
80 | | - * |
81 | | - * @param response The response |
82 | | - */ |
83 | | - @Override |
84 | | - public void configConnection(final Response response) { |
85 | | - return; |
86 | | - } |
87 | | - |
88 | | - /** |
89 | | - * Generate the chunked upload response result |
90 | | - * |
91 | | - * @param request the HTTP request |
92 | | - * @param connection the HTTP connection |
93 | | - * @param serializer the serializer |
94 | | - * @param logger the system logger |
95 | | - * @return the chunked upload result, which could be either an uploaded item or error |
96 | | - * @throws Exception an exception occurs if the request was unable to complete for any reason |
97 | | - */ |
98 | | - @Override |
99 | | - public ChunkedUploadResult<UploadType> generateResult( |
100 | | - final IHttpRequest request, |
101 | | - final IConnection connection, |
102 | | - final ISerializer serializer, |
103 | | - final ILogger logger) throws Exception { |
104 | | - InputStream in = null; |
105 | | - |
106 | | - try { |
107 | | - if (connection.getResponseCode() == HttpResponseCode.HTTP_ACCEPTED) { |
108 | | - logger.logDebug("Chunk bytes has been accepted by the server."); |
109 | | - in = new BufferedInputStream(connection.getInputStream()); |
110 | | - final UploadSession session = serializer.deserializeObject( |
111 | | - DefaultHttpProvider.streamToString(in), UploadSession.class); |
112 | | - |
113 | | - return new ChunkedUploadResult<UploadType>(session); |
114 | | - |
115 | | - } else if (connection.getResponseCode() == HttpResponseCode.HTTP_CREATED |
116 | | - || connection.getResponseCode() == HttpResponseCode.HTTP_OK) { |
117 | | - logger.logDebug("Upload session is completed, uploaded item returned."); |
118 | | - in = new BufferedInputStream(connection.getInputStream()); |
119 | | - String rawJson = DefaultHttpProvider.streamToString(in); |
120 | | - UploadType uploadedItem = serializer.deserializeObject(rawJson, |
121 | | - this.deserializeTypeClass); |
122 | | - |
123 | | - return new ChunkedUploadResult<UploadType>(uploadedItem); |
124 | | - } else if (connection.getResponseCode() >= HttpResponseCode.HTTP_CLIENT_ERROR) { |
125 | | - logger.logDebug("Receiving error during upload, see detail on result error"); |
126 | | - |
127 | | - return new ChunkedUploadResult<UploadType>( |
128 | | - GraphServiceException.createFromConnection(request, null, serializer, |
129 | | - connection, logger)); |
130 | | - } |
131 | | - } finally { |
132 | | - if (in != null) { |
133 | | - try{ |
134 | | - in.close(); |
135 | | - } catch(IOException e) { |
136 | | - logger.logError(e.getMessage(), e); |
137 | | - } |
138 | | - } |
139 | | - } |
140 | | - |
141 | | - return null; |
142 | | - } |
143 | | - |
144 | | - /** |
145 | | - * Generate the chunked upload response result |
146 | | - * |
147 | | - * @param request the HTTP request |
148 | | - * @param response the HTTP response |
149 | | - * @param serializer the serializer |
150 | | - * @param logger the system logger |
151 | | - * @return the chunked upload result, which could be either an uploaded item or error |
152 | | - * @throws Exception an exception occurs if the request was unable to complete for any reason |
153 | | - */ |
154 | | - @Override |
155 | | - public ChunkedUploadResult<UploadType> generateResult( |
156 | | - final IHttpRequest request, |
157 | | - final Response response, |
158 | | - final ISerializer serializer, |
159 | | - final ILogger logger) throws Exception { |
160 | | - InputStream in = null; |
161 | | - try { |
162 | | - if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) { |
163 | | - logger.logDebug("Receiving error during upload, see detail on result error"); |
164 | | - |
165 | | - return new ChunkedUploadResult<UploadType>( |
166 | | - GraphServiceException.createFromConnection(request, null, serializer, |
167 | | - response, logger)); |
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 | | - } |
191 | | - } finally { |
192 | | - if (in != null) { |
193 | | - try{ |
194 | | - in.close(); |
195 | | - } catch(IOException e) { |
196 | | - logger.logError(e.getMessage(), e); |
197 | | - } |
198 | | - } |
199 | | - } |
200 | | - return null; |
201 | | - } |
| 52 | + implements IStatefulResponseHandler<ChunkedUploadResult<UploadType>, UploadType> { |
| 53 | + |
| 54 | + private final static HttpResponseHeadersHelper responseHeadersHelper = new HttpResponseHeadersHelper(); |
| 55 | + /** |
| 56 | + * The expected deserialized upload type |
| 57 | + */ |
| 58 | + private final Class<UploadType> deserializeTypeClass; |
| 59 | + |
| 60 | + /** |
| 61 | + * Creates a chunked upload response handler |
| 62 | + * |
| 63 | + * @param uploadType the expected upload item type |
| 64 | + */ |
| 65 | + public ChunkedUploadResponseHandler(final Class<UploadType> uploadType) { |
| 66 | + this.deserializeTypeClass = uploadType; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Do nothing before getting the response |
| 71 | + * |
| 72 | + * @param connection the connection |
| 73 | + */ |
| 74 | + @Override |
| 75 | + public void configConnection(final IConnection connection) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Do nothing before getting the response |
| 81 | + * |
| 82 | + * @param response The response |
| 83 | + */ |
| 84 | + @Override |
| 85 | + public void configConnection(final Response response) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Generate the chunked upload response result |
| 91 | + * |
| 92 | + * @param request the HTTP request |
| 93 | + * @param connection the HTTP connection |
| 94 | + * @param serializer the serializer |
| 95 | + * @param logger the system logger |
| 96 | + * @return the chunked upload result, which could be either an uploaded item or error |
| 97 | + * @throws Exception an exception occurs if the request was unable to complete for any reason |
| 98 | + */ |
| 99 | + @Override |
| 100 | + public ChunkedUploadResult<UploadType> generateResult( |
| 101 | + final IHttpRequest request, |
| 102 | + final IConnection connection, |
| 103 | + final ISerializer serializer, |
| 104 | + final ILogger logger) throws Exception { |
| 105 | + InputStream in = null; |
| 106 | + |
| 107 | + try { |
| 108 | + if (connection.getResponseCode() == HttpResponseCode.HTTP_ACCEPTED) { |
| 109 | + logger.logDebug("Chunk bytes has been accepted by the server."); |
| 110 | + in = new BufferedInputStream(connection.getInputStream()); |
| 111 | + final UploadSession session = serializer.deserializeObject( |
| 112 | + DefaultHttpProvider.streamToString(in), UploadSession.class); |
| 113 | + |
| 114 | + return new ChunkedUploadResult<UploadType>(session); |
| 115 | + |
| 116 | + } else if (connection.getResponseCode() == HttpResponseCode.HTTP_CREATED |
| 117 | + || connection.getResponseCode() == HttpResponseCode.HTTP_OK) { |
| 118 | + logger.logDebug("Upload session is completed, uploaded item returned."); |
| 119 | + in = new BufferedInputStream(connection.getInputStream()); |
| 120 | + String rawJson = DefaultHttpProvider.streamToString(in); |
| 121 | + UploadType uploadedItem = serializer.deserializeObject(rawJson, |
| 122 | + this.deserializeTypeClass); |
| 123 | + |
| 124 | + return new ChunkedUploadResult<UploadType>(uploadedItem); |
| 125 | + } else if (connection.getResponseCode() >= HttpResponseCode.HTTP_CLIENT_ERROR) { |
| 126 | + logger.logDebug("Receiving error during upload, see detail on result error"); |
| 127 | + |
| 128 | + return new ChunkedUploadResult<UploadType>( |
| 129 | + GraphServiceException.createFromConnection(request, null, serializer, |
| 130 | + connection, logger)); |
| 131 | + } |
| 132 | + } finally { |
| 133 | + if (in != null) { |
| 134 | + try{ |
| 135 | + in.close(); |
| 136 | + } catch(IOException e) { |
| 137 | + logger.logError(e.getMessage(), e); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Generate the chunked upload response result |
| 147 | + * |
| 148 | + * @param request the HTTP request |
| 149 | + * @param response the HTTP response |
| 150 | + * @param serializer the serializer |
| 151 | + * @param logger the system logger |
| 152 | + * @return the chunked upload result, which could be either an uploaded item or error |
| 153 | + * @throws Exception an exception occurs if the request was unable to complete for any reason |
| 154 | + */ |
| 155 | + @Override |
| 156 | + public ChunkedUploadResult<UploadType> generateResult( |
| 157 | + final IHttpRequest request, |
| 158 | + final Response response, |
| 159 | + final ISerializer serializer, |
| 160 | + final ILogger logger) throws Exception { |
| 161 | + InputStream in = null; |
| 162 | + try { |
| 163 | + if (response.code() >= HttpResponseCode.HTTP_CLIENT_ERROR) { |
| 164 | + logger.logDebug("Receiving error during upload, see detail on result error"); |
| 165 | + |
| 166 | + return new ChunkedUploadResult<UploadType>( |
| 167 | + GraphServiceException.createFromConnection(request, null, serializer, |
| 168 | + response, logger)); |
| 169 | + } else if (response.code() >= HttpResponseCode.HTTP_OK |
| 170 | + && response.code() < HttpResponseCode.HTTP_MULTIPLE_CHOICES) { |
| 171 | + final Map<String, String> headers = responseHeadersHelper.getResponseHeadersAsMapStringString(response); |
| 172 | + final String contentType = headers.get(Constants.CONTENT_TYPE_HEADER_NAME); |
| 173 | + final String location = headers.get("Location"); |
| 174 | + if(contentType != null |
| 175 | + && contentType.contains(Constants.JSON_CONTENT_TYPE)) { |
| 176 | + in = new BufferedInputStream(response.body().byteStream()); |
| 177 | + final String rawJson = DefaultHttpProvider.streamToString(in); |
| 178 | + final UploadSession session = serializer.deserializeObject(rawJson, UploadSession.class); |
| 179 | + if(session == null) { |
| 180 | + logger.logDebug("Upload session is completed (ODSP), uploaded item returned."); |
| 181 | + final UploadType uploadedItem = serializer.deserializeObject(rawJson, this.deserializeTypeClass); |
| 182 | + return new ChunkedUploadResult<UploadType>(uploadedItem); |
| 183 | + } else { |
| 184 | + logger.logDebug("Chunk bytes has been accepted by the server."); |
| 185 | + return new ChunkedUploadResult<UploadType>(session); |
| 186 | + } |
| 187 | + } else if(location != null) { |
| 188 | + logger.logDebug("Upload session is completed (Outlook), uploaded item returned."); |
| 189 | + return new ChunkedUploadResult<UploadType>(this.deserializeTypeClass.getDeclaredConstructor().newInstance()); |
| 190 | + } else { |
| 191 | + logger.logDebug("Upload session returned an unexpected response"); |
| 192 | + } |
| 193 | + } |
| 194 | + } finally { |
| 195 | + if (in != null) { |
| 196 | + try{ |
| 197 | + in.close(); |
| 198 | + } catch(IOException e) { |
| 199 | + logger.logError(e.getMessage(), e); |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + return new ChunkedUploadResult<UploadType>(new ClientException("Received an unexpected response from the service, response code: " + response.code(), null)); |
| 204 | + } |
202 | 205 | } |
0 commit comments