ResponseHelper { public static void throwIfNotSuccessful(@NonNull Response response) { synchronized (response) { if (Status.Family.SUCCESSFUL.equals(response.getStatusInfo().getFamily())) { return; } if (response.getStatus() == Status.NOT_MODIFIED.getStatusCode()) { return; } String opcRequestId = response.getHeaderString(OPC_REQUEST_ID_HEADER); if (!typeEqual(MediaType.APPLICATION_JSON_TYPE, response.getMediaType())) { String responseBody = "Cannot read response body!"; try { responseBody = response.readEntity(String.class); } catch (ProcessingException e) { LOG.warn("Unable to read response body", e); } try { throw new BmcException( response.getStatus(), "Unknown", String.format( "Unexpected Content-Type: %s instead of %s. Response body: %s", response.getMediaType(), MediaType.APPLICATION_JSON_TYPE, responseBody), opcRequestId); } finally { closeResponseSilently(response); } } boolean isBuffered = false; try { if (response.getLength() < MAX_RESPONSE_BUFFER_BYTES) { try { isBuffered = response.bufferEntity(); } catch (IllegalStateException e) { LOG.info( "Unable to buffer response entity as the response has already been closed", e); } } final ErrorCodeAndMessage errorCodeAndMessage = response.readEntity(ErrorCodeAndMessage.class); if (errorCodeAndMessage == null) { String message = DEFAULT_ERROR_MESSAGES.get(response.getStatus()); throw new BmcException( response.getStatus(), "Unknown", message != null ? message : "Detailed exception information not available", opcRequestId); } else { throw new BmcException( response.getStatus(), errorCodeAndMessage.getCode(), errorCodeAndMessage.getMessage(), opcRequestId); } } catch (ProcessingException e) { String message = isBuffered ? "Unable to parse error response: " + response.readEntity(String.class) : "Unable to parse error response."; int status = response.getStatus(); closeResponseSilently(response); throw new BmcException(status, "Unknown", message, opcRequestId, e); } } }  static void throwIfNotSuccessful(@NonNull Response response); static void readWithoutEntity(@NonNull final Response response); static T readEntity(@NonNull final Response response, GenericType<T> entityType); static T readEntity(@NonNull final Response response, Class<T> entityType); static void closeResponseSilently(final Response response); static void closeResponseSilentlyIfNotBuffered(@NonNull final Response response);  }
@Test public void test_throwIfNotSuccessful_InvalidHTMLResponse() { final Response htmlResponse = buildMockResponse(OPC_REQUEST_ID, HTML_MEDIA_TYPE, BAD_GATEWAY_STATUS); try { ResponseHelper.throwIfNotSuccessful(htmlResponse); fail("Should have thrown"); } catch (BmcException exception) { validateExceptionFields( exception, OPC_REQUEST_ID, BAD_GATEWAY_STATUS, "Unknown", "Unexpected Content-Type: " + HTML_MEDIA_TYPE); } }
@Test public void test_throwIfNotSuccessful_ValidJsonResponse() { final Response jsonResponse = buildMockResponse(OPC_REQUEST_ID, JSON_MEDIA_TYPE, Response.Status.OK); ResponseHelper.throwIfNotSuccessful(jsonResponse); }