1 17 18 package org.apache.james.transport.matchers; 19 20 import java.util.Iterator ; 21 import java.util.Collection ; 22 import java.util.ArrayList ; 23 import javax.mail.MessagingException ; 24 import org.apache.mailet.GenericMatcher; 25 import org.apache.mailet.MailAddress; 26 import org.apache.mailet.Mail; 27 28 37 abstract public class AbstractQuotaMatcher extends GenericMatcher { 38 39 47 public final Collection match(Mail mail) throws MessagingException { 48 Collection matching = null; 49 if (isSenderChecked(mail.getSender())) { 50 matching = new ArrayList (); 51 for (Iterator i = mail.getRecipients().iterator(); i.hasNext(); ) { 52 MailAddress recipient = (MailAddress) i.next(); 53 if (isRecipientChecked(recipient) && isOverQuota(recipient, mail)) { 54 matching.add(recipient); 55 } 56 } 57 } 58 return matching; 59 } 60 61 70 protected boolean isOverQuota(MailAddress address, Mail mail) { 71 String user = address.getUser(); 72 try { 73 boolean over = getQuota(address, mail) < getUsed(address, mail); 74 if (over) log(address + " is over quota."); 75 return over; 76 } catch (Throwable e) { 77 log("Exception checking quota for: " + address, e); 78 return false; 79 } 80 } 81 82 90 protected boolean isSenderChecked(MailAddress sender) throws MessagingException { 91 return !(sender == null || getMailetContext().getPostmaster().equals(sender)); 92 } 93 94 102 protected boolean isRecipientChecked(MailAddress recipient) throws MessagingException { 103 return !(getMailetContext().getPostmaster().equals(recipient)); 104 } 105 106 112 abstract protected long getQuota(MailAddress address, Mail mail) throws MessagingException ; 113 114 120 abstract protected long getUsed(MailAddress address, Mail mail) throws MessagingException ; 121 122 130 protected long parseQuota(String amount) throws MessagingException { 131 long quota; 132 try { 133 if (amount.endsWith("k")) { 134 amount = amount.substring(0, amount.length() - 1); 135 quota = Long.parseLong(amount) * 1024; 136 } else if (amount.endsWith("m")) { 137 amount = amount.substring(0, amount.length() - 1); 138 quota = Long.parseLong(amount) * 1024 * 1024; 139 } else { 140 quota = Long.parseLong(amount); 141 } 142 return quota; 143 } 144 catch (Exception e) { 145 throw new MessagingException ("Exception parsing quota", e); 146 } 147 } 148 } 149 | Popular Tags |