/**
* Creates a dynamic context for the given exchange
*/
protected DynamicQueryContext createDynamicContext(Exchange exchange) throws Exception {
Configuration config = getConfiguration();
DynamicQueryContext dynamicQueryContext = new DynamicQueryContext(config);
Message in = exchange.getIn();
Item item = in.getBody(Item.class);
if (item != null) {
dynamicQueryContext.setContextItem(item);
} else {
Source source = in.getBody(Source.class);
if (source == null) {
Object body = in.getBody();
// lets try coerce some common types into something JAXP can deal with
if (body instanceof GenericFile) {
// special for files so we can work with them out of the box
InputStream is = exchange.getContext().getTypeConverter().convertTo(InputStream.class, body);
source = converter.toDOMSource(is);
} else if (body instanceof BeanInvocation) {
// if its a null bean invocation then handle that
BeanInvocation bi = exchange.getContext().getTypeConverter().convertTo(BeanInvocation.class, body);
if (bi.getArgs() != null && bi.getArgs().length == 1 && bi.getArgs()[0] == null) {
// its a null argument from the bean invocation so use null as answer
source = null;
}
} else if (body instanceof String) {
source = converter.toDOMSource(body.toString());
} else {
// try some of Camels type converters
InputStream is = in.getBody(InputStream.class);
if (is != null) {
source = converter.toDOMSource(is);
}
// fallback and use String
if (source == null) {
String s = in.getBody(String.class);
if (s != null) {
source = converter.toDOMSource(s);
}
}
}
if (source == null) {
// indicate it was not possible to convert to a Source type
throw new NoTypeConversionAvailableException(body, Source.class);
}
}
DocumentInfo doc = getStaticQueryContext().buildDocument(source);
dynamicQueryContext.setContextItem(doc);
}
configureQuery(dynamicQueryContext, exchange);
// call the reset if the in message body is StreamCache
MessageHelper.resetStreamCache(exchange.getIn());