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;
}
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);
}
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;
}
Aggregations