View Javadoc

1   /*
2    * $Id: ContentBasedRouter.java 20875 2011-01-04 16:09:25Z aperepel $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  package org.mule.config.dsl.routers;
11  
12  import org.mule.RequestContext;
13  import org.mule.api.MessagingException;
14  import org.mule.api.MuleEvent;
15  import org.mule.api.MuleException;
16  import org.mule.api.MuleMessage;
17  import org.mule.api.endpoint.ImmutableEndpoint;
18  import org.mule.api.processor.MessageProcessor;
19  import org.mule.routing.outbound.AbstractOutboundRouter;
20  
21  /**
22   * TODO
23   */
24  public class ContentBasedRouter extends AbstractOutboundRouter
25  {
26      @Override
27      public MuleEvent route(MuleEvent theEvent) throws MessagingException
28      {
29          MuleMessage message = theEvent.getMessage();
30  
31          for (MessageProcessor target : routes)
32          {
33              try
34              {
35                  if (isMatch(message))
36                  {
37                      MuleEvent event = RequestContext.setEvent(theEvent);
38                      return target.process(event);
39                  }
40              }
41              catch (MuleException e)
42              {
43                  throw new MessagingException(e.getI18nMessage(), theEvent, e);
44              }
45          }
46          //TODO
47          throw new RuntimeException("Event not processed");
48      }
49  
50      public boolean isMatch(MuleMessage message) throws MuleException
51      {
52          for (MessageProcessor target : routes)
53          {
54              if (target instanceof ImmutableEndpoint)
55              {
56                  ImmutableEndpoint endpoint = (ImmutableEndpoint)target;
57                  if (endpoint.getFilter() == null || endpoint.getFilter().accept(message))
58                  {
59                      return true;
60                  }
61              }
62              else
63              {
64                  return true;
65              }
66          }
67          return false;
68      }
69  }