@Override public Object read(Class<?> clazz, HttpInputMessage inputMessage) { throw new HttpMessageNotReadableException("Could not read", inputMessage); }
private Object readResolved(Type resolvedType, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { Reader reader = getReader(inputMessage); try { return readInternal(resolvedType, reader); } catch (Exception ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex, inputMessage); } }
@SuppressWarnings("deprecation") // on JDK 9 private SAXSource readSAXSource(InputStream body, HttpInputMessage inputMessage) throws IOException { try { XMLReader xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); xmlReader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); xmlReader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); if (!isProcessExternalEntities()) { xmlReader.setEntityResolver(NO_OP_ENTITY_RESOLVER); } byte[] bytes = StreamUtils.copyToByteArray(body); return new SAXSource(xmlReader, new InputSource(new ByteArrayInputStream(bytes))); } catch (SAXException ex) { throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage); } }
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { String value = this.stringHttpMessageConverter.readInternal(String.class, inputMessage); Object result = this.conversionService.convert(value, clazz); if (result == null) { throw new HttpMessageNotReadableException( "Unexpected null conversion result for '" + value + "' to " + clazz, inputMessage); } return result; }
throw new HttpMessageNotReadableException("NPE while unmarshalling: This can happen " + "due to the presence of DTD declarations which are disabled.", ex, inputMessage); throw new HttpMessageNotReadableException( "Could not set feature: " + ex.getMessage(), ex, inputMessage); throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage);
@Override public final T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { return readFromSource(clazz, inputMessage.getHeaders(), new StreamSource(inputMessage.getBody())); } catch (IOException | HttpMessageConversionException ex) { throw ex; } catch (Exception ex) { throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(), ex, inputMessage); } }
private Source readStAXSource(InputStream body, HttpInputMessage inputMessage) { try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, isSupportDtd()); inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities()); if (!isProcessExternalEntities()) { inputFactory.setXMLResolver(NO_OP_XML_RESOLVER); } XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body); return new StAXSource(streamReader); } catch (XMLStreamException ex) { throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage); } }
throw new HttpMessageNotReadableException( "Cannot unmarshal to [" + elementClass + "]", inputMessage); throw new HttpMessageNotReadableException( "Failed to read XML stream: " + ex.getMessage(), ex, inputMessage); throw new HttpMessageNotReadableException( "Could not unmarshal to [" + elementClass + "]: " + ex.getMessage(), ex, inputMessage);
private Object readResolved(Type resolvedType, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { Reader reader = getReader(inputMessage); try { return readInternal(resolvedType, reader); } catch (Exception ex) { throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex, inputMessage); } }
@Override protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { String value = this.stringHttpMessageConverter.readInternal(String.class, inputMessage); Object result = this.conversionService.convert(value, clazz); if (result == null) { throw new HttpMessageNotReadableException( "Unexpected null conversion result for '" + value + "' to " + clazz, inputMessage); } return result; }
@Override protected Resource readInternal(Class<? extends Resource> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { if (this.supportsReadStreaming && InputStreamResource.class == clazz) { return new InputStreamResource(inputMessage.getBody()) { @Override public String getFilename() { return inputMessage.getHeaders().getContentDisposition().getFilename(); } }; } else if (Resource.class == clazz || ByteArrayResource.class.isAssignableFrom(clazz)) { byte[] body = StreamUtils.copyToByteArray(inputMessage.getBody()); return new ByteArrayResource(body) { @Override @Nullable public String getFilename() { return inputMessage.getHeaders().getContentDisposition().getFilename(); } }; } else { throw new HttpMessageNotReadableException("Unsupported resource class: " + clazz, inputMessage); } }
@Override protected final E readFromSource(Class<? extends E> clazz, HttpHeaders headers, Source source) throws IOException { try { JAXBElement<? extends I> jaxbElement = createUnmarshaller().unmarshal(source, internalClass); return convertToExternal(jaxbElement.getValue()); } catch (UnmarshalException ex) { throw new HttpMessageNotReadableException("Could not unmarshal to [" + clazz + "]: " + ex.getMessage(), ex); } catch (JAXBException ex) { throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex); } }
@Override protected OAuth2AccessTokenResponse readInternal(Class<? extends OAuth2AccessTokenResponse> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try { @SuppressWarnings("unchecked") Map<String, String> tokenResponseParameters = (Map<String, String>) this.jsonMessageConverter.read( PARAMETERIZED_RESPONSE_TYPE.getType(), null, inputMessage); return this.tokenResponseConverter.convert(tokenResponseParameters); } catch (Exception ex) { throw new HttpMessageNotReadableException("An error occurred reading the OAuth 2.0 Access Token Response: " + ex.getMessage(), ex, inputMessage); } }
@Override protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, MethodParameter parameter, Type paramType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException { HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class); Assert.state(servletRequest != null, "No HttpServletRequest"); ServletServerHttpRequest inputMessage = new ServletServerHttpRequest(servletRequest); Object arg = readWithMessageConverters(inputMessage, parameter, paramType); if (arg == null && checkRequired(parameter)) { throw new HttpMessageNotReadableException("Required request body is missing: " + parameter.getExecutable().toGenericString(), inputMessage); } return arg; }
@Test public void httpMessageNotReadable() { Exception ex = new HttpMessageNotReadableException("message"); testException(ex); }
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) throws IOException { try { if (inputMessage instanceof MappingJacksonInputMessage) { Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView(); if (deserializationView != null) { return this.objectMapper.readerWithView(deserializationView).forType(javaType). readValue(inputMessage.getBody()); } } return this.objectMapper.readValue(inputMessage.getBody(), javaType); } catch (InvalidDefinitionException ex) { throw new HttpMessageConversionException("Type definition error: " + ex.getType(), ex); } catch (JsonProcessingException ex) { throw new HttpMessageNotReadableException("JSON parse error: " + ex.getOriginalMessage(), ex, inputMessage); } }
MediaType contentType = inputMessage.getHeaders().getContentType(); if (contentType == null) { throw new HttpMessageNotReadableException("No Content-Type header", inputMessage); throw new HttpMessageNotReadableException( "Could not find javax.imageio.ImageReader for Content-Type [" + contentType + "]", inputMessage);
@Override @SuppressWarnings("unchecked") protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { WireFeedInput feedInput = new WireFeedInput(); MediaType contentType = inputMessage.getHeaders().getContentType(); Charset charset = (contentType != null && contentType.getCharset() != null ? contentType.getCharset() : DEFAULT_CHARSET); try { Reader reader = new InputStreamReader(inputMessage.getBody(), charset); return (T) feedInput.build(reader); } catch (FeedException ex) { throw new HttpMessageNotReadableException("Could not read WireFeed: " + ex.getMessage(), ex, inputMessage); } }
@Override @SuppressWarnings("unchecked") protected T readInternal(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { InputStream body = inputMessage.getBody(); if (DOMSource.class == clazz) { return (T) readDOMSource(body, inputMessage); } else if (SAXSource.class == clazz) { return (T) readSAXSource(body, inputMessage); } else if (StAXSource.class == clazz) { return (T) readStAXSource(body, inputMessage); } else if (StreamSource.class == clazz || Source.class == clazz) { return (T) readStreamSource(body); } else { throw new HttpMessageNotReadableException("Could not read class [" + clazz + "]. Only DOMSource, SAXSource, StAXSource, and StreamSource are supported.", inputMessage); } }
@Test public void handleHttpMessageNotReadable() { HttpMessageNotReadableException ex = new HttpMessageNotReadableException("foo"); ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex); assertNotNull("No ModelAndView returned", mav); assertTrue("No Empty ModelAndView returned", mav.isEmpty()); assertEquals("Invalid status code", 400, response.getStatus()); }