Impact code to use james3 components (POSTAGE-23)
git-svn-id: https://svn.apache.org/repos/asf/james/postage/trunk@1350154 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/main/java/org/apache/james/postage/client/POP3Client.java b/src/main/java/org/apache/james/postage/client/POP3Client.java
index 2e467db..02f1b6d 100644
--- a/src/main/java/org/apache/james/postage/client/POP3Client.java
+++ b/src/main/java/org/apache/james/postage/client/POP3Client.java
@@ -41,16 +41,16 @@
private static Log log = LogFactory.getLog(POP3Client.class);
- private String m_host;
- private int m_port;
- private UserList m_internalUsers;
- private PostageRunnerResult m_results;
+ private String host;
+ private int port;
+ private UserList internalUsers;
+ private PostageRunnerResult results;
public POP3Client(String host, int port, UserList internalUsers, PostageRunnerResult results) {
- m_host = host;
- m_port = port;
- m_internalUsers = internalUsers;
- m_results = results;
+ this.host = host;
+ this.port = port;
+ this.internalUsers = internalUsers;
+ this.results = results;
}
/**
@@ -58,7 +58,7 @@
*/
public boolean checkAvailability() throws StartupException {
try {
- org.apache.commons.net.pop3.POP3Client pop3Client = openConnection(m_internalUsers.getRandomUsername());
+ org.apache.commons.net.pop3.POP3Client pop3Client = openConnection(this.internalUsers.getRandomUsername());
closeSession(pop3Client);
} catch (PostageException e) {
throw new StartupException("error checking availability");
@@ -78,8 +78,10 @@
private org.apache.commons.net.pop3.POP3Client openConnection(String username) throws PostageException {
org.apache.commons.net.pop3.POP3Client pop3Client = new org.apache.commons.net.pop3.POP3Client();
try {
- pop3Client.connect(m_host, m_port);
- pop3Client.login(username, m_internalUsers.getPassword());
+ pop3Client.connect(this.host, this.port);
+ if (!pop3Client.login(this.internalUsers.getEmailAddress(username), this.internalUsers.getPassword())) {
+ log.warn("Login did not work for user: " + username);
+ }
} catch (IOException e) {
throw new PostageException("POP3 service not available", e);
}
@@ -90,7 +92,7 @@
* take one POP3 sample for a random user
*/
public void doSample() throws SamplingException {
- String username = m_internalUsers.getRandomUsername();
+ String username = this.internalUsers.getRandomUsername();
try {
findAllMatchingTestMail(username);
@@ -105,7 +107,7 @@
* unprocessed by the random access. this is done by iterating over all user accounts, looking for mail
*/
public void doMatchMailForAllUsers() {
- Iterator<String> usernames = m_internalUsers.getUsernames();
+ Iterator<String> usernames = this.internalUsers.getUsernames();
while (usernames.hasNext()) {
String username = usernames.next();
try {
@@ -131,7 +133,7 @@
entries = pop3Client.listMessages();
} catch (Exception e) {
String errorMessage = "failed to read pop3 account mail list for " + username;
- m_results.addError(500, errorMessage);
+ this.results.addError(500, errorMessage);
log.info(errorMessage);
return;
}
@@ -140,7 +142,7 @@
POP3MessageInfo entry = entries[i];
try {
- new POP3MailAnalyzeStrategy("pop3", m_results, pop3Client, entry.number, i).handle();
+ new POP3MailAnalyzeStrategy("pop3", this.results, pop3Client, entry.number, i).handle();
} catch (Exception exception) {
log.warn("error processing pop3 mail", exception);
}
diff --git a/src/main/java/org/apache/james/postage/client/POP3MailAnalyzeStrategy.java b/src/main/java/org/apache/james/postage/client/POP3MailAnalyzeStrategy.java
index 328378f..62edc4f 100644
--- a/src/main/java/org/apache/james/postage/client/POP3MailAnalyzeStrategy.java
+++ b/src/main/java/org/apache/james/postage/client/POP3MailAnalyzeStrategy.java
@@ -26,7 +26,6 @@
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
-import org.apache.james.smtpserver.ReaderInputStream;
import org.apache.james.postage.mail.MailAnalyzeStrategy;
import org.apache.james.postage.result.PostageRunnerResult;
diff --git a/src/main/java/org/apache/james/postage/smtpserver/SimpleServiceManager.java b/src/main/java/org/apache/james/postage/client/ReaderInputStream.java
similarity index 61%
rename from src/main/java/org/apache/james/postage/smtpserver/SimpleServiceManager.java
rename to src/main/java/org/apache/james/postage/client/ReaderInputStream.java
index 65d53fd..2d71b50 100644
--- a/src/main/java/org/apache/james/postage/smtpserver/SimpleServiceManager.java
+++ b/src/main/java/org/apache/james/postage/client/ReaderInputStream.java
@@ -17,33 +17,24 @@
* under the License. *
****************************************************************/
+package org.apache.james.postage.client;
-package org.apache.james.postage.smtpserver;
-
-import org.apache.avalon.framework.service.ServiceException;
-
-import java.util.HashMap;
-
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
/**
- * mock-up of ServiceManager
+ * Simple class to allow a cast from a java.io.Reader to a java.io.InputStream
+ *
*/
-public class SimpleServiceManager implements org.apache.avalon.framework.service.ServiceManager {
-
- private HashMap<String, Object> m_serviceMap = new HashMap<String, Object>();
-
- public Object lookup(String serviceName) throws ServiceException {
- return m_serviceMap.get(serviceName);
+public class ReaderInputStream extends InputStream {
+ private Reader reader = null;
+ public ReaderInputStream(Reader reader) {
+ this.reader = reader;
}
-
- public boolean hasService(String serviceName) {
- return m_serviceMap.get(serviceName) != null;
- }
-
- public void put(String name, Object service) {
- m_serviceMap.put(name, service);
- }
-
- public void release(Object object) {
- // trivial implementation
+ /**
+ * @see java.io.InputStream#read()
+ */
+ public int read() throws IOException {
+ return reader.read();
}
}
diff --git a/src/main/java/org/apache/james/postage/client/RemoteManagerClient.java b/src/main/java/org/apache/james/postage/client/RemoteManagerClient.java
deleted file mode 100644
index 86ee015..0000000
--- a/src/main/java/org/apache/james/postage/client/RemoteManagerClient.java
+++ /dev/null
@@ -1,157 +0,0 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one *
- * or more contributor license agreements. See the NOTICE file *
- * distributed with this work for additional information *
- * regarding copyright ownership. The ASF licenses this file *
- * to you under the Apache License, Version 2.0 (the *
- * "License"); you may not use this file except in compliance *
- * with the License. You may obtain a copy of the License at *
- * *
- * http://www.apache.org/licenses/LICENSE-2.0 *
- * *
- * Unless required by applicable law or agreed to in writing, *
- * software distributed under the License is distributed on an *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
- * KIND, either express or implied. See the License for the *
- * specific language governing permissions and limitations *
- * under the License. *
- ****************************************************************/
-
-
-package org.apache.james.postage.client;
-
-import org.apache.commons.net.telnet.TelnetClient;
-
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.io.Writer;
-import java.util.ArrayList;
-import java.util.List;
-
-/**
- * Apache James Server specific client to access its Remote Manager.<br/>
- * This is used for creating user accounts.
- */
-public class RemoteManagerClient {
-
- private String m_host = null;
- private int m_port = -1;
- private String m_username = null;
- private String m_password = null;
- private TelnetClient m_telnetClient;
- private BufferedReader m_reader;
- private Writer m_writer;
-
- public RemoteManagerClient(String host, int port, String username, String password) {
- m_host = host;
- m_port = port;
- m_username = username;
- m_password = password;
- }
-
- public boolean login() {
- m_telnetClient = new TelnetClient();
- try {
- m_telnetClient.connect(m_host, m_port);
- m_reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(m_telnetClient.getInputStream(), 1024), "ASCII"));
- m_writer = new OutputStreamWriter(m_telnetClient.getOutputStream());
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
-
- try {
- sendCommand(m_username);
- delay();
- sendCommand(m_password);
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- List<String> answers = readAnswer();
- if (answers == null || answers.size() == 0 || !answers.get(answers.size() - 1).startsWith("Welcome"))
- {
- disconnect();
- return false;
- }
- return true;
- }
-
- public void disconnect() {
- if (m_telnetClient == null) return;
- try {
- m_telnetClient.disconnect();
- } catch (IOException e) {
- e.printStackTrace();
- } finally{
- m_telnetClient = null;
- m_reader = null;
- m_writer = null;
- }
- }
-
- public List<String> executeCommand(String command) {
- try {
- sendCommand(command);
- } catch (IOException e) {
- e.printStackTrace();
- return null;
- }
-
- List<String> list = readAnswer();
- return list;
- }
-
- protected void sendCommand(String command) throws IOException {
- m_writer.write(command + "\n");
- m_writer.flush();
- }
-
- public List<String> readAnswer() {
- return readAnswer(0);
- }
-
- protected List<String> readAnswer(int numLines) {
- List<String> allAnswerLines = new ArrayList<String>();
- try {
- delay();
- if (numLines > 0) {
- for (int i = 0; i < numLines; i++) {
- readline(allAnswerLines);
- }
- } else {
- readline(allAnswerLines);
-
- while (m_reader.ready()) {
- while (m_reader.ready()) {
- readline(allAnswerLines);
- }
- delay();
- }
- }
- return allAnswerLines;
- } catch (NullPointerException e) {
- return null;
- } catch (IOException e) {
- return null;
- }
- }
-
- private void readline(List<String> allAnswerLines) throws IOException {
- if (!m_reader.ready()) return;
- String line = m_reader.readLine();
- if (line != null) allAnswerLines.add(line);
- }
-
- private void delay() {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- }
- }
-
-
-}
diff --git a/src/main/java/org/apache/james/postage/client/SMTPClient.java b/src/main/java/org/apache/james/postage/client/SMTPClient.java
index 2aa941e..e7a5e8a 100644
--- a/src/main/java/org/apache/james/postage/client/SMTPClient.java
+++ b/src/main/java/org/apache/james/postage/client/SMTPClient.java
@@ -20,21 +20,22 @@
package org.apache.james.postage.client;
-import org.apache.james.postage.SamplingException;
-import org.apache.james.postage.StartupException;
-import org.apache.james.postage.mail.HeaderConstants;
-import org.apache.james.postage.configuration.MailSender;
-import org.apache.james.postage.execution.Sampler;
-import org.apache.james.postage.result.MailProcessingRecord;
-import org.apache.james.postage.result.PostageRunnerResult;
-import org.apache.james.postage.user.UserList;
+import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
-import java.util.Properties;
+
+import org.apache.james.postage.SamplingException;
+import org.apache.james.postage.StartupException;
+import org.apache.james.postage.configuration.MailSender;
+import org.apache.james.postage.execution.Sampler;
+import org.apache.james.postage.mail.HeaderConstants;
+import org.apache.james.postage.result.MailProcessingRecord;
+import org.apache.james.postage.result.PostageRunnerResult;
+import org.apache.james.postage.user.UserList;
/**
* connects as a SMTP client and handles all mail according to its configuration.<br/>
@@ -42,20 +43,20 @@
*/
public class SMTPClient implements Sampler {
- private String m_host;
- private int m_port;
- private UserList m_internalUsers;
- private UserList m_externalUsers;
- private PostageRunnerResult m_results;
- private MailSender m_mailSender;
+ private String host;
+ private int port;
+ private UserList internalUsers;
+ private UserList externalUsers;
+ private PostageRunnerResult results;
+ private MailSender mailSender;
public SMTPClient(String host, int port, UserList internalUsers, UserList externalUsers, PostageRunnerResult results, MailSender mailSender) {
- m_host = host;
- m_port = port;
- m_internalUsers = internalUsers;
- m_externalUsers = externalUsers;
- m_mailSender = mailSender;
- m_results = results;
+ this.host = host;
+ this.port = port;
+ this.internalUsers = internalUsers;
+ this.externalUsers = externalUsers;
+ this.mailSender = mailSender;
+ this.results = results;
}
public boolean checkAvailability() throws StartupException {
@@ -64,11 +65,11 @@
MailProcessingRecord proformaMailProcessingRecord = new MailProcessingRecord();
Session session = getMailSession();
proformaMailProcessingRecord.setMailId(HeaderConstants.JAMES_POSTAGE_STARTUPCHECK_HEADER_ID);
- Message message = m_mailSender.createMail(session, proformaMailProcessingRecord);
+ Message message = this.mailSender.createMail(session, proformaMailProcessingRecord);
setMailFromAndTo(message, proformaMailProcessingRecord);
Transport.send(message);
} catch (Exception e) {
- throw new StartupException("inbound SMTP service not available", e);
+ throw new StartupException("Inbound SMTP service not available with " + this.toString() , e);
}
return true;
}
@@ -77,15 +78,15 @@
String senderUsername;
String senderMailAddress;
- if (m_mailSender.getParentProfile().isSourceInternal()) {
- senderUsername = m_internalUsers.getRandomUsername();
+ if (this.mailSender.getParentProfile().isSourceInternal()) {
+ senderUsername = this.internalUsers.getRandomUsername();
} else {
- senderUsername = m_externalUsers.getRandomUsername();
+ senderUsername = this.externalUsers.getRandomUsername();
}
- if (m_mailSender.getParentProfile().isSourceInternal()) {
- senderMailAddress = m_internalUsers.getEmailAddress(senderUsername);
+ if (this.mailSender.getParentProfile().isSourceInternal()) {
+ senderMailAddress = this.internalUsers.getEmailAddress(senderUsername);
} else {
- senderMailAddress = m_externalUsers.getEmailAddress(senderUsername);
+ senderMailAddress = this.externalUsers.getEmailAddress(senderUsername);
}
mailProcessingRecord.setSender(senderUsername);
mailProcessingRecord.setSenderMailAddress(senderMailAddress);
@@ -93,15 +94,15 @@
String recepientUsername;
String recepientMailAddress;
- if (m_mailSender.getParentProfile().isTargetInternal()) {
- recepientUsername = m_internalUsers.getRandomUsername();
+ if (this.mailSender.getParentProfile().isTargetInternal()) {
+ recepientUsername = this.internalUsers.getRandomUsername();
} else {
- recepientUsername = m_externalUsers.getRandomUsername();
+ recepientUsername = this.externalUsers.getRandomUsername();
}
- if (m_mailSender.getParentProfile().isTargetInternal()) {
- recepientMailAddress = m_internalUsers.getEmailAddress(recepientUsername);
+ if (this.mailSender.getParentProfile().isTargetInternal()) {
+ recepientMailAddress = this.internalUsers.getEmailAddress(recepientUsername);
} else {
- recepientMailAddress = m_externalUsers.getEmailAddress(recepientUsername);
+ recepientMailAddress = this.externalUsers.getEmailAddress(recepientUsername);
}
mailProcessingRecord.setReceiver(recepientUsername);
mailProcessingRecord.setReceiverMailAddress(recepientMailAddress);
@@ -112,14 +113,14 @@
MailProcessingRecord mailProcessingRecord = new MailProcessingRecord();
mailProcessingRecord.setMailId(MailProcessingRecord.getNextId());
- m_results.addNewMailRecord(mailProcessingRecord);
+ this.results.addNewMailRecord(mailProcessingRecord);
mailProcessingRecord.setTimeConnectStart(System.currentTimeMillis());
Message message = null;
try {
try {
Session session = getMailSession();
- message = m_mailSender.createMail(session, mailProcessingRecord);
+ message = this.mailSender.createMail(session, mailProcessingRecord);
} catch (Exception e) {
mailProcessingRecord.setErrorTextSending("Could not send mail");
throw e;
@@ -145,8 +146,14 @@
private Session getMailSession() {
Properties props = System.getProperties();
- props.put("mail.smtp.host", m_host);
- props.put("mail.smtp.port", Integer.toString(m_port));
+ props.put("mail.smtp.host", this.host);
+ props.put("mail.smtp.port", Integer.toString(this.port));
return Session.getDefaultInstance(props, null);
}
+
+ @Override
+ public String toString() {
+ return "SMTPClient [host=" + host + ", port=" + port + "]";
+ }
+
}
diff --git a/src/main/java/org/apache/james/postage/smtpserver/AvalonToPostageLogger.java b/src/main/java/org/apache/james/postage/smtpserver/AvalonToPostageLogger.java
deleted file mode 100644
index 56d2878..0000000
--- a/src/main/java/org/apache/james/postage/smtpserver/AvalonToPostageLogger.java
+++ /dev/null
@@ -1,103 +0,0 @@
-/****************************************************************
- * Licensed to the Apache Software Foundation (ASF) under one *
- * or more contributor license agreements. See the NOTICE file *
- * distributed with this work for additional information *
- * regarding copyright ownership. The ASF licenses this file *
- * to you under the Apache License, Version 2.0 (the *
- * "License"); you may not use this file except in compliance *
- * with the License. You may obtain a copy of the License at *
- * *
- * http://www.apache.org/licenses/LICENSE-2.0 *
- * *
- * Unless required by applicable law or agreed to in writing, *
- * software distributed under the License is distributed on an *
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
- * KIND, either express or implied. See the License for the *
- * specific language governing permissions and limitations *
- * under the License. *
- ****************************************************************/
-
-
-package org.apache.james.postage.smtpserver;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-/**
- * adapter to redirect log avalon messages to commons-logging
- */
-public class AvalonToPostageLogger implements org.apache.avalon.framework.logger.Logger {
-
- private static Log log = LogFactory.getLog(AvalonToPostageLogger.class);
-
- private boolean m_debugEnabled = true;
-
- public void debug(java.lang.String string) {
- log.debug(string);
- }
-
- public void debug(java.lang.String string, java.lang.Throwable throwable) {
- log.debug(string, throwable);
- }
-
- public boolean isDebugEnabled() {
- return m_debugEnabled;
- }
-
- public void disableDebug() {
- m_debugEnabled = false;
- }
-
- public void info(java.lang.String string) {
- log.debug(string);
- }
-
- public void info(java.lang.String string, java.lang.Throwable throwable) {
- log.debug(string, throwable);
- }
-
- public boolean isInfoEnabled() {
- return true;
- }
-
- public void warn(java.lang.String string) {
- log.info(string);
- }
-
- public void warn(java.lang.String string, java.lang.Throwable throwable) {
- log.info(string, throwable);
- }
-
- public boolean isWarnEnabled() {
- return true;
- }
-
- public void error(java.lang.String string) {
- log.info(string);
- }
-
- public void error(java.lang.String string, java.lang.Throwable throwable) {
- log.info(string, throwable);
- }
-
- public boolean isErrorEnabled() {
- return true;
- }
-
- public void fatalError(java.lang.String string) {
- log.error(string);
- }
-
- public void fatalError(java.lang.String string, java.lang.Throwable throwable) {
- log.error(string, throwable);
- }
-
- public boolean isFatalErrorEnabled() {
- return true;
- }
-
- public org.apache.avalon.framework.logger.Logger getChildLogger(java.lang.String string) {
- return this;
- }
-
-}
diff --git a/src/main/java/org/apache/james/postage/smtpserver/SMTPMailSink.java b/src/main/java/org/apache/james/postage/smtpserver/SMTPMailSink.java
index 207001c..c731d01 100644
--- a/src/main/java/org/apache/james/postage/smtpserver/SMTPMailSink.java
+++ b/src/main/java/org/apache/james/postage/smtpserver/SMTPMailSink.java
@@ -16,108 +16,261 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
-
package org.apache.james.postage.smtpserver;
-import org.apache.avalon.cornerstone.blocks.sockets.DefaultServerSocketFactory;
-import org.apache.avalon.cornerstone.blocks.sockets.DefaultSocketFactory;
-import org.apache.avalon.cornerstone.services.sockets.ServerSocketFactory;
-import org.apache.avalon.cornerstone.services.sockets.SocketFactory;
-import org.apache.avalon.cornerstone.services.sockets.SocketManager;
-import org.apache.avalon.cornerstone.services.threads.ThreadManager;
-import org.apache.avalon.excalibur.thread.impl.DefaultThreadPool;
-import org.apache.avalon.framework.configuration.DefaultConfiguration;
-import org.apache.avalon.framework.service.ServiceManager;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import org.apache.excalibur.thread.ThreadPool;
-import org.apache.james.api.dnsservice.DNSService;
-import org.apache.james.dnsserver.DNSServer;
+import org.apache.james.dnsservice.api.DNSService;
+import org.apache.james.domainlist.api.mock.SimpleDomainList;
+import org.apache.james.filesystem.api.FileSystem;
+import org.apache.james.filesystem.api.mock.MockFileSystem;
+import org.apache.james.mailrepository.api.MailRepositoryStore;
+import org.apache.james.mailrepository.mock.MockMailRepositoryStore;
import org.apache.james.postage.SamplingException;
import org.apache.james.postage.execution.Sampler;
import org.apache.james.postage.result.PostageRunnerResult;
-import org.apache.james.smtpserver.SMTPServer;
-import org.apache.james.socket.JamesConnectionManager;
-import org.apache.james.util.connection.SimpleConnectionManager;
+import org.apache.james.protocols.lib.mock.MockProtocolHandlerLoader;
+import org.apache.james.queue.api.MailQueue;
+import org.apache.james.queue.api.MailQueueFactory;
+import org.apache.james.queue.api.mock.MockMailQueue;
+import org.apache.james.queue.api.mock.MockMailQueueFactory;
+import org.apache.james.rrt.api.RecipientRewriteTable;
+import org.apache.james.rrt.api.RecipientRewriteTableException;
+import org.apache.james.smtpserver.netty.SMTPServer;
+import org.apache.james.user.api.UsersRepository;
+import org.apache.james.user.lib.mock.MockUsersRepository;
+import org.apache.mailet.HostAddress;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
- * puts up a gateway SMTP server acting as a mail sink for the external mail sent out by James.<br/>
- * mails are catched, test mails are identified and tracked
+ * <p>
+ * Puts up a gateway SMTP server acting as a mail sink for the external mail sent out by James.
+ * </p>
+ * <p>
+ * Mails are catched, test mails are identified and tracked.
+ * </p>
*/
-public class SMTPMailSink implements Sampler, SocketManager, ThreadManager {
+public class SMTPMailSink implements Sampler {
private static Log log = LogFactory.getLog(SMTPMailSink.class);
- private int m_smtpListenerPort = 2525;
- private SimpleMailServer m_mailServer = new SimpleMailServer();
- private SMTPServer m_smtpServer = new SMTPServer();
+ private int smtpListenerPort = 2525;
+ private SimpleMailServer mailServer = new SimpleMailServer();
+ private SMTPServer smtpServer = new SMTPServer();
+
+ protected HierarchicalConfiguration configuration;
+ protected UsersRepository usersRepository = new MockUsersRepository();
+ protected AlterableDNSServer dnsServer;
+ protected MailRepositoryStore store;
+ protected FileSystem fileSystem;
+ protected DNSService dnsService;
+ protected MockProtocolHandlerLoader chain;
+ protected MailQueueFactory queueFactory;
+ protected MailQueue queue;
public int getSmtpListenerPort() {
- return m_smtpListenerPort;
+ return this.smtpListenerPort;
}
public void setSmtpListenerPort(int smtpListenerPort) {
- m_smtpListenerPort = smtpListenerPort;
+ this.smtpListenerPort = smtpListenerPort;
}
public void setResults(PostageRunnerResult results) {
- m_mailServer.setResults(results);
+ this.mailServer.setResults(results);
}
+
public void initialize() throws Exception {
- m_smtpServer.enableLogging(new AvalonToPostageLogger());
- m_smtpServer.service(setUpServiceManager());
+ Logger log = LoggerFactory.getLogger("SMTP");
- SimpleSMTPServerConfiguration testConfiguration = new SimpleSMTPServerConfiguration(m_smtpListenerPort);
- testConfiguration.init();
- m_smtpServer.configure(testConfiguration);
+ chain = new MockProtocolHandlerLoader();
- m_smtpServer.initialize();
+ chain.put("usersrepository", this.usersRepository);
+
+ this.dnsServer = new AlterableDNSServer();
+ chain.put("dnsservice", this.dnsServer);
+
+ store = new MockMailRepositoryStore();
+ chain.put("mailStore", store);
+ fileSystem = new MockFileSystem();
+
+ chain.put("filesystem", fileSystem);
+ chain.put("org.apache.james.smtpserver.protocol.DNSService", dnsService);
+ chain.put("recipientrewritetable", new RecipientRewriteTable() {
+ @Override
+ public void addRegexMapping(String user, String domain, String regex) throws RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public void removeRegexMapping(String user, String domain, String regex) throws
+ RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public void addAddressMapping(String user, String domain, String address) throws
+ RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public void removeAddressMapping(String user, String domain, String address) throws
+ RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public void addErrorMapping(String user, String domain, String error) throws RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public void removeErrorMapping(String user, String domain, String error) throws
+ RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public Collection<String> getUserDomainMappings(String user, String domain) throws
+ RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public void addMapping(String user, String domain, String mapping) throws RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public void removeMapping(String user, String domain, String mapping) throws RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public Map<String, Collection<String>> getAllMappings() throws RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public void addAliasDomainMapping(String aliasDomain, String realDomain) throws
+ RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public void removeAliasDomainMapping(String aliasDomain, String realDomain) throws
+ RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ @Override
+ public Collection<String> getMappings(String user, String domain) throws ErrorMappingException,
+ RecipientRewriteTableException {
+ throw new UnsupportedOperationException("Not implemented");
+ }
+ });
+
+ chain.put("org.apache.james.smtpserver.protocol.DNSService", dnsService);
+ queueFactory = new MockMailQueueFactory();
+ queue = (MockMailQueue) queueFactory.getQueue(MockMailQueueFactory.SPOOL);
+ chain.put("mailqueuefactory", queueFactory);
+ chain.put("domainlist", new SimpleDomainList() {
+
+ @Override
+ public boolean containsDomain(String serverName) {
+ return "localhost".equals(serverName);
+ }
+ });
+
+ this.smtpServer = new SMTPServer();
+ this.smtpServer.setDNSService(this.dnsServer);
+ this.smtpServer.setFileSystem(fileSystem);
+
+ this.smtpServer.setProtocolHandlerLoader(chain);
+
+ this.smtpServer.setLog(log);
+
+// this.configuration = new SimpleSMTPServerConfiguration(getSmtpListenerPort());
+// this.smtpServer.configure(this.configuration);
+
+ this.smtpServer.init();
+
}
- private ServiceManager setUpServiceManager() throws Exception {
- SimpleServiceManager serviceManager = new SimpleServiceManager();
- SimpleConnectionManager connectionManager = new SimpleConnectionManager();
- connectionManager.enableLogging(new AvalonToPostageLogger());
- serviceManager.put(JamesConnectionManager.ROLE, connectionManager);
- serviceManager.put("org.apache.mailet.MailetContext", new TrivialMailContext());
- serviceManager.put("org.apache.james.services.MailServer", m_mailServer);
- serviceManager.put("org.apache.james.services.UsersRepository", null); //m_usersRepository);
- DNSServer server = new DNSServer();
- server.enableLogging(new AvalonToPostageLogger());
- server.configure(new DefaultConfiguration("dnsserver"));
- server.initialize();
- serviceManager.put(DNSService.ROLE, server);
- serviceManager.put(SocketManager.ROLE, this);
- serviceManager.put(ThreadManager.ROLE, this);
- return serviceManager;
- }
-
- public ServerSocketFactory getServerSocketFactory(String string) throws Exception {
- return new DefaultServerSocketFactory();
- }
-
- public SocketFactory getSocketFactory(String string) throws Exception {
- return new DefaultSocketFactory();
- }
-
- public ThreadPool getThreadPool(String string) throws IllegalArgumentException {
- return getDefaultThreadPool();
- }
-
- public ThreadPool getDefaultThreadPool() {
- try {
- DefaultThreadPool defaultThreadPool = new DefaultThreadPool(1);
- defaultThreadPool.enableLogging(new AvalonToPostageLogger());
- return defaultThreadPool;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
+ protected void setUpFakeLoader() throws Exception {
}
public void doSample() throws SamplingException {
log.debug("sampling while mails are coming in.");
}
+
+ final class AlterableDNSServer implements DNSService {
+ private InetAddress localhostByName = null;
+ @Override
+ public Collection<String> findMXRecords(String hostname) {
+ List<String> res = new ArrayList<String>();
+ if (hostname == null) {
+ return res;
+ }
+ if ("james.apache.org".equals(hostname)) {
+ res.add("nagoya.apache.org");
+ }
+ return res;
+ }
+ public Iterator<HostAddress> getSMTPHostAddresses(String domainName) {
+ throw new UnsupportedOperationException("Unimplemented mock service");
+ }
+ @Override
+ public InetAddress[] getAllByName(String host) throws UnknownHostException {
+ return new InetAddress[]{getByName(host)};
+ }
+ @Override
+ public InetAddress getByName(String host) throws UnknownHostException {
+ if (getLocalhostByName() != null) {
+ if ("127.0.0.1".equals(host)) {
+ return getLocalhostByName();
+ }
+ }
+ if ("1.0.0.127.bl.spamcop.net.".equals(host)) {
+ return InetAddress.getByName("localhost");
+ }
+ if ("james.apache.org".equals(host)) {
+ return InetAddress.getByName("james.apache.org");
+ }
+ if ("abgsfe3rsf.de".equals(host)) {
+ throw new UnknownHostException();
+ }
+ if ("128.0.0.1".equals(host) || "192.168.0.1".equals(host) || "127.0.0.1".equals(host) || "127.0.0.0".equals(
+ host) || "255.0.0.0".equals(host) || "255.255.255.255".equals(host)) {
+ return InetAddress.getByName(host);
+ }
+ throw new UnsupportedOperationException("getByName not implemented in mock for host: " + host);
+ }
+ @Override
+ public Collection<String> findTXTRecords(String hostname) {
+ List<String> res = new ArrayList<String>();
+ if (hostname == null) {
+ return res;
+ }
+ if ("2.0.0.127.bl.spamcop.net.".equals(hostname)) {
+ res.add("Blocked - see http://www.spamcop.net/bl.shtml?127.0.0.2");
+ }
+ return res;
+ }
+ public InetAddress getLocalhostByName() {
+ return localhostByName;
+ }
+ public void setLocalhostByName(InetAddress localhostByName) {
+ this.localhostByName = localhostByName;
+ }
+ @Override
+ public String getHostName(InetAddress addr) {
+ return addr.getHostName();
+ }
+ @Override
+ public InetAddress getLocalHost() throws UnknownHostException {
+ return InetAddress.getLocalHost();
+ }
+ }
+
}
diff --git a/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java b/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java
index 65cd9d2..cde1198 100644
--- a/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java
+++ b/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java
@@ -16,38 +16,37 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
-
package org.apache.james.postage.smtpserver;
-import org.apache.james.postage.result.PostageRunnerResult;
-import org.apache.james.services.MailRepository;
-import org.apache.james.services.MailServer;
-import org.apache.mailet.Mail;
-import org.apache.mailet.MailAddress;
+import java.io.InputStream;
+import java.util.Collection;
+import java.util.HashSet;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
-import java.io.InputStream;
-import java.util.Collection;
-import java.util.HashSet;
-
+import org.apache.james.mailrepository.api.MailRepository;
+import org.apache.james.postage.result.PostageRunnerResult;
+import org.apache.james.smtpserver.netty.SMTPServer;
+import org.apache.mailet.Mail;
+import org.apache.mailet.MailAddress;
/**
- * a quite simple (only receiving) SMTP server which reads mails and tries to match them with sent test mails.<br/>
- * reuses James' own SMTP server components
+ * <p>
+ * A quite simple (only receiving) {@link SMTPServer} which reads mails and tries to match
+ * them with sent test mails. It reuses James' own SMTP server components.
+ * </p>
*/
-public class SimpleMailServer implements MailServer {
+public class SimpleMailServer {
- private int m_counter = 0;
- private PostageRunnerResult m_results;
+ private int counter = 0;
+ private PostageRunnerResult results;
public void sendMail(MailAddress sender, Collection recipients, MimeMessage message) throws MessagingException {
try {
- new SMTPMailAnalyzeStrategy("smtpOutbound", m_results, message).handle();
+ new SMTPMailAnalyzeStrategy("smtpOutbound", this.results, message).handle();
} catch (Exception e) {
throw new MessagingException("error handling message", e);
}
@@ -84,8 +83,8 @@
}
public synchronized String getId() {
- m_counter++;
- return "SimpleMailServer-ID-" + m_counter;
+ this.counter++;
+ return "SimpleMailServer-ID-" + this.counter;
}
public boolean addUser(String userName, String password) {
@@ -97,11 +96,9 @@
}
public void setResults(PostageRunnerResult results) {
- m_results = results;
+ this.results = results;
}
- /* JAMES 3.0-SNAPSHOT specific methods */
-
public String getDefaultDomain() {
return "localhost";
}
diff --git a/src/main/java/org/apache/james/postage/smtpserver/SimpleSMTPServerConfiguration.java b/src/main/java/org/apache/james/postage/smtpserver/SimpleSMTPServerConfiguration.java
index a7bb77f..5a44090 100644
--- a/src/main/java/org/apache/james/postage/smtpserver/SimpleSMTPServerConfiguration.java
+++ b/src/main/java/org/apache/james/postage/smtpserver/SimpleSMTPServerConfiguration.java
@@ -16,66 +16,89 @@
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
-
-
package org.apache.james.postage.smtpserver;
-import org.apache.avalon.framework.configuration.DefaultConfiguration;
-import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.DefaultConfigurationBuilder;
+import org.apache.james.smtpserver.CoreCmdHandlerLoader;
+import org.apache.james.smtpserver.fastfail.DNSRBLHandler;
+import org.apache.james.smtpserver.fastfail.MaxRcptHandler;
+import org.apache.james.smtpserver.fastfail.ResolvableEhloHeloHandler;
+import org.apache.james.smtpserver.fastfail.ReverseEqualsEhloHeloHandler;
+import org.apache.james.smtpserver.fastfail.ValidSenderDomainHandler;
-public class SimpleSMTPServerConfiguration extends DefaultConfiguration {
- private int m_smtpListenerPort;
- private String m_authorizedAddresses = "127.0.0.0/8";
- private String m_authorizingMode = "false";
-
- public static Configuration getValuedConfiguration(String name, String value) {
- DefaultConfiguration defaultConfiguration = new DefaultConfiguration(name);
- defaultConfiguration.setValue(value);
- return defaultConfiguration;
- }
-
+public class SimpleSMTPServerConfiguration extends DefaultConfigurationBuilder {
+ private static final long serialVersionUID = 6459491961488047925L;
+ private int smtpListenerPort;
+ private int maxMessageSizeKB = 0;
+ private String authorizedAddresses = "127.0.0.0/8";
+ private String authorizingMode = "false";
+ private boolean verifyIdentity = false;
+ private Integer connectionLimit = null;
+ private Integer connectionBacklog = null;
+ private boolean heloResolv = false;
+ private boolean ehloResolv = false;
+ private boolean senderDomainResolv = false;
+ private boolean checkAuthNetworks = false;
+ private boolean heloEhloEnforcement = true;
+ private boolean reverseEqualsHelo = false;
+ private boolean reverseEqualsEhlo = false;
+ private int maxRcpt = 0;
+ private boolean useRBL = false;
+ private boolean addressBracketsEnforcement = true;
+ private boolean startTLS = false;
+
public SimpleSMTPServerConfiguration(int smtpListenerPort) {
- super("smptserver");
-
- m_smtpListenerPort = smtpListenerPort;
+ this.smtpListenerPort = smtpListenerPort;
}
- public String getAuthorizedAddresses() {
- return m_authorizedAddresses;
- }
+ public void init() throws ConfigurationException {
- public void setAuthorizedAddresses(String authorizedAddresses) {
- m_authorizedAddresses = authorizedAddresses;
- }
+ addProperty("[@enabled]", true);
- public void setAuthorizingNotRequired() {
- m_authorizingMode = "false";
- }
+ addProperty("bind", "127.0.0.1:" + smtpListenerPort);
+ if (connectionLimit != null)
+ addProperty("connectionLimit", "" + connectionLimit.intValue());
+ if (connectionBacklog != null)
+ addProperty("connectionBacklog", "" + connectionBacklog.intValue());
- public void setAuthorizingRequired() {
- m_authorizingMode = "true";
- //m_verifyIdentity = true;
- }
+ addProperty("helloName", "myMailServer");
+ addProperty("connectiontimeout", 360000);
+ addProperty("authorizedAddresses", authorizedAddresses);
+ addProperty("maxmessagesize", maxMessageSizeKB);
+ addProperty("authRequired", authorizingMode);
+ addProperty("heloEhloEnforcement", heloEhloEnforcement);
+ addProperty("addressBracketsEnforcement", addressBracketsEnforcement);
- public void setAuthorizingAnnounce() {
- m_authorizingMode = "announce";
- //m_verifyIdentity = true;
- }
+ addProperty("tls.[@startTLS]", startTLS);
+ addProperty("tls.keystore", "file://conf/test_keystore");
+ addProperty("tls.secret", "jamestest");
+ if (verifyIdentity)
+ addProperty("verifyIdentity", verifyIdentity);
- public void init() {
+ // add the rbl handler
+ if (useRBL) {
- setAttribute("enabled", true);
-
- addChild(getValuedConfiguration("port", "" + m_smtpListenerPort));
-
- DefaultConfiguration handlerConfig = new DefaultConfiguration("handler");
- handlerConfig.addChild(getValuedConfiguration("helloName", "myMailServer"));
- handlerConfig.addChild(getValuedConfiguration("connectiontimeout", "360000"));
- handlerConfig.addChild(getValuedConfiguration("authorizedAddresses", m_authorizedAddresses));
- handlerConfig.addChild(getValuedConfiguration("maxmessagesize", "" + 0));
- handlerConfig.addChild(getValuedConfiguration("authRequired", m_authorizingMode));
-
- addChild(handlerConfig);
+ addProperty("handlerchain.handler.[@class]", DNSRBLHandler.class.getName());
+ addProperty("handlerchain.handler.rblservers.blacklist", "bl.spamcop.net.");
+ }
+ if (heloResolv || ehloResolv) {
+ addProperty("handlerchain.handler.[@class]", ResolvableEhloHeloHandler.class.getName());
+ addProperty("handlerchain.handler.checkAuthNetworks", checkAuthNetworks);
+ }
+ if (reverseEqualsHelo || reverseEqualsEhlo) {
+ addProperty("handlerchain.handler.[@class]", ReverseEqualsEhloHeloHandler.class.getName());
+ addProperty("handlerchain.handler.checkAuthNetworks", checkAuthNetworks);
+ }
+ if (senderDomainResolv) {
+ addProperty("handlerchain.handler.[@class]", ValidSenderDomainHandler.class.getName());
+ addProperty("handlerchain.handler.checkAuthNetworks", checkAuthNetworks);
+ }
+ if (maxRcpt > 0) {
+ addProperty("handlerchain.handler.[@class]", MaxRcptHandler.class.getName());
+ addProperty("handlerchain.handler.maxRcpt", maxRcpt);
+ }
+ addProperty("handlerchain.[@coreHandlersPackage]", CoreCmdHandlerLoader.class.getName());
}
}