1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.builder.xml; |
19 |
|
|
20 |
|
import org.apache.camel.Exchange; |
21 |
|
import org.apache.camel.Message; |
22 |
|
import org.apache.commons.logging.Log; |
23 |
|
import org.apache.commons.logging.LogFactory; |
24 |
|
|
25 |
|
import javax.xml.namespace.QName; |
26 |
|
import javax.xml.xpath.XPathVariableResolver; |
27 |
|
import java.util.HashMap; |
28 |
|
import java.util.Map; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
16 |
public class MessageVariableResolver implements XPathVariableResolver { |
37 |
|
public static final String SYSTEM_PROPERTIES_NAMESPACE = "http://camel.apache.org/xml/variables/system-properties"; |
38 |
|
public static final String ENVIRONMENT_VARIABLES = "http://camel.apache.org/xml/variables/environment-variables"; |
39 |
|
public static final String EXCHANGE_PROPERTY = "http://camel.apache.org/xml/variables/exchange-property"; |
40 |
|
public static final String IN_HEADER = "http://camel.apache.org/xml/variables/in-header"; |
41 |
|
public static final String OUT_HEADER = "http://camel.apache.org/xml/variables/out-header"; |
42 |
|
|
43 |
1 |
private static final transient Log log = LogFactory.getLog(MessageVariableResolver.class); |
44 |
|
|
45 |
|
private Exchange exchange; |
46 |
16 |
private Map<String, Object> variables = new HashMap<String, Object>(); |
47 |
|
|
48 |
|
public Exchange getExchange() { |
49 |
0 |
return exchange; |
50 |
|
} |
51 |
|
|
52 |
|
public void setExchange(Exchange exchange) { |
53 |
21 |
this.exchange = exchange; |
54 |
21 |
} |
55 |
|
|
56 |
|
public Object resolveVariable(QName name) { |
57 |
5 |
String uri = name.getNamespaceURI(); |
58 |
5 |
String localPart = name.getLocalPart(); |
59 |
5 |
Object answer = null; |
60 |
|
|
61 |
5 |
if (uri == null || uri.length() == 0) { |
62 |
5 |
answer = variables.get(localPart); |
63 |
5 |
if (answer == null) { |
64 |
4 |
Message message = exchange.getIn(); |
65 |
4 |
if (message != null) { |
66 |
4 |
answer = message.getHeader(localPart); |
67 |
|
} |
68 |
4 |
if (answer == null) { |
69 |
0 |
answer = exchange.getProperty(localPart); |
70 |
|
} |
71 |
4 |
} |
72 |
|
} |
73 |
0 |
else if (uri.equals(SYSTEM_PROPERTIES_NAMESPACE)) { |
74 |
|
try { |
75 |
0 |
answer = System.getProperty(localPart); |
76 |
|
} |
77 |
0 |
catch (Exception e) { |
78 |
0 |
log.debug("Security exception evaluating system property: " + localPart + ". Reason: " + e, e); |
79 |
0 |
} |
80 |
0 |
} |
81 |
0 |
else if (uri.equals(ENVIRONMENT_VARIABLES)) { |
82 |
0 |
answer = System.getenv().get(localPart); |
83 |
0 |
} |
84 |
0 |
else if (uri.equals(EXCHANGE_PROPERTY)) { |
85 |
0 |
answer = exchange.getProperty(localPart); |
86 |
0 |
} |
87 |
0 |
else if (uri.equals(IN_HEADER)) { |
88 |
0 |
answer = exchange.getIn().getHeader(localPart); |
89 |
0 |
} |
90 |
0 |
else if (uri.equals(OUT_HEADER)) { |
91 |
0 |
answer = exchange.getOut().getHeader(localPart); |
92 |
|
} |
93 |
|
|
94 |
|
|
95 |
5 |
return answer; |
96 |
|
} |
97 |
|
|
98 |
|
public void addVariable(String localPart, Object value) { |
99 |
1 |
variables.put(localPart, value); |
100 |
1 |
} |
101 |
|
} |