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

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

Introduction

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

Prototype

public HttpMessageConversionException(String msg, @Nullable Throwable cause) 

Source Link

Document

Create a new HttpMessageConversionException.

Usage

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

/**
 * Creates a new {@link Marshaller} for the given class.
 *
 * @param clazz the class to create the marshaller for
 * @return the {@code Marshaller}//from w ww.  j a va  2 s  . c  o  m
 * @throws HttpMessageConversionException in case of JAXB errors
 */
protected final Marshaller createWrapperMarshaller(Class clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        return jaxbContext.createMarshaller();
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException(
                "Could not create Marshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
    }
}

From source file:com.androidwhy.modules.mapper.JaxbMapper.java

protected static JAXBContext getJaxbContext(Class clazz) {
    Assert.notNull(clazz, "'clazz' must not be null");
    JAXBContext jaxbContext = jaxbContexts.get(clazz);
    if (jaxbContext == null) {
        try {/*  ww w  .ja  v a2  s  .  c o  m*/
            jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class);
            jaxbContexts.putIfAbsent(clazz, jaxbContext);
        } catch (JAXBException ex) {
            throw new HttpMessageConversionException(
                    "Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
        }
    }
    return jaxbContext;
}

From source file:net.solarnetwork.web.support.SimpleXmlHttpMessageConverter.java

@Override
protected void writeInternal(Object t, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    OutputStream out = outputMessage.getBody();
    try {//  w  w w  .  j  a  v a 2s  .  c o  m
        XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(out, "UTF-8");
        writer.writeStartDocument("UTF-8", "1.0");
        outputObject(t, t.getClass().getSimpleName(), writer);
        writer.flush();
    } catch (XMLStreamException e) {
        throw new HttpMessageConversionException("Error creating XMLStreamWriter", e);
    }
}

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

/**
 * Creates a new {@link Unmarshaller} for the given class.
 *
 * @param clazz the class to create the unmarshaller for
 * @return the {@code Unmarshaller}//from  w  w w . j a v a  2 s.c o m
 * @throws HttpMessageConversionException in case of JAXB errors
 */
protected final Unmarshaller createWrapperUnmarshaller(Class clazz) throws JAXBException {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setProperty("com.sun.xml.bind.ObjectFactory", new ObjectFactoryWrapper());
        return unmarshaller;
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException(
                "Could not create Unmarshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
    }
}

From source file:org.slc.sli.dashboard.web.util.ControllerInputValidatorAspect.java

/**
 * Validate param using param specific validator and validate all strings using a blacklist validator
 * @param arg//from  w w  w  .j  ava2  s .c om
 * @param argName
 */
private void validateArg(Object arg, String argName) {
    BindingResult result = new BeanPropertyBindingResult(arg, argName);
    ValidationUtils.invokeValidator(getValidator(), arg, result);
    // force string validation for bad chars
    if (arg instanceof String) {
        ValidationUtils.invokeValidator(getValidator(), new DefaultStringValidatable((String) arg), result);
    }
    if (result.hasErrors()) {
        throw new HttpMessageConversionException("Invalid input parameter " + argName,
                new BindException(result));
    }
}

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

/**
 * Returns a {@link JAXBContext} for the given class.
 *
 * @param clazz the class to return the context for
 * @return the {@code JAXBContext}//  www .j av a 2s  . c o m
 * @throws HttpMessageConversionException in case of JAXB errors
 */
protected final JAXBContext getWrapperJaxbContext(Class clazz) {
    Assert.notNull(clazz, "'clazz' must not be null");
    JAXBContext jaxbContext = jaxbContexts.get(clazz);
    if (jaxbContext == null) {
        try {
            jaxbContext = JAXBContext.newInstance(clazz);
            jaxbContexts.putIfAbsent(clazz, jaxbContext);
        } catch (JAXBException ex) {
            throw new HttpMessageConversionException(
                    "Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
        }
    }
    return jaxbContext;
}

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 {//from   w w  w  . j av  a2s .c  om
        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.jasig.portlet.courses.dao.xml.Jaxb2CourseSummaryHttpMessageConverter.java

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
    try {/*w  ww  .j  a v a 2  s .c om*/
        Class clazz = ClassUtils.getUserClass(o);
        Marshaller marshaller = createWrapperMarshaller(clazz);
        setCharset(headers.getContentType(), marshaller);
        marshaller.marshal(o, result);
    } catch (MarshalException ex) {
        throw new HttpMessageNotWritableException("Could not marshal [" + o + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}

From source file:org.alfresco.repo.publishing.JaxbHttpMessageConverter.java

@Override
protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source source) throws IOException {
    try {/*from   w  w w  .  j a  v a2s .  c  o  m*/
        Unmarshaller unmarshaller = createUnmarshaller(clazz);
        if (clazz.isAnnotationPresent(XmlRootElement.class)) {
            return unmarshaller.unmarshal(source);
        } else {
            JAXBElement<?> 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.alfresco.repo.publishing.JaxbHttpMessageConverter.java

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
    try {//from   w w w . ja va2 s .c om
        Class<?> clazz = ClassUtils.getUserClass(o);
        Marshaller marshaller = createMarshaller(clazz);
        setCharset(headers.getContentType(), marshaller);
        marshaller.marshal(o, result);
    } catch (MarshalException ex) {
        throw new HttpMessageNotWritableException("Could not marshal [" + o + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}