logger.debug("Reading to [" + className + "] as \"" + contentType + "\""); return (T) messageConverter.read((Class) this.responseClass, responseWrapper);
@SuppressWarnings("unchecked") @Override public void handleError(ClientHttpResponse response) throws IOException { for (HttpMessageConverter<?> converter : messageConverters) { if (converter.canRead(OAuth2Exception.class, response.getHeaders().getContentType())) { OAuth2Exception ex; try { ex = ((HttpMessageConverter<OAuth2Exception>) converter).read(OAuth2Exception.class, response); } catch (Exception e) { // ignore continue; } throw ex; } } super.handleError(response); }
private void mockResponseBody(String expectedBody, MediaType mediaType) throws Exception { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(mediaType); responseHeaders.setContentLength(expectedBody.length()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream(expectedBody.getBytes())); given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expectedBody); }
@Override protected ExceptionReport readInternal(Class<? extends ExceptionReport> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { for (HttpMessageConverter<?> converter : messageConverters) { for (MediaType mediaType : converter.getSupportedMediaTypes()) { if (converter.canRead(Map.class, mediaType)) { @SuppressWarnings({ "rawtypes", "unchecked" }) HttpMessageConverter<Map> messageConverter = (HttpMessageConverter<Map>) converter; @SuppressWarnings("unchecked") Map<String, String> map = messageConverter.read(Map.class, inputMessage); return new ExceptionReport(getException(map)); } } } return null; }
logger.debug("Reading to [" + className + "] as \"" + contentType + "\""); return (T) messageConverter.read((Class) this.responseClass, responseWrapper);
@Test public void resolveArgumentOptionalWithContent() throws Exception { servletRequest.setContentType("text/plain"); servletRequest.setContent("body".getBytes()); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn("body"); assertEquals(Optional.of("body"), processor.resolveArgument(paramOptionalString, mavContainer, webRequest, new ValidatingBinderFactory())); }
private void testResolveArgumentWithValidation(SimpleBean simpleBean) throws Exception { MediaType contentType = MediaType.TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); servletRequest.setContent("payload".getBytes(StandardCharsets.UTF_8)); @SuppressWarnings("unchecked") HttpMessageConverter<SimpleBean> beanConverter = mock(HttpMessageConverter.class); given(beanConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.TEXT_PLAIN)); given(beanConverter.canRead(SimpleBean.class, contentType)).willReturn(true); given(beanConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean); processor = new RequestResponseBodyMethodProcessor(Collections.singletonList(beanConverter)); processor.resolveArgument(paramValidBean, mavContainer, webRequest, new ValidatingBinderFactory()); }
@Test @SuppressWarnings("unchecked") public void normal() throws IOException { HttpMessageConverter<String> converter = mock(HttpMessageConverter.class); HttpHeaders responseHeaders = new HttpHeaders(); MediaType contentType = MediaType.TEXT_PLAIN; responseHeaders.setContentType(contentType); String expected = "Foo"; extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream(expected.getBytes())); given(converter.canRead(String.class, contentType)).willReturn(true); given(converter.read(eq(String.class), any(HttpInputMessage.class))).willReturn(expected); Object result = extractor.extractData(response); assertEquals(expected, result); }
@Test // SPR-13592 @SuppressWarnings("unchecked") public void converterThrowsIOException() throws IOException { HttpMessageConverter<String> converter = mock(HttpMessageConverter.class); HttpHeaders responseHeaders = new HttpHeaders(); MediaType contentType = MediaType.TEXT_PLAIN; responseHeaders.setContentType(contentType); extractor = new HttpMessageConverterExtractor<>(String.class, createConverterList(converter)); given(response.getRawStatusCode()).willReturn(HttpStatus.OK.value()); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(new ByteArrayInputStream("Foobar".getBytes())); given(converter.canRead(String.class, contentType)).willReturn(true); given(converter.read(eq(String.class), any(HttpInputMessage.class))).willThrow(IOException.class); exception.expect(RestClientException.class); exception.expectMessage("Error while extracting response for type " + "[class java.lang.String] and content type [text/plain]"); exception.expectCause(Matchers.instanceOf(IOException.class)); extractor.extractData(response); }
@Test public void resolveArgumentNotRequiredWithContent() throws Exception { servletRequest.setContentType("text/plain"); servletRequest.setContent("body".getBytes()); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn("body"); assertEquals("body", processor.resolveArgument(paramStringNotRequired, mavContainer, webRequest, new ValidatingBinderFactory())); }
getAdvice().beforeBodyRead(message, parameter, targetType, converterType); body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) : ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse)); body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);
@Test public void postForObjectNull() throws Exception { mockTextPlainHttpMessageConverter(); HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.TEXT_PLAIN); responseHeaders.setContentLength(10); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(StreamUtils.emptyInput()); given(converter.read(String.class, response)).willReturn(null); String result = template.postForObject("http://example.com", null, String.class); assertNull("Invalid POST result", result); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); verify(response).close(); }
@Test public void postForEntityNull() throws Exception { mockTextPlainHttpMessageConverter(); HttpHeaders requestHeaders = new HttpHeaders(); mockSentRequest(POST, "http://example.com", requestHeaders); mockResponseStatus(HttpStatus.OK); HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.setContentType(MediaType.TEXT_PLAIN); responseHeaders.setContentLength(10); given(response.getHeaders()).willReturn(responseHeaders); given(response.getBody()).willReturn(StreamUtils.emptyInput()); given(converter.read(String.class, response)).willReturn(null); ResponseEntity<String> result = template.postForEntity("http://example.com", null, String.class); assertFalse("Invalid POST result", result.hasBody()); assertEquals("Invalid Content-Type", MediaType.TEXT_PLAIN, result.getHeaders().getContentType()); assertEquals("Invalid content length", 0, requestHeaders.getContentLength()); assertEquals("Invalid status code", HttpStatus.OK, result.getStatusCode()); verify(response).close(); }
@Test public void resolveOptionalRequestPart() throws Exception { SimpleBean simpleBean = new SimpleBean("foo"); given(messageConverter.canRead(SimpleBean.class, MediaType.TEXT_PLAIN)).willReturn(true); given(messageConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(simpleBean); ModelAndViewContainer mavContainer = new ModelAndViewContainer(); Object actualValue = resolver.resolveArgument( optionalRequestPart, mavContainer, webRequest, new ValidatingBinderFactory()); assertEquals("Invalid argument value", Optional.of(simpleBean), actualValue); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); actualValue = resolver.resolveArgument(optionalRequestPart, mavContainer, webRequest, new ValidatingBinderFactory()); assertEquals("Invalid argument value", Optional.of(simpleBean), actualValue); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); }
@Test public void resolveArgument() throws Exception { MediaType contentType = MediaType.TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); String body = "Foo"; servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8)); given(stringMessageConverter.canRead(String.class, contentType)).willReturn(true); given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body); Object result = processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, new ValidatingBinderFactory()); assertEquals("Invalid argument", body, result); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); }
@Test public void shouldResolveHttpEntityArgument() throws Exception { String body = "Foo"; MediaType contentType = TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8)); given(stringHttpMessageConverter.canRead(String.class, contentType)).willReturn(true); given(stringHttpMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body); Object result = processor.resolveArgument(paramHttpEntity, mavContainer, webRequest, null); assertTrue(result instanceof HttpEntity); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); assertEquals("Invalid argument", body, ((HttpEntity<?>) result).getBody()); }
@Test(expected = HttpMessageNotReadableException.class) // SPR-9942 public void resolveArgumentRequiredNoContent() throws Exception { servletRequest.setContentType(MediaType.TEXT_PLAIN_VALUE); servletRequest.setContent(new byte[0]); given(stringMessageConverter.canRead(String.class, MediaType.TEXT_PLAIN)).willReturn(true); given(stringMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(null); assertNull(processor.resolveArgument(paramRequestBodyString, mavContainer, webRequest, new ValidatingBinderFactory())); }
private void testResolveArgument(SimpleBean argValue, MethodParameter parameter) throws Exception { given(messageConverter.canRead(SimpleBean.class, MediaType.TEXT_PLAIN)).willReturn(true); given(messageConverter.read(eq(SimpleBean.class), isA(HttpInputMessage.class))).willReturn(argValue); ModelAndViewContainer mavContainer = new ModelAndViewContainer(); Object actualValue = resolver.resolveArgument(parameter, mavContainer, webRequest, new ValidatingBinderFactory()); assertEquals("Invalid argument value", argValue, actualValue); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); }
getAdvice().beforeBodyRead(message, parameter, targetType, converterType); body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) : ((HttpMessageConverter<T>) converter).read(targetClass, msgToUse)); body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);
@Test public void shouldResolveRequestEntityArgument() throws Exception { String body = "Foo"; MediaType contentType = TEXT_PLAIN; servletRequest.addHeader("Content-Type", contentType.toString()); servletRequest.setMethod("GET"); servletRequest.setServerName("www.example.com"); servletRequest.setServerPort(80); servletRequest.setRequestURI("/path"); servletRequest.setContent(body.getBytes(StandardCharsets.UTF_8)); given(stringHttpMessageConverter.canRead(String.class, contentType)).willReturn(true); given(stringHttpMessageConverter.read(eq(String.class), isA(HttpInputMessage.class))).willReturn(body); Object result = processor.resolveArgument(paramRequestEntity, mavContainer, webRequest, null); assertTrue(result instanceof RequestEntity); assertFalse("The requestHandled flag shouldn't change", mavContainer.isRequestHandled()); RequestEntity<?> requestEntity = (RequestEntity<?>) result; assertEquals("Invalid method", HttpMethod.GET, requestEntity.getMethod()); // using default port (which is 80), so do not need to append the port (-1 means ignore) URI uri = new URI("http", null, "www.example.com", -1, "/path", null, null); assertEquals("Invalid url", uri, requestEntity.getUrl()); assertEquals("Invalid argument", body, requestEntity.getBody()); }