use of org.apache.jsieve.exception.SieveException in project zm-mailbox by Zimbra.
the class StringTest method executeBasic.
/*
* (non-Javadoc)
*
* @see
* org.apache.jsieve.tests.AbstractTest#executeBasic(org.apache.jsieve.mail.
* MailAdapter, org.apache.jsieve.Arguments, org.apache.jsieve.SieveContext)
*/
@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
if (mail instanceof DummyMailAdapter) {
return true;
}
if (!(mail instanceof ZimbraMailAdapter)) {
return false;
}
ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
String matchType = null;
String comparator = null;
String operator = null;
List<String> sourceValues = null;
List<String> keyValues = null;
boolean nextArgumentIsRelationalSign = false;
ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
boolean stop = false;
// <source: string-list> <key-list: string-list>
while (!stop && argumentsIter.hasNext()) {
Argument argument = argumentsIter.next();
if (argument instanceof TagArgument) {
final String tag = ((TagArgument) argument).getTag();
// [COMPARATOR]?
if (comparator == null && COMPARATOR_TAG.equalsIgnoreCase(tag)) {
// The next argument must be a stringlist
if (argumentsIter.hasNext()) {
argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
List<String> stringList = ((StringListArgument) argument).getList();
if (stringList.size() != 1) {
throw context.getCoordinate().syntaxException("Expecting exactly one String");
}
comparator = stringList.get(0);
} else {
throw context.getCoordinate().syntaxException("Expecting a StringList");
}
}
} else // [MATCH-TYPE]?
if (matchType == null && (IS_TAG.equalsIgnoreCase(tag) || CONTAINS_TAG.equalsIgnoreCase(tag) || MATCHES_TAG.equalsIgnoreCase(tag) || COUNT_TAG.equalsIgnoreCase(tag) || VALUE_TAG.equalsIgnoreCase(tag))) {
matchType = tag;
nextArgumentIsRelationalSign = true;
} else {
throw context.getCoordinate().syntaxException("Found unexpected TagArgument: \"" + tag + "\"");
}
} else {
if (nextArgumentIsRelationalSign && argument instanceof StringListArgument) {
String symbol = ((StringListArgument) argument).getList().get(0);
if (matchType != null && (GT_OP.equalsIgnoreCase(symbol) || GE_OP.equalsIgnoreCase(symbol) || LT_OP.equalsIgnoreCase(symbol) || LE_OP.equalsIgnoreCase(symbol) || EQ_OP.equalsIgnoreCase(symbol) || NE_OP.equalsIgnoreCase(symbol))) {
operator = symbol;
} else {
argumentsIter.previous();
stop = true;
}
nextArgumentIsRelationalSign = false;
} else {
// Stop when a non-tag argument is encountered
argumentsIter.previous();
stop = true;
}
}
}
// The next argument MUST be a string-list of header names
if (argumentsIter.hasNext()) {
final Argument argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
List<String> sourceStringList = ((StringListArgument) argument).getList();
if (null == sourceStringList || 0 == sourceStringList.size()) {
throw context.getCoordinate().syntaxException("Expecting a StringListof header names");
}
sourceValues = new ArrayList<String>();
for (String source : sourceStringList) {
sourceValues.add(FilterUtil.replaceVariables(mailAdapter, source));
}
}
}
// The next argument MUST be a string-list of keys
if (argumentsIter.hasNext()) {
final Argument argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
List<String> keyStringList = ((StringListArgument) argument).getList();
if (null == keyStringList || 0 == keyStringList.size()) {
throw context.getCoordinate().syntaxException("Expecting a StringList of keys");
}
keyValues = new ArrayList<String>();
for (String key : keyStringList) {
keyValues.add(FilterUtil.replaceVariables(mailAdapter, key));
}
}
}
if (argumentsIter.hasNext()) {
throw context.getCoordinate().syntaxException("Found unexpected arguments");
}
if (null == matchType) {
matchType = IS_TAG;
}
if (null == comparator) {
if (matchType.equalsIgnoreCase(VALUE_TAG) || matchType.equalsIgnoreCase(COUNT_TAG)) {
comparator = ASCII_NUMERIC_COMPARATOR;
} else {
comparator = ASCII_CASEMAP_COMPARATOR;
}
}
boolean result = match(mail, comparator, matchType, operator, sourceValues, keyValues, context);
if (result) {
if (matchType.equals(MatchTypeTags.MATCHES_TAG)) {
try {
HeaderTest.evaluateVarExp(mailAdapter, sourceValues, HeaderTest.SourceType.LITERAL, keyValues);
} catch (MessagingException e) {
throw new SieveException("Exception occured while evaluating variable expression.", e);
}
}
}
return result;
}
use of org.apache.jsieve.exception.SieveException in project zm-mailbox by Zimbra.
the class EnvelopeTest method executeBasic.
/**
* <p>
* From RFC 5228, Section 5.4...
* </p>
* <code>
* Syntax: envelope [COMPARATOR] [ADDRESS-PART] [MATCH-TYPE]
* <envelope-part: string-list> <key-list: string-list>
* </code>
*/
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
if (mail instanceof DummyMailAdapter) {
return true;
}
if (!(mail instanceof ZimbraMailAdapter)) {
return false;
}
ZimbraComparatorUtils.TestParameters params = ZimbraComparatorUtils.parseTestArguments(mail, arguments, context);
params.setKeys(HeaderTest.replaceVariables(params.getKeys(), mail));
params.setHeaderNames(HeaderTest.replaceVariables(params.getHeaderNames(), mail));
for (String headerName : params.getHeaderNames()) {
FilterUtil.headerNameHasSpace(headerName);
}
if (MatchTypeTags.MATCHES_TAG.equals(params.getMatchType())) {
ZimbraMailAdapter zma = (ZimbraMailAdapter) mail;
try {
HeaderTest.evaluateVarExp(zma, params.getHeaderNames(), HeaderTest.SourceType.ENVELOPE, params.getKeys());
} catch (MessagingException e) {
throw new SieveException("Exception occured while evaluating variable expression.", e);
}
}
if (COUNT_TAG.equals(params.getMatchType()) || VALUE_TAG.equals(params.getMatchType()) || IS_TAG.equals(params.getMatchType())) {
return match(mail, (params.getAddressPart() == null ? ALL_TAG : params.getAddressPart()), ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()), params.getMatchType(), params.getOperator(), params.getHeaderNames(), params.getKeys(), context);
} else {
return match(mail, (params.getAddressPart() == null ? ALL_TAG : params.getAddressPart()), ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()), (params.getMatchType() == null ? IS_TAG : params.getMatchType()), params.getHeaderNames(), params.getKeys(), context);
}
}
use of org.apache.jsieve.exception.SieveException in project zm-mailbox by Zimbra.
the class AddressTest method executeBasic.
/**
* <p>
* From RFC 5228, Section 5.1...
* </p>
* <code>
* Syntax: address [ADDRESS-PART] [COMPARATOR] [MATCH-TYPE]
* <header-list: string-list> <key-list: string-list>
* </code>
*
* @see org.apache.jsieve.tests.AbstractComparatorTest#executeBasic(MailAdapter,
* Arguments, SieveContext)
*/
@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
if (mail instanceof DummyMailAdapter) {
return true;
}
if (!(mail instanceof ZimbraMailAdapter)) {
return false;
}
ZimbraComparatorUtils.TestParameters params = ZimbraComparatorUtils.parseTestArguments(mail, arguments, context);
params.setKeys(HeaderTest.replaceVariables(params.getKeys(), mail));
params.setHeaderNames(HeaderTest.replaceVariables(params.getHeaderNames(), mail));
for (String headerName : params.getHeaderNames()) {
FilterUtil.headerNameHasSpace(headerName);
}
if (MatchTypeTags.MATCHES_TAG.equals(params.getMatchType())) {
ZimbraMailAdapter zma = (ZimbraMailAdapter) mail;
try {
HeaderTest.evaluateVarExp(zma, params.getHeaderNames(), HeaderTest.SourceType.HEADER, params.getKeys());
} catch (MessagingException e) {
throw new SieveException("Exception occured while evaluating variable expression.", e);
}
}
if (COUNT_TAG.equals(params.getMatchType()) || VALUE_TAG.equals(params.getMatchType()) || IS_TAG.equalsIgnoreCase(params.getMatchType())) {
return match(mail, (params.getAddressPart() == null ? ALL_TAG : params.getAddressPart()), ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()), params.getMatchType(), params.getOperator(), params.getHeaderNames(), params.getKeys(), context);
} else {
return match(mail, (params.getAddressPart() == null ? ALL_TAG : params.getAddressPart()), ZimbraComparatorUtils.getComparator(params.getComparator(), params.getMatchType()), (params.getMatchType() == null ? IS_TAG : params.getMatchType()), params.getHeaderNames(), params.getKeys(), context);
}
}
Aggregations