|
12 | 12 | import java.net.ProtocolException; |
13 | 13 | import java.net.URL; |
14 | 14 |
|
15 | | -import com.sparkpost.exception.SparkPostErrorServerResponseException; |
16 | 15 | import org.apache.commons.codec.binary.Base64; |
17 | 16 | import org.apache.commons.lang3.StringUtils; |
18 | 17 | import org.apache.log4j.Logger; |
19 | 18 |
|
20 | 19 | import com.sparkpost.Build; |
21 | 20 | import com.sparkpost.Client; |
22 | | -import com.sparkpost.exception.SparkPostException; |
23 | | -import com.sparkpost.exception.SparkPostIllegalServerResponseException; |
24 | 21 | import com.sparkpost.exception.SparkPostAccessForbiddenException; |
25 | 22 | import com.sparkpost.exception.SparkPostAuthorizationFailedException; |
| 23 | +import com.sparkpost.exception.SparkPostErrorServerResponseException; |
| 24 | +import com.sparkpost.exception.SparkPostException; |
| 25 | +import com.sparkpost.exception.SparkPostIllegalServerResponseException; |
26 | 26 | import com.sparkpost.model.responses.Response; |
27 | 27 |
|
28 | 28 | /** |
@@ -236,6 +236,14 @@ private void sendRequest(HttpURLConnection conn, String data, Response response) |
236 | 236 | // Read response body from server |
237 | 237 | private Response receiveResponse(HttpURLConnection conn, Response response) throws SparkPostException { |
238 | 238 |
|
| 239 | + try { |
| 240 | + if (conn.getResponseCode() == 204 || conn.getContentLength() == 0) { |
| 241 | + return receiveEmptyResponse(conn, response); |
| 242 | + } |
| 243 | + } catch (IOException e) { |
| 244 | + throw new SparkPostIllegalServerResponseException("Unexpected error (" + e.getMessage() + ")"); |
| 245 | + } |
| 246 | + |
239 | 247 | if (!conn.getContentType().toLowerCase().startsWith("application/json")) { |
240 | 248 | throw new SparkPostIllegalServerResponseException("Unexpected content type (" + conn.getContentType() + ") from " + conn.getURL()); |
241 | 249 | } |
@@ -263,33 +271,45 @@ private Response receiveResponse(HttpURLConnection conn, Response response) thro |
263 | 271 | response.setResponseBody(""); |
264 | 272 | } catch (IOException ex) { |
265 | 273 | String line = ""; |
266 | | - try (BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getErrorStream(), DEFAULT_CHARSET))) { |
267 | 274 |
|
268 | | - while ((line = rd.readLine()) != null) { |
269 | | - sb.append(line); |
270 | | - } |
| 275 | + try { |
271 | 276 |
|
272 | | - response.setResponseBody(sb.toString()); |
273 | | - response.setRequestId(conn.getHeaderField("X-SparkPost-Request-Id")); |
| 277 | + try (BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getErrorStream(), DEFAULT_CHARSET))) { |
| 278 | + |
| 279 | + while ((line = rd.readLine()) != null) { |
| 280 | + sb.append(line); |
| 281 | + } |
| 282 | + |
| 283 | + response.setResponseBody(sb.toString()); |
| 284 | + response.setRequestId(conn.getHeaderField("X-SparkPost-Request-Id")); |
274 | 285 |
|
275 | | - logger.error("Server Response:\n" + sb.toString() + "\n"); |
| 286 | + logger.error("Server Response:\n" + sb.toString() + "\n"); |
276 | 287 |
|
277 | | - } catch (IOException ex2) { |
278 | | - // Ignore we are going to throw an exception anyway |
| 288 | + } catch (IOException ex2) { |
| 289 | + // Ignore we are going to throw an exception anyway |
| 290 | + } |
| 291 | + } catch (Exception e) { |
| 292 | + // Log but ignore we are going to throw an exception anyway |
| 293 | + logger.error("Error while handlign an HTTP response error. Ignoring and will use orginal exception", e); |
279 | 294 | } |
| 295 | + |
280 | 296 | if (logger.isDebugEnabled()) { |
281 | 297 | logger.error("Server Response:" + response); |
282 | 298 | } |
283 | 299 |
|
284 | 300 | throw new SparkPostErrorServerResponseException( |
285 | 301 | "Error reading server response: " + ex.toString() + ": " + sb.toString() + "(" + response.getResponseMessage() + ")", |
286 | | - response.getResponseCode() |
287 | | - ); |
| 302 | + response.getResponseCode()); |
288 | 303 | } |
289 | 304 | return response; |
290 | 305 |
|
291 | 306 | } |
292 | 307 |
|
| 308 | + private Response receiveEmptyResponse(HttpURLConnection conn, Response response) throws SparkPostException { |
| 309 | + response.setRequestId(conn.getHeaderField("X-SparkPost-Request-Id")); |
| 310 | + return response; |
| 311 | + } |
| 312 | + |
293 | 313 | // This method actually performs an HTTP request. |
294 | 314 | // It is called by get(), put(), post() and delete() below |
295 | 315 | private Response doHttpMethod(String path, Method method, String data, Response response) throws SparkPostException { |
|
0 commit comments