Search in sources :

Example 16 with HttpMessageNotReadableException

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);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException)

Example 17 with HttpMessageNotReadableException

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);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) XMLStreamException(javax.xml.stream.XMLStreamException) StAXSource(javax.xml.transform.stax.StAXSource) XMLInputFactory(javax.xml.stream.XMLInputFactory)

Example 18 with HttpMessageNotReadableException

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);
    }
}
Also used : HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) WebException(org.n52.web.exception.WebException) InternalServerException(org.n52.web.exception.InternalServerException) BadRequestException(org.n52.web.exception.BadRequestException) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 19 with HttpMessageNotReadableException

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));
}
Also used : HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) HttpOutputMessage(org.springframework.http.HttpOutputMessage) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

HttpMessageNotReadableException (org.springframework.http.converter.HttpMessageNotReadableException)19 MediaType (org.springframework.http.MediaType)7 Test (org.junit.Test)5 IOException (java.io.IOException)4 BufferedImage (java.awt.image.BufferedImage)3 InputStream (java.io.InputStream)3 ImageReadParam (javax.imageio.ImageReadParam)3 ImageReader (javax.imageio.ImageReader)3 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)3 UnavailableException (javax.servlet.UnavailableException)3 HttpHeaders (org.springframework.http.HttpHeaders)3 ResponseEntity (org.springframework.http.ResponseEntity)3 HttpMessageNotWritableException (org.springframework.http.converter.HttpMessageNotWritableException)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 InputStreamReader (java.io.InputStreamReader)2 Charset (java.nio.charset.Charset)2 JAXBException (javax.xml.bind.JAXBException)2 UnmarshalException (javax.xml.bind.UnmarshalException)2 Unmarshaller (javax.xml.bind.Unmarshaller)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)2