Search in sources :

Example 1 with ExchangeProperty

use of org.apache.camel.ExchangeProperty 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);
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Headers(org.apache.camel.Headers) ExchangeProperty(org.apache.camel.ExchangeProperty) Endpoint(org.apache.camel.Endpoint) Annotation(java.lang.annotation.Annotation) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) Header(org.apache.camel.Header) Parameter(java.lang.reflect.Parameter) Body(org.apache.camel.Body) Map(java.util.Map)

Example 2 with ExchangeProperty

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

the class BeanInfo method createParameterUnmarshalExpressionForAnnotation.

private Expression createParameterUnmarshalExpressionForAnnotation(Class<?> clazz, Method method, Class<?> parameterType, Annotation annotation) {
    if (annotation instanceof AttachmentObjects) {
        return ExpressionBuilder.attachmentObjectsExpression();
    } else if (annotation instanceof Attachments) {
        return ExpressionBuilder.attachmentsExpression();
    } else if (annotation instanceof Property) {
        Property propertyAnnotation = (Property) annotation;
        return ExpressionBuilder.exchangePropertyExpression(propertyAnnotation.value());
    } else if (annotation instanceof ExchangeProperty) {
        ExchangeProperty propertyAnnotation = (ExchangeProperty) annotation;
        return ExpressionBuilder.exchangePropertyExpression(propertyAnnotation.value());
    } else if (annotation instanceof Properties) {
        return ExpressionBuilder.exchangePropertiesExpression();
    } else if (annotation instanceof ExchangeProperties) {
        return ExpressionBuilder.exchangePropertiesExpression();
    } else if (annotation instanceof Header) {
        Header headerAnnotation = (Header) annotation;
        return ExpressionBuilder.headerExpression(headerAnnotation.value());
    } else if (annotation instanceof Headers) {
        return ExpressionBuilder.headersExpression();
    } else if (annotation instanceof OutHeaders) {
        return ExpressionBuilder.outHeadersExpression();
    } else if (annotation instanceof ExchangeException) {
        return ExpressionBuilder.exchangeExceptionExpression(CastUtils.cast(parameterType, Exception.class));
    } else if (annotation instanceof PropertyInject) {
        PropertyInject propertyAnnotation = (PropertyInject) annotation;
        Expression inject = ExpressionBuilder.propertiesComponentExpression(propertyAnnotation.value(), null, propertyAnnotation.defaultValue());
        return ExpressionBuilder.convertToExpression(inject, parameterType);
    } else {
        LanguageAnnotation languageAnnotation = annotation.annotationType().getAnnotation(LanguageAnnotation.class);
        if (languageAnnotation != null) {
            Class<?> type = languageAnnotation.factory();
            Object object = camelContext.getInjector().newInstance(type);
            if (object instanceof AnnotationExpressionFactory) {
                AnnotationExpressionFactory expressionFactory = (AnnotationExpressionFactory) object;
                return expressionFactory.createExpression(camelContext, annotation, languageAnnotation, parameterType);
            } else {
                LOG.warn("Ignoring bad annotation: " + languageAnnotation + "on method: " + method + " which declares a factory: " + type.getName() + " which does not implement " + AnnotationExpressionFactory.class.getName());
            }
        }
    }
    return null;
}
Also used : OutHeaders(org.apache.camel.OutHeaders) Headers(org.apache.camel.Headers) OutHeaders(org.apache.camel.OutHeaders) ExchangeProperty(org.apache.camel.ExchangeProperty) ExchangeProperties(org.apache.camel.ExchangeProperties) Properties(org.apache.camel.Properties) AttachmentObjects(org.apache.camel.AttachmentObjects) Attachments(org.apache.camel.Attachments) ExchangeProperties(org.apache.camel.ExchangeProperties) Header(org.apache.camel.Header) Expression(org.apache.camel.Expression) ExchangeException(org.apache.camel.ExchangeException) Property(org.apache.camel.Property) ExchangeProperty(org.apache.camel.ExchangeProperty) PropertyInject(org.apache.camel.PropertyInject) LanguageAnnotation(org.apache.camel.language.LanguageAnnotation)

Aggregations

ExchangeProperty (org.apache.camel.ExchangeProperty)2 Header (org.apache.camel.Header)2 Headers (org.apache.camel.Headers)2 Annotation (java.lang.annotation.Annotation)1 Parameter (java.lang.reflect.Parameter)1 Map (java.util.Map)1 AttachmentObjects (org.apache.camel.AttachmentObjects)1 Attachments (org.apache.camel.Attachments)1 Body (org.apache.camel.Body)1 Endpoint (org.apache.camel.Endpoint)1 Exchange (org.apache.camel.Exchange)1 ExchangeException (org.apache.camel.ExchangeException)1 ExchangeProperties (org.apache.camel.ExchangeProperties)1 Expression (org.apache.camel.Expression)1 OutHeaders (org.apache.camel.OutHeaders)1 Properties (org.apache.camel.Properties)1 Property (org.apache.camel.Property)1 PropertyInject (org.apache.camel.PropertyInject)1 DefaultExchange (org.apache.camel.impl.DefaultExchange)1 LanguageAnnotation (org.apache.camel.language.LanguageAnnotation)1