KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jsmtpd > plugins > filters > builtin > RcptMatcher


1 /*
2  *
3  * Jsmtpd, Java SMTP daemon
4  * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  */

21 package org.jsmtpd.plugins.filters.builtin;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.jsmtpd.core.common.PluginInitException;
30 import org.jsmtpd.core.common.filter.FilterTreeFailureException;
31 import org.jsmtpd.core.common.filter.FilterTreeSuccesException;
32 import org.jsmtpd.core.common.filter.IFilter;
33 import org.jsmtpd.core.mail.Email;
34 import org.jsmtpd.core.mail.EmailAddress;
35 import org.jsmtpd.core.mail.InvalidAddress;
36 import org.jsmtpd.core.mail.Rcpt;
37
38 /**
39  * This filter returns true if the RCPTS specified are present.
40  * If multiple RCPTS are present, you can configure the logical operator between the matches
41  * by default all rcpt are required to validate the filter. set the properet orOperator to true
42  * will return true at the first rcpt found.
43  *
44  * Additionnally, you can set failOnError to true if you wish to break the tree when rcpt are not found.
45  * You can also validate the whole tree is the filter is passed successfully by setting validateOnSucces to true.
46  *
47  * @author Jean-Francois POUX
48  */

49 public class RcptMatcher implements IFilter {
50
51     private boolean orOperator = false;
52
53     private List JavaDoc<EmailAddress> rcptToMatch = new LinkedList JavaDoc<EmailAddress>();
54     private Log log = LogFactory.getLog(RcptMatcher.class);
55     private boolean failOnError = false;
56     private boolean validateOnSucces = false;
57
58     /* (non-Javadoc)
59      * @see org.jsmtpd.core.common.filter.IFilter#doFilter(org.jsmtpd.core.mail.Email)
60      */

61     public boolean doFilter(Email input) throws FilterTreeFailureException, FilterTreeSuccesException {
62         List JavaDoc<Rcpt> rcptInMail = input.getRcpt();
63         boolean res;
64         if (orOperator) {
65             for (Iterator JavaDoc iterator = rcptInMail.iterator(); iterator.hasNext();) {
66                 Rcpt element = (Rcpt) iterator.next();
67                 EmailAddress oneRcptInMail = element.getEmailAddress();
68                 for (Iterator JavaDoc iter = rcptToMatch.iterator(); iter.hasNext();) {
69                     EmailAddress oneRcptToMatch = (EmailAddress) iter.next();
70                     if (oneRcptToMatch.isEqual(oneRcptInMail)) {
71                         res = true;
72                         break;
73                     }
74                 }
75             }
76
77             res = false;
78         } else {
79             for (Iterator JavaDoc iter = rcptToMatch.iterator(); iter.hasNext();) {
80                 EmailAddress oneRcptToMatch = (EmailAddress) iter.next();
81                 boolean found = false;
82                 for (Iterator JavaDoc iterator = rcptInMail.iterator(); iterator.hasNext();) {
83                     Rcpt element = (Rcpt) iterator.next();
84                     EmailAddress oneRcptInMail = element.getEmailAddress();
85                     if (oneRcptToMatch.isEqual(oneRcptInMail)) {
86                         found = true;
87                         break;
88                     }
89                 }
90                 if (!found) {
91                     res = false;
92                 }
93             }
94             res = true;
95         }
96         if (failOnError && (!res)) {
97             throw new FilterTreeFailureException();
98         }
99
100         if (validateOnSucces && res)
101             throw new FilterTreeSuccesException();
102
103         return res;
104     }
105
106     /* (non-Javadoc)
107      * @see org.jsmtpd.core.common.IGenericPlugin#getPluginName()
108      */

109     public String JavaDoc getPluginName() {
110         return "Recipient filter matcher for Jsmtp";
111     }
112
113     /* (non-Javadoc)
114      * @see org.jsmtpd.core.common.IGenericPlugin#initPlugin()
115      */

116     public void initPlugin() throws PluginInitException {
117
118     }
119
120     /* (non-Javadoc)
121      * @see org.jsmtpd.core.common.IGenericPlugin#shutdownPlugin()
122      */

123     public void shutdownPlugin() {
124
125     }
126
127     public void setRcpt(String JavaDoc rcpt) {
128         try {
129             EmailAddress rcptToAdd = EmailAddress.parseAddress(rcpt);
130             rcptToMatch.add(rcptToAdd);
131         } catch (InvalidAddress e) {
132             log.error("Plugin: " + getPluginName() + " can't add rcpt to matching list, " + rcpt + " is not a valid email adress");
133         }
134     }
135
136     public void setOrOperator(boolean cond) {
137         orOperator = cond;
138     }
139
140     public void setFailOnError(boolean op) {
141         failOnError = op;
142     }
143
144     public void setValidateOnSucces(boolean op) {
145         validateOnSucces = op;
146     }
147 }
Popular Tags