Search in sources :

Example 6 with ParseException

use of org.apache.jsieve.parser.generated.ParseException in project zm-mailbox by Zimbra.

the class RuleManager method getAdminRulesAsXML.

public static List<FilterRule> getAdminRulesAsXML(Entry entry, FilterType filterType, AdminFilterType afType) throws ServiceException {
    Node node;
    try {
        node = getRulesNode(entry, filterType, afType);
    } catch (ParseException | TokenMgrError e) {
        throw ServiceException.PARSE_ERROR("parsing Sieve script", e);
    }
    String sieveScriptAttrName = getAdminScriptAttributeName(filterType, afType);
    SieveToSoap sieveToSoap = new SieveToSoap(getRuleNames(entry.getAttr(sieveScriptAttrName)));
    sieveToSoap.accept(node);
    return sieveToSoap.toFilterRules();
}
Also used : Node(org.apache.jsieve.parser.generated.Node) TokenMgrError(org.apache.jsieve.parser.generated.TokenMgrError) ParseException(org.apache.jsieve.parser.generated.ParseException)

Example 7 with ParseException

use of org.apache.jsieve.parser.generated.ParseException in project zm-mailbox by Zimbra.

the class RuleManager method folderRenamed.

private static void folderRenamed(Account account, String originalPath, String newPath, String sieveScriptAttrName, String rulesCacheKey) throws ServiceException {
    String script = getRules(account, sieveScriptAttrName);
    if (script != null) {
        Node node;
        try {
            node = parse(script);
        } catch (ParseException e) {
            ZimbraLog.filter.warn("Unable to update filter rules with new folder path '%s'.", e);
            return;
        }
        FolderRenamer renamer = new FolderRenamer(originalPath, newPath);
        renamer.accept(node);
        if (renamer.renamed()) {
            // Kind of a hacky way to convert a Node tree to a script.  We
            // convert to XML first, and then to a String.  Unfortunately
            // jSieve 0.2 doesn't have an API that generates a script from
            // a Node tree.
            List<String> ruleNames = getRuleNames(script);
            SieveToSoap sieveToSoap = new SieveToSoap(ruleNames);
            sieveToSoap.accept(node);
            SoapToSieve soapToSieve = new SoapToSieve(sieveToSoap.toFilterRules());
            String newScript = soapToSieve.getSieveScript();
            setRules(account, newScript, sieveScriptAttrName, rulesCacheKey);
            ZimbraLog.filter.info("Updated %s due to folder move or rename from %s to %s.", sieveScriptAttrName, originalPath, newPath);
            ZimbraLog.filter.debug("Old rules:\n%s, new rules:\n%s", script, newScript);
        }
    }
}
Also used : Node(org.apache.jsieve.parser.generated.Node) ParseException(org.apache.jsieve.parser.generated.ParseException)

Example 8 with ParseException

use of org.apache.jsieve.parser.generated.ParseException in project zm-mailbox by Zimbra.

the class RuleManager method folderDeleted.

private static void folderDeleted(Account account, String originalPath, String sieveScriptAttrName, String rulesCacheKey) throws ServiceException {
    String script = getRules(account, sieveScriptAttrName);
    if (script != null) {
        Node node;
        try {
            node = parse(script);
        } catch (ParseException e) {
            ZimbraLog.filter.warn("Unable to update filter rules after folder '%s' was deleted.", originalPath, e);
            return;
        }
        FolderDeleted deleted = new FolderDeleted(originalPath);
        deleted.accept(node);
        if (deleted.modified()) {
            // Kind of a hacky way to convert a Node tree to a script.  We
            // convert to XML first, and then to a String.  Unfortunately
            // jSieve 0.2 doesn't have an API that generates a script from
            // a Node tree.
            List<String> ruleNames = getRuleNames(script);
            SieveToSoap sieveToSoap = new SieveToSoap(ruleNames);
            sieveToSoap.accept(node);
            SoapToSieve soapToSieve = new SoapToSieve(sieveToSoap.toFilterRules());
            String newScript = soapToSieve.getSieveScript();
            setRules(account, newScript, sieveScriptAttrName, rulesCacheKey);
            ZimbraLog.filter.info("Updated %s filter rules after folder %s was deleted.", sieveScriptAttrName, originalPath);
            ZimbraLog.filter.debug("Old rules:\n%s, new rules:\n%s", script, newScript);
        }
    }
}
Also used : Node(org.apache.jsieve.parser.generated.Node) ParseException(org.apache.jsieve.parser.generated.ParseException)

Aggregations

Node (org.apache.jsieve.parser.generated.Node)8 ParseException (org.apache.jsieve.parser.generated.ParseException)8 TokenMgrError (org.apache.jsieve.parser.generated.TokenMgrError)4 Account (com.zimbra.cs.account.Account)2 ServiceException (com.zimbra.common.service.ServiceException)1 Element (com.zimbra.common.soap.Element)1 Cos (com.zimbra.cs.account.Cos)1 Domain (com.zimbra.cs.account.Domain)1 Server (com.zimbra.cs.account.Server)1 ZimbraHit (com.zimbra.cs.index.ZimbraHit)1 ZimbraQueryResults (com.zimbra.cs.index.ZimbraQueryResults)1 NoSuchItemException (com.zimbra.cs.mailbox.MailServiceException.NoSuchItemException)1 Mailbox (com.zimbra.cs.mailbox.Mailbox)1 OperationContext (com.zimbra.cs.mailbox.OperationContext)1 ZimbraSoapContext (com.zimbra.soap.ZimbraSoapContext)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 SieveException (org.apache.jsieve.exception.SieveException)1