Example usage for org.springframework.http.converter HttpMessageNotReadableException HttpMessageNotReadableException

List of usage examples for org.springframework.http.converter HttpMessageNotReadableException HttpMessageNotReadableException

Introduction

In this page you can find the example usage for org.springframework.http.converter HttpMessageNotReadableException HttpMessageNotReadableException.

Prototype

public HttpMessageNotReadableException(String msg, HttpInputMessage httpInputMessage) 

Source Link

Document

Create a new HttpMessageNotReadableException.

Usage

From source file:org.jasig.portlet.courses.dao.xml.Jaxb2CourseSummaryHttpMessageConverter.java

@Override
protected Object readFromSource(Class<? extends Object> clazz, HttpHeaders headers, Source source)
        throws IOException {
    try {//  ww w.  j  a va2  s  .c  o  m
        Unmarshaller unmarshaller = createWrapperUnmarshaller(clazz);
        if (clazz.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(source);
        } else {
            JAXBElement<? extends Object> jaxbElement = unmarshaller.unmarshal(source, clazz);
            return 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);
    }
}

From source file:org.resthub.web.converter.MappingJackson2XmlHttpMessageConverter.java

@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {

    JavaType javaType = getJavaType(clazz);
    try {/*from  w  w  w .j  a  v a 2 s .com*/
        return this.objectMapper.readValue(inputMessage.getBody(), javaType);
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("Could not read XML: " + ex.getMessage(), ex);
    }
}

From source file:com.wq.common.web.springmvc.MappingJackson2HttpMessageConverter.java

@Override
protected Object readInternal(Class<?> clazz, HttpInputMessage inputMessage)
        throws IOException, HttpMessageNotReadableException {

    JavaType javaType = getJavaType(clazz);
    try {/*from   w w  w  . jav  a2s .c  o m*/
        return this.objectMapper.readValue(inputMessage.getBody(), javaType);
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    }
}

From source file:demo.SourceHttpMessageConverter.java

private DOMSource readDOMSource(InputStream body) throws IOException {
    try {/*w  w w  .j  av  a2 s  .c  o m*/
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        documentBuilderFactory.setNamespaceAware(true);
        documentBuilderFactory.setFeature("http://xml.org/sax/features/external-general-entities",
                isProcessExternalEntities());
        documentBuilderFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl",
                !isSupportDtd());
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        if (!isProcessExternalEntities()) {
            documentBuilder.setEntityResolver(NO_OP_ENTITY_RESOLVER);
        }
        Document document = documentBuilder.parse(body);
        return new DOMSource(document);
    } 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);
    }
}

From source file:org.springframework.http.converter.json.MappingJackson2HttpMessageConverter.java

private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
    try {// ww w.  j a v a  2 s .c o m
        return this.objectMapper.readValue(inputMessage.getBody(), javaType);
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    }
}

From source file:demo.SourceHttpMessageConverter.java

private SAXSource readSAXSource(InputStream body) throws IOException {
    try {/* ww  w . ja v  a 2 s .c  o m*/
        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd());
        reader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
        if (!isProcessExternalEntities()) {
            reader.setEntityResolver(NO_OP_ENTITY_RESOLVER);
        }
        byte[] bytes = StreamUtils.copyToByteArray(body);
        return new SAXSource(reader, new InputSource(new ByteArrayInputStream(bytes)));
    } catch (SAXException ex) {
        throw new HttpMessageNotReadableException("Could not parse document: " + ex.getMessage(), ex);
    }
}

From source file:demo.SourceHttpMessageConverter.java

private Source readStAXSource(InputStream body) {
    try {/*ww w.jav a 2s .  com*/
        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);
    }
}

From source file:org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.java

private Object readPatch(IncomingRequest request, ObjectMapper mapper, Object existingObject) {

    try {/*from   w  ww  . j a v a 2s.  c  om*/

        JsonPatchHandler handler = new JsonPatchHandler(mapper, reader);
        return handler.apply(request, existingObject);

    } catch (Exception o_O) {

        if (o_O instanceof HttpMessageNotReadableException) {
            throw (HttpMessageNotReadableException) o_O;
        }

        throw new HttpMessageNotReadableException(String.format(ERROR_MESSAGE, existingObject.getClass()), o_O);
    }
}

From source file:org.springframework.data.rest.webmvc.config.PersistentEntityResourceHandlerMethodArgumentResolver.java

private Object readPutForUpdate(IncomingRequest request, ObjectMapper mapper, Object existingObject) {

    try {/*from  ww  w.  j  av  a2s  .c o  m*/

        JsonPatchHandler handler = new JsonPatchHandler(mapper, reader);
        JsonNode jsonNode = mapper.readTree(request.getBody());

        return handler.applyPut((ObjectNode) jsonNode, existingObject);

    } catch (Exception o_O) {
        throw new HttpMessageNotReadableException(String.format(ERROR_MESSAGE, existingObject.getClass()), o_O);
    }
}

From source file:com.fiadot.springjsoncrypt.json.CryptMappingJacson2HttpMessageConverter.java

private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
    try {//w  w w. j a  v  a2  s .c om
        CipherDecryptUtils cryptoUtil = new CipherDecryptUtils("AES", "AES/CBC/PKCS7Padding",
                "ls4h+XaXU+A5m72HRpwkeQ==", "W46YspHuEiQlKDcLTqoySw==");

        BufferedReader in = new BufferedReader(new InputStreamReader(inputMessage.getBody(), "UTF-8"));

        String raw_message = in.readLine();

        //byte[] data = new byte[4096]; 
        //inputMessage.getBody().read(data);

        //String raw_message = data.toString();
        logger.info("encoded=" + raw_message);

        String decoded_str = cryptoUtil.decrypt(raw_message);
        logger.info("decoded=" + decoded_str);

        ReqDto obj = this.objectMapper.readValue(decoded_str, ReqDto.class);
        // mapper.readValue(str_res, AppHashAddRes.class);
        //Object obj =  this.objectMapper.readValue(decoded_str, javaType); 

        return obj;

        // TODO : make input stream 
        //InputStream inputStream = new ByteArrayInputStream(encStr.getBytes());
        //return this.objectMapper.readValue(inputStream, javaType);
    } catch (IOException ex) {
        throw new HttpMessageNotReadableException("Could not read JSON: " + ex.getMessage(), ex);
    } catch (Exception ex) {
        throw new HttpMessageNotReadableException("Could not read JSON(crypto): " + ex.getMessage(), ex);
    }
}