FileDocCategorySizeDatePackage
AbstractQuotaMatcher.javaAPI DocApache James 2.3.16129Fri Jan 12 12:56:30 GMT 2007org.apache.james.transport.matchers

AbstractQuotaMatcher

public abstract class AbstractQuotaMatcher extends org.apache.mailet.GenericMatcher

Abstract matcher checking whether a recipient has exceeded a maximum allowed quota.

"Quota" at this level is an abstraction whose specific interpretation will be done by subclasses.

Although extending GenericMatcher, its logic is recipient oriented.

version
CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
since
2.2.0

Fields Summary
Constructors Summary
Methods Summary
protected abstract longgetQuota(org.apache.mailet.MailAddress address, org.apache.mailet.Mail mail)
Gets the quota to check against.

param
address the address holding the quota if applicable
param
mail the mail involved if needed

protected abstract longgetUsed(org.apache.mailet.MailAddress address, org.apache.mailet.Mail mail)
Gets the used amount to check against the quota.

param
address the address involved
param
mail the mail involved if needed

protected booleanisOverQuota(org.apache.mailet.MailAddress address, org.apache.mailet.Mail mail)
Does the quota check. Checks if {@link #getQuota} < {@link #getUsed} for a recipient. Catches any throwable returning false, and so should any override do.

param
address the recipient addresss to check
param
mail the mail involved in the check
return
true if over quota

        String user = address.getUser();
        try {
            boolean over = getQuota(address, mail) < getUsed(address, mail);
            if (over) log(address + " is over quota.");
            return over;
        } catch (Throwable e) {
            log("Exception checking quota for: " + address, e);
            return false;
        }
    
protected booleanisRecipientChecked(org.apache.mailet.MailAddress recipient)
Checks the recipient. The default behaviour is to check that the recipient is not the local postmaster. If a subclass overrides this method it should "and" super.isRecipientChecked to its check.

param
recipient the recipient to check

        return !(getMailetContext().getPostmaster().equals(recipient));
    
protected booleanisSenderChecked(org.apache.mailet.MailAddress sender)
Checks the sender. The default behaviour is to check that the sender is not null nor the local postmaster. If a subclass overrides this method it should "and" super.isSenderChecked to its check.

param
sender the sender to check

        return !(sender == null || getMailetContext().getPostmaster().equals(sender));
    
public final java.util.Collectionmatch(org.apache.mailet.Mail mail)
Standard matcher entrypoint. First of all, checks the sender using {@link #isSenderChecked}. Then, for each recipient checks it using {@link #isRecipientChecked} and {@link #isOverQuota}.

throws
MessagingException if either isSenderChecked or isRecipientChecked throw an exception

        Collection matching = null;
        if (isSenderChecked(mail.getSender())) {
            matching = new ArrayList();
            for (Iterator i = mail.getRecipients().iterator(); i.hasNext(); ) {
                MailAddress recipient = (MailAddress) i.next();
                if (isRecipientChecked(recipient) && isOverQuota(recipient, mail)) {
                    matching.add(recipient);
                }
            }
        }
        return matching;
    
protected longparseQuota(java.lang.String amount)
Utility method that parses an amount string. You can use 'k' and 'm' as optional postfixes to the amount (both upper and lowercase). In other words, "1m" is the same as writing "1024k", which is the same as "1048576".

param
amount the amount string to parse

        long quota;
        try {
            if (amount.endsWith("k")) {
                amount = amount.substring(0, amount.length() - 1);
                quota = Long.parseLong(amount) * 1024;
            } else if (amount.endsWith("m")) {
                amount = amount.substring(0, amount.length() - 1);
                quota = Long.parseLong(amount) * 1024 * 1024;
            } else {
                quota = Long.parseLong(amount);
            }
            return quota;
        }
        catch (Exception e) {
            throw new MessagingException("Exception parsing quota", e);
        }