1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.outbound; |
12 | |
|
13 | |
import org.mule.impl.MuleMessage; |
14 | |
import org.mule.umo.UMOMessage; |
15 | |
import org.mule.umo.endpoint.UMOEndpoint; |
16 | |
import org.mule.util.ExceptionUtils; |
17 | |
import org.mule.util.IOUtils; |
18 | |
import org.mule.util.StringUtils; |
19 | |
|
20 | |
import java.io.InputStream; |
21 | |
import java.io.StringReader; |
22 | |
import java.util.HashMap; |
23 | |
import java.util.Iterator; |
24 | |
import java.util.LinkedList; |
25 | |
import java.util.List; |
26 | |
import java.util.Map; |
27 | |
|
28 | |
import org.dom4j.Document; |
29 | |
import org.dom4j.DocumentHelper; |
30 | |
import org.dom4j.Element; |
31 | |
import org.dom4j.Node; |
32 | |
import org.dom4j.XPath; |
33 | |
import org.dom4j.io.SAXReader; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 36 | public class FilteringXmlMessageSplitter extends AbstractMessageSplitter |
47 | |
{ |
48 | |
|
49 | |
public static final String APACHE_XML_FEATURES_VALIDATION_SCHEMA = "http://apache.org/xml/features/validation/schema"; |
50 | |
public static final String APACHE_XML_FEATURES_VALIDATION_SCHEMA_FULL_CHECKING = "http://apache.org/xml/features/validation/schema-full-checking"; |
51 | |
|
52 | |
|
53 | |
public static final String JAXP_PROPERTIES_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; |
54 | |
|
55 | |
|
56 | |
|
57 | |
public static final String JAXP_PROPERTIES_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; |
58 | |
public static final String JAXP_PROPERTIES_SCHEMA_LANGUAGE_VALUE = "http://www.w3.org/2001/XMLSchema"; |
59 | |
|
60 | 36 | protected final ThreadLocal propertiesContext = new ThreadLocal(); |
61 | 36 | protected final ThreadLocal nodesContext = new ThreadLocal(); |
62 | |
|
63 | 36 | protected volatile String splitExpression = ""; |
64 | 36 | protected volatile Map namespaces = null; |
65 | 36 | protected volatile boolean validateSchema = false; |
66 | 36 | protected volatile String externalSchemaLocation = ""; |
67 | |
|
68 | |
public void setSplitExpression(String splitExpression) |
69 | |
{ |
70 | 28 | this.splitExpression = StringUtils.trimToEmpty(splitExpression); |
71 | 28 | } |
72 | |
|
73 | |
public void setNamespaces(Map namespaces) |
74 | |
{ |
75 | 28 | this.namespaces = namespaces; |
76 | 28 | } |
77 | |
|
78 | |
public String getSplitExpression() |
79 | |
{ |
80 | 0 | return splitExpression; |
81 | |
} |
82 | |
|
83 | |
public boolean isValidateSchema() |
84 | |
{ |
85 | 32 | return validateSchema; |
86 | |
} |
87 | |
|
88 | |
public void setValidateSchema(boolean validateSchema) |
89 | |
{ |
90 | 34 | this.validateSchema = validateSchema; |
91 | 34 | } |
92 | |
|
93 | |
public String getExternalSchemaLocation() |
94 | |
{ |
95 | 28 | return externalSchemaLocation; |
96 | |
} |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public void setExternalSchemaLocation(String externalSchemaLocation) |
105 | |
{ |
106 | 34 | this.externalSchemaLocation = externalSchemaLocation; |
107 | 34 | } |
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
protected void initialise(UMOMessage message) |
117 | |
{ |
118 | 48 | super.initialise(message); |
119 | |
|
120 | 48 | if (logger.isDebugEnabled()) |
121 | |
{ |
122 | 0 | if (splitExpression.length() == 0) |
123 | |
{ |
124 | 0 | logger.warn("splitExpression is not specified, no processing will take place"); |
125 | |
} |
126 | |
else |
127 | |
{ |
128 | 0 | logger.debug("splitExpression is " + splitExpression); |
129 | |
} |
130 | |
} |
131 | |
|
132 | 48 | Object src = message.getPayload(); |
133 | |
|
134 | |
try |
135 | |
{ |
136 | 48 | if (src instanceof byte[]) |
137 | |
{ |
138 | 12 | src = new String((byte[])src); |
139 | |
} |
140 | |
|
141 | |
Document dom4jDoc; |
142 | |
|
143 | 48 | if (src instanceof String) |
144 | |
{ |
145 | 32 | String xml = (String)src; |
146 | 32 | SAXReader reader = new SAXReader(); |
147 | 32 | setDoSchemaValidation(reader, isValidateSchema()); |
148 | |
|
149 | 28 | dom4jDoc = reader.read(new StringReader(xml)); |
150 | 24 | } |
151 | 16 | else if (src instanceof org.dom4j.Document) |
152 | |
{ |
153 | 8 | dom4jDoc = (org.dom4j.Document)src; |
154 | |
} |
155 | |
else |
156 | |
{ |
157 | 8 | logger.error("Non-XML message payload: " + src.getClass().toString()); |
158 | 8 | return; |
159 | |
} |
160 | |
|
161 | 32 | if (dom4jDoc != null) |
162 | |
{ |
163 | 32 | if (splitExpression.length() > 0) |
164 | |
{ |
165 | 32 | XPath xpath = dom4jDoc.createXPath(splitExpression); |
166 | 32 | if (namespaces != null) |
167 | |
{ |
168 | 32 | xpath.setNamespaceURIs(namespaces); |
169 | |
} |
170 | |
|
171 | 32 | List foundNodes = xpath.selectNodes(dom4jDoc); |
172 | 32 | if (enableCorrelation != ENABLE_CORRELATION_NEVER) |
173 | |
{ |
174 | 28 | message.setCorrelationGroupSize(foundNodes.size()); |
175 | |
} |
176 | 32 | if (logger.isDebugEnabled()) |
177 | |
{ |
178 | 0 | logger.debug("Split into " + foundNodes.size()); |
179 | |
} |
180 | |
|
181 | 32 | List parts = new LinkedList(); |
182 | |
|
183 | |
|
184 | |
|
185 | 32 | for (Iterator iterator = foundNodes.iterator(); iterator.hasNext();) |
186 | |
{ |
187 | 88 | Node node = (Node)iterator.next(); |
188 | 88 | if (node instanceof Element) |
189 | |
{ |
190 | |
|
191 | |
|
192 | 88 | node = (Node)node.clone(); |
193 | 88 | parts.add(DocumentHelper.createDocument((Element)node)); |
194 | |
} |
195 | |
else |
196 | |
{ |
197 | 0 | logger.warn("Dcoument node: " + node.asXML() |
198 | |
+ " is not an element and thus is not a valid part"); |
199 | |
} |
200 | 88 | } |
201 | 32 | nodesContext.set(parts); |
202 | 32 | } |
203 | |
} |
204 | |
else |
205 | |
{ |
206 | 0 | logger.warn("Unsupported message type, ignoring"); |
207 | |
} |
208 | |
} |
209 | 8 | catch (Exception ex) |
210 | |
{ |
211 | 8 | throw new IllegalArgumentException("Failed to initialise the payload: " |
212 | |
+ ExceptionUtils.getStackTrace(ex)); |
213 | 32 | } |
214 | |
|
215 | 32 | Map theProperties = new HashMap(); |
216 | 32 | for (Iterator iterator = message.getPropertyNames().iterator(); iterator.hasNext();) |
217 | |
{ |
218 | 28 | String propertyKey = (String)iterator.next(); |
219 | 28 | theProperties.put(propertyKey, message.getProperty(propertyKey)); |
220 | 28 | } |
221 | 32 | propertiesContext.set(theProperties); |
222 | 32 | } |
223 | |
|
224 | |
|
225 | |
protected void cleanup() |
226 | |
{ |
227 | 44 | nodesContext.set(null); |
228 | 44 | propertiesContext.set(null); |
229 | 44 | super.cleanup(); |
230 | 44 | } |
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
protected UMOMessage getMessagePart(UMOMessage message, UMOEndpoint endpoint) |
242 | |
{ |
243 | 112 | List nodes = (List)nodesContext.get(); |
244 | |
|
245 | 112 | if (nodes == null) |
246 | |
{ |
247 | 12 | logger.error("Error: nodes are null"); |
248 | 12 | return null; |
249 | |
} |
250 | |
|
251 | 100 | for (Iterator i = nodes.iterator(); i.hasNext();) |
252 | |
{ |
253 | 40 | Document doc = (Document)i.next(); |
254 | |
|
255 | |
try |
256 | |
{ |
257 | 40 | Map theProperties = (Map)propertiesContext.get(); |
258 | 40 | UMOMessage result = new MuleMessage(doc, new HashMap(theProperties)); |
259 | |
|
260 | 40 | if (endpoint.getFilter() == null || endpoint.getFilter().accept(result)) |
261 | |
{ |
262 | 40 | if (logger.isDebugEnabled()) |
263 | |
{ |
264 | 0 | logger.debug("Endpoint filter matched for node " + i + " of " + nodes.size() |
265 | |
+ ". Routing message over: " + endpoint.getEndpointURI().toString()); |
266 | |
} |
267 | 40 | i.remove(); |
268 | 40 | return result; |
269 | |
} |
270 | |
else |
271 | |
{ |
272 | 0 | if (logger.isDebugEnabled()) |
273 | |
{ |
274 | 0 | logger.debug("Endpoint filter did not match, returning null"); |
275 | |
} |
276 | |
} |
277 | |
} |
278 | 0 | catch (Exception e) |
279 | |
{ |
280 | 0 | logger.error("Unable to create message for node at position " + i, e); |
281 | 0 | return null; |
282 | 0 | } |
283 | 0 | } |
284 | |
|
285 | 60 | return null; |
286 | |
} |
287 | |
|
288 | |
protected void setDoSchemaValidation(SAXReader reader, boolean validate) throws Exception |
289 | |
{ |
290 | 32 | reader.setValidation(validate); |
291 | 32 | reader.setFeature(APACHE_XML_FEATURES_VALIDATION_SCHEMA, validate); |
292 | 32 | reader.setFeature(APACHE_XML_FEATURES_VALIDATION_SCHEMA_FULL_CHECKING, true); |
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | 32 | if (!validate) |
299 | |
{ |
300 | 8 | return; |
301 | |
} |
302 | |
|
303 | 24 | InputStream xsdAsStream = IOUtils.getResourceAsStream(getExternalSchemaLocation(), getClass()); |
304 | 24 | if (xsdAsStream == null) |
305 | |
{ |
306 | 4 | throw new IllegalArgumentException("Couldn't find schema at " |
307 | |
+ getExternalSchemaLocation()); |
308 | |
} |
309 | |
|
310 | |
|
311 | |
|
312 | 20 | reader.setProperty(JAXP_PROPERTIES_SCHEMA_LANGUAGE, JAXP_PROPERTIES_SCHEMA_LANGUAGE_VALUE); |
313 | |
|
314 | |
|
315 | 20 | reader.setProperty(JAXP_PROPERTIES_SCHEMA_SOURCE, xsdAsStream); |
316 | 20 | } |
317 | |
} |