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 | 0 | public class FilteringOutboundRouter extends AbstractOutboundRouter |
40 | |
{ |
41 | |
private UMOTransformer transformer; |
42 | |
|
43 | |
private UMOFilter filter; |
44 | |
|
45 | 0 | private boolean useTemplates = false; |
46 | |
|
47 | |
|
48 | 0 | private TemplateParser parser = TemplateParser.createSquareBracesStyleParser(); |
49 | |
|
50 | |
public UMOMessage route(UMOMessage message, UMOSession session, boolean synchronous) |
51 | |
throws RoutingException |
52 | |
{ |
53 | 0 | UMOMessage result = null; |
54 | |
|
55 | 0 | if (endpoints == null || endpoints.size() == 0) |
56 | |
{ |
57 | 0 | throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), message, null); |
58 | |
} |
59 | |
|
60 | 0 | UMOEndpoint ep = getEndpoint(0, message); |
61 | |
|
62 | |
try |
63 | |
{ |
64 | 0 | if (synchronous) |
65 | |
{ |
66 | 0 | result = send(session, message, ep); |
67 | |
} |
68 | |
else |
69 | |
{ |
70 | 0 | dispatch(session, message, ep); |
71 | |
} |
72 | |
} |
73 | 0 | catch (UMOException e) |
74 | |
{ |
75 | 0 | throw new CouldNotRouteOutboundMessageException(message, ep, e); |
76 | 0 | } |
77 | 0 | return result; |
78 | |
} |
79 | |
|
80 | |
public UMOFilter getFilter() |
81 | |
{ |
82 | 0 | return filter; |
83 | |
} |
84 | |
|
85 | |
public void setFilter(UMOFilter filter) |
86 | |
{ |
87 | 0 | this.filter = filter; |
88 | 0 | } |
89 | |
|
90 | |
public boolean isMatch(UMOMessage message) throws RoutingException |
91 | |
{ |
92 | 0 | if (getFilter() == null) |
93 | |
{ |
94 | 0 | return true; |
95 | |
} |
96 | 0 | if (transformer != null) |
97 | |
{ |
98 | |
try |
99 | |
{ |
100 | 0 | Object payload = transformer.transform(message.getPayload()); |
101 | 0 | 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 | 0 | } |
109 | |
} |
110 | 0 | 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 | 0 | this.transformer = transformer; |
121 | 0 | } |
122 | |
|
123 | |
public void addEndpoint(UMOEndpoint endpoint) |
124 | |
{ |
125 | 0 | if (!useTemplates && parser.isContainsTemplate(endpoint.getEndpointURI().toString())) |
126 | |
{ |
127 | 0 | useTemplates = true; |
128 | |
} |
129 | 0 | super.addEndpoint(endpoint); |
130 | 0 | } |
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 | 0 | if (!useTemplates) |
147 | |
{ |
148 | 0 | return (UMOEndpoint) endpoints.get(index); |
149 | |
} |
150 | |
else |
151 | |
{ |
152 | 0 | UMOEndpoint ep = (UMOEndpoint) endpoints.get(index); |
153 | 0 | String uri = ep.getEndpointURI().toString(); |
154 | 0 | if (logger.isDebugEnabled()) |
155 | |
{ |
156 | 0 | logger.debug("Uri before parsing is: " + uri); |
157 | |
} |
158 | 0 | Map props = new HashMap(); |
159 | |
|
160 | |
|
161 | 0 | props.putAll(ep.getProperties()); |
162 | 0 | for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();) |
163 | |
{ |
164 | 0 | String propertyKey = (String) iterator.next(); |
165 | 0 | props.put(propertyKey, message.getProperty(propertyKey)); |
166 | |
} |
167 | 0 | String newUriString = parser.parse(props, uri); |
168 | 0 | if (logger.isDebugEnabled()) |
169 | |
{ |
170 | 0 | logger.debug("Uri after parsing is: " + uri); |
171 | |
} |
172 | |
try |
173 | |
{ |
174 | 0 | UMOEndpointURI newUri = new MuleEndpointURI(newUriString); |
175 | 0 | if (!newUri.getScheme().equalsIgnoreCase(ep.getEndpointURI().getScheme())) |
176 | |
{ |
177 | 0 | throw new CouldNotRouteOutboundMessageException( |
178 | |
CoreMessages.schemeCannotChangeForRouter(ep.getEndpointURI().getScheme(), |
179 | |
newUri.getScheme()), message, ep); |
180 | |
} |
181 | 0 | ep.setEndpointURI(newUri); |
182 | |
} |
183 | 0 | catch (EndpointException e) |
184 | |
{ |
185 | 0 | throw new CouldNotRouteOutboundMessageException( |
186 | |
CoreMessages.templateCausedMalformedEndpoint(uri, newUriString), |
187 | |
message, ep, e); |
188 | 0 | } |
189 | |
|
190 | 0 | return ep; |
191 | |
} |
192 | |
} |
193 | |
|
194 | |
public boolean isUseTemplates() |
195 | |
{ |
196 | 0 | return useTemplates; |
197 | |
} |
198 | |
|
199 | |
public void setUseTemplates(boolean useTemplates) |
200 | |
{ |
201 | 0 | this.useTemplates = useTemplates; |
202 | 0 | } |
203 | |
} |