1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.config.i18n.CoreMessages; |
14 | |
import org.mule.impl.MuleMessage; |
15 | |
import org.mule.impl.endpoint.MuleEndpointURI; |
16 | |
import org.mule.umo.UMOException; |
17 | |
import org.mule.umo.UMOFilter; |
18 | |
import org.mule.umo.UMOMessage; |
19 | |
import org.mule.umo.UMOSession; |
20 | |
import org.mule.umo.endpoint.EndpointException; |
21 | |
import org.mule.umo.endpoint.UMOEndpoint; |
22 | |
import org.mule.umo.endpoint.UMOEndpointURI; |
23 | |
import org.mule.umo.routing.CouldNotRouteOutboundMessageException; |
24 | |
import org.mule.umo.routing.RoutePathNotFoundException; |
25 | |
import org.mule.umo.routing.RoutingException; |
26 | |
import org.mule.umo.transformer.TransformerException; |
27 | |
import org.mule.umo.transformer.UMOTransformer; |
28 | |
import org.mule.util.TemplateParser; |
29 | |
|
30 | |
import java.util.HashMap; |
31 | |
import java.util.Iterator; |
32 | |
import java.util.Map; |
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 468 | public class FilteringOutboundRouter extends AbstractOutboundRouter |
40 | |
{ |
41 | |
private UMOTransformer transformer; |
42 | |
|
43 | |
private UMOFilter filter; |
44 | |
|
45 | 468 | private boolean useTemplates = false; |
46 | |
|
47 | |
|
48 | 468 | private TemplateParser parser = TemplateParser.createSquareBracesStyleParser(); |
49 | |
|
50 | |
public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) |
51 | |
throws RoutingException |
52 | |
{ |
53 | 12 | UMOMessage result = null; |
54 | |
|
55 | 12 | if (endpoints == null || endpoints.size() == 0) |
56 | |
{ |
57 | 0 | throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), message, null); |
58 | |
} |
59 | |
|
60 | 12 | UMOEndpoint ep = getEndpoint(0, message); |
61 | |
|
62 | |
try |
63 | |
{ |
64 | 12 | if (synchronous) |
65 | |
{ |
66 | 2 | result = send(session, message, ep); |
67 | |
} |
68 | |
else |
69 | |
{ |
70 | 10 | dispatch(session, message, ep); |
71 | |
} |
72 | |
} |
73 | 0 | catch (UMOException e) |
74 | |
{ |
75 | 0 | throw new CouldNotRouteOutboundMessageException(message, ep, e); |
76 | 12 | } |
77 | 12 | return result; |
78 | |
} |
79 | |
|
80 | |
public UMOFilter getFilter() |
81 | |
{ |
82 | 142 | return filter; |
83 | |
} |
84 | |
|
85 | |
public void setFilter(UMOFilter filter) |
86 | |
{ |
87 | 36 | this.filter = filter; |
88 | 36 | } |
89 | |
|
90 | |
public boolean isMatch(UMOMessage message) throws RoutingException |
91 | |
{ |
92 | 70 | if (getFilter() == null) |
93 | |
{ |
94 | 16 | return true; |
95 | |
} |
96 | 54 | if (transformer != null) |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 2 | Object payload = transformer.transform(message.getPayload()); |
101 | 2 | message = new MuleMessage(payload, message); |
102 | |
} |
103 | 0 | catch (TransformerException e) |
104 | |
{ |
105 | 0 | throw new RoutingException( |
106 | |
CoreMessages.transformFailedBeforeFilter(), |
107 | |
message, (UMOEndpoint) endpoints.get(0), e); |
108 | 2 | } |
109 | |
} |
110 | 54 | return getFilter().accept(message); |
111 | |
} |
112 | |
|
113 | |
public UMOTransformer getTransformer() |
114 | |
{ |
115 | 0 | return transformer; |
116 | |
} |
117 | |
|
118 | |
public void setTransformer(UMOTransformer transformer) |
119 | |
{ |
120 | 2 | this.transformer = transformer; |
121 | 2 | } |
122 | |
|
123 | |
public void addEndpoint(UMOEndpoint endpoint) |
124 | |
{ |
125 | 476 | if (!useTemplates && parser.isContainsTemplate(endpoint.getEndpointURI().toString())) |
126 | |
{ |
127 | 4 | useTemplates = true; |
128 | |
} |
129 | 476 | super.addEndpoint(endpoint); |
130 | 476 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
public UMOEndpoint getEndpoint(int index, UMOMessage message) |
144 | |
throws CouldNotRouteOutboundMessageException |
145 | |
{ |
146 | 52 | if (!useTemplates) |
147 | |
{ |
148 | 42 | return (UMOEndpoint) endpoints.get(index); |
149 | |
} |
150 | |
else |
151 | |
{ |
152 | 10 | UMOEndpoint ep = (UMOEndpoint) endpoints.get(index); |
153 | 10 | String uri = ep.getEndpointURI().toString(); |
154 | |
|
155 | 10 | if (logger.isDebugEnabled()) |
156 | |
{ |
157 | 0 | logger.debug("Uri before parsing is: " + uri); |
158 | |
} |
159 | |
|
160 | 10 | Map props = new HashMap(); |
161 | |
|
162 | |
|
163 | 10 | props.putAll(ep.getProperties()); |
164 | 10 | for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();) |
165 | |
{ |
166 | 22 | String propertyKey = (String) iterator.next(); |
167 | 22 | props.put(propertyKey, message.getProperty(propertyKey)); |
168 | 22 | } |
169 | |
|
170 | 10 | String newUriString = parser.parse(props, uri); |
171 | 10 | if (logger.isDebugEnabled()) |
172 | |
{ |
173 | 0 | logger.debug("Uri after parsing is: " + uri); |
174 | |
} |
175 | |
|
176 | |
try |
177 | |
{ |
178 | 10 | UMOEndpointURI newUri = new MuleEndpointURI(newUriString); |
179 | 10 | if (!newUri.getScheme().equalsIgnoreCase(ep.getEndpointURI().getScheme())) |
180 | |
{ |
181 | 0 | throw new CouldNotRouteOutboundMessageException(CoreMessages.schemeCannotChangeForRouter( |
182 | |
ep.getEndpointURI().getScheme(), newUri.getScheme()), message, ep); |
183 | |
} |
184 | |
|
185 | 10 | UMOEndpoint resolvedEndpoint = (UMOEndpoint) ep.clone(); |
186 | 10 | resolvedEndpoint.setEndpointURI(newUri); |
187 | 10 | return resolvedEndpoint; |
188 | |
} |
189 | 0 | catch (EndpointException e) |
190 | |
{ |
191 | 0 | throw new CouldNotRouteOutboundMessageException( |
192 | |
CoreMessages.templateCausedMalformedEndpoint(uri, newUriString), |
193 | |
message, ep, e); |
194 | |
} |
195 | |
} |
196 | |
} |
197 | |
|
198 | |
public boolean isUseTemplates() |
199 | |
{ |
200 | 4 | return useTemplates; |
201 | |
} |
202 | |
|
203 | |
public void setUseTemplates(boolean useTemplates) |
204 | |
{ |
205 | 0 | this.useTemplates = useTemplates; |
206 | 0 | } |
207 | |
} |