@Test public void testNonJsonResponse() { User response = handler.handle(null, mockResponse().contentType(PLAIN_TEXT_UTF_8).body("hello").build()); assertSame(response, DEFAULT_VALUE); }
@Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { return defaultValue; } String contentType = response.getHeader(CONTENT_TYPE); if (contentType != null && !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return defaultValue; } try { return jsonCodec.fromJson(ByteStreams.toByteArray(response.getInputStream())); } catch (Exception e) { return defaultValue; } }
DefaultingJsonResponseHandler implements ResponseHandler<T, RuntimeException> { @Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { return defaultValue; } String contentType = response.getHeader(CONTENT_TYPE); if (contentType != null && !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return defaultValue; } try { return jsonCodec.fromJson(ByteStreams.toByteArray(response.getInputStream())); } catch (Exception e) { return defaultValue; } } }
DefaultingJsonResponseHandler implements ResponseHandler<T, RuntimeException> { @Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { return defaultValue; } String contentType = response.getHeader(CONTENT_TYPE); if (contentType != null && !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return defaultValue; } try { return jsonCodec.fromJson(ByteStreams.toByteArray(response.getInputStream())); } catch (Exception e) { return defaultValue; } } private  DefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue); private  DefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); }
DefaultingJsonResponseHandler implements ResponseHandler<T, RuntimeException> { @Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { return defaultValue; } String contentType = response.getHeader(CONTENT_TYPE); if (contentType != null && !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return defaultValue; } try { return jsonCodec.fromJson(ByteStreams.toByteArray(response.getInputStream())); } catch (Exception e) { return defaultValue; } } private  DefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue); private  DefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); static DefaultingJsonResponseHandler<T> createDefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue); static DefaultingJsonResponseHandler<T> createDefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); @Override T handleException(Request request, Exception exception); @Override T handle(Request request, Response response); }
DefaultingJsonResponseHandler implements ResponseHandler<T, RuntimeException> { @Override public T handle(Request request, Response response) { if (!successfulResponseCodes.contains(response.getStatusCode())) { return defaultValue; } String contentType = response.getHeader(CONTENT_TYPE); if (contentType != null && !MediaType.parse(contentType).is(MEDIA_TYPE_JSON)) { return defaultValue; } try { return jsonCodec.fromJson(ByteStreams.toByteArray(response.getInputStream())); } catch (Exception e) { return defaultValue; } } private  DefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue); private  DefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); static DefaultingJsonResponseHandler<T> createDefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue); static DefaultingJsonResponseHandler<T> createDefaultingJsonResponseHandler(JsonCodec<T> jsonCodec, T defaultValue, int firstSuccessfulResponseCode, int... otherSuccessfulResponseCodes); @Override T handleException(Request request, Exception exception); @Override T handle(Request request, Response response);  }