001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.matchers;
019:
020: import java.util.Collection;
021:
022: import javax.mail.MessagingException;
023:
024: import org.apache.mailet.GenericMatcher;
025: import org.apache.mailet.Mail;
026: import org.apache.mailet.MatcherConfig;
027:
028: /**
029: * <p>This Matcher determines if the mail contains the attribute specified in
030: * the condition and if the value answered when the method toString() is
031: * invoked on the attribute is equal to the String value specified in the
032: * condition. If both tests are true, all recipients are returned, else null.
033: * </p>
034: *
035: * <p>Notes:</p>
036: * <p>The current matcher implementation expects a single String value to match
037: * on. This matcher requires two values, the attribute name and attribute
038: * value. This requires some implicit rules to govern how the single value
039: * supplied to the matcher is parsed into two values.</p>
040: * <ul>
041: * <li>In the match condition, the split between the attribute name and the
042: * attribute value is made at the first comma. Attribute names that include
043: * a comma will parse incorrectly and therefore are not supported by this
044: * matcher.
045: * </li>
046: * <li>Leading and trailing spaces are removed from both the attribute name and
047: * attribute value specified in the condition and the tested attribute value in
048: * the mail prior to matching. Therefore, "abc" , " abc", "abc " and " abc "
049: * are considered equivalent.
050: * </li>
051: * <li>To test for an empty string, do not specify an attribute value after the
052: * comma.
053: * </li>
054: * </ul>
055: *
056: * <p>Sample configuration:</p>
057: * <pre><code>
058: * <mailet match="HasMailAttributeWithValue=name, value" class="<any-class>">
059: * </code></pre>
060: *
061: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
062: * @since 2.2.0
063: **/
064: public class HasMailAttributeWithValue extends GenericMatcher {
065:
066: /**
067: * The name of the attribute to match
068: */
069: private String fieldAttributeName;
070:
071: /**
072: * The value of the attribute to match
073: */
074: private String fieldAttributeValue;
075:
076: /**
077: * <p>Answers the recipients of the mail if the attribute is present,
078: * and has a toString() value equal to the configured value.</p>
079: *
080: * @see org.apache.mailet.Matcher#match(Mail)
081: */
082: public Collection match(Mail mail) throws MessagingException {
083: Object attributeValue = mail.getAttribute(getAttributeName());
084:
085: if (attributeValue != null
086: && attributeValue.toString().trim().equals(
087: getAttributeValue()))
088: return mail.getRecipients();
089: return null;
090: }
091:
092: /**
093: * Returns the attributeName.
094: * @return String
095: */
096: protected String getAttributeName() {
097: return fieldAttributeName;
098: }
099:
100: /**
101: * Returns the attributeValue.
102: * @return String
103: */
104: protected String getAttributeValue() {
105: return fieldAttributeValue;
106: }
107:
108: /**
109: * Sets the attributeName.
110: * @param attributeName The attributeName to set
111: */
112: protected void setAttributeName(String attributeName) {
113: fieldAttributeName = attributeName;
114: }
115:
116: /**
117: * Sets the attributeValue.
118: * @param attributeValue The attributeValue to set
119: */
120: protected void setAttributeValue(String attributeValue) {
121: fieldAttributeValue = attributeValue;
122: }
123:
124: /**
125: * @see org.apache.mailet.Matcher#init(MatcherConfig)
126: */
127: public void init(MatcherConfig config) throws MessagingException {
128: super .init(config);
129: String condition = config.getCondition().trim();
130: int commaPosition = condition.indexOf(',');
131:
132: if (-1 == commaPosition)
133: throw new MessagingException("Syntax Error. Missing ','.");
134:
135: if (0 == commaPosition)
136: throw new MessagingException(
137: "Syntax Error. Missing attribute name.");
138:
139: setAttributeName(condition.substring(0, commaPosition).trim());
140: setAttributeValue(condition.substring(commaPosition + 1).trim());
141: }
142:
143: /**
144: * Return a string describing this matcher.
145: *
146: * @return a string describing this matcher
147: */
148: public String getMatcherInfo() {
149: return "Has Mail Attribute With Value Matcher";
150: }
151: }
|