Search in sources :

Example 1 with HttpMessageNotReadableException

use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.

the class AbstractWireFeedHttpMessageConverter method readInternal.

@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);
    }
}
Also used : WireFeedInput(com.rometools.rome.io.WireFeedInput) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) InputStreamReader(java.io.InputStreamReader) FeedException(com.rometools.rome.io.FeedException) MediaType(org.springframework.http.MediaType) Charset(java.nio.charset.Charset) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader)

Example 2 with HttpMessageNotReadableException

use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.

the class Jaxb2CollectionHttpMessageConverter method read.

@Override
@SuppressWarnings("unchecked")
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
    ParameterizedType parameterizedType = (ParameterizedType) type;
    T result = createCollection((Class<?>) parameterizedType.getRawType());
    Class<?> elementClass = (Class<?>) parameterizedType.getActualTypeArguments()[0];
    try {
        Unmarshaller unmarshaller = createUnmarshaller(elementClass);
        XMLStreamReader streamReader = this.inputFactory.createXMLStreamReader(inputMessage.getBody());
        int event = moveToFirstChildOfRootElement(streamReader);
        while (event != XMLStreamReader.END_DOCUMENT) {
            if (elementClass.isAnnotationPresent(XmlRootElement.class)) {
                result.add(unmarshaller.unmarshal(streamReader));
            } else if (elementClass.isAnnotationPresent(XmlType.class)) {
                result.add(unmarshaller.unmarshal(streamReader, elementClass).getValue());
            } else {
                // should not happen, since we check in canRead(Type)
                throw new HttpMessageConversionException("Could not unmarshal to [" + elementClass + "]");
            }
            event = moveToNextElement(streamReader);
        }
        return result;
    } catch (UnmarshalException ex) {
        throw new HttpMessageNotReadableException("Could not unmarshal to [" + elementClass + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    } catch (XMLStreamException ex) {
        throw new HttpMessageConversionException(ex.getMessage(), ex);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) XMLStreamException(javax.xml.stream.XMLStreamException) UnmarshalException(javax.xml.bind.UnmarshalException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 3 with HttpMessageNotReadableException

use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.

the class SourceHttpMessageConverter method readSAXSource.

// on JDK 9
@SuppressWarnings("deprecation")
private SAXSource readSAXSource(InputStream body) 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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) ByteArrayInputStream(java.io.ByteArrayInputStream) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 4 with HttpMessageNotReadableException

use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.

the class HttpMessageConverterExtractor method extractData.

@Override
@SuppressWarnings({ "unchecked", "rawtypes", "resource" })
public T extractData(ClientHttpResponse response) throws IOException {
    MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response);
    if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) {
        return null;
    }
    MediaType contentType = getContentType(responseWrapper);
    try {
        for (HttpMessageConverter<?> messageConverter : this.messageConverters) {
            if (messageConverter instanceof GenericHttpMessageConverter) {
                GenericHttpMessageConverter<?> genericMessageConverter = (GenericHttpMessageConverter<?>) messageConverter;
                if (genericMessageConverter.canRead(this.responseType, null, contentType)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Reading [" + this.responseType + "] as \"" + contentType + "\" using [" + messageConverter + "]");
                    }
                    return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);
                }
            }
            if (this.responseClass != null) {
                if (messageConverter.canRead(this.responseClass, contentType)) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Reading [" + this.responseClass.getName() + "] as \"" + contentType + "\" using [" + messageConverter + "]");
                    }
                    return (T) messageConverter.read((Class) this.responseClass, responseWrapper);
                }
            }
        }
    } catch (IOException | HttpMessageNotReadableException exc) {
        throw new RestClientException("Error while extracting response for type [" + this.responseType + "] and content type [" + contentType + "]", exc);
    }
    throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " + "for response type [" + this.responseType + "] and content type [" + contentType + "]");
}
Also used : HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) MediaType(org.springframework.http.MediaType) IOException(java.io.IOException) GenericHttpMessageConverter(org.springframework.http.converter.GenericHttpMessageConverter)

Example 5 with HttpMessageNotReadableException

use of org.springframework.http.converter.HttpMessageNotReadableException in project spring-framework by spring-projects.

the class Jaxb2CollectionHttpMessageConverterTests method readXmlRootElementExternalEntityDisabled.

@Test
@SuppressWarnings("unchecked")
public void readXmlRootElementExternalEntityDisabled() throws Exception {
    Resource external = new ClassPathResource("external.txt", getClass());
    String content = "<!DOCTYPE root [" + "  <!ELEMENT external ANY >\n" + "  <!ENTITY ext SYSTEM \"" + external.getURI() + "\" >]>" + "  <list><rootElement><type s=\"1\"/><external>&ext;</external></rootElement></list>";
    MockHttpInputMessage inputMessage = new MockHttpInputMessage(content.getBytes("UTF-8"));
    converter = new Jaxb2CollectionHttpMessageConverter<Collection<Object>>() {

        @Override
        protected XMLInputFactory createXmlInputFactory() {
            XMLInputFactory inputFactory = super.createXmlInputFactory();
            inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, true);
            return inputFactory;
        }
    };
    try {
        Collection<RootElement> result = converter.read(rootElementListType, null, inputMessage);
        assertEquals(1, result.size());
        assertEquals("", result.iterator().next().external);
    } catch (HttpMessageNotReadableException ex) {
    // Some parsers raise an exception
    }
}
Also used : MockHttpInputMessage(org.springframework.http.MockHttpInputMessage) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) HttpMessageNotReadableException(org.springframework.http.converter.HttpMessageNotReadableException) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) Collection(java.util.Collection) ClassPathResource(org.springframework.core.io.ClassPathResource) XMLInputFactory(javax.xml.stream.XMLInputFactory) 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