Search in sources :

Example 41 with JAXBException

use of javax.xml.bind.JAXBException in project spring-framework by spring-projects.

the class AbstractJaxb2HttpMessageConverter method createMarshaller.

/**
	 * Create a new {@link Marshaller} for the given class.
	 * @param clazz the class to create the marshaller for
	 * @return the {@code Marshaller}
	 * @throws HttpMessageConversionException in case of JAXB errors
	 */
protected final Marshaller createMarshaller(Class<?> clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        Marshaller marshaller = jaxbContext.createMarshaller();
        customizeMarshaller(marshaller);
        return marshaller;
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not create Marshaller for class [" + clazz + "]: " + ex.getMessage(), ex);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException) JAXBContext(javax.xml.bind.JAXBContext)

Example 42 with JAXBException

use of javax.xml.bind.JAXBException 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 43 with JAXBException

use of javax.xml.bind.JAXBException in project spring-framework by spring-projects.

the class Jaxb2RootElementHttpMessageConverter method writeToResult.

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
    try {
        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);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) MarshalException(javax.xml.bind.MarshalException) HttpMessageNotWritableException(org.springframework.http.converter.HttpMessageNotWritableException) JAXBException(javax.xml.bind.JAXBException) HttpMessageConversionException(org.springframework.http.converter.HttpMessageConversionException)

Example 44 with JAXBException

use of javax.xml.bind.JAXBException in project spring-framework by spring-projects.

the class Jaxb2XmlEncoder method encode.

@Override
protected Flux<DataBuffer> encode(Object value, DataBufferFactory dataBufferFactory, ResolvableType type, MimeType mimeType, Map<String, Object> hints) {
    try {
        DataBuffer buffer = dataBufferFactory.allocateBuffer(1024);
        OutputStream outputStream = buffer.asOutputStream();
        Class<?> clazz = ClassUtils.getUserClass(value);
        Marshaller marshaller = jaxbContexts.createMarshaller(clazz);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
        marshaller.marshal(value, outputStream);
        return Flux.just(buffer);
    } catch (JAXBException ex) {
        return Flux.error(ex);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) OutputStream(java.io.OutputStream) JAXBException(javax.xml.bind.JAXBException) DataBuffer(org.springframework.core.io.buffer.DataBuffer)

Example 45 with JAXBException

use of javax.xml.bind.JAXBException in project camel by apache.

the class DefaultBulkApiClient method marshalRequest.

private void marshalRequest(Object input, Request request, String contentType) throws SalesforceException {
    try {
        Marshaller marshaller = context.createMarshaller();
        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        marshaller.marshal(input, byteStream);
        request.content(new BytesContentProvider(contentType, byteStream.toByteArray()));
    } catch (JAXBException e) {
        throw new SalesforceException(String.format("Error marshaling request for {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
    } catch (IllegalArgumentException e) {
        throw new SalesforceException(String.format("Error marshaling request for {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider)

Aggregations

JAXBException (javax.xml.bind.JAXBException)402 JAXBContext (javax.xml.bind.JAXBContext)126 IOException (java.io.IOException)93 Unmarshaller (javax.xml.bind.Unmarshaller)91 Marshaller (javax.xml.bind.Marshaller)69 ArrayList (java.util.ArrayList)38 StringWriter (java.io.StringWriter)35 List (java.util.List)35 Map (java.util.Map)33 SAXException (org.xml.sax.SAXException)32 File (java.io.File)29 InputStream (java.io.InputStream)29 AMConsoleException (com.sun.identity.console.base.model.AMConsoleException)28 HashSet (java.util.HashSet)28 JAXBElement (javax.xml.bind.JAXBElement)24 XMLStreamException (javax.xml.stream.XMLStreamException)23 SAML2MetaException (com.sun.identity.saml2.meta.SAML2MetaException)22 StringReader (java.io.StringReader)21 HashMap (java.util.HashMap)21 SAML2MetaManager (com.sun.identity.saml2.meta.SAML2MetaManager)20