Search in sources :

Example 46 with JAXBException

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

the class DefaultBulkApiClient method unmarshalResponse.

private <T> T unmarshalResponse(InputStream response, Request request, Class<T> resultClass) throws SalesforceException {
    try {
        Unmarshaller unmarshaller = context.createUnmarshaller();
        JAXBElement<T> result = unmarshaller.unmarshal(new StreamSource(response), resultClass);
        return result.getValue();
    } catch (JAXBException e) {
        throw new SalesforceException(String.format("Error unmarshaling response {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
    } catch (IllegalArgumentException e) {
        throw new SalesforceException(String.format("Error unmarshaling response for {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(javax.xml.bind.JAXBException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 47 with JAXBException

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

the class RecordsUtil method createXMLFile.

public static void createXMLFile() {
    File in = new File("target/in/records.xml");
    if (in.exists()) {
        return;
    } else {
        if (!in.getParentFile().exists() && !in.getParentFile().mkdirs()) {
            throw new RuntimeException("can't create " + in.getParent());
        }
    }
    Records records = new Records();
    for (int i = 0; i < 10; i++) {
        Record record = new Record();
        record.setKey(Integer.toString(i));
        record.setValue("#" + i);
        records.getRecord().add(record);
    }
    Marshaller marshaller;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(Records.class.getPackage().getName());
        marshaller = jaxbCtx.createMarshaller();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    FileWriter writer = null;
    try {
        writer = new FileWriter(in);
        marshaller.marshal(records, writer);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    } finally {
        if (writer != null) {
            try {
                writer.flush();
                writer.close();
            } catch (IOException e) {
            // no-op
            }
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) FileWriter(java.io.FileWriter) JAXBContext(javax.xml.bind.JAXBContext) IOException(java.io.IOException) File(java.io.File)

Example 48 with JAXBException

use of javax.xml.bind.JAXBException in project springside4 by springside.

the class XmlMapper method createMarshaller.

/**
	 * 创建Marshaller并设定encoding(可为null).
	 * 线程不安全,需要每次创建或pooling。
	 */
public static Marshaller createMarshaller(Class clazz, String encoding) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        if (StringUtils.isNotBlank(encoding)) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        }
        return marshaller;
    } catch (JAXBException e) {
        throw ExceptionUtil.unchecked(e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

Example 49 with JAXBException

use of javax.xml.bind.JAXBException in project springside4 by springside.

the class XmlMapper method toXml.

/**
	 * Java Collection->Xml with encoding, 特别支持Root Element是Collection的情形.
	 */
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
    try {
        CollectionWrapper wrapper = new CollectionWrapper();
        wrapper.collection = root;
        JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName), CollectionWrapper.class, wrapper);
        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(wrapperElement, writer);
        return writer.toString();
    } catch (JAXBException e) {
        throw ExceptionUtil.unchecked(e);
    }
}
Also used : StringWriter(java.io.StringWriter) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement)

Example 50 with JAXBException

use of javax.xml.bind.JAXBException in project springside4 by springside.

the class XmlMapper method getJaxbContext.

protected static JAXBContext getJaxbContext(Class clazz) {
    Validate.notNull(clazz, "'clazz' must not be null");
    JAXBContext jaxbContext = jaxbContexts.get(clazz);
    if (jaxbContext == null) {
        try {
            jaxbContext = JAXBContext.newInstance(clazz, CollectionWrapper.class);
            jaxbContexts.putIfAbsent(clazz, jaxbContext);
        } catch (JAXBException ex) {
            throw new RuntimeException("Could not instantiate JAXBContext for class [" + clazz + "]: " + ex.getMessage(), ex);
        }
    }
    return jaxbContext;
}
Also used : JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext)

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