@Test public void testJsonReadException() throws IOException { InputStream inputStream = mock(InputStream.class); IOException expectedException = new IOException("test exception"); when(inputStream.read()).thenThrow(expectedException); when(inputStream.read(any(byte[].class))).thenThrow(expectedException); when(inputStream.read(any(byte[].class), anyInt(), anyInt())).thenThrow(expectedException); try { handler.handle(null, mockResponse() .contentType(MEDIA_TYPE_SMILE) .body(inputStream) .build()); fail("expected exception"); } catch (RuntimeException e) { assertEquals(e.getMessage(), "Error reading SMILE response from server"); assertSame(e.getCause(), expectedException); } }
@Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { throw new UnexpectedResponseException( String.format("Expected response code to be %s, but was %d: %s", successfulResponseCodes, response.getStatusCode(), response.getStatusMessage()), request, response); } String contentType = response.getHeader(CONTENT_TYPE); if (contentType == null) { throw new UnexpectedResponseException("Content-Type is not set for response", request, response); } if (!MediaType.parse(contentType).is(MEDIA_TYPE_SMILE)) { throw new UnexpectedResponseException("Expected application/x-jackson-smile response from server but got " + contentType, request, response); } try { JsonParser jsonParser = new SmileFactory().createParser(response.getInputStream()); ObjectMapper objectMapper = OBJECT_MAPPER_SUPPLIER.get(); jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); return objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(jsonCodec.getType())); } catch (InvalidFormatException e) { throw new IllegalArgumentException("Unable to create " + jsonCodec.getType() + " from SMILE response", e); } catch (IOException e) { throw new RuntimeException("Error reading SMILE response from server", e); } }
SmileResponseHandler implements ResponseHandler<T, RuntimeException> { @Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { throw new UnexpectedResponseException( String.format("Expected response code to be %s, but was %d: %s", successfulResponseCodes, response.getStatusCode(), response.getStatusMessage()), request, response); } String contentType = response.getHeader(CONTENT_TYPE); if (contentType == null) { throw new UnexpectedResponseException("Content-Type is not set for response", request, response); } if (!MediaType.parse(contentType).is(MEDIA_TYPE_SMILE)) { throw new UnexpectedResponseException("Expected application/x-jackson-smile response from server but got " + contentType, request, response); } try { JsonParser jsonParser = new SmileFactory().createParser(response.getInputStream()); ObjectMapper objectMapper = OBJECT_MAPPER_SUPPLIER.get(); jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); return objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(jsonCodec.getType())); } catch (InvalidFormatException e) { throw new IllegalArgumentException("Unable to create " + jsonCodec.getType() + " from SMILE response", e); } catch (IOException e) { throw new RuntimeException("Error reading SMILE response from server", e); } } }
SmileResponseHandler implements ResponseHandler<T, RuntimeException> { @Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { throw new UnexpectedResponseException( String.format("Expected response code to be %s, but was %d: %s", successfulResponseCodes, response.getStatusCode(), response.getStatusMessage()), request, response); } String contentType = response.getHeader(CONTENT_TYPE); if (contentType == null) { throw new UnexpectedResponseException("Content-Type is not set for response", request, response); } if (!MediaType.parse(contentType).is(MEDIA_TYPE_SMILE)) { throw new UnexpectedResponseException("Expected application/x-jackson-smile response from server but got " + contentType, request, response); } try { JsonParser jsonParser = new SmileFactory().createParser(response.getInputStream()); ObjectMapper objectMapper = OBJECT_MAPPER_SUPPLIER.get(); jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); return objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(jsonCodec.getType())); } catch (InvalidFormatException e) { throw new IllegalArgumentException("Unable to create " + jsonCodec.getType() + " from SMILE response", e); } catch (IOException e) { throw new RuntimeException("Error reading SMILE response from server", e); } } private  SmileResponseHandler(JsonCodec<T> jsonCodec); private  SmileResponseHandler(JsonCodec<T> jsonCodec, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); }
SmileResponseHandler implements ResponseHandler<T, RuntimeException> { @Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { throw new UnexpectedResponseException( String.format("Expected response code to be %s, but was %d: %s", successfulResponseCodes, response.getStatusCode(), response.getStatusMessage()), request, response); } String contentType = response.getHeader(CONTENT_TYPE); if (contentType == null) { throw new UnexpectedResponseException("Content-Type is not set for response", request, response); } if (!MediaType.parse(contentType).is(MEDIA_TYPE_SMILE)) { throw new UnexpectedResponseException("Expected application/x-jackson-smile response from server but got " + contentType, request, response); } try { JsonParser jsonParser = new SmileFactory().createParser(response.getInputStream()); ObjectMapper objectMapper = OBJECT_MAPPER_SUPPLIER.get(); jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); return objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(jsonCodec.getType())); } catch (InvalidFormatException e) { throw new IllegalArgumentException("Unable to create " + jsonCodec.getType() + " from SMILE response", e); } catch (IOException e) { throw new RuntimeException("Error reading SMILE response from server", e); } } private  SmileResponseHandler(JsonCodec<T> jsonCodec); private  SmileResponseHandler(JsonCodec<T> jsonCodec, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); static SmileResponseHandler<T> createSmileResponseHandler(JsonCodec<T> jsonCodec); static SmileResponseHandler<T> createSmileResponseHandler(JsonCodec<T> jsonCodec, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); @Override T handleException(Request request, Exception exception); @Override T handle(Request request, Response response); }
SmileResponseHandler implements ResponseHandler<T, RuntimeException> { @Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { throw new UnexpectedResponseException( String.format("Expected response code to be %s, but was %d: %s", successfulResponseCodes, response.getStatusCode(), response.getStatusMessage()), request, response); } String contentType = response.getHeader(CONTENT_TYPE); if (contentType == null) { throw new UnexpectedResponseException("Content-Type is not set for response", request, response); } if (!MediaType.parse(contentType).is(MEDIA_TYPE_SMILE)) { throw new UnexpectedResponseException("Expected application/x-jackson-smile response from server but got " + contentType, request, response); } try { JsonParser jsonParser = new SmileFactory().createParser(response.getInputStream()); ObjectMapper objectMapper = OBJECT_MAPPER_SUPPLIER.get(); jsonParser.disable(JsonParser.Feature.AUTO_CLOSE_SOURCE); return objectMapper.readValue(jsonParser, objectMapper.getTypeFactory().constructType(jsonCodec.getType())); } catch (InvalidFormatException e) { throw new IllegalArgumentException("Unable to create " + jsonCodec.getType() + " from SMILE response", e); } catch (IOException e) { throw new RuntimeException("Error reading SMILE response from server", e); } } private  SmileResponseHandler(JsonCodec<T> jsonCodec); private  SmileResponseHandler(JsonCodec<T> jsonCodec, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); static SmileResponseHandler<T> createSmileResponseHandler(JsonCodec<T> jsonCodec); static SmileResponseHandler<T> createSmileResponseHandler(JsonCodec<T> jsonCodec, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); @Override T handleException(Request request, Exception exception); @Override T handle(Request request, Response response);  }