1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.api.MuleEvent; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.MuleMessage; |
16 | |
import org.mule.api.endpoint.EndpointException; |
17 | |
import org.mule.api.endpoint.EndpointURI; |
18 | |
import org.mule.api.endpoint.ImmutableEndpoint; |
19 | |
import org.mule.api.endpoint.OutboundEndpoint; |
20 | |
import org.mule.api.expression.ExpressionManager; |
21 | |
import org.mule.api.lifecycle.InitialisationException; |
22 | |
import org.mule.api.processor.MessageProcessor; |
23 | |
import org.mule.api.routing.CouldNotRouteOutboundMessageException; |
24 | |
import org.mule.api.routing.RoutePathNotFoundException; |
25 | |
import org.mule.api.routing.RoutingException; |
26 | |
import org.mule.api.routing.TransformingMatchable; |
27 | |
import org.mule.api.routing.filter.Filter; |
28 | |
import org.mule.api.transformer.Transformer; |
29 | |
import org.mule.config.i18n.CoreMessages; |
30 | |
import org.mule.endpoint.DynamicURIOutboundEndpoint; |
31 | |
import org.mule.endpoint.MuleEndpointURI; |
32 | |
import org.mule.util.TemplateParser; |
33 | |
|
34 | |
import java.util.HashMap; |
35 | |
import java.util.LinkedList; |
36 | |
import java.util.List; |
37 | |
import java.util.Map; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | 0 | public class FilteringOutboundRouter extends AbstractOutboundRouter implements TransformingMatchable |
45 | |
{ |
46 | |
protected ExpressionManager expressionManager; |
47 | |
|
48 | 0 | private List<Transformer> transformers = new LinkedList<Transformer>(); |
49 | |
|
50 | |
private Filter filter; |
51 | |
|
52 | 0 | private boolean useTemplates = true; |
53 | |
|
54 | |
|
55 | 0 | private TemplateParser parser = TemplateParser.createSquareBracesStyleParser(); |
56 | |
|
57 | |
@Override |
58 | |
public void initialise() throws InitialisationException |
59 | |
{ |
60 | 0 | super.initialise(); |
61 | 0 | expressionManager = muleContext.getExpressionManager(); |
62 | 0 | } |
63 | |
|
64 | |
@Override |
65 | |
public MuleEvent route(MuleEvent event) throws RoutingException |
66 | |
{ |
67 | |
MuleEvent result; |
68 | |
|
69 | 0 | MuleMessage message = event.getMessage(); |
70 | |
|
71 | 0 | if (routes == null || routes.size() == 0) |
72 | |
{ |
73 | 0 | throw new RoutePathNotFoundException(CoreMessages.noEndpointsForRouter(), event, null); |
74 | |
} |
75 | |
|
76 | 0 | MessageProcessor ep = getRoute(0, event); |
77 | |
|
78 | |
try |
79 | |
{ |
80 | 0 | result = sendRequest(event, message, ep, true); |
81 | |
} |
82 | 0 | catch (RoutingException e) |
83 | |
{ |
84 | 0 | throw e; |
85 | |
} |
86 | 0 | catch (MuleException e) |
87 | |
{ |
88 | 0 | throw new CouldNotRouteOutboundMessageException(event, ep, e); |
89 | 0 | } |
90 | 0 | return result; |
91 | |
} |
92 | |
|
93 | |
public Filter getFilter() |
94 | |
{ |
95 | 0 | return filter; |
96 | |
} |
97 | |
|
98 | |
public void setFilter(Filter filter) |
99 | |
{ |
100 | 0 | this.filter = filter; |
101 | 0 | } |
102 | |
|
103 | |
public boolean isMatch(MuleMessage message) throws MuleException |
104 | |
{ |
105 | 0 | if (getFilter() == null) |
106 | |
{ |
107 | 0 | return true; |
108 | |
} |
109 | |
|
110 | 0 | message.applyTransformers(null, transformers); |
111 | |
|
112 | 0 | return getFilter().accept(message); |
113 | |
} |
114 | |
|
115 | |
public List<Transformer> getTransformers() |
116 | |
{ |
117 | 0 | return transformers; |
118 | |
} |
119 | |
|
120 | |
public void setTransformers(List<Transformer> transformers) |
121 | |
{ |
122 | 0 | this.transformers = transformers; |
123 | 0 | } |
124 | |
|
125 | |
@Override |
126 | |
public synchronized void addRoute(MessageProcessor target) throws MuleException |
127 | |
{ |
128 | 0 | if (!useTemplates) |
129 | |
{ |
130 | 0 | if (target instanceof ImmutableEndpoint) |
131 | |
{ |
132 | 0 | ImmutableEndpoint endpoint = (ImmutableEndpoint) target; |
133 | 0 | if (parser.isContainsTemplate(endpoint.getEndpointURI().toString())) |
134 | |
{ |
135 | 0 | useTemplates = true; |
136 | |
} |
137 | |
} |
138 | |
} |
139 | 0 | super.addRoute(target); |
140 | 0 | } |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
public MessageProcessor getRoute(int index, MuleEvent event) throws CouldNotRouteOutboundMessageException |
154 | |
{ |
155 | 0 | if (!useTemplates) |
156 | |
{ |
157 | 0 | return routes.get(index); |
158 | |
} |
159 | |
else |
160 | |
{ |
161 | 0 | MuleMessage message = event.getMessage(); |
162 | 0 | MessageProcessor mp = routes.get(index); |
163 | 0 | if (!(mp instanceof ImmutableEndpoint)) |
164 | |
{ |
165 | 0 | return routes.get(index); |
166 | |
} |
167 | 0 | OutboundEndpoint ep = (OutboundEndpoint) mp; |
168 | 0 | String uri = ep.getAddress(); |
169 | |
|
170 | 0 | if (logger.isDebugEnabled()) |
171 | |
{ |
172 | 0 | logger.debug("Uri before parsing is: " + uri); |
173 | |
} |
174 | |
|
175 | 0 | Map<String, Object> props = new HashMap<String, Object>(); |
176 | |
|
177 | |
|
178 | 0 | props.putAll(ep.getProperties()); |
179 | 0 | for (String propertyKey : message.getOutboundPropertyNames()) |
180 | |
{ |
181 | 0 | Object value = message.getOutboundProperty(propertyKey); |
182 | 0 | props.put(propertyKey, value); |
183 | 0 | } |
184 | |
|
185 | 0 | propagateMagicProperties(message, message); |
186 | |
|
187 | 0 | if (!parser.isContainsTemplate(uri)) |
188 | |
{ |
189 | 0 | logger.debug("Uri does not contain template(s)"); |
190 | 0 | return ep; |
191 | |
} |
192 | |
else |
193 | |
{ |
194 | |
|
195 | 0 | String newUriString = parser.parse(props, uri); |
196 | 0 | if (parser.isContainsTemplate(newUriString)) |
197 | |
{ |
198 | 0 | newUriString = this.getMuleContext().getExpressionManager().parse(newUriString, message, true); |
199 | |
} |
200 | 0 | if (logger.isDebugEnabled()) |
201 | |
{ |
202 | 0 | logger.debug("Uri after parsing is: " + uri); |
203 | |
} |
204 | |
try |
205 | |
{ |
206 | 0 | EndpointURI newUri = new MuleEndpointURI(newUriString, muleContext); |
207 | 0 | EndpointURI endpointURI = ep.getEndpointURI(); |
208 | 0 | if (endpointURI != null && !newUri.getScheme().equalsIgnoreCase(endpointURI.getScheme())) |
209 | |
{ |
210 | 0 | throw new CouldNotRouteOutboundMessageException( |
211 | |
CoreMessages.schemeCannotChangeForRouter(ep.getEndpointURI().getScheme(), |
212 | |
newUri.getScheme()), event, ep); |
213 | |
} |
214 | 0 | newUri.initialise(); |
215 | |
|
216 | 0 | return new DynamicURIOutboundEndpoint(ep, newUri); |
217 | |
} |
218 | 0 | catch (EndpointException e) |
219 | |
{ |
220 | 0 | throw new CouldNotRouteOutboundMessageException( |
221 | |
CoreMessages.templateCausedMalformedEndpoint(uri, newUriString), event, ep, e); |
222 | |
} |
223 | 0 | catch (InitialisationException e) |
224 | |
{ |
225 | 0 | throw new CouldNotRouteOutboundMessageException( |
226 | |
CoreMessages.templateCausedMalformedEndpoint(uri, newUriString), event, ep, e); |
227 | |
} |
228 | |
} |
229 | |
} |
230 | |
} |
231 | |
|
232 | |
public boolean isUseTemplates() |
233 | |
{ |
234 | 0 | return useTemplates; |
235 | |
} |
236 | |
|
237 | |
public void setUseTemplates(boolean useTemplates) |
238 | |
{ |
239 | 0 | this.useTemplates = useTemplates; |
240 | 0 | } |
241 | |
|
242 | |
public boolean isTransformBeforeMatch() |
243 | |
{ |
244 | 0 | return !transformers.isEmpty(); |
245 | |
} |
246 | |
|
247 | |
} |