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