use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class FilterUtil method replaceVariables.
/**
* Look-up the variable table to get the set value.
* If the 'sourceStr' contains a variable-ref (formatted with ${variable-name}), this method looks up the
* variable table with its variable-name and replaces the variable with the text value assigned by
* the 'set' command.
* According to the RFC 5229 Section 4., "Variable names are case insensitive."
*
* @param variables a list of variable name/value
* @param matchedVariables a list of Matched Variables
* @param sourceStr text string that may contain "variable-ref" (RFC 5229 Section 3.)
* @return Replaced text string
* @throws SyntaxException
*/
public static String replaceVariables(ZimbraMailAdapter mailAdapter, String sourceStr) throws SyntaxException {
if (null == mailAdapter) {
return sourceStr;
}
if (sourceStr.indexOf("${") == -1) {
return sourceStr;
}
validateVariableIndex(sourceStr);
try {
Require.checkCapability(mailAdapter, CAPABILITY_VARIABLES);
} catch (SyntaxException e) {
ZimbraLog.filter.info("\"variables\" capability is not declared. No variables will be replaced");
return sourceStr;
}
Map<String, String> variables = mailAdapter.getVariables();
List<String> matchedVariables = mailAdapter.getMatchedValues();
ZimbraLog.filter.debug("Variable: %s ", sourceStr);
ZimbraLog.filter.debug("Variable available: %s : %s", variables, matchedVariables);
String resultStr = sourceStr;
// (1) Resolve the Matched Variables (numeric variables; "${N}" (N=0,1,...9)
int i = 0;
for (; i < matchedVariables.size() && i < 10; i++) {
String keyName = "(?i)" + "\\$\\{0*" + String.valueOf(i) + "\\}";
resultStr = resultStr.replaceAll(keyName, Matcher.quoteReplacement(matchedVariables.get(i)));
}
// (2) Replace the empty string to Matched Variables whose index is out of range
for (; i < 10; i++) {
String keyName = "(?i)" + "\\$\\{0*" + String.valueOf(i) + "\\}";
resultStr = resultStr.replaceAll(keyName, Matcher.quoteReplacement(""));
}
// (3) Resolve the named variables ("${xxx}")
resultStr = leastGreedyReplace(variables, resultStr, mailAdapter.getMimeVariables());
ZimbraLog.filter.debug("Sieve: variable value is: %s", resultStr);
return resultStr;
}
use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class FilterUtil method validateVariableIndex.
private static void validateVariableIndex(String srcStr) throws SyntaxException {
boolean match = false;
String negativeIndexPattern = "\\$\\{-\\d*\\}";
String exceedsIndexPattern = "\\$\\{0*[1-9]{1,}\\d{1,}\\}";
Pattern pattern = Pattern.compile(negativeIndexPattern);
Matcher matcher = pattern.matcher(srcStr);
match = matcher.find();
if (!match) {
pattern = Pattern.compile(exceedsIndexPattern);
matcher = pattern.matcher(srcStr);
match = matcher.find();
}
if (match) {
ZimbraLog.filter.debug("Invalid variable index %s ", srcStr);
throw new SyntaxException("Invalid variable index " + srcStr);
}
}
use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class ZimbraComparatorUtils method parseTestArguments.
/**
* Parses the address test string in the 'if' part of the filter rule
*
* @see org.apache.jsieve.tests.AbstractComparatorTest#executeBasic(MailAdapter mail, Arguments arguments, SieveContext context)
*
* @param mail
* @param arguments
* @param context
* @return TestParameters
*/
public static TestParameters parseTestArguments(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
String addressPart = null;
String comparator = null;
String matchType = null;
String operator = null;
List<String> headerNames = null;
List<String> keys = null;
boolean nextArgumentIsRelationalSign = false;
ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
boolean stop = false;
// Tag processing
while (!stop && argumentsIter.hasNext()) {
Argument argument = argumentsIter.next();
if (argument instanceof TagArgument) {
String tag = ((TagArgument) argument).getTag();
// [ADDRESS-PART]?
if (addressPart == null && (LOCALPART_TAG.equalsIgnoreCase(tag) || DOMAIN_TAG.equalsIgnoreCase(tag) || ALL_TAG.equalsIgnoreCase(tag))) {
addressPart = tag;
} else // [COMPARATOR]?
if (comparator == null && COMPARATOR_TAG.equalsIgnoreCase(tag)) {
// The next argument must be a string list
if (argumentsIter.hasNext()) {
argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
List stringList = ((StringListArgument) argument).getList();
if (stringList.size() != 1) {
throw new SyntaxException("Expecting exactly one String");
}
comparator = (String) stringList.get(0);
} else {
throw new SyntaxException("Expecting a StringList");
}
}
} else // [MATCH-TYPE]?
if (matchType == null && (IS_TAG.equalsIgnoreCase(tag) || CONTAINS_TAG.equalsIgnoreCase(tag) || MATCHES_TAG.equalsIgnoreCase(tag) || HeaderConstants.COUNT.equalsIgnoreCase(tag) || HeaderConstants.VALUE.equalsIgnoreCase(tag))) {
matchType = tag;
nextArgumentIsRelationalSign = true;
} else {
throw context.getCoordinate().syntaxException("Found unexpected TagArgument");
}
} else {
if (nextArgumentIsRelationalSign && argument instanceof StringListArgument) {
String symbol = ((StringListArgument) argument).getList().get(0);
if (matchType != null && (HeaderConstants.GT_OP.equalsIgnoreCase(symbol) || HeaderConstants.GE_OP.equalsIgnoreCase(symbol) || HeaderConstants.LT_OP.equalsIgnoreCase(symbol) || HeaderConstants.LE_OP.equalsIgnoreCase(symbol) || HeaderConstants.EQ_OP.equalsIgnoreCase(symbol) || HeaderConstants.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()) {
Argument argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
headerNames = ((StringListArgument) argument).getList();
}
}
if (headerNames == null) {
throw context.getCoordinate().syntaxException("Expecting a StringList of header names");
}
// The next argument MUST be a string-list of keys
if (argumentsIter.hasNext()) {
Argument argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
keys = ((StringListArgument) argument).getList();
}
}
if (keys == null) {
throw context.getCoordinate().syntaxException("Expecting a StringList of keys");
}
if (argumentsIter.hasNext()) {
throw context.getCoordinate().syntaxException("Found unexpected arguments");
}
TestParameters result = new TestParameters(mail, addressPart, comparator, matchType, operator, headerNames, keys);
return result;
}
use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class ZimbraVariablesCtrl method validateArguments.
@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
List<Argument> args = arguments.getArgumentList();
if (args.size() > 1) {
throw new SyntaxException("More than one argument found (" + args.size() + ")");
}
for (Argument arg : arguments.getArgumentList()) {
if (arg instanceof TagArgument) {
TagArgument tag = (TagArgument) arg;
String tagValue = tag.getTag();
if (!RESET.equalsIgnoreCase(tagValue)) {
throw new SyntaxException("Invalid tag: [" + tagValue + "]");
}
} else {
if (arg instanceof StringListArgument) {
String argument = ((StringListArgument) arg).getList().get(0);
throw new SyntaxException("Invalid argument: [" + argument + "]");
} else {
throw new SyntaxException("Invalid argument");
}
}
}
}
use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class ZimbraVariablesCtrl method executeBasic.
@Override
protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext context) throws SieveException {
if (!(mail instanceof ZimbraMailAdapter)) {
return null;
}
ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
for (Argument arg : arguments.getArgumentList()) {
if (arg instanceof TagArgument) {
TagArgument tag = (TagArgument) arg;
String tagValue = tag.getTag();
if (RESET.equalsIgnoreCase(tagValue)) {
mailAdapter.resetValues();
} else {
throw new SyntaxException("Invalid tag: [" + tagValue + "]");
}
}
}
return null;
}
Aggregations