From b0e52531b84ac4cce56e01a3978524990086983e Mon Sep 17 00:00:00 2001 From: Bernd Fondermann Date: Wed, 27 Sep 2006 11:55:44 +0000 Subject: [PATCH] added fixcrlf to ant build file. apart from that, no effective code changes, just format adjustments and copyright header git-svn-id: https://svn.apache.org/repos/asf/james/postage/trunk@450409 13f79535-47bb-0310-9956-ffa450edef68 --- build.xml | 2 + .../apache/james/postage/PostageRunner.java | 6 +- .../postage/PostageRuntimeException.java | 80 ++--- .../classloading/CachedInstanceFactory.java | 116 +++---- .../james/postage/client/POP3Client.java | 8 +- .../client/POP3MailAnalyzeStrategy.java | 198 ++++++------ .../postage/mail/AbstractMailFactory.java | 202 ++++++------ .../postage/mail/DefaultMailFactory.java | 70 ++-- .../postage/mail/DefaultMailValidator.java | 108 +++---- .../postage/mail/MailAnalyzeStrategy.java | 188 +++++------ .../james/postage/mail/MailMatchingUtils.java | 300 +++++++++--------- .../james/postage/mail/MailValidator.java | 64 ++-- .../postage/result/MailProcessingRecord.java | 4 +- .../result/PostageRunnerResultImpl.java | 14 +- .../smtpserver/SMTPMailAnalyzeStrategy.java | 78 ++--- .../postage/smtpserver/SimpleMailServer.java | 8 +- 16 files changed, 733 insertions(+), 713 deletions(-) diff --git a/build.xml b/build.xml index 6d631b1..6311bc7 100644 --- a/build.xml +++ b/build.xml @@ -86,6 +86,8 @@ + + diff --git a/src/main/java/org/apache/james/postage/PostageRunner.java b/src/main/java/org/apache/james/postage/PostageRunner.java index bac0d04..01d2694 100644 --- a/src/main/java/org/apache/james/postage/PostageRunner.java +++ b/src/main/java/org/apache/james/postage/PostageRunner.java @@ -81,7 +81,7 @@ public class PostageRunner implements Runnable { private static String m_messageIdPrefix = null; public static String getMessageIdPrefix() { - return m_messageIdPrefix + "-"; + return m_messageIdPrefix + "-"; } /** @@ -96,9 +96,9 @@ public static String getMessageIdPrefix() { * @param postageConfiguration */ public PostageRunner(PostageConfiguration postageConfiguration) { - m_messageIdPrefix = "" + System.currentTimeMillis(); + m_messageIdPrefix = "" + System.currentTimeMillis(); - m_postageConfiguration = postageConfiguration; + m_postageConfiguration = postageConfiguration; int totalMailsPerMin = m_postageConfiguration.getTotalMailsPerMin(); int durationMinutes = m_postageConfiguration.getDurationMinutes(); diff --git a/src/main/java/org/apache/james/postage/PostageRuntimeException.java b/src/main/java/org/apache/james/postage/PostageRuntimeException.java index dc5b41e..0870397 100644 --- a/src/main/java/org/apache/james/postage/PostageRuntimeException.java +++ b/src/main/java/org/apache/james/postage/PostageRuntimeException.java @@ -1,40 +1,40 @@ -/**************************************************************** - * 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; - -/** - * base exception for Postage - */ -public class PostageRuntimeException extends RuntimeException { - public PostageRuntimeException() { - super(); - } - - public PostageRuntimeException(String message) { - super(message); - } - - public PostageRuntimeException(Throwable cause) { - super(cause); - } - - public PostageRuntimeException(String message, Throwable cause) { - super(message, cause); - } -} +/**************************************************************** + * 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; + +/** + * base exception for Postage + */ +public class PostageRuntimeException extends RuntimeException { + public PostageRuntimeException() { + super(); + } + + public PostageRuntimeException(String message) { + super(message); + } + + public PostageRuntimeException(Throwable cause) { + super(cause); + } + + public PostageRuntimeException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/org/apache/james/postage/classloading/CachedInstanceFactory.java b/src/main/java/org/apache/james/postage/classloading/CachedInstanceFactory.java index b67540b..501af6b 100644 --- a/src/main/java/org/apache/james/postage/classloading/CachedInstanceFactory.java +++ b/src/main/java/org/apache/james/postage/classloading/CachedInstanceFactory.java @@ -1,58 +1,58 @@ -/**************************************************************** - * 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.classloading; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -public class CachedInstanceFactory { - - private static Log log = LogFactory.getLog(CachedInstanceFactory.class); - - private final static Map m_classes = new HashMap(); - - public static Object createInstance(String classname) { - Object object = null; - - Class clazz = null; - // class is configured, but not yet loaded - if (classname != null && m_classes.get(classname) == null) { - try { - clazz = Class.forName(classname); - } catch (ClassNotFoundException e) { - log.error("failed to load class " + classname, e); - } - } - - // create instance, if custom class is given - if (clazz != null) { - try { - object = clazz.newInstance(); - } catch (Exception e) { - log.error("failed to create instance of class " + classname, e); - } - } - - return object; - } - -} +/**************************************************************** + * 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.classloading; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class CachedInstanceFactory { + + private static Log log = LogFactory.getLog(CachedInstanceFactory.class); + + private final static Map m_classes = new HashMap(); + + public static Object createInstance(String classname) { + Object object = null; + + Class clazz = null; + // class is configured, but not yet loaded + if (classname != null && m_classes.get(classname) == null) { + try { + clazz = Class.forName(classname); + } catch (ClassNotFoundException e) { + log.error("failed to load class " + classname, e); + } + } + + // create instance, if custom class is given + if (clazz != null) { + try { + object = clazz.newInstance(); + } catch (Exception e) { + log.error("failed to create instance of class " + classname, e); + } + } + + return object; + } + +} 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 54c136d..33f0660 100644 --- a/src/main/java/org/apache/james/postage/client/POP3Client.java +++ b/src/main/java/org/apache/james/postage/client/POP3Client.java @@ -121,10 +121,10 @@ private void findAllMatchingTestMail(String username) throws SamplingException { POP3MessageInfo entry = entries[i]; try { - new POP3MailAnalyzeStrategy("pop3", m_results, pop3Client, entry.number, i).handle(); - } catch (Exception exception) { - log.warn("error processing pop3 mail", exception); - } + new POP3MailAnalyzeStrategy("pop3", m_results, pop3Client, entry.number, i).handle(); + } catch (Exception exception) { + log.warn("error processing pop3 mail", exception); + } } closeSession(pop3Client); 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 11ff124..2c32f58 100644 --- a/src/main/java/org/apache/james/postage/client/POP3MailAnalyzeStrategy.java +++ b/src/main/java/org/apache/james/postage/client/POP3MailAnalyzeStrategy.java @@ -1,99 +1,99 @@ -/**************************************************************** - * 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 java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; - -import javax.mail.MessagingException; -import javax.mail.internet.MimeMessage; - -import org.apache.james.fetchmail.ReaderInputStream; -import org.apache.james.postage.mail.MailAnalyzeStrategy; -import org.apache.james.postage.result.PostageRunnerResult; - -public class POP3MailAnalyzeStrategy extends MailAnalyzeStrategy { - - private int mailNumber; - private int mailIndex; - private org.apache.commons.net.pop3.POP3Client pop3Client; - - public POP3MailAnalyzeStrategy(String receivingQueueName, PostageRunnerResult results, - org.apache.commons.net.pop3.POP3Client pop3Client, - int mailNumber, int mailIndex) { - super(receivingQueueName, results); - this.pop3Client = pop3Client; - this.mailNumber = mailNumber; - this.mailIndex = mailIndex; - } - - protected MimeMessage loadMessage() throws Exception { - Reader reader = pop3Client.retrieveMessage(mailNumber); - BufferedReader mailReader = new BufferedReader(reader); - InputStream in = new ReaderInputStream(mailReader); - MimeMessage message; - try { - message = new MimeMessage(null, in); - in.close(); - } catch (IOException e) { - log.info("failed to close POP3 mail reader."); - throw e; - } catch (MessagingException e) { - log.info("failed to process POP3 mail. remains on server"); - throw e; - } finally { - if (in != null) { - try { - in.close(); - } catch (IOException e) { - log.warn("error closing mail input stream"); - } - } - if (mailReader != null) { - try { - mailReader.close(); - } catch (IOException e) { - log.warn("error closing mail reader"); - } - } - if (reader != null) { - try { - reader.close(); - } catch (IOException e) { - log.warn("error closing (mail) reader"); - } - } - } - - return message; - } - - protected void dismissMessage() throws Exception { - try { - pop3Client.deleteMessage(mailIndex + 1); // don't retrieve again next time - } catch (Exception e) { - log.info("failed to delete POP3 mail."); - throw e; - } - } - - -} +/**************************************************************** + * 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 java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.Reader; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +import org.apache.james.fetchmail.ReaderInputStream; +import org.apache.james.postage.mail.MailAnalyzeStrategy; +import org.apache.james.postage.result.PostageRunnerResult; + +public class POP3MailAnalyzeStrategy extends MailAnalyzeStrategy { + + private int mailNumber; + private int mailIndex; + private org.apache.commons.net.pop3.POP3Client pop3Client; + + public POP3MailAnalyzeStrategy(String receivingQueueName, PostageRunnerResult results, + org.apache.commons.net.pop3.POP3Client pop3Client, + int mailNumber, int mailIndex) { + super(receivingQueueName, results); + this.pop3Client = pop3Client; + this.mailNumber = mailNumber; + this.mailIndex = mailIndex; + } + + protected MimeMessage loadMessage() throws Exception { + Reader reader = pop3Client.retrieveMessage(mailNumber); + BufferedReader mailReader = new BufferedReader(reader); + InputStream in = new ReaderInputStream(mailReader); + MimeMessage message; + try { + message = new MimeMessage(null, in); + in.close(); + } catch (IOException e) { + log.info("failed to close POP3 mail reader."); + throw e; + } catch (MessagingException e) { + log.info("failed to process POP3 mail. remains on server"); + throw e; + } finally { + if (in != null) { + try { + in.close(); + } catch (IOException e) { + log.warn("error closing mail input stream"); + } + } + if (mailReader != null) { + try { + mailReader.close(); + } catch (IOException e) { + log.warn("error closing mail reader"); + } + } + if (reader != null) { + try { + reader.close(); + } catch (IOException e) { + log.warn("error closing (mail) reader"); + } + } + } + + return message; + } + + protected void dismissMessage() throws Exception { + try { + pop3Client.deleteMessage(mailIndex + 1); // don't retrieve again next time + } catch (Exception e) { + log.info("failed to delete POP3 mail."); + throw e; + } + } + + +} diff --git a/src/main/java/org/apache/james/postage/mail/AbstractMailFactory.java b/src/main/java/org/apache/james/postage/mail/AbstractMailFactory.java index 2cacdfb..5466d0f 100644 --- a/src/main/java/org/apache/james/postage/mail/AbstractMailFactory.java +++ b/src/main/java/org/apache/james/postage/mail/AbstractMailFactory.java @@ -1,92 +1,110 @@ -package org.apache.james.postage.mail; - -import javax.mail.Message; -import javax.mail.MessagingException; -import javax.mail.Session; -import javax.mail.internet.MimeMessage; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.james.postage.configuration.MailSender; -import org.apache.james.postage.result.MailProcessingRecord; - -public abstract class AbstractMailFactory { - - private static Log log = LogFactory.getLog(DefaultMailFactory.class); - - private static final char[] CHARSET = new char[] - {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', - 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', - 'u', 'v', 'w', 'x', 'y', 'z', - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', - 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', - 'U', 'V', 'W', 'X', 'Y', 'Z'}; - - public static char getRandomChar() { - return CHARSET[getRandomInt()]; - } - - private static int getRandomInt() { - return (int)(Math.random() * (double)(CHARSET.length - 1)); - } - - public static byte getRandomByte() { - return (byte)CHARSET[getRandomInt()]; - } - - public AbstractMailFactory() { - super(); - } - - /** - * generates a mail containing data common to all test mails: postage headers, - */ - public Message createMail(Session mailSession, MailSender mailSender, MailProcessingRecord mailProcessingRecord) { - - MimeMessage message = new MimeMessage(mailSession); - - try { - message.addHeader(HeaderConstants.JAMES_POSTAGE_HEADER, "This is a test mail sent by James Postage"); - message.addHeader(HeaderConstants.JAMES_POSTAGE_VALIDATORCLASSNAME_HEADER, getValidatorClass().getName()); - message.setSubject(mailSender.getSubject()); - message.addHeader("Message-ID", "Postage-" + System.currentTimeMillis()); - mailProcessingRecord.setSubject(mailSender.getSubject()); - - if (mailProcessingRecord.getMailId() != null) { - message.addHeader(HeaderConstants.MAIL_ID_HEADER, mailProcessingRecord.getMailId()); - } else { - log.warn("ID header is NULL!"); - throw new RuntimeException("could not create mail with ID = NULL"); - } - - populateMessage(message, mailSender, mailProcessingRecord); - - } catch (MessagingException e) { - mailProcessingRecord.setErrorTextSending(e.toString()); - log.error("mail could not be created", e); - return null; - } - return message; - } - - /** - * here, the test case specific data must be added to the message. - * @param message - * @param mailSender - * @param mailProcessingRecord - * @throws MessagingException - */ - abstract protected void populateMessage(MimeMessage message, MailSender mailSender, MailProcessingRecord mailProcessingRecord) throws MessagingException; - - /** - * the class representing the validator - * - * @return validator class - */ - abstract protected Class getValidatorClass(); - - protected int generateRandomPartSize(int sizeMin, int sizeMax) { - return (int)(Math.random() * (double)(sizeMax - sizeMin)) + sizeMin; - } - -} \ No newline at end of file +/**************************************************************** + * 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.mail; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.MimeMessage; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.james.postage.configuration.MailSender; +import org.apache.james.postage.result.MailProcessingRecord; + +public abstract class AbstractMailFactory { + + private static Log log = LogFactory.getLog(DefaultMailFactory.class); + + private static final char[] CHARSET = new char[] + {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', + 'u', 'v', 'w', 'x', 'y', 'z', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', + 'U', 'V', 'W', 'X', 'Y', 'Z'}; + + public static char getRandomChar() { + return CHARSET[getRandomInt()]; + } + + private static int getRandomInt() { + return (int)(Math.random() * (double)(CHARSET.length - 1)); + } + + public static byte getRandomByte() { + return (byte)CHARSET[getRandomInt()]; + } + + public AbstractMailFactory() { + super(); + } + + /** + * generates a mail containing data common to all test mails: postage headers, + */ + public Message createMail(Session mailSession, MailSender mailSender, MailProcessingRecord mailProcessingRecord) { + + MimeMessage message = new MimeMessage(mailSession); + + try { + message.addHeader(HeaderConstants.JAMES_POSTAGE_HEADER, "This is a test mail sent by James Postage"); + message.addHeader(HeaderConstants.JAMES_POSTAGE_VALIDATORCLASSNAME_HEADER, getValidatorClass().getName()); + message.setSubject(mailSender.getSubject()); + message.addHeader("Message-ID", "Postage-" + System.currentTimeMillis()); + mailProcessingRecord.setSubject(mailSender.getSubject()); + + if (mailProcessingRecord.getMailId() != null) { + message.addHeader(HeaderConstants.MAIL_ID_HEADER, mailProcessingRecord.getMailId()); + } else { + log.warn("ID header is NULL!"); + throw new RuntimeException("could not create mail with ID = NULL"); + } + + populateMessage(message, mailSender, mailProcessingRecord); + + } catch (MessagingException e) { + mailProcessingRecord.setErrorTextSending(e.toString()); + log.error("mail could not be created", e); + return null; + } + return message; + } + + /** + * here, the test case specific data must be added to the message. + * @param message + * @param mailSender + * @param mailProcessingRecord + * @throws MessagingException + */ + abstract protected void populateMessage(MimeMessage message, MailSender mailSender, MailProcessingRecord mailProcessingRecord) throws MessagingException; + + /** + * the class representing the validator + * + * @return validator class + */ + abstract protected Class getValidatorClass(); + + protected int generateRandomPartSize(int sizeMin, int sizeMax) { + return (int)(Math.random() * (double)(sizeMax - sizeMin)) + sizeMin; + } + +} diff --git a/src/main/java/org/apache/james/postage/mail/DefaultMailFactory.java b/src/main/java/org/apache/james/postage/mail/DefaultMailFactory.java index 3c19009..657e9a1 100644 --- a/src/main/java/org/apache/james/postage/mail/DefaultMailFactory.java +++ b/src/main/java/org/apache/james/postage/mail/DefaultMailFactory.java @@ -33,49 +33,49 @@ public class DefaultMailFactory extends AbstractMailFactory implements MailFactory { - protected void populateMessage(MimeMessage message, MailSender mailSender, MailProcessingRecord mailProcessingRecord) throws MessagingException { + protected void populateMessage(MimeMessage message, MailSender mailSender, MailProcessingRecord mailProcessingRecord) throws MessagingException { message.addHeader("Mime-Version", "1.0"); message.addHeader("Content-Type", "multipart/mixed"); - Multipart multipart = new MimeMultipart("mixed"); + Multipart multipart = new MimeMultipart("mixed"); - if (mailSender.sendTextPart()) { - int sizeMinText = mailSender.getSizeMinText(); - int sizeMaxText = mailSender.getSizeMaxText(); - MimeBodyPart part = new MimeBodyPart(); + if (mailSender.sendTextPart()) { + int sizeMinText = mailSender.getSizeMinText(); + int sizeMaxText = mailSender.getSizeMaxText(); + MimeBodyPart part = new MimeBodyPart(); - int mailSize = generateRandomPartSize(sizeMinText, sizeMaxText); - mailProcessingRecord.setByteSendText(mailSize); + int mailSize = generateRandomPartSize(sizeMinText, sizeMaxText); + mailProcessingRecord.setByteSendText(mailSize); - StringBuffer textBody = new StringBuffer(mailSize); - for (int i = 0; i < mailSize; i++) textBody.append(getRandomChar()); + StringBuffer textBody = new StringBuffer(mailSize); + for (int i = 0; i < mailSize; i++) textBody.append(getRandomChar()); - part.setText(textBody.toString()); + part.setText(textBody.toString()); // part.setDataHandler(new DataHandler(textBody.toString(), "text/plain")); - - multipart.addBodyPart(part); - } - - if (mailSender.sendBinaryPart()) { - int sizeMinBinary = mailSender.getSizeMinBinary(); - int sizeMaxBinary = mailSender.getSizeMaxBinary(); - MimeBodyPart part = new MimeBodyPart(); - - int mailSize = generateRandomPartSize(sizeMinBinary, sizeMaxBinary); - mailProcessingRecord.setByteSendBinary(mailSize); - - byte[] bytes = new byte[mailSize]; - for (int i = 0; i < mailSize; i++) bytes[i] = getRandomByte(); - - part.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, "application/octet-stream"))); - multipart.addBodyPart(part); - } - message.setContent(multipart); - } - - protected Class getValidatorClass() { - return DefaultMailValidator.class; - } + + multipart.addBodyPart(part); + } + + if (mailSender.sendBinaryPart()) { + int sizeMinBinary = mailSender.getSizeMinBinary(); + int sizeMaxBinary = mailSender.getSizeMaxBinary(); + MimeBodyPart part = new MimeBodyPart(); + + int mailSize = generateRandomPartSize(sizeMinBinary, sizeMaxBinary); + mailProcessingRecord.setByteSendBinary(mailSize); + + byte[] bytes = new byte[mailSize]; + for (int i = 0; i < mailSize; i++) bytes[i] = getRandomByte(); + + part.setDataHandler(new DataHandler(new ByteArrayDataSource(bytes, "application/octet-stream"))); + multipart.addBodyPart(part); + } + message.setContent(multipart); + } + + protected Class getValidatorClass() { + return DefaultMailValidator.class; + } } diff --git a/src/main/java/org/apache/james/postage/mail/DefaultMailValidator.java b/src/main/java/org/apache/james/postage/mail/DefaultMailValidator.java index 638b936..51f367c 100644 --- a/src/main/java/org/apache/james/postage/mail/DefaultMailValidator.java +++ b/src/main/java/org/apache/james/postage/mail/DefaultMailValidator.java @@ -1,54 +1,54 @@ -/**************************************************************** - * 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.mail; - -import javax.mail.Message; -import javax.mail.MessagingException; -import javax.mail.internet.MimeMultipart; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.james.postage.result.MailProcessingRecord; - -public class DefaultMailValidator implements MailValidator { - - private static Log log = LogFactory.getLog(DefaultMailValidator.class); - - public boolean validate(Message message, MailProcessingRecord record) { - - MimeMultipart mimeMultipart; - try { - mimeMultipart = new MimeMultipart(message.getDataHandler().getDataSource()); - } catch (MessagingException e) { - return false; - } - - // figuring out the parts created by DefaultMailFactory - int textPartSize = MailMatchingUtils.getMimePartSize(mimeMultipart, "text/plain"); - record.setByteReceivedText(textPartSize); - int binaryPartSize = MailMatchingUtils.getMimePartSize(mimeMultipart, "application/octet-stream"); - record.setByteReceivedBinary(binaryPartSize); - - boolean textPartValid = textPartSize == record.getByteSendText(); - boolean binaryPartValid = binaryPartSize == record.getByteSendBinary(); - boolean valid = textPartValid && binaryPartValid; - return valid; - } - -} +/**************************************************************** + * 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.mail; + +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.internet.MimeMultipart; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.james.postage.result.MailProcessingRecord; + +public class DefaultMailValidator implements MailValidator { + + private static Log log = LogFactory.getLog(DefaultMailValidator.class); + + public boolean validate(Message message, MailProcessingRecord record) { + + MimeMultipart mimeMultipart; + try { + mimeMultipart = new MimeMultipart(message.getDataHandler().getDataSource()); + } catch (MessagingException e) { + return false; + } + + // figuring out the parts created by DefaultMailFactory + int textPartSize = MailMatchingUtils.getMimePartSize(mimeMultipart, "text/plain"); + record.setByteReceivedText(textPartSize); + int binaryPartSize = MailMatchingUtils.getMimePartSize(mimeMultipart, "application/octet-stream"); + record.setByteReceivedBinary(binaryPartSize); + + boolean textPartValid = textPartSize == record.getByteSendText(); + boolean binaryPartValid = binaryPartSize == record.getByteSendBinary(); + boolean valid = textPartValid && binaryPartValid; + return valid; + } + +} diff --git a/src/main/java/org/apache/james/postage/mail/MailAnalyzeStrategy.java b/src/main/java/org/apache/james/postage/mail/MailAnalyzeStrategy.java index 4e5701f..e78f60f 100644 --- a/src/main/java/org/apache/james/postage/mail/MailAnalyzeStrategy.java +++ b/src/main/java/org/apache/james/postage/mail/MailAnalyzeStrategy.java @@ -1,94 +1,94 @@ -/**************************************************************** - * 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.mail; - -import javax.mail.MessagingException; -import javax.mail.internet.MimeMessage; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.james.postage.result.MailProcessingRecord; -import org.apache.james.postage.result.PostageRunnerResult; - -/** - * handles the process of retrieving, analyzing and finally (optionally) dismissing the - */ -public abstract class MailAnalyzeStrategy { - - protected static Log log = LogFactory.getLog(MailAnalyzeStrategy.class); - - private String queue = null; - private PostageRunnerResult results = null; - - public MailAnalyzeStrategy(String receivingQueueName, PostageRunnerResult results) { - this.queue = receivingQueueName; - this.results = results; - } - - public void handle() throws Exception { - MailProcessingRecord mailProcessingRecord = prepareRecord(); - - MimeMessage message = loadMessage(); - - // do we _really_ have to handle this? - if (!MailMatchingUtils.isMatchCandidate(message)) return; - - String id = MailMatchingUtils.getMailIdHeader(message); - try { - mailProcessingRecord.setByteReceivedTotal(message.getSize()); - - mailProcessingRecord.setMailId(id); - mailProcessingRecord.setSubject(message.getSubject()); - - mailProcessingRecord.setTimeFetchEnd(System.currentTimeMillis()); - - } catch (MessagingException e) { - log.info(queue + ": failed to process mail. remains on server"); - return; - } finally { - MailProcessingRecord matchedAndMergedRecord = results.matchMailRecord(mailProcessingRecord); - if (matchedAndMergedRecord != null) { - MailMatchingUtils.validateMail(message, matchedAndMergedRecord); - results.recordValidatedMatch(matchedAndMergedRecord); - } - } - - dismissMessage(); - } - - /** - * mandatory override to make the message available - */ - protected MimeMessage loadMessage() throws Exception { - return null; - } - - /** - * optional override to delete the message. - */ - protected void dismissMessage() throws Exception { - ; // empty body - } - - private MailProcessingRecord prepareRecord() { - MailProcessingRecord mailProcessingRecord = new MailProcessingRecord(); - mailProcessingRecord.setReceivingQueue(queue); - mailProcessingRecord.setTimeFetchStart(System.currentTimeMillis()); - return mailProcessingRecord; - } -} +/**************************************************************** + * 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.mail; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.james.postage.result.MailProcessingRecord; +import org.apache.james.postage.result.PostageRunnerResult; + +/** + * handles the process of retrieving, analyzing and finally (optionally) dismissing the + */ +public abstract class MailAnalyzeStrategy { + + protected static Log log = LogFactory.getLog(MailAnalyzeStrategy.class); + + private String queue = null; + private PostageRunnerResult results = null; + + public MailAnalyzeStrategy(String receivingQueueName, PostageRunnerResult results) { + this.queue = receivingQueueName; + this.results = results; + } + + public void handle() throws Exception { + MailProcessingRecord mailProcessingRecord = prepareRecord(); + + MimeMessage message = loadMessage(); + + // do we _really_ have to handle this? + if (!MailMatchingUtils.isMatchCandidate(message)) return; + + String id = MailMatchingUtils.getMailIdHeader(message); + try { + mailProcessingRecord.setByteReceivedTotal(message.getSize()); + + mailProcessingRecord.setMailId(id); + mailProcessingRecord.setSubject(message.getSubject()); + + mailProcessingRecord.setTimeFetchEnd(System.currentTimeMillis()); + + } catch (MessagingException e) { + log.info(queue + ": failed to process mail. remains on server"); + return; + } finally { + MailProcessingRecord matchedAndMergedRecord = results.matchMailRecord(mailProcessingRecord); + if (matchedAndMergedRecord != null) { + MailMatchingUtils.validateMail(message, matchedAndMergedRecord); + results.recordValidatedMatch(matchedAndMergedRecord); + } + } + + dismissMessage(); + } + + /** + * mandatory override to make the message available + */ + protected MimeMessage loadMessage() throws Exception { + return null; + } + + /** + * optional override to delete the message. + */ + protected void dismissMessage() throws Exception { + ; // empty body + } + + private MailProcessingRecord prepareRecord() { + MailProcessingRecord mailProcessingRecord = new MailProcessingRecord(); + mailProcessingRecord.setReceivingQueue(queue); + mailProcessingRecord.setTimeFetchStart(System.currentTimeMillis()); + return mailProcessingRecord; + } +} diff --git a/src/main/java/org/apache/james/postage/mail/MailMatchingUtils.java b/src/main/java/org/apache/james/postage/mail/MailMatchingUtils.java index 54f6c5e..f484edd 100644 --- a/src/main/java/org/apache/james/postage/mail/MailMatchingUtils.java +++ b/src/main/java/org/apache/james/postage/mail/MailMatchingUtils.java @@ -1,150 +1,150 @@ -/**************************************************************** - * 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.mail; - -import org.apache.james.postage.PostageRunner; -import org.apache.james.postage.PostageRuntimeException; -import org.apache.james.postage.classloading.CachedInstanceFactory; -import org.apache.james.postage.result.MailProcessingRecord; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import javax.mail.BodyPart; -import javax.mail.MessagingException; -import javax.mail.internet.MimeMessage; -import javax.mail.internet.MimeMultipart; - -import java.util.regex.Pattern; - -/** - * helps matching, analysing and validating result mails and sent test mails - */ -public class MailMatchingUtils { - - private static Log log = LogFactory.getLog(MailMatchingUtils.class); - - /** - * if this mail was created by postage, whatever run - but by startup check - */ - public static boolean isPostageStartupCheckMail(MimeMessage message) { - String headerValue = getMailIdHeader(message); - return HeaderConstants.JAMES_POSTAGE_STARTUPCHECK_HEADER_ID.equals(headerValue); - } - - /** - * if this mail was created by postage, whatever run - but not by startup check - */ - public static boolean isPostageTestMail(MimeMessage message) { - return isPostageMail(message) && !isPostageStartupCheckMail(message); - } - - /** - * if this mail was created by postage, whatever run - if startup check or live test - */ - public static boolean isPostageMail(MimeMessage message) { - return null != getUniqueHeader(message, HeaderConstants.JAMES_POSTAGE_HEADER); - } - - public static boolean isPostageIdHeaderPresent(MimeMessage message) { - return null != getMailIdHeader(message); - } - - public static String getMailIdHeader(MimeMessage message) { - return getUniqueHeader(message, HeaderConstants.MAIL_ID_HEADER); - } - - /** - * if this mail was created by the currently running postage scenario - not by - * any of those before. - */ - public static boolean isCurrentRunnerMail(MimeMessage message) { - String headerValue = getMailIdHeader(message); - return headerValue != null && headerValue.startsWith(PostageRunner.getMessageIdPrefix()); - } - - public static boolean matchHeader(MimeMessage message, String header, String valueRegex) { - return Pattern.matches(valueRegex, getUniqueHeader(message, header)); - } - - public static String getUniqueHeader(MimeMessage message, String header) { - String[] idHeaders; - try { - idHeaders = message.getHeader(header); - } catch (MessagingException e) { - throw new PostageRuntimeException(e); - } - if (idHeaders != null && idHeaders.length > 0) { - return idHeaders[0]; // there should be exactly one. - } - return null; - } - - public static boolean isMatchCandidate(MimeMessage message) { - try { - if (!isPostageIdHeaderPresent(message)) { - if (isPostageMail(message)) { - log.warn(HeaderConstants.MAIL_ID_HEADER + " header is missing from James test mail"); - } - else log.info("skipping non-postage mail. remains on server. subject was: " + message.getSubject()); - return false; - } - } catch (MessagingException e) { - log.info("failed to get mail subject for logging. remains on server. mails might be corrupt."); - return false; - } - if (MailMatchingUtils.isPostageStartupCheckMail(message)) return false; - return true; - } - - public static boolean validateMail(MimeMessage message, MailProcessingRecord mailProcessingRecord) { - String classname = getUniqueHeader(message, HeaderConstants.JAMES_POSTAGE_VALIDATORCLASSNAME_HEADER); - MailValidator validator = (MailValidator)CachedInstanceFactory.createInstance(classname); - if (validator == null) return false; - - boolean isValid = validator.validate(message, mailProcessingRecord); - if (isValid) mailProcessingRecord.setValid(); - else log.warn("failed to validate mail"); - - return isValid; - } - - public static MimeMultipart convertToMimeMultipart(MimeMessage message) { - try { - return new MimeMultipart(message.getDataHandler().getDataSource()); - } catch (MessagingException e) { - throw new RuntimeException("could not convert MimeMessage to MimeMultipart", e); - } - } - - public static int getMimePartSize(MimeMultipart parts, String mimeType) { - if (parts != null) { - try { - for (int i = 0; i < parts.getCount(); i++) { - BodyPart bodyPart = parts.getBodyPart(i); - if (bodyPart.getContentType().startsWith(mimeType)) { - return bodyPart.getSize(); - } - } - } catch (MessagingException e) { - log.info("failed to process body parts.", e); - } - } - return 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.mail; + +import org.apache.james.postage.PostageRunner; +import org.apache.james.postage.PostageRuntimeException; +import org.apache.james.postage.classloading.CachedInstanceFactory; +import org.apache.james.postage.result.MailProcessingRecord; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.mail.BodyPart; +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; + +import java.util.regex.Pattern; + +/** + * helps matching, analysing and validating result mails and sent test mails + */ +public class MailMatchingUtils { + + private static Log log = LogFactory.getLog(MailMatchingUtils.class); + + /** + * if this mail was created by postage, whatever run - but by startup check + */ + public static boolean isPostageStartupCheckMail(MimeMessage message) { + String headerValue = getMailIdHeader(message); + return HeaderConstants.JAMES_POSTAGE_STARTUPCHECK_HEADER_ID.equals(headerValue); + } + + /** + * if this mail was created by postage, whatever run - but not by startup check + */ + public static boolean isPostageTestMail(MimeMessage message) { + return isPostageMail(message) && !isPostageStartupCheckMail(message); + } + + /** + * if this mail was created by postage, whatever run - if startup check or live test + */ + public static boolean isPostageMail(MimeMessage message) { + return null != getUniqueHeader(message, HeaderConstants.JAMES_POSTAGE_HEADER); + } + + public static boolean isPostageIdHeaderPresent(MimeMessage message) { + return null != getMailIdHeader(message); + } + + public static String getMailIdHeader(MimeMessage message) { + return getUniqueHeader(message, HeaderConstants.MAIL_ID_HEADER); + } + + /** + * if this mail was created by the currently running postage scenario - not by + * any of those before. + */ + public static boolean isCurrentRunnerMail(MimeMessage message) { + String headerValue = getMailIdHeader(message); + return headerValue != null && headerValue.startsWith(PostageRunner.getMessageIdPrefix()); + } + + public static boolean matchHeader(MimeMessage message, String header, String valueRegex) { + return Pattern.matches(valueRegex, getUniqueHeader(message, header)); + } + + public static String getUniqueHeader(MimeMessage message, String header) { + String[] idHeaders; + try { + idHeaders = message.getHeader(header); + } catch (MessagingException e) { + throw new PostageRuntimeException(e); + } + if (idHeaders != null && idHeaders.length > 0) { + return idHeaders[0]; // there should be exactly one. + } + return null; + } + + public static boolean isMatchCandidate(MimeMessage message) { + try { + if (!isPostageIdHeaderPresent(message)) { + if (isPostageMail(message)) { + log.warn(HeaderConstants.MAIL_ID_HEADER + " header is missing from James test mail"); + } + else log.info("skipping non-postage mail. remains on server. subject was: " + message.getSubject()); + return false; + } + } catch (MessagingException e) { + log.info("failed to get mail subject for logging. remains on server. mails might be corrupt."); + return false; + } + if (MailMatchingUtils.isPostageStartupCheckMail(message)) return false; + return true; + } + + public static boolean validateMail(MimeMessage message, MailProcessingRecord mailProcessingRecord) { + String classname = getUniqueHeader(message, HeaderConstants.JAMES_POSTAGE_VALIDATORCLASSNAME_HEADER); + MailValidator validator = (MailValidator)CachedInstanceFactory.createInstance(classname); + if (validator == null) return false; + + boolean isValid = validator.validate(message, mailProcessingRecord); + if (isValid) mailProcessingRecord.setValid(); + else log.warn("failed to validate mail"); + + return isValid; + } + + public static MimeMultipart convertToMimeMultipart(MimeMessage message) { + try { + return new MimeMultipart(message.getDataHandler().getDataSource()); + } catch (MessagingException e) { + throw new RuntimeException("could not convert MimeMessage to MimeMultipart", e); + } + } + + public static int getMimePartSize(MimeMultipart parts, String mimeType) { + if (parts != null) { + try { + for (int i = 0; i < parts.getCount(); i++) { + BodyPart bodyPart = parts.getBodyPart(i); + if (bodyPart.getContentType().startsWith(mimeType)) { + return bodyPart.getSize(); + } + } + } catch (MessagingException e) { + log.info("failed to process body parts.", e); + } + } + return 0; + } +} diff --git a/src/main/java/org/apache/james/postage/mail/MailValidator.java b/src/main/java/org/apache/james/postage/mail/MailValidator.java index dbeafc2..e615384 100644 --- a/src/main/java/org/apache/james/postage/mail/MailValidator.java +++ b/src/main/java/org/apache/james/postage/mail/MailValidator.java @@ -1,32 +1,32 @@ -/**************************************************************** - * 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.mail; - -import javax.mail.Message; - -import org.apache.james.postage.result.MailProcessingRecord; - -/** - * implementations validate matched and received messages by checking for the expected properties. - * for example, they could check for spam-marking headers or mime parts. - * most commonly, each MailValidator implementation matches one corresponding MailFactory. - */ -public interface MailValidator { - public boolean validate(Message message, MailProcessingRecord record); -} +/**************************************************************** + * 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.mail; + +import javax.mail.Message; + +import org.apache.james.postage.result.MailProcessingRecord; + +/** + * implementations validate matched and received messages by checking for the expected properties. + * for example, they could check for spam-marking headers or mime parts. + * most commonly, each MailValidator implementation matches one corresponding MailFactory. + */ +public interface MailValidator { + public boolean validate(Message message, MailProcessingRecord record); +} diff --git a/src/main/java/org/apache/james/postage/result/MailProcessingRecord.java b/src/main/java/org/apache/james/postage/result/MailProcessingRecord.java index 6ecad9a..79597ee 100644 --- a/src/main/java/org/apache/james/postage/result/MailProcessingRecord.java +++ b/src/main/java/org/apache/james/postage/result/MailProcessingRecord.java @@ -65,11 +65,11 @@ public boolean isMatchedSentAndReceived() { } public void setValid() { - isReceivedValid = true; + isReceivedValid = true; } public boolean isReceivedValid() { - return isReceivedValid; + return isReceivedValid; } public long getTimeConnectStart() { diff --git a/src/main/java/org/apache/james/postage/result/PostageRunnerResultImpl.java b/src/main/java/org/apache/james/postage/result/PostageRunnerResultImpl.java index 830e306..ccb9f5e 100644 --- a/src/main/java/org/apache/james/postage/result/PostageRunnerResultImpl.java +++ b/src/main/java/org/apache/james/postage/result/PostageRunnerResultImpl.java @@ -93,13 +93,13 @@ public synchronized MailProcessingRecord matchMailRecord(MailProcessingRecord ma } public void recordValidatedMatch(MailProcessingRecord matchedAndMergedRecord) { - if (!m_matchedMailResults.values().contains(matchedAndMergedRecord)) { - log.error("cannot record validation result for (already written?) result having id " - + matchedAndMergedRecord.getMailId()); - return; - } - - if (matchedAndMergedRecord.isReceivedValid()) m_validMailCounter++; + if (!m_matchedMailResults.values().contains(matchedAndMergedRecord)) { + log.error("cannot record validation result for (already written?) result having id " + + matchedAndMergedRecord.getMailId()); + return; + } + + if (matchedAndMergedRecord.isReceivedValid()) m_validMailCounter++; } public void addJVMResult(JVMResourcesRecord jvmResourcesRecord) { diff --git a/src/main/java/org/apache/james/postage/smtpserver/SMTPMailAnalyzeStrategy.java b/src/main/java/org/apache/james/postage/smtpserver/SMTPMailAnalyzeStrategy.java index fc319eb..91c2998 100644 --- a/src/main/java/org/apache/james/postage/smtpserver/SMTPMailAnalyzeStrategy.java +++ b/src/main/java/org/apache/james/postage/smtpserver/SMTPMailAnalyzeStrategy.java @@ -1,39 +1,39 @@ -/**************************************************************** - * 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 javax.mail.internet.MimeMessage; - -import org.apache.james.postage.mail.MailAnalyzeStrategy; -import org.apache.james.postage.result.PostageRunnerResult; - -public class SMTPMailAnalyzeStrategy extends MailAnalyzeStrategy { - - private MimeMessage message = null; - - public SMTPMailAnalyzeStrategy(String receivingQueueName, PostageRunnerResult results, MimeMessage message) { - super(receivingQueueName, results); - this.message = message; - } - - protected MimeMessage loadMessage() throws Exception { - return message; - } - -} +/**************************************************************** + * 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 javax.mail.internet.MimeMessage; + +import org.apache.james.postage.mail.MailAnalyzeStrategy; +import org.apache.james.postage.result.PostageRunnerResult; + +public class SMTPMailAnalyzeStrategy extends MailAnalyzeStrategy { + + private MimeMessage message = null; + + public SMTPMailAnalyzeStrategy(String receivingQueueName, PostageRunnerResult results, MimeMessage message) { + super(receivingQueueName, results); + this.message = message; + } + + protected MimeMessage loadMessage() throws Exception { + return message; + } + +} 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 12f9c6f..8e5c2ed 100644 --- a/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java +++ b/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java @@ -55,11 +55,11 @@ public class SimpleMailServer implements MailServer { private PostageRunnerResult m_results; public void sendMail(MailAddress sender, Collection recipients, MimeMessage message) throws MessagingException { - try { + try { new SMTPMailAnalyzeStrategy("smtpOutbound", m_results, message).handle(); - } catch (Exception e) { - throw new MessagingException("error handling message", e); - } + } catch (Exception e) { + throw new MessagingException("error handling message", e); + } } public void sendMail(MailAddress sender, Collection recipients, InputStream msg) throws MessagingException {