01: /*
02: * $Id: MessageAttachmentsListExpressionEvaluator.java 11297 2008-03-09 20:11:02Z rossmason $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
10:
11: package org.mule.util.expression;
12:
13: import org.mule.api.transport.MessageAdapter;
14: import org.mule.config.i18n.CoreMessages;
15:
16: import java.util.ArrayList;
17: import java.util.List;
18: import java.util.StringTokenizer;
19:
20: /**
21: * Looks up the attachment(s) on the message using the expression given. The expression can contain a comma-separated list
22: * of header names to lookup. A {@link java.util.List} of values is returned.
23: *
24: * @see MessageAttachmentsExpressionEvaluator
25: * @see MessageAttachmentExpressionEvaluator
26: * @see ExpressionEvaluator
27: * @see ExpressionEvaluatorManager
28: */
29: public class MessageAttachmentsListExpressionEvaluator implements
30: ExpressionEvaluator {
31: public static final String NAME = "attachments-list";
32: public static final String DELIM = ",";
33:
34: public Object evaluate(String expression, Object message) {
35: boolean required = false;
36:
37: //This is a bit of a hack to manage required headers
38: if (expression.endsWith("required")) {
39: required = true;
40: expression = expression.substring(0,
41: expression.length() - 8);
42: }
43:
44: if (message instanceof MessageAdapter) {
45:
46: StringTokenizer tokenizer = new StringTokenizer(expression,
47: DELIM);
48: List result = new ArrayList(tokenizer.countTokens());
49: while (tokenizer.hasMoreTokens()) {
50: String s = tokenizer.nextToken();
51: s = s.trim();
52: Object val = ((MessageAdapter) message)
53: .getAttachment(s);
54: if (val != null) {
55: result.add(val);
56: } else if (required) {
57: throw new ExpressionRuntimeException(CoreMessages
58: .expressionEvaluatorReturnedNull(NAME,
59: expression));
60: }
61: }
62: if (result.size() == 0) {
63: return null;
64: } else {
65: return result;
66: }
67: }
68: return null;
69: }
70:
71: /** {@inheritDoc} */
72: public String getName() {
73: return NAME;
74: }
75:
76: /** {@inheritDoc} */
77: public void setName(String name) {
78: throw new UnsupportedOperationException("setName");
79: }
80: }
|