use of org.apache.camel.Body in project camel by apache.
the class AbstractCamelInvocationHandler method invokeProxy.
@SuppressWarnings("unchecked")
protected Object invokeProxy(final Method method, final ExchangePattern pattern, Object[] args, boolean binding) throws Throwable {
final Exchange exchange = new DefaultExchange(endpoint, pattern);
//Need to check if there are mutiple arguments and the parameters have no annotations for binding,
//then use the original bean invocation.
boolean canUseBinding = method.getParameterCount() == 1;
if (!canUseBinding) {
for (Parameter parameter : method.getParameters()) {
if (parameter.isAnnotationPresent(Header.class) || parameter.isAnnotationPresent(Headers.class) || parameter.isAnnotationPresent(ExchangeProperty.class) || parameter.isAnnotationPresent(Body.class)) {
canUseBinding = true;
}
}
}
if (binding && canUseBinding) {
// in binding mode we bind the passed in arguments (args) to the created exchange
// using the existing Camel @Body, @Header, @Headers, @ExchangeProperty annotations
// if no annotation then its bound as the message body
int index = 0;
for (Annotation[] row : method.getParameterAnnotations()) {
Object value = args[index];
if (row == null || row.length == 0) {
// assume its message body when there is no annotations
exchange.getIn().setBody(value);
} else {
for (Annotation ann : row) {
if (ann.annotationType().isAssignableFrom(Header.class)) {
Header header = (Header) ann;
String name = header.value();
exchange.getIn().setHeader(name, value);
} else if (ann.annotationType().isAssignableFrom(Headers.class)) {
Map map = exchange.getContext().getTypeConverter().tryConvertTo(Map.class, exchange, value);
if (map != null) {
exchange.getIn().getHeaders().putAll(map);
}
} else if (ann.annotationType().isAssignableFrom(ExchangeProperty.class)) {
ExchangeProperty ep = (ExchangeProperty) ann;
String name = ep.value();
exchange.setProperty(name, value);
} else if (ann.annotationType().isAssignableFrom(Body.class)) {
exchange.getIn().setBody(value);
} else {
// assume its message body when there is no annotations
exchange.getIn().setBody(value);
}
}
}
index++;
}
} else {
// no binding so use the old behavior with BeanInvocation as the body
BeanInvocation invocation = new BeanInvocation(method, args);
exchange.getIn().setBody(invocation);
}
if (binding) {
LOG.trace("Binding to service interface as @Body,@Header,@ExchangeProperty detected when calling proxy method: {}", method);
} else {
LOG.trace("No binding to service interface as @Body,@Header,@ExchangeProperty not detected. Using BeanInvocation as message body when calling proxy method: {}");
}
return doInvoke(method, exchange);
}
Aggregations