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.ArrayList; |
16 | |
import java.util.Collections; |
17 | |
import java.util.List; |
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 | 0 | public class MessageAttachmentsListExpressionEvaluator implements ExpressionEvaluator |
36 | |
{ |
37 | |
public static final String NAME = "attachments-list"; |
38 | |
|
39 | |
public Object evaluate(String expression, MuleMessage message) |
40 | |
{ |
41 | |
boolean required; |
42 | |
|
43 | |
List<DataHandler> result; |
44 | |
|
45 | 0 | if (expression.contains(ALL_ARGUMENT)) |
46 | |
{ |
47 | 0 | WildcardFilter filter = new WildcardFilter(expression); |
48 | 0 | result = new ArrayList<DataHandler>(message.getInboundAttachmentNames().size()); |
49 | 0 | for (String name : message.getInboundAttachmentNames()) |
50 | |
{ |
51 | 0 | if (filter.accept(name)) |
52 | |
{ |
53 | 0 | result.add(message.getInboundAttachment(name)); |
54 | |
} |
55 | |
} |
56 | 0 | } |
57 | |
else |
58 | |
{ |
59 | 0 | StringTokenizer tokenizer = new StringTokenizer(expression, DELIM); |
60 | 0 | result = new ArrayList<DataHandler>(tokenizer.countTokens()); |
61 | 0 | while (tokenizer.hasMoreTokens()) |
62 | |
{ |
63 | 0 | String s = tokenizer.nextToken(); |
64 | 0 | s = s.trim(); |
65 | 0 | if (s.endsWith(OPTIONAL_ARGUMENT)) |
66 | |
{ |
67 | 0 | s = s.substring(0, s.length() - OPTIONAL_ARGUMENT.length()); |
68 | 0 | required = false; |
69 | |
} |
70 | |
else |
71 | |
{ |
72 | 0 | required = true; |
73 | |
} |
74 | 0 | DataHandler val = message.getInboundAttachment(s); |
75 | 0 | if (val != null) |
76 | |
{ |
77 | 0 | result.add(val); |
78 | |
} |
79 | 0 | else if (required) |
80 | |
{ |
81 | 0 | throw new RequiredValueException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression)); |
82 | |
} |
83 | 0 | } |
84 | |
} |
85 | 0 | if (result.size() == 0) |
86 | |
{ |
87 | 0 | return Collections.unmodifiableList(Collections.<DataHandler>emptyList()); |
88 | |
} |
89 | |
else |
90 | |
{ |
91 | 0 | return Collections.unmodifiableList(result); |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
public String getName() |
99 | |
{ |
100 | 0 | return NAME; |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
public void setName(String name) |
107 | |
{ |
108 | 0 | throw new UnsupportedOperationException("setName"); |
109 | |
} |
110 | |
} |