Search in sources :

Example 81 with Message

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

the class AtomPollingConsumerTest method testNoSplitEntries.

@Test
public void testNoSplitEntries() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(1);
    mock.assertIsSatisfied();
    Exchange exchange = mock.getExchanges().get(0);
    Message in = exchange.getIn();
    assertNotNull(in);
    assertTrue(in.getBody() instanceof List);
    assertTrue(in.getHeader(AtomConstants.ATOM_FEED) instanceof Feed);
    Feed feed = in.getHeader(AtomConstants.ATOM_FEED, Feed.class);
    assertEquals("James Strachan", feed.getAuthor().getName());
    List<?> entries = in.getBody(List.class);
    assertEquals(7, entries.size());
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) List(java.util.List) Feed(org.apache.abdera.model.Feed) Test(org.junit.Test)

Example 82 with Message

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

the class BeanInfo method chooseBestPossibleMethodInfo.

private MethodInfo chooseBestPossibleMethodInfo(Exchange exchange, Collection<MethodInfo> operationList, Object body, List<MethodInfo> possibles, List<MethodInfo> possiblesWithException, List<MethodInfo> possibleWithCustomAnnotation) throws AmbiguousMethodCallException {
    Exception exception = ExpressionBuilder.exchangeExceptionExpression().evaluate(exchange, Exception.class);
    if (exception != null && possiblesWithException.size() == 1) {
        LOG.trace("Exchange has exception set so we prefer method that also has exception as parameter");
        // prefer the method that accepts exception in case we have an exception also
        return possiblesWithException.get(0);
    } else if (possibles.size() == 1) {
        return possibles.get(0);
    } else if (possibles.isEmpty()) {
        LOG.trace("No possible methods so now trying to convert body to parameter types");
        // let's try converting
        Object newBody = null;
        MethodInfo matched = null;
        int matchCounter = 0;
        for (MethodInfo methodInfo : operationList) {
            if (methodInfo.getBodyParameterType() != null) {
                if (methodInfo.getBodyParameterType().isInstance(body)) {
                    return methodInfo;
                }
                // we should only try to convert, as we are looking for best match
                Object value = exchange.getContext().getTypeConverter().tryConvertTo(methodInfo.getBodyParameterType(), exchange, body);
                if (value != null) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace("Converted body from: {} to: {}", body.getClass().getCanonicalName(), methodInfo.getBodyParameterType().getCanonicalName());
                    }
                    matchCounter++;
                    newBody = value;
                    matched = methodInfo;
                }
            }
        }
        if (matchCounter > 1) {
            throw new AmbiguousMethodCallException(exchange, Arrays.asList(matched, matched));
        }
        if (matched != null) {
            LOG.trace("Setting converted body: {}", body);
            Message in = exchange.getIn();
            in.setBody(newBody);
            return matched;
        }
    } else {
        // if we only have a single method with custom annotations, let's use that one
        if (possibleWithCustomAnnotation.size() == 1) {
            MethodInfo answer = possibleWithCustomAnnotation.get(0);
            LOG.trace("There are only one method with annotations so we choose it: {}", answer);
            return answer;
        }
        // try to choose among multiple methods with annotations
        MethodInfo chosen = chooseMethodWithCustomAnnotations(exchange, possibles);
        if (chosen != null) {
            return chosen;
        }
        // just make sure the methods aren't all actually the same
        chosen = getSingleCovariantMethod(possibles);
        if (chosen != null) {
            return chosen;
        }
        throw new AmbiguousMethodCallException(exchange, possibles);
    }
    // cannot find a good method to use
    return null;
}
Also used : Message(org.apache.camel.Message) ExchangeException(org.apache.camel.ExchangeException)

Example 83 with Message

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

the class BeanProcessor method process.

public boolean process(Exchange exchange, AsyncCallback callback) {
    // do we have an explicit method name we always should invoke (either configured on endpoint or as a header)
    String explicitMethodName = exchange.getIn().getHeader(Exchange.BEAN_METHOD_NAME, method, String.class);
    Object bean;
    BeanInfo beanInfo;
    try {
        bean = beanHolder.getBean();
        // get bean info for this bean instance (to avoid thread issue)
        beanInfo = beanHolder.getBeanInfo(bean);
        if (beanInfo == null) {
            // fallback and use old way
            beanInfo = beanHolder.getBeanInfo();
        }
    } catch (Throwable e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // but only do this if allowed
    if (allowProcessor(explicitMethodName, beanInfo)) {
        Processor processor = getProcessor();
        if (processor == null) {
            // so if there is a custom type converter for the bean to processor
            processor = exchange.getContext().getTypeConverter().tryConvertTo(Processor.class, exchange, bean);
        }
        if (processor != null) {
            LOG.trace("Using a custom adapter as bean invocation: {}", processor);
            try {
                processor.process(exchange);
            } catch (Throwable e) {
                exchange.setException(e);
            }
            callback.done(true);
            return true;
        }
    }
    Message in = exchange.getIn();
    // is the message proxied using a BeanInvocation?
    BeanInvocation beanInvoke = null;
    if (in.getBody() != null && in.getBody() instanceof BeanInvocation) {
        // BeanInvocation would be stored directly as the message body
        // do not force any type conversion attempts as it would just be unnecessary and cost a bit performance
        // so a regular instanceof check is sufficient
        beanInvoke = (BeanInvocation) in.getBody();
    }
    if (beanInvoke != null) {
        // Now it gets a bit complicated as ProxyHelper can proxy beans which we later
        // intend to invoke (for example to proxy and invoke using spring remoting).
        // and therefore the message body contains a BeanInvocation object.
        // However this can causes problem if we in a Camel route invokes another bean,
        // so we must test whether BeanHolder and BeanInvocation is the same bean or not
        LOG.trace("Exchange IN body is a BeanInvocation instance: {}", beanInvoke);
        Class<?> clazz = beanInvoke.getMethod().getDeclaringClass();
        boolean sameBean = clazz.isInstance(bean);
        if (LOG.isDebugEnabled()) {
            LOG.debug("BeanHolder bean: {} and beanInvocation bean: {} is same instance: {}", new Object[] { bean.getClass(), clazz, sameBean });
        }
        if (sameBean) {
            try {
                beanInvoke.invoke(bean, exchange);
                if (exchange.hasOut()) {
                    // propagate headers
                    exchange.getOut().getHeaders().putAll(exchange.getIn().getHeaders());
                }
            } catch (Throwable e) {
                exchange.setException(e);
            }
            callback.done(true);
            return true;
        }
    }
    // set temporary header which is a hint for the bean info that introspect the bean
    if (in.getHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY) == null) {
        in.setHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY, isMultiParameterArray());
    }
    MethodInvocation invocation;
    // set explicit method name to invoke as a header, which is how BeanInfo can detect it
    if (explicitMethodName != null) {
        in.setHeader(Exchange.BEAN_METHOD_NAME, explicitMethodName);
    }
    try {
        invocation = beanInfo.createInvocation(bean, exchange);
    } catch (Throwable e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    } finally {
        // must remove headers as they were provisional
        in.removeHeader(Exchange.BEAN_MULTI_PARAMETER_ARRAY);
        in.removeHeader(Exchange.BEAN_METHOD_NAME);
    }
    if (invocation == null) {
        exchange.setException(new IllegalStateException("No method invocation could be created, no matching method could be found on: " + bean));
        callback.done(true);
        return true;
    }
    // invoke invocation
    return invocation.proceed(callback);
}
Also used : Processor(org.apache.camel.Processor) AsyncProcessor(org.apache.camel.AsyncProcessor) Message(org.apache.camel.Message)

Example 84 with Message

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

the class MessageVariableResolver method resolveVariable.

public Object resolveVariable(QName name) {
    String uri = name.getNamespaceURI();
    String localPart = name.getLocalPart();
    Object answer = null;
    Message in = exchange.get().getIn();
    if (uri == null || uri.length() == 0) {
        answer = variables.get(localPart);
        if (answer == null) {
            Message message = in;
            if (message != null) {
                answer = message.getHeader(localPart);
            }
            if (answer == null) {
                answer = exchange.get().getProperty(localPart);
            }
        }
    } else if (uri.equals(SYSTEM_PROPERTIES_NAMESPACE)) {
        try {
            answer = System.getProperty(localPart);
        } catch (Exception e) {
            LOG.debug("Security exception evaluating system property: " + localPart + ". Reason: " + e, e);
        }
    } else if (uri.equals(ENVIRONMENT_VARIABLES)) {
        answer = System.getenv().get(localPart);
    } else if (uri.equals(EXCHANGE_PROPERTY)) {
        answer = exchange.get().getProperty(localPart);
    } else if (uri.equals(IN_NAMESPACE)) {
        answer = in.getHeader(localPart);
        if (answer == null && localPart.equals("body")) {
            answer = in.getBody();
        }
    } else if (uri.equals(OUT_NAMESPACE)) {
        if (exchange.get().hasOut()) {
            Message out = exchange.get().getOut();
            answer = out.getHeader(localPart);
            if (answer == null && localPart.equals("body")) {
                answer = out.getBody();
            }
        }
    }
    // if we return null, then the JDK default XPathEngine will throw an exception
    if (answer == null) {
        return "";
    } else {
        return answer;
    }
}
Also used : Message(org.apache.camel.Message)

Example 85 with Message

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

the class DefaultProducerTemplate method createSetBodyProcessor.

protected Processor createSetBodyProcessor(final Object body) {
    return new Processor() {

        public void process(Exchange exchange) {
            Message in = exchange.getIn();
            in.setBody(body);
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) ConvertBodyProcessor(org.apache.camel.processor.ConvertBodyProcessor) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message)

Aggregations

Message (org.apache.camel.Message)721 Exchange (org.apache.camel.Exchange)341 Test (org.junit.Test)215 Processor (org.apache.camel.Processor)118 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)104 DefaultMessage (org.apache.camel.impl.DefaultMessage)51 DefaultExchange (org.apache.camel.impl.DefaultExchange)44 Endpoint (org.apache.camel.Endpoint)40 Response (javax.ws.rs.core.Response)38 InputStream (java.io.InputStream)36 HashMap (java.util.HashMap)35 ArrayList (java.util.ArrayList)26 RouteBuilder (org.apache.camel.builder.RouteBuilder)25 Customer (org.apache.camel.component.cxf.jaxrs.testbean.Customer)25 ActionResponse (org.openstack4j.model.common.ActionResponse)25 IOException (java.io.IOException)24 Map (java.util.Map)24 DataHandler (javax.activation.DataHandler)21 Producer (org.apache.camel.Producer)21 CxfOperationException (org.apache.camel.component.cxf.CxfOperationException)19