1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.expression; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.expression.ExpressionEvaluator; |
15 | |
import org.mule.api.expression.RequiredValueException; |
16 | |
import org.mule.config.i18n.CoreMessages; |
17 | |
import org.mule.routing.filters.WildcardFilter; |
18 | |
|
19 | |
import java.util.Collections; |
20 | |
import java.util.HashMap; |
21 | |
import java.util.Map; |
22 | |
import java.util.StringTokenizer; |
23 | |
|
24 | |
import javax.activation.DataHandler; |
25 | |
|
26 | |
import static org.mule.expression.ExpressionConstants.ALL_ARGUMENT; |
27 | |
import static org.mule.expression.ExpressionConstants.DELIM; |
28 | |
import static org.mule.expression.ExpressionConstants.OPTIONAL_ARGUMENT; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 0 | public class MessageAttachmentsExpressionEvaluator implements ExpressionEvaluator |
40 | |
{ |
41 | |
public static final String NAME = "attachments"; |
42 | |
|
43 | |
public Object evaluate(String expression, MuleMessage message) |
44 | |
{ |
45 | |
boolean required; |
46 | |
|
47 | |
Map<String, DataHandler> result; |
48 | |
|
49 | 0 | if (expression.contains(ALL_ARGUMENT)) |
50 | |
{ |
51 | 0 | WildcardFilter filter = new WildcardFilter(expression); |
52 | 0 | result = new HashMap<String, DataHandler>(message.getInboundAttachmentNames().size()); |
53 | 0 | for (String name : message.getInboundAttachmentNames()) |
54 | |
{ |
55 | 0 | if (filter.accept(name)) |
56 | |
{ |
57 | 0 | result.put(name, message.getInboundAttachment(name)); |
58 | |
} |
59 | |
} |
60 | 0 | } |
61 | |
else |
62 | |
{ |
63 | 0 | StringTokenizer tokenizer = new StringTokenizer(expression, DELIM); |
64 | 0 | result = new HashMap<String, DataHandler>(tokenizer.countTokens()); |
65 | 0 | while (tokenizer.hasMoreTokens()) |
66 | |
{ |
67 | 0 | String s = tokenizer.nextToken(); |
68 | 0 | s = s.trim(); |
69 | 0 | if (s.endsWith(OPTIONAL_ARGUMENT)) |
70 | |
{ |
71 | 0 | s = s.substring(0, s.length() - OPTIONAL_ARGUMENT.length()); |
72 | 0 | required = false; |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | 0 | required = true; |
77 | |
} |
78 | 0 | DataHandler val = message.getInboundAttachment(s); |
79 | 0 | if (val != null) |
80 | |
{ |
81 | 0 | result.put(s, val); |
82 | |
} |
83 | 0 | else if (required) |
84 | |
{ |
85 | 0 | throw new RequiredValueException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression)); |
86 | |
} |
87 | 0 | } |
88 | |
} |
89 | 0 | if (result.size() == 0) |
90 | |
{ |
91 | 0 | return Collections.unmodifiableMap(Collections.<String, DataHandler>emptyMap()); |
92 | |
} |
93 | |
else |
94 | |
{ |
95 | 0 | return Collections.unmodifiableMap(result); |
96 | |
} |
97 | |
} |
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public String getName() |
103 | |
{ |
104 | 0 | return NAME; |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
public void setName(String name) |
111 | |
{ |
112 | 0 | throw new UnsupportedOperationException(); |
113 | |
} |
114 | |
} |