use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class StringTest method validateArguments.
@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
if (arguments.getArgumentList().size() < 3) {
throw new SyntaxException("At least 3 argument are needed. Found " + arguments);
}
for (Argument a : arguments.getArgumentList()) {
System.out.println(a);
ZimbraLog.filter.debug(a);
}
}
use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class NotifyMailto method execute.
/**
* Execute RFC Compliant notify
* @param mail The ZimbraMailAdapter
* @param arguments The Sieve Arguments
* @param context The Sieve Context
* @throws SyntaxException
*/
private Object execute(MailAdapter mail, Arguments arguments, SieveContext context) throws SyntaxException {
if (!(mail instanceof ZimbraMailAdapter)) {
return null;
}
ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
/*
* RFC 5435 3.1. Notify Action
* usage: notify [":from" string]
* [":importance" <"1" / "2" / "3">]
* [":options" string-list]
* [":message" string]
* <method: string>
*/
String from = null;
int importance = 0;
String message = null;
String method = null;
String mailto = null;
Map<String, String> options = null;
Map<String, List<String>> mailtoParams = null;
ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
boolean stop = false;
// Tag processing
while (!stop && argumentsIter.hasNext()) {
Argument argument = argumentsIter.next();
if (argument instanceof TagArgument) {
final String tag = ((TagArgument) argument).getTag();
if (from == null && NOTIFY_FROM.equals(tag)) {
// The next argument must be a string
if (argumentsIter.hasNext()) {
argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
List<String> stringList = ((StringListArgument) argument).getList();
if (stringList.size() != 1) {
throw new SyntaxException("Expecting exactly one String for " + NOTIFY_FROM);
}
String email = FilterUtil.replaceVariables(mailAdapter, (String) stringList.get(0));
// Validate email address using javax.mail.internet.InternetAddress
try {
InternetAddress addr = new InternetAddress(email);
addr.validate();
from = email;
} catch (AddressException ex) {
// if the :from addr is not valid, the FilterUtil.notifyMailto() method takes
// care of the From header before composing the notification message.
ZimbraLog.filter.info("The value of the \":from\" [" + email + "] is not valid");
}
} else {
throw new SyntaxException("Expecting a StringList for " + NOTIFY_FROM);
}
} else {
throw new SyntaxException("Expecting a parameter for " + NOTIFY_FROM);
}
} else if (importance == 0 && NOTIFY_IMPORTANCE.equals(tag)) {
// The next argument must be a number
if (argumentsIter.hasNext()) {
argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
List<String> stringList = ((StringListArgument) argument).getList();
if (stringList.size() != 1) {
throw new SyntaxException("Expecting exactly one String for " + NOTIFY_IMPORTANCE);
}
String strImportance = (String) stringList.get(0);
FilterUtil.replaceVariables(mailAdapter, strImportance);
importance = Integer.parseInt(strImportance);
if (!(importance == 1 || importance == 2 || importance == 3)) {
throw new SyntaxException("Expecting an integer number (1, 2, 3) for " + NOTIFY_IMPORTANCE);
}
} else {
throw new SyntaxException("Expecting a StringList for " + NOTIFY_IMPORTANCE);
}
} else {
throw new SyntaxException("Expecting a parameter for " + NOTIFY_IMPORTANCE);
}
} else if (options == null && NOTIFY_OPTIONS.equals(tag)) {
// The next argument must be a string-list of options "<optionname>=<value>[,<optionname>=<value]*"
if (argumentsIter.hasNext()) {
argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
List<String> listOptions = ((StringListArgument) argument).getList();
if (listOptions.size() == 0) {
throw new SyntaxException("Expecting exactly one String for " + NOTIFY_OPTIONS);
}
options = new HashMap<String, String>();
for (String option : listOptions) {
String[] token = option.split("=");
String key = null;
String value = null;
if (token.length == 2) {
key = token[0];
value = token[1];
} else if (token.length == 1) {
key = token[0];
value = "";
} else {
key = "";
value = "";
}
key = FilterUtil.replaceVariables(mailAdapter, key);
value = FilterUtil.replaceVariables(mailAdapter, value);
options.put(key, value);
}
} else {
throw new SyntaxException("Expecting a StringList for " + NOTIFY_OPTIONS);
}
} else {
throw new SyntaxException("Expecting a parameter for " + NOTIFY_OPTIONS);
}
} else if (message == null && NOTIFY_MESSAGE.equals(tag)) {
// The next argment must be a string
if (argumentsIter.hasNext()) {
argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
List<String> stringList = ((StringListArgument) argument).getList();
if (stringList.size() != 1) {
throw new SyntaxException("Expecting exactly one String for " + NOTIFY_MESSAGE);
}
message = FilterUtil.replaceVariables(mailAdapter, (String) stringList.get(0));
} else {
throw new SyntaxException("Expecting a StringList for " + NOTIFY_MESSAGE);
}
} else {
throw new SyntaxException("Expecting a parameter for " + NOTIFY_MESSAGE);
}
}
} else {
// Stop when a non-tag argument is encountered
argumentsIter.previous();
stop = true;
}
}
// The next argument MUST be a <method: string>
if (argumentsIter.hasNext()) {
Argument argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
List<String> stringList = ((StringListArgument) argument).getList();
if (stringList.size() != 1) {
throw new SyntaxException("Expecting exactly one String");
}
method = (String) stringList.get(0);
} else {
throw new SyntaxException("Expecting a StringList");
}
}
if (method == null) {
throw context.getCoordinate().syntaxException("Expecting a method string");
} else {
method = FilterUtil.replaceVariables(mailAdapter, method);
}
mailtoParams = new TreeMap<String, List<String>>(String.CASE_INSENSITIVE_ORDER);
try {
URL url = new URL(method);
mailto = FilterUtil.replaceVariables(mailAdapter, url.getPath());
String query = url.getQuery();
if (!StringUtil.isNullOrEmpty(query)) {
String[] params = query.split("&");
for (String param : params) {
String[] token = param.split("=");
if (token.length > 2) {
throw new SyntaxException("'mailto' method syntax error: too many parameters");
} else {
if (StringUtils.isEmpty(token[0])) {
throw new SyntaxException("'mailto' method syntax error: empty parameter name");
}
FilterUtil.headerNameHasSpace(token[0]);
}
// If the value or parameter name is URL encoded, it should be
// decoded. If it is not even URL encoded, more or less decoding
// the string does not do harm the contents.
String headerName = null;
String headerValue = null;
try {
headerName = URLDecoder.decode(token[0], "UTF-8");
} catch (UnsupportedEncodingException e) {
// No exception should be thrown because the charset is always "UTF-8"
} catch (IllegalArgumentException e) {
headerName = token[0];
}
headerName = Character.toUpperCase(headerName.charAt(0)) + headerName.substring(1);
if (token.length == 1) {
// The value must be empty
headerValue = "";
} else {
headerValue = token[1];
}
try {
headerValue = URLDecoder.decode(headerValue, "UTF-8");
} catch (UnsupportedEncodingException e) {
// No exception should be thrown because the charset is always "UTF-8"
} catch (IllegalArgumentException e) {
// Use token[1] as is
}
if (!mailtoParams.containsKey(headerName)) {
// Create a new entry for a header
List<String> value = new ArrayList<String>();
value.add(headerValue);
mailtoParams.put(headerName, value);
} else {
// Some headers, such as to or cc fields, can be specified multiple times
mailtoParams.get(headerName).add(headerValue);
}
}
}
} catch (MalformedURLException e) {
throw new SyntaxException("'mailto' method syntax error", e);
}
mail.addAction(new ActionNotifyMailto(from, options, importance, message, mailto, mailtoParams));
return null;
}
use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class BodyTest method executeBasic.
@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
String comparison = null;
boolean caseSensitive = false;
String key = null;
@SuppressWarnings("unchecked") ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
// TODO: handles ":matches" with * and ?
if (argumentsIter.hasNext()) {
Object argument = argumentsIter.next();
if (argument instanceof TagArgument) {
String tag = ((TagArgument) argument).getTag();
if (tag.equals(CONTAINS))
comparison = tag;
else
throw new SyntaxException("Found unexpected TagArgument: \"" + tag + "\"");
}
}
if (null == comparison)
throw new SyntaxException("Expecting \"" + CONTAINS + "\"");
// Second argument could be :comparator tag or else the value (string)
if (argumentsIter.hasNext()) {
Object argument = argumentsIter.next();
if (argument instanceof TagArgument) {
String tag = ((TagArgument) argument).getTag();
if (tag.equals(COMPARATOR)) {
if (argumentsIter.hasNext()) {
argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
StringListArgument strList = (StringListArgument) argument;
try {
String comparator = strList.getList().get(0);
if (ASCII_NUMERIC_COMPARATOR.equalsIgnoreCase(comparator) && mail instanceof ZimbraMailAdapter) {
Require.checkCapability((ZimbraMailAdapter) mail, ASCII_NUMERIC_COMPARATOR);
}
caseSensitive = Sieve.Comparator.ioctet == Sieve.Comparator.fromString(comparator);
} catch (ServiceException e) {
throw new SyntaxException(e.getMessage());
}
// Move to the last argument
if (argumentsIter.hasNext())
argument = argumentsIter.next();
} else {
throw new SyntaxException("Found unexpected argument after :comparator");
}
} else {
throw new SyntaxException("Unexpected end of arguments");
}
} else {
throw new SyntaxException("Found unexpected TagArgument: \"" + tag + "\"");
}
}
if (argument instanceof StringListArgument) {
StringListArgument strList = (StringListArgument) argument;
key = strList.getList().get(0);
}
}
if (null == key)
throw new SyntaxException("Expecting a string");
// There MUST NOT be any further arguments
if (argumentsIter.hasNext())
throw new SyntaxException("Found unexpected argument(s)");
return mail instanceof ZimbraMailAdapter && test(mail, caseSensitive, key);
}
use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class FileInto method validateArguments.
@Override
protected void validateArguments(Arguments arguments, SieveContext context) throws SieveException {
List<Argument> args = arguments.getArgumentList();
if (args.size() < 1 || args.size() > 2) {
throw new SyntaxException("Exactly 1 or 2 arguments permitted. Found " + args.size());
}
Argument argument;
String copyArg;
if (args.size() == 1) {
// folder list argument
argument = (Argument) args.get(0);
} else {
copyArg = ((Argument) args.get(0)).getValue().toString();
// if arguments size is 2; first argument should be :copy
if (!Copy.COPY.equalsIgnoreCase(copyArg)) {
throw new SyntaxException("Error in sieve fileinto. Expecting argument :copy");
}
// folder list argument
argument = (Argument) args.get(1);
}
// folder list argument should be a String list
if (!(argument instanceof StringListArgument)) {
throw new SyntaxException("Expecting a string-list");
}
// folder list argument should contain exactly one folder name
if (1 != ((StringListArgument) argument).getList().size()) {
throw new SyntaxException("Expecting exactly one argument");
}
}
use of org.apache.jsieve.exception.SyntaxException in project zm-mailbox by Zimbra.
the class ImportanceTest method executeBasic.
@Override
protected boolean executeBasic(MailAdapter mail, Arguments arguments, SieveContext context) throws SieveException {
ListIterator<Argument> argumentsIter = arguments.getArgumentList().listIterator();
FilterTest.Importance importance;
if (argumentsIter.hasNext()) {
Argument argument = argumentsIter.next();
if (argument instanceof StringListArgument) {
importance = FilterTest.Importance.valueOf(((StringListArgument) argument).getList().get(0));
} else {
throw new SyntaxException("Expecting a string");
}
} else {
throw new SyntaxException("Unexpected end of arguments");
}
// First check "Importance" header
List<String> headers = Arrays.asList("Importance");
List<String> values = null;
switch(importance) {
case high:
values = Arrays.asList("High");
break;
case low:
values = Arrays.asList("Low");
break;
case normal:
values = Arrays.asList("High", "Low");
}
boolean result1 = match(mail, Sieve.Comparator.iasciicasemap.toString(), MatchTypeTags.IS_TAG, headers, values, context);
// Now check "X-Priority" header
headers = Arrays.asList("X-Priority");
values = null;
switch(importance) {
case high:
values = Arrays.asList("1");
break;
case low:
values = Arrays.asList("5");
break;
case normal:
// normal is when it is neither high importance nor low importance
values = Arrays.asList("1", "5");
}
boolean result2 = match(mail, Sieve.Comparator.iasciicasemap.toString(), MatchTypeTags.IS_TAG, headers, values, context);
// normal is when it is neither high importance nor low importance
return importance == FilterTest.ImportanceTest.Importance.normal ? !(result1 || result2) : result1 || result2;
}
Aggregations