1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
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 | |
|
23 | |
|
24 | 0 | public class ContentBasedRouter extends AbstractOutboundRouter |
25 | |
{ |
26 | |
@Override |
27 | |
public MuleEvent route(MuleEvent theEvent) throws MessagingException |
28 | |
{ |
29 | 0 | MuleMessage message = theEvent.getMessage(); |
30 | |
|
31 | 0 | for (MessageProcessor target : routes) |
32 | |
{ |
33 | |
try |
34 | |
{ |
35 | 0 | if (isMatch(message)) |
36 | |
{ |
37 | 0 | MuleEvent event = RequestContext.cloneAndUpdateEventEndpoint(theEvent, target); |
38 | 0 | return target.process(event); |
39 | |
} |
40 | |
} |
41 | 0 | catch (MuleException e) |
42 | |
{ |
43 | 0 | throw new MessagingException(e.getI18nMessage(), theEvent, e); |
44 | 0 | } |
45 | |
} |
46 | |
|
47 | 0 | throw new RuntimeException("Event not processed"); |
48 | |
} |
49 | |
|
50 | |
public boolean isMatch(MuleMessage message) throws MuleException |
51 | |
{ |
52 | 0 | for (MessageProcessor target : routes) |
53 | |
{ |
54 | 0 | if (target instanceof ImmutableEndpoint) |
55 | |
{ |
56 | 0 | ImmutableEndpoint endpoint = (ImmutableEndpoint)target; |
57 | 0 | if (endpoint.getFilter() == null || endpoint.getFilter().accept(message)) |
58 | |
{ |
59 | 0 | return true; |
60 | |
} |
61 | 0 | } |
62 | |
else |
63 | |
{ |
64 | 0 | return true; |
65 | |
} |
66 | |
} |
67 | 0 | return false; |
68 | |
} |
69 | |
} |