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