Search in sources :

Example 1 with OperationException

use of org.apache.jsieve.exception.OperationException in project zm-mailbox by Zimbra.

the class DeleteHeader method executeBasic.

/** (non-Javadoc)
     * @see org.apache.jsieve.commands.AbstractCommand#executeBasic(org.apache.jsieve.mail.MailAdapter, org.apache.jsieve.Arguments, org.apache.jsieve.Block, org.apache.jsieve.SieveContext)
     */
@SuppressWarnings("unchecked")
@Override
protected Object executeBasic(MailAdapter mail, Arguments args, Block block, SieveContext sieveContext) throws SieveException {
    if (!(mail instanceof ZimbraMailAdapter)) {
        ZimbraLog.filter.info("deleteheader: Zimbra mail adapter not found.");
        return null;
    }
    // make sure zcs do not delete immutable header
    if (ehe.isImmutableHeaderKey()) {
        ZimbraLog.filter.info("deleteheader: %s is immutable header, so exiting silently.", ehe.getKey());
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    // replace sieve variables
    ehe.replaceVariablesInValueList(mailAdapter);
    ehe.replaceVariablesInKey(mailAdapter);
    FilterUtil.headerNameHasSpace(ehe.getKey());
    MimeMessage mm = mailAdapter.getMimeMessage();
    Enumeration<Header> headers;
    try {
        headers = mm.getAllHeaders();
        if (!headers.hasMoreElements()) {
            ZimbraLog.filter.info("deleteheader: No headers found in mime.");
            return null;
        }
    } catch (MessagingException e) {
        throw new OperationException("deleteheader: Error occured while fetching all headers from mime.", e);
    }
    List<String> matchingeHeaderList = ehe.getMatchingHeaders(mm);
    int headerCount = matchingeHeaderList.size();
    if (headerCount < 1) {
        ZimbraLog.filter.info("deleteheader: No headers found matching with \"%s\" in mime.", ehe.getKey());
        return null;
    }
    ehe.setEffectiveIndex(headerCount);
    int matchIndex = 0;
    Set<String> removedHeaders = new HashSet<String>();
    try {
        while (headers.hasMoreElements()) {
            Header header = headers.nextElement();
            boolean deleteCurrentHeader = false;
            if (header.getName().equalsIgnoreCase(ehe.getKey())) {
                matchIndex++;
                if (ehe.getIndex() == null || (ehe.getIndex() != null && ehe.getIndex() == matchIndex)) {
                    if (ehe.getValueList() == null || ehe.getValueList().isEmpty()) {
                        deleteCurrentHeader = true;
                    } else {
                        for (String value : ehe.getValueList()) {
                            deleteCurrentHeader = ehe.matchCondition(mailAdapter, header, matchingeHeaderList, value, sieveContext);
                            if (deleteCurrentHeader) {
                                break;
                            }
                        }
                    }
                }
            }
            if (!(removedHeaders.contains(header.getName()))) {
                mm.removeHeader(header.getName());
                removedHeaders.add(header.getName());
            }
            // if deleteCurrentHeader is true, don't add header to mime
            if (!deleteCurrentHeader) {
                mm.addHeaderLine(header.getName() + ": " + header.getValue());
            }
        }
        mm.saveChanges();
        mailAdapter.updateIncomingBlob();
    } catch (MessagingException me) {
        throw new OperationException("deleteheader: Error occured while operating mime.", me);
    }
    return null;
}
Also used : Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) OperationException(org.apache.jsieve.exception.OperationException) HashSet(java.util.HashSet)

Example 2 with OperationException

use of org.apache.jsieve.exception.OperationException in project zm-mailbox by Zimbra.

the class EditHeaderExtension method setupEditHeaderData.

// Utility methods
/**
     * This method sets values provided with replaceheader or deleteheader in <b>EditHeaderExtension</b> object.
     * @param arguments
     * @param ac
     * @throws SyntaxException
     * @throws OperationException
     */
public void setupEditHeaderData(Arguments arguments, AbstractCommand ac) throws SyntaxException, OperationException {
    // set up class variables
    Iterator<Argument> itr = arguments.getArgumentList().iterator();
    while (itr.hasNext()) {
        Argument arg = itr.next();
        if (arg instanceof TagArgument) {
            TagArgument tag = (TagArgument) arg;
            if (tag.is(INDEX)) {
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof NumberArgument) {
                        this.index = ((NumberArgument) arg).getInteger();
                    } else {
                        throw new SyntaxException("Invalid index provided with replaceheader : " + arg);
                    }
                }
            } else if (tag.is(LAST)) {
                this.last = true;
            } else if (tag.is(NEW_NAME)) {
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        String origNewName = sla.getList().get(0);
                        if (StringUtil.isNullOrEmpty(origNewName)) {
                            throw new SyntaxException("New name must be present with :newname in replaceheader : " + arg);
                        }
                        this.newName = origNewName;
                    } else {
                        throw new SyntaxException("New name not provided with :newname in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(NEW_VALUE)) {
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        this.newValue = sla.getList().get(0);
                    } else {
                        throw new SyntaxException("New value not provided with :newValue in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(COUNT)) {
                if (this.valueTag) {
                    throw new SyntaxException(":count and :value both can not be used with replaceheader");
                }
                this.countTag = true;
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        this.relationalComparator = sla.getList().get(0);
                    } else {
                        throw new SyntaxException("Relational comparator not provided with :count in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(VALUE)) {
                if (this.countTag) {
                    throw new SyntaxException(":count and :value both can not be used with replaceheader");
                }
                this.valueTag = true;
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        this.relationalComparator = sla.getList().get(0);
                    } else {
                        throw new SyntaxException("Relational comparator not provided with :value in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(ComparatorTags.COMPARATOR_TAG)) {
                if (itr.hasNext()) {
                    arg = itr.next();
                    if (arg instanceof StringListArgument) {
                        StringListArgument sla = (StringListArgument) arg;
                        this.comparator = sla.getList().get(0);
                    } else {
                        throw new SyntaxException("Comparator not provided with :comparator in replaceheader : " + arg);
                    }
                }
            } else if (tag.is(MatchTypeTags.CONTAINS_TAG)) {
                this.contains = true;
            } else if (tag.is(MatchTypeTags.IS_TAG)) {
                this.is = true;
            } else if (tag.is(MatchTypeTags.MATCHES_TAG)) {
                this.matches = true;
            } else {
                throw new SyntaxException("Invalid tag argument provided with replaceheader.");
            }
        } else if (arg instanceof StringListArgument) {
            if (ac instanceof ReplaceHeader) {
                StringListArgument sla = (StringListArgument) arg;
                this.key = sla.getList().get(0);
                if (itr.hasNext()) {
                    arg = itr.next();
                    sla = (StringListArgument) arg;
                    this.valueList = sla.getList();
                }
            } else if (ac instanceof DeleteHeader) {
                StringListArgument sla = (StringListArgument) arg;
                this.key = sla.getList().get(0);
                if (itr.hasNext()) {
                    arg = itr.next();
                    sla = (StringListArgument) arg;
                    this.valueList = sla.getList();
                } else {
                    ZimbraLog.filter.info("Value for " + this.key + " is not provided in deleteheader. So all headers with this key will be deleted.");
                }
            } else {
                throw new OperationException("Invalid instance of AbstractCommand is obtained.");
            }
        } else {
            ZimbraLog.filter.info("Unknown argument provided: " + arg.getValue());
        }
    }
    if (!(isIs() || isContains() || isMatches() || isCountTag() || isValueTag())) {
        this.is = true;
    }
}
Also used : NumberArgument(org.apache.jsieve.NumberArgument) Argument(org.apache.jsieve.Argument) StringListArgument(org.apache.jsieve.StringListArgument) TagArgument(org.apache.jsieve.TagArgument) NumberArgument(org.apache.jsieve.NumberArgument) SyntaxException(org.apache.jsieve.exception.SyntaxException) TagArgument(org.apache.jsieve.TagArgument) StringListArgument(org.apache.jsieve.StringListArgument) OperationException(org.apache.jsieve.exception.OperationException)

Example 3 with OperationException

use of org.apache.jsieve.exception.OperationException in project zm-mailbox by Zimbra.

the class AddHeader method executeBasic.

@SuppressWarnings("unchecked")
@Override
protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext context) throws SieveException {
    if (!(mail instanceof ZimbraMailAdapter)) {
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    headerName = FilterUtil.replaceVariables(mailAdapter, headerName);
    FilterUtil.headerNameHasSpace(headerName);
    headerValue = FilterUtil.replaceVariables(mailAdapter, headerValue);
    try {
        headerValue = MimeUtility.fold(headerName.length() + 2, MimeUtility.encodeText(headerValue));
    } catch (UnsupportedEncodingException uee) {
        throw new OperationException("addheader: Error occured while encoding header value.", uee);
    }
    MimeMessage mm = mailAdapter.getMimeMessage();
    if (headerName != null && headerValue != null) {
        try {
            if (last) {
                mm.addHeaderLine(headerName + ": " + headerValue);
            } else {
                List<Header> headerList = new ArrayList<Header>();
                Enumeration<Header> e = mm.getAllHeaders();
                // keep it at the first line.
                if (e.hasMoreElements()) {
                    Header temp = e.nextElement();
                    if ("Return-Path".equalsIgnoreCase(temp.getName())) {
                        mm.removeHeader(temp.getName());
                        headerList.add(temp);
                    } else {
                        // reset the iterator (push back the first item of the headers list)
                        e = mm.getAllHeaders();
                    }
                }
                headerList.add(new Header(headerName, headerValue));
                while (e.hasMoreElements()) {
                    Header temp = e.nextElement();
                    mm.removeHeader(temp.getName());
                    headerList.add(temp);
                }
                for (Header header : headerList) {
                    mm.addHeaderLine(header.getName() + ": " + header.getValue());
                }
            }
            mm.saveChanges();
            mailAdapter.updateIncomingBlob();
            ZimbraLog.filter.info("New header is added in mime with name: %s and value: %s", headerName, headerValue);
        } catch (MessagingException e) {
            throw new OperationException("Error occured while adding new header in mime.", e);
        }
        return null;
    }
    return null;
}
Also used : Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) OperationException(org.apache.jsieve.exception.OperationException)

Example 4 with OperationException

use of org.apache.jsieve.exception.OperationException in project zm-mailbox by Zimbra.

the class ReplaceHeader method executeBasic.

/** (non-Javadoc)
     * @see org.apache.jsieve.commands.AbstractCommand#executeBasic(org.apache.jsieve.mail.MailAdapter, org.apache.jsieve.Arguments, org.apache.jsieve.Block, org.apache.jsieve.SieveContext)
     */
@SuppressWarnings("unchecked")
@Override
protected Object executeBasic(MailAdapter mail, Arguments arguments, Block block, SieveContext sieveContext) throws SieveException {
    if (!(mail instanceof ZimbraMailAdapter)) {
        ZimbraLog.filter.info("replaceheader: Zimbra mail adapter not found.");
        return null;
    }
    // make sure zcs do not edit immutable header
    if (ehe.isImmutableHeaderKey()) {
        ZimbraLog.filter.info("replaceheader: %s is immutable header, so exiting silently.", ehe.getKey());
        return null;
    }
    ZimbraMailAdapter mailAdapter = (ZimbraMailAdapter) mail;
    // replace sieve variables
    ehe.replaceVariablesInValueList(mailAdapter);
    ehe.replaceVariablesInKey(mailAdapter);
    if (ehe.getValueList() == null || ehe.getValueList().isEmpty()) {
        ehe.setValueList(Arrays.asList("*"));
    }
    FilterUtil.headerNameHasSpace(ehe.getKey());
    MimeMessage mm = mailAdapter.getMimeMessage();
    Enumeration<Header> headers;
    try {
        headers = mm.getAllHeaders();
        if (!headers.hasMoreElements()) {
            ZimbraLog.filter.info("replaceheader: No headers found in mime.");
            return null;
        }
    } catch (MessagingException e) {
        throw new OperationException("replaceheader: Error occured while fetching all headers from mime.", e);
    }
    List<String> matchingeHeaderList = ehe.getMatchingHeaders(mm);
    int headerCount = matchingeHeaderList.size();
    if (headerCount < 1) {
        ZimbraLog.filter.info("replaceheader: No headers found matching with \"%s\" in mime.", ehe.getKey());
        return null;
    }
    ehe.setEffectiveIndex(headerCount);
    int matchIndex = 0;
    List<Header> newHeaderList = new ArrayList<Header>();
    try {
        while (headers.hasMoreElements()) {
            Header header = headers.nextElement();
            String newHeaderName = null;
            String newHeaderValue = null;
            boolean replace = false;
            if (header.getName().equalsIgnoreCase(ehe.getKey())) {
                matchIndex++;
                if (ehe.getIndex() == null || (ehe.getIndex() != null && ehe.getIndex() == matchIndex)) {
                    ZimbraLog.filter.debug("replaceheader: header before processing\n%d  %s: %s", matchIndex, header.getName(), header.getValue());
                    for (String value : ehe.getValueList()) {
                        ZimbraLog.filter.debug("replaceheader: working with %s value", value);
                        replace = ehe.matchCondition(mailAdapter, header, matchingeHeaderList, value, sieveContext);
                        if (replace) {
                            if (ehe.getNewName() != null) {
                                newHeaderName = FilterUtil.replaceVariables(mailAdapter, ehe.getNewName());
                                FilterUtil.headerNameHasSpace(newHeaderName);
                            } else {
                                newHeaderName = header.getName();
                            }
                            if (ehe.getNewValue() != null) {
                                newHeaderValue = FilterUtil.replaceVariables(mailAdapter, ehe.getNewValue());
                                newHeaderValue = MimeUtility.fold(newHeaderName.length() + 2, MimeUtility.encodeText(newHeaderValue));
                            } else {
                                newHeaderValue = header.getValue();
                            }
                            ZimbraLog.filter.debug("replaceheader: header after processing\n%s: %s", newHeaderName, newHeaderValue);
                            header = new Header(newHeaderName, newHeaderValue);
                            break;
                        }
                    }
                }
            }
            newHeaderList.add(header);
        }
        // now remove all headers from mime and add from new list
        headers = mm.getAllHeaders();
        while (headers.hasMoreElements()) {
            Header header = headers.nextElement();
            mm.removeHeader(header.getName());
        }
        for (Header header : newHeaderList) {
            mm.addHeaderLine(header.getName() + ": " + header.getValue());
        }
        mm.saveChanges();
        mailAdapter.updateIncomingBlob();
    } catch (MessagingException me) {
        throw new OperationException("replaceheader: Error occured while operating mime.", me);
    } catch (UnsupportedEncodingException uee) {
        throw new OperationException("replaceheader: Error occured while encoding header value.", uee);
    }
    return null;
}
Also used : MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Header(javax.mail.Header) MimeMessage(javax.mail.internet.MimeMessage) ZimbraMailAdapter(com.zimbra.cs.filter.ZimbraMailAdapter) OperationException(org.apache.jsieve.exception.OperationException)

Aggregations

OperationException (org.apache.jsieve.exception.OperationException)4 ZimbraMailAdapter (com.zimbra.cs.filter.ZimbraMailAdapter)3 Header (javax.mail.Header)3 MessagingException (javax.mail.MessagingException)3 MimeMessage (javax.mail.internet.MimeMessage)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)1 Argument (org.apache.jsieve.Argument)1 NumberArgument (org.apache.jsieve.NumberArgument)1 StringListArgument (org.apache.jsieve.StringListArgument)1 TagArgument (org.apache.jsieve.TagArgument)1 SyntaxException (org.apache.jsieve.exception.SyntaxException)1