use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.
the class MarshallingHttpMessageConverterTests method readWithMarshallingFailureException.
@Test
public void readWithMarshallingFailureException() throws Exception {
MockHttpInputMessage inputMessage = new MockHttpInputMessage(new byte[0]);
UnmarshallingFailureException ex = new UnmarshallingFailureException("forced");
Unmarshaller unmarshaller = mock(Unmarshaller.class);
given(unmarshaller.unmarshal(isA(StreamSource.class))).willThrow(ex);
MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
converter.setUnmarshaller(unmarshaller);
try {
converter.read(Object.class, inputMessage);
fail("HttpMessageNotReadableException should be thrown");
} catch (HttpMessageNotReadableException e) {
assertTrue("Invalid exception hierarchy", e.getCause() == ex);
}
}
use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.
the class RequestResponseBodyMethodProcessor method readWithMessageConverters.
@Override
protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter, Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest);
Object arg = readWithMessageConverters(inputMessage, parameter, paramType);
if (arg == null) {
if (checkRequired(parameter)) {
throw new HttpMessageNotReadableException("Required request body is missing: " + parameter.getMethod().toGenericString());
}
}
return arg;
}
use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.
the class ResponseEntityExceptionHandlerTests method httpMessageNotReadable.
@Test
public void httpMessageNotReadable() {
Exception ex = new HttpMessageNotReadableException("message");
testException(ex);
}
use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-security-oauth by spring-projects.
the class SparklrController method photo.
@RequestMapping("/sparklr/photos/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id, HttpServletRequest request) throws Exception {
InputStream photo = sparklrService.loadSparklrPhoto(id);
if (photo == null) {
throw new UnavailableException("The requested photo does not exist");
}
BufferedImage body;
MediaType contentType = MediaType.IMAGE_JPEG;
Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
if (imageReaders.hasNext()) {
ImageReader imageReader = imageReaders.next();
ImageReadParam irp = imageReader.getDefaultReadParam();
imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
body = imageReader.read(0, irp);
} else {
throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
request.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(MediaType.IMAGE_JPEG));
return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-security-oauth by spring-projects.
the class SparklrRedirectController method photo.
@RequestMapping("/sparklr/redirect/{id}")
public ResponseEntity<BufferedImage> photo(@PathVariable String id) throws Exception {
InputStream photo = sparklrService.loadSparklrPhoto(id);
if (photo == null) {
throw new UnavailableException("The requested photo does not exist");
}
BufferedImage body;
MediaType contentType = MediaType.IMAGE_JPEG;
Iterator<ImageReader> imageReaders = ImageIO.getImageReadersByMIMEType(contentType.toString());
if (imageReaders.hasNext()) {
ImageReader imageReader = imageReaders.next();
ImageReadParam irp = imageReader.getDefaultReadParam();
imageReader.setInput(new MemoryCacheImageInputStream(photo), true);
body = imageReader.read(0, irp);
} else {
throw new HttpMessageNotReadableException("Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]");
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
return new ResponseEntity<BufferedImage>(body, headers, HttpStatus.OK);
}
Aggregations