use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.
the class SourceHttpMessageConverter method readDOMSource.
private DOMSource readDOMSource(InputStream body) throws IOException {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);
documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
if (!isProcessExternalEntities()) {
documentBuilder.setEntityResolver(NO_OP_ENTITY_RESOLVER);
}
Document document = documentBuilder.parse(body);
return new DOMSource(document);
} catch (NullPointerException ex) {
if (!isSupportDtd()) {
throw new HttpMessageNotReadableException("NPE while unmarshalling: " + "This can happen due to the presence of DTD declarations which are disabled.", ex);
}
throw ex;
} catch (ParserConfigurationException ex) {
throw new HttpMessageNotReadableException("Could not set feature: " + ex.getMessage(), ex);
} catch (SAXException ex) {
throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
}
}
use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.
the class SourceHttpMessageConverter method readStAXSource.
private Source readStAXSource(InputStream body) {
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);
}
}
use of org.springframework.http.converter.HttpMessageNotReadableException in project series-rest-api by 52North.
the class BaseController method handleException.
@ExceptionHandler(value = { RuntimeException.class, Exception.class, Throwable.class })
public void handleException(Exception e, HttpServletRequest request, HttpServletResponse response) {
if (e instanceof HttpMessageNotReadableException) {
WebException wrappedException = new BadRequestException("The request could not been read.", e);
wrappedException.addHint("Check the message which has been sent to the server. Probably it is not valid.");
writeExceptionResponse(wrappedException, response, HttpStatus.BAD_REQUEST);
} else {
WebException wrappedException = new InternalServerException("Unexpected Exception occured.", e);
writeExceptionResponse(wrappedException, response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
use of org.springframework.http.converter.HttpMessageNotReadableException in project java-chassis by ServiceComb.
the class TestCseHttpMessageConverter method testAll.
@Test
public void testAll() {
MockUtil.getInstance().mockReflectionUtils();
MockUtil.getInstance().mockCseClientHttpRequest();
CseHttpMessageConverter lCseHttpMessageConverter = new CseHttpMessageConverter();
lCseHttpMessageConverter.canWrite(null, null);
lCseHttpMessageConverter.getSupportedMediaTypes();
try {
lCseHttpMessageConverter.read(this.getClass(), null);
} catch (HttpMessageNotReadableException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
try {
HttpOutputMessage httpOutputMessage = Mockito.mock(CseClientHttpRequest.class);
lCseHttpMessageConverter.write(null, null, httpOutputMessage);
} catch (HttpMessageNotWritableException | IOException e) {
// TODO Auto-generated catch block
}
Assert.assertEquals(true, lCseHttpMessageConverter.canRead(null, null));
}
Aggregations