Search in sources :

Example 11 with TypeConversionException

use of org.apache.camel.TypeConversionException in project camel by apache.

the class FallbackTypeConverter method marshall.

protected <T> T marshall(Class<T> type, Exchange exchange, Object value, Method objectFactoryMethod) throws JAXBException, XMLStreamException, FactoryConfigurationError, TypeConversionException {
    LOG.trace("Marshal from value {} to type {}", value, type);
    T answer = null;
    if (parentTypeConverter != null) {
        // lets convert the object to a JAXB source and try convert that to
        // the required source
        JAXBContext context = createContext(value.getClass());
        // must create a new instance of marshaller as its not thread safe
        Marshaller marshaller = context.createMarshaller();
        Writer buffer = new StringWriter();
        if (isPrettyPrint()) {
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        }
        if (exchange != null && exchange.getProperty(Exchange.CHARSET_NAME, String.class) != null) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, exchange.getProperty(Exchange.CHARSET_NAME, String.class));
        }
        Object toMarshall = value;
        if (objectFactoryMethod != null) {
            try {
                Object instance = objectFactoryMethod.getDeclaringClass().newInstance();
                if (instance != null) {
                    toMarshall = objectFactoryMethod.invoke(instance, value);
                }
            } catch (Exception e) {
                LOG.debug("Unable to create JAXBElement object for type " + value.getClass() + " due to " + e.getMessage(), e);
            }
        }
        if (needFiltering(exchange)) {
            XMLStreamWriter writer = parentTypeConverter.convertTo(XMLStreamWriter.class, buffer);
            FilteringXmlStreamWriter filteringWriter = new FilteringXmlStreamWriter(writer);
            marshaller.marshal(toMarshall, filteringWriter);
        } else {
            marshaller.marshal(toMarshall, buffer);
        }
        // we need to pass the exchange
        answer = parentTypeConverter.convertTo(type, exchange, buffer.toString());
    }
    return answer;
}
Also used : Marshaller(javax.xml.bind.Marshaller) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) JAXBContext(javax.xml.bind.JAXBContext) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringWriter(java.io.StringWriter) Writer(java.io.Writer) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) XMLStreamException(javax.xml.stream.XMLStreamException) JAXBException(javax.xml.bind.JAXBException) TypeConversionException(org.apache.camel.TypeConversionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 12 with TypeConversionException

use of org.apache.camel.TypeConversionException in project camel by apache.

the class ModelHelper method modelToXml.

private static <T extends NamedNode> T modelToXml(CamelContext context, InputStream is, String xml, Class<T> type) throws JAXBException {
    JAXBContext jaxbContext = getJAXBContext(context);
    XmlConverter xmlConverter = newXmlConverter(context);
    Document dom = null;
    try {
        if (is != null) {
            dom = xmlConverter.toDOMDocument(is, null);
        } else if (xml != null) {
            dom = xmlConverter.toDOMDocument(xml, null);
        }
    } catch (Exception e) {
        throw new TypeConversionException(xml, Document.class, e);
    }
    if (dom == null) {
        throw new IllegalArgumentException("InputStream and XML is both null");
    }
    Map<String, String> namespaces = new LinkedHashMap<>();
    extractNamespaces(dom, namespaces);
    Binder<Node> binder = jaxbContext.createBinder();
    Object result = binder.unmarshal(dom);
    if (result == null) {
        throw new JAXBException("Cannot unmarshal to " + type + " using JAXB");
    }
    // Restore namespaces to anything that's NamespaceAware
    if (result instanceof RoutesDefinition) {
        List<RouteDefinition> routes = ((RoutesDefinition) result).getRoutes();
        for (RouteDefinition route : routes) {
            applyNamespaces(route, namespaces);
        }
    } else if (result instanceof RouteDefinition) {
        RouteDefinition route = (RouteDefinition) result;
        applyNamespaces(route, namespaces);
    }
    return type.cast(result);
}
Also used : TypeConversionException(org.apache.camel.TypeConversionException) Node(org.w3c.dom.Node) NamedNode(org.apache.camel.NamedNode) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) Document(org.w3c.dom.Document) TransformerException(javax.xml.transform.TransformerException) JAXBException(javax.xml.bind.JAXBException) TypeConversionException(org.apache.camel.TypeConversionException) XmlConverter(org.apache.camel.converter.jaxp.XmlConverter) LinkedHashMap(java.util.LinkedHashMap)

Example 13 with TypeConversionException

use of org.apache.camel.TypeConversionException in project ddf by codice.

the class FrameworkProducer method readBodyDataAsMetacards.

/**
     * Reads in Metacard data from message body of exchange
     *
     * @param exchange
     *            the exchange containing the message data
     * @return {@link java.util.List} of Metacard objects
     */
private List<Metacard> readBodyDataAsMetacards(final Exchange exchange) {
    List<Metacard> metacardsToProcess = new ArrayList<Metacard>();
    try {
        if (exchange.getIn().getBody() == null) {
            LOGGER.debug("Body is null");
            return metacardsToProcess;
        }
        // first try to read in a single Metacard
        LOGGER.debug("Reading in body data as Metacard...");
        final Metacard metacardToProcess = exchange.getIn().getBody(Metacard.class);
        if (metacardToProcess != null) {
            metacardsToProcess.add(metacardToProcess);
            LOGGER.debug("Successfully read in body data as Metacard ");
            return metacardsToProcess;
        }
        LOGGER.debug("Problem reading in body data as Metacard");
        // if we get here, then we possibly have List<Metacard>
        LOGGER.debug("Reading in body data as List<Metacard>...");
        metacardsToProcess = exchange.getIn().getBody(List.class);
        if (metacardsToProcess == null) {
            LOGGER.debug("Problem reading in body data as List<?>");
            metacardsToProcess = new ArrayList<Metacard>();
            return metacardsToProcess;
        }
        LOGGER.debug("Successfully read in body data as List<?>");
    } catch (TypeConversionException tce1) {
        LOGGER.debug("Invalid message body. Expected either Metacard or List<Metacard>", tce1);
    }
    return metacardsToProcess;
}
Also used : Metacard(ddf.catalog.data.Metacard) TypeConversionException(org.apache.camel.TypeConversionException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TypeConversionException (org.apache.camel.TypeConversionException)13 JAXBException (javax.xml.bind.JAXBException)4 Exchange (org.apache.camel.Exchange)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 JAXBContext (javax.xml.bind.JAXBContext)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 StringWriter (java.io.StringWriter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 LinkedHashMap (java.util.LinkedHashMap)2 Marshaller (javax.xml.bind.Marshaller)2 XMLStreamException (javax.xml.stream.XMLStreamException)2 TransformerException (javax.xml.transform.TransformerException)2 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)2 Processor (org.apache.camel.Processor)2 TypeConverter (org.apache.camel.TypeConverter)2 XmlConverter (org.apache.camel.converter.jaxp.XmlConverter)2 Document (org.w3c.dom.Document)2 Metacard (ddf.catalog.data.Metacard)1 Writer (java.io.Writer)1