KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > smtpserver > SMTPTestConfiguration


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20
21
22 package org.apache.james.smtpserver;
23
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.configuration.DefaultConfiguration;
26 import org.apache.james.smtpserver.core.CoreCmdHandlerLoader;
27 import org.apache.james.smtpserver.core.filter.CoreFilterCmdHandlerLoader;
28 import org.apache.james.smtpserver.core.filter.fastfail.DNSRBLHandler;
29 import org.apache.james.smtpserver.core.filter.fastfail.MaxRcptHandler;
30 import org.apache.james.smtpserver.core.filter.fastfail.ResolvableEhloHeloHandler;
31 import org.apache.james.smtpserver.core.filter.fastfail.ReverseEqualsEhloHeloHandler;
32 import org.apache.james.smtpserver.core.filter.fastfail.ValidSenderDomainHandler;
33 import org.apache.james.test.util.Util;
34
35 public class SMTPTestConfiguration extends DefaultConfiguration {
36
37     private int m_smtpListenerPort;
38     private int m_maxMessageSizeKB = 0;
39     private String JavaDoc m_authorizedAddresses = "127.0.0.0/8";
40     private String JavaDoc m_authorizingMode = "false";
41     private boolean m_verifyIdentity = false;
42     private Integer JavaDoc m_connectionLimit = null;
43     private Integer JavaDoc m_connectionBacklog = null;
44     private boolean m_heloResolv = false;
45     private boolean m_ehloResolv = false;
46     private boolean m_senderDomainResolv = false;
47     private boolean m_checkAuthNetworks = false;
48     private boolean m_checkAuthClients = false;
49     private boolean m_heloEhloEnforcement = true;
50     private boolean m_reverseEqualsHelo = false;
51     private boolean m_reverseEqualsEhlo = false;
52     private int m_maxRcpt = 0;
53     private boolean m_useRBL = false;
54     private boolean m_addressBracketsEnforcement = true;
55
56     
57     public SMTPTestConfiguration(int smtpListenerPort) {
58         super("smptserver");
59
60         m_smtpListenerPort = smtpListenerPort;
61     }
62     
63     public void setCheckAuthNetworks(boolean checkAuth) {
64         m_checkAuthNetworks = checkAuth;
65     }
66
67
68     public void setMaxMessageSize(int kilobytes)
69     {
70         m_maxMessageSizeKB = kilobytes;
71     }
72     
73     public int getMaxMessageSize() {
74         return m_maxMessageSizeKB;
75     }
76
77     public String JavaDoc getAuthorizedAddresses() {
78         return m_authorizedAddresses;
79     }
80
81     public void setAuthorizedAddresses(String JavaDoc authorizedAddresses) {
82         m_authorizedAddresses = authorizedAddresses;
83     }
84
85     public void setAuthorizingNotRequired() {
86         m_authorizingMode = "false";
87         m_verifyIdentity = false;
88     }
89
90     public void setAuthorizingRequired() {
91         m_authorizingMode = "true";
92         m_verifyIdentity = true;
93     }
94
95     public void setAuthorizingAnnounce() {
96         m_authorizingMode = "announce";
97         m_verifyIdentity = true;
98     }
99
100     public void setConnectionLimit(int iConnectionLimit) {
101         m_connectionLimit = new Integer JavaDoc(iConnectionLimit);
102     }
103     
104     public void setConnectionBacklog(int iConnectionBacklog) {
105         m_connectionBacklog = new Integer JavaDoc(iConnectionBacklog);
106     }
107     
108     public void setHeloResolv() {
109         m_heloResolv = true;
110     }
111     
112     public void setEhloResolv() {
113         m_ehloResolv = true;
114     }
115     
116     public void setReverseEqualsHelo() {
117         m_reverseEqualsHelo = true;
118     }
119     
120     public void setReverseEqualsEhlo() {
121         m_reverseEqualsEhlo = true;
122     }
123     
124     public void setSenderDomainResolv() {
125         m_senderDomainResolv = true;
126     }
127     
128     public void setCheckAuthClients(boolean ignore) {
129         m_checkAuthClients = ignore;
130     }
131     
132     public void setMaxRcpt(int maxRcpt) {
133         m_maxRcpt = maxRcpt;
134     }
135     
136     public void setHeloEhloEnforcement(boolean heloEhloEnforcement) {
137         m_heloEhloEnforcement = heloEhloEnforcement;
138     }
139     
140     public void useRBL(boolean useRBL) {
141         m_useRBL = useRBL;
142     }
143     
144     public void setAddressBracketsEnforcement(boolean addressBracketsEnforcement) {
145         this.m_addressBracketsEnforcement = addressBracketsEnforcement;
146     }
147
148     public void init() throws ConfigurationException {
149
150         setAttribute("enabled", true);
151
152         addChild(Util.getValuedConfiguration("port", "" + m_smtpListenerPort));
153         if (m_connectionLimit != null) addChild(Util.getValuedConfiguration("connectionLimit", "" + m_connectionLimit.intValue()));
154         if (m_connectionBacklog != null) addChild(Util.getValuedConfiguration("connectionBacklog", "" + m_connectionBacklog.intValue()));
155         
156         DefaultConfiguration handlerConfig = new DefaultConfiguration("handler");
157         handlerConfig.addChild(Util.getValuedConfiguration("helloName", "myMailServer"));
158         handlerConfig.addChild(Util.getValuedConfiguration("connectiontimeout", "360000"));
159         handlerConfig.addChild(Util.getValuedConfiguration("authorizedAddresses", m_authorizedAddresses));
160         handlerConfig.addChild(Util.getValuedConfiguration("maxmessagesize", "" + m_maxMessageSizeKB));
161         handlerConfig.addChild(Util.getValuedConfiguration("authRequired", m_authorizingMode));
162         handlerConfig.addChild(Util.getValuedConfiguration("heloEhloEnforcement", m_heloEhloEnforcement+""));
163         handlerConfig.addChild(Util.getValuedConfiguration("addressBracketsEnforcement", m_addressBracketsEnforcement+""));
164         if (m_verifyIdentity) handlerConfig.addChild(Util.getValuedConfiguration("verifyIdentity", "" + m_verifyIdentity));
165  
166         DefaultConfiguration config = new DefaultConfiguration("handlerchain");
167
168         // add the rbl handler
169
if (m_useRBL) {
170             DefaultConfiguration handler = new DefaultConfiguration("handler");
171             handler.setAttribute("class", DNSRBLHandler.class.getName());
172             handler.setAttribute("command", "RCPT");
173
174             DefaultConfiguration blacklist = new DefaultConfiguration(
175                     "blacklist");
176             blacklist.setValue("bl.spamcop.net.");
177             DefaultConfiguration rblServers = new DefaultConfiguration(
178                     "rblservers");
179             rblServers.addChild(blacklist);
180
181             handler.addChild(rblServers);
182             config.addChild(handler);
183
184         }
185
186         config.addChild(createHandler(CoreFilterCmdHandlerLoader.class
187                 .getName(), null));
188
189         if (m_heloResolv || m_ehloResolv) {
190             DefaultConfiguration d = createHandler(
191                     ResolvableEhloHeloHandler.class.getName(), null);
192             d.setAttribute("command", "EHLO,HELO,RCPT");
193             d.addChild(Util.getValuedConfiguration("checkAuthNetworks",
194                     m_checkAuthNetworks + ""));
195             config.addChild(d);
196         }
197         if (m_reverseEqualsHelo || m_reverseEqualsEhlo) {
198             DefaultConfiguration d = createHandler(
199                     ReverseEqualsEhloHeloHandler.class.getName(), null);
200             d.setAttribute("command", "EHLO,HELO,RCPT");
201             d.addChild(Util.getValuedConfiguration("checkAuthNetworks",
202                     m_checkAuthNetworks + ""));
203             config.addChild(d);
204         }
205         if (m_senderDomainResolv) {
206             DefaultConfiguration d = createHandler(
207                     ValidSenderDomainHandler.class.getName(), null);
208             d.setAttribute("command", "MAIL");
209             d.addChild(Util.getValuedConfiguration("checkAuthClients",
210                     m_checkAuthClients + ""));
211             config.addChild(d);
212         }
213         if (m_maxRcpt > 0) {
214             DefaultConfiguration d = createHandler(MaxRcptHandler.class
215                     .getName(), null);
216             d.setAttribute("command", "RCPT");
217             d.addChild(Util.getValuedConfiguration("maxRcpt", m_maxRcpt + ""));
218             config.addChild(d);
219         }
220         config.addChild(createHandler(CoreCmdHandlerLoader.class.getName(),
221                 null));
222         config.addChild(createHandler(
223                 org.apache.james.smtpserver.core.SendMailHandler.class
224                         .getName(), null));
225         handlerConfig.addChild(config);
226         addChild(handlerConfig);
227     }
228
229     private DefaultConfiguration createHandler(String JavaDoc className,
230             String JavaDoc commandName) {
231         DefaultConfiguration d = new DefaultConfiguration("handler");
232         if (commandName != null) {
233             d.setAttribute("command", commandName);
234         }
235         d.setAttribute("class", className);
236         return d;
237     }
238     
239 }
240
Popular Tags