You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by be...@apache.org on 2006/09/06 11:02:00 UTC

svn commit: r440660 - in /james/postage/trunk/src/main/java/org/apache/james/postage: classloading/ client/ mail/ result/ smtpserver/

Author: berndf
Date: Wed Sep  6 02:01:59 2006
New Revision: 440660

URL: http://svn.apache.org/viewvc?view=rev&rev=440660
Log:
implements POSTAGE-5: validate received test mails

Added:
    james/postage/trunk/src/main/java/org/apache/james/postage/classloading/
    james/postage/trunk/src/main/java/org/apache/james/postage/classloading/CachedInstanceFactory.java
    james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailValidator.java
    james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailValidator.java
Modified:
    james/postage/trunk/src/main/java/org/apache/james/postage/client/POP3Client.java
    james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailFactory.java
    james/postage/trunk/src/main/java/org/apache/james/postage/mail/HeaderConstants.java
    james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailMatchingUtils.java
    james/postage/trunk/src/main/java/org/apache/james/postage/result/MailProcessingRecord.java
    james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResult.java
    james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResultImpl.java
    james/postage/trunk/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java

Added: james/postage/trunk/src/main/java/org/apache/james/postage/classloading/CachedInstanceFactory.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/classloading/CachedInstanceFactory.java?view=auto&rev=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/classloading/CachedInstanceFactory.java (added)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/classloading/CachedInstanceFactory.java Wed Sep  6 02:01:59 2006
@@ -0,0 +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;
+    }
+
+}

Modified: james/postage/trunk/src/main/java/org/apache/james/postage/client/POP3Client.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/client/POP3Client.java?view=diff&rev=440660&r1=440659&r2=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/client/POP3Client.java (original)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/client/POP3Client.java Wed Sep  6 02:01:59 2006
@@ -209,19 +209,24 @@
             log.info("failed to process mail. remains on server");
             return;
         } finally {
-            matchMails(mailProcessingRecord);
+        	MailProcessingRecord matchedAndMergedRecord = matchMails(mailProcessingRecord);
+            if (matchedAndMergedRecord != null) {
+            	MailMatchingUtils.validateMail(message, matchedAndMergedRecord);
+            }
         }
     }
 
-    private void matchMails(MailProcessingRecord mailProcessingRecord) {
-        boolean matched = m_results.matchMailRecord(mailProcessingRecord);
-        if (!matched) {
+    private MailProcessingRecord matchMails(MailProcessingRecord mailProcessingRecord) {
+        MailProcessingRecord matchedAndMergedRecord = m_results.matchMailRecord(mailProcessingRecord);
+        if (matchedAndMergedRecord == null) {
+        	// (but only do this if sure this is a Postage test mail for this runner)
             String oldMailId = mailProcessingRecord.getMailId();
             String newMailId = MailProcessingRecord.getNextId();
             mailProcessingRecord.setMailId(newMailId);
             log.info("changed mail id from " + oldMailId + " to " + newMailId);
             m_results.addNewMailRecord(mailProcessingRecord);
         }
+        return matchedAndMergedRecord;
     }
 
     private int getPartSize(MimeMultipart parts, String mimeType) {

Modified: james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailFactory.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailFactory.java?view=diff&rev=440660&r1=440659&r2=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailFactory.java (original)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailFactory.java Wed Sep  6 02:01:59 2006
@@ -54,6 +54,7 @@
         try {
             message.addHeader("Mime-Version", "1.0");
             message.addHeader(HeaderConstants.JAMES_POSTAGE_HEADER, "This is a test mail sent by James Postage");
+            message.addHeader(HeaderConstants.JAMES_POSTAGE_VALIDATORCLASSNAME_HEADER, "org.apache.james.postage.mail.DefaultMailValidator");
             message.setSubject(mailSender.getSubject());
             message.addHeader("Message-ID", "Postage-" + System.currentTimeMillis());
             mailProcessingRecord.setSubject(mailSender.getSubject());

Added: james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailValidator.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailValidator.java?view=auto&rev=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailValidator.java (added)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/mail/DefaultMailValidator.java Wed Sep  6 02:01:59 2006
@@ -0,0 +1,31 @@
+/****************************************************************
+ * 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;
+
+public class DefaultMailValidator implements MailValidator {
+
+	public boolean validate(Message message, MailProcessingRecord record) {
+		return true;
+	}
+
+}

Modified: james/postage/trunk/src/main/java/org/apache/james/postage/mail/HeaderConstants.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/mail/HeaderConstants.java?view=diff&rev=440660&r1=440659&r2=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/mail/HeaderConstants.java (original)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/mail/HeaderConstants.java Wed Sep  6 02:01:59 2006
@@ -26,6 +26,7 @@
 public class HeaderConstants {
     public static final String MAIL_ID_HEADER = "X-James-Postage-Count";
     public static final String JAMES_POSTAGE_HEADER = "X-James-Postage";
+    public static final String JAMES_POSTAGE_VALIDATORCLASSNAME_HEADER = "X-James-Validator";
 
     public static final String JAMES_POSTAGE_STARTUPCHECK_HEADER_ID = "PROFORMA";
 }

Modified: james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailMatchingUtils.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailMatchingUtils.java?view=diff&rev=440660&r1=440659&r2=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailMatchingUtils.java (original)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailMatchingUtils.java Wed Sep  6 02:01:59 2006
@@ -20,6 +20,8 @@
 
 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;
 
@@ -105,5 +107,17 @@
         }
         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;
     }
 }

Added: james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailValidator.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailValidator.java?view=auto&rev=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailValidator.java (added)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/mail/MailValidator.java Wed Sep  6 02:01:59 2006
@@ -0,0 +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);
+}

Modified: james/postage/trunk/src/main/java/org/apache/james/postage/result/MailProcessingRecord.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/result/MailProcessingRecord.java?view=diff&rev=440660&r1=440659&r2=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/result/MailProcessingRecord.java (original)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/result/MailProcessingRecord.java Wed Sep  6 02:01:59 2006
@@ -31,6 +31,7 @@
     private static int m_messageId = 1;
 
     boolean matchedSentAndReceived = false;
+    boolean isReceivedValid = false;
 
     long timeConnectStart;
     String mailId;
@@ -62,6 +63,14 @@
     public boolean isMatchedSentAndReceived() {
         return matchedSentAndReceived;
     }
+    
+    public void setValid() {
+    	isReceivedValid = true;
+    }
+    
+    public boolean isReceivedValid() {
+    	return isReceivedValid;
+    }
 
     public long getTimeConnectStart() {
         return timeConnectStart;
@@ -256,6 +265,7 @@
         if (timeFetchEnd == 0) timeFetchEnd = anotherRecord.timeFetchEnd;
         if (timeServerReceived == 0) timeServerReceived = anotherRecord.timeServerReceived;
         if (receivingQueue == null) receivingQueue = anotherRecord.receivingQueue;
+        if (anotherRecord.isReceivedValid) isReceivedValid = anotherRecord.isReceivedValid;
     }
 
     public static StringBuffer writeHeader() {
@@ -282,6 +292,7 @@
         stringBuffer.append("timeFetchEnd").append(SEPARATOR);
         stringBuffer.append("timeServerReceived").append(SEPARATOR);
         stringBuffer.append("receivingQueue").append(SEPARATOR);
+        stringBuffer.append("valid").append(SEPARATOR);
         stringBuffer.append("\r\n");
 
         return stringBuffer;
@@ -311,6 +322,7 @@
         stringBuffer.append(timeFetchEnd).append(SEPARATOR);
         stringBuffer.append(timeServerReceived).append(SEPARATOR);
         stringBuffer.append(receivingQueue).append(SEPARATOR);
+        stringBuffer.append(isReceivedValid).append(SEPARATOR);
         stringBuffer.append("\r\n");
 
         return stringBuffer;

Modified: james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResult.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResult.java?view=diff&rev=440660&r1=440659&r2=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResult.java (original)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResult.java Wed Sep  6 02:01:59 2006
@@ -28,7 +28,12 @@
 
     void addNewMailRecord(MailProcessingRecord mailProcessingRecord);
 
-    boolean matchMailRecord(MailProcessingRecord mailProcessingRecord);
+    /**
+     * 
+     * @param mailProcessingRecord record for whom a match is searched
+     * @return null, if no match is found or matching and merged record otherwise
+     */
+    MailProcessingRecord matchMailRecord(MailProcessingRecord mailProcessingRecord);
 
     void addJVMResult(JVMResourcesRecord jvmResourcesRecord);
 

Modified: james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResultImpl.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResultImpl.java?view=diff&rev=440660&r1=440659&r2=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResultImpl.java (original)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/result/PostageRunnerResultImpl.java Wed Sep  6 02:01:59 2006
@@ -66,10 +66,10 @@
         }
     }
 
-    public synchronized boolean matchMailRecord(MailProcessingRecord mailProcessingRecord) {
-        if (mailProcessingRecord == null) return false;
+    public synchronized MailProcessingRecord matchMailRecord(MailProcessingRecord mailProcessingRecord) {
+        if (mailProcessingRecord == null) return null;
         String mailId = mailProcessingRecord.getMailId();
-        if (mailId == null) return false;
+        if (mailId == null) return null;
 
         if (m_unmatchedMailResults.containsKey(mailId)) {
             // merge both mail result objects into one and move it to matched list
@@ -80,14 +80,14 @@
 
             m_matchedMailResults.put(mailId, match);
             m_matchedMailCounter++;
-            return true;
+            return match;
         } else if (m_matchedMailResults.containsKey(mailId)) {
             log.warn("mail already matched for mailId = " + mailId);
         } else {
             log.warn("mail match candidate has unknown (purged?) mailId = " + mailId);
         }
 
-        return false;
+        return null;
     }
 
     public void addJVMResult(JVMResourcesRecord jvmResourcesRecord) {

Modified: james/postage/trunk/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java
URL: http://svn.apache.org/viewvc/james/postage/trunk/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java?view=diff&rev=440660&r1=440659&r2=440660
==============================================================================
--- james/postage/trunk/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java (original)
+++ james/postage/trunk/src/main/java/org/apache/james/postage/smtpserver/SimpleMailServer.java Wed Sep  6 02:01:59 2006
@@ -51,20 +51,20 @@
     private int m_counter = 0;
     private PostageRunnerResult m_results;
 
-    public void sendMail(MailAddress sender, Collection recipients, MimeMessage msg) throws MessagingException {
+    public void sendMail(MailAddress sender, Collection recipients, MimeMessage message) throws MessagingException {
         //log.info("start processing incoming mail having id = " + msg.getMessageID());
         MailProcessingRecord mailProcessingRecord = new MailProcessingRecord();
         mailProcessingRecord.setReceivingQueue("smtpOutbound");
         mailProcessingRecord.setTimeFetchStart(System.currentTimeMillis());
-        mailProcessingRecord.setByteReceivedTotal(msg.getSize());
+        mailProcessingRecord.setByteReceivedTotal(message.getSize());
 
         try {
-            if (!MailMatchingUtils.isMatchCandidate(msg)) return;
+            if (!MailMatchingUtils.isMatchCandidate(message)) return;
 
-            String id = MailMatchingUtils.getMailIdHeader(msg);
+            String id = MailMatchingUtils.getMailIdHeader(message);
             mailProcessingRecord.setMailId(id);
 
-            String[] subjectHeader = msg.getHeader("Subject");
+            String[] subjectHeader = message.getHeader("Subject");
             if (subjectHeader != null && subjectHeader.length > 0) {
                 mailProcessingRecord.setSubject(subjectHeader[0]);
             }
@@ -77,10 +77,13 @@
             log.error("error processing incoming mail: " + e.getMessage());
             throw e; // rethrow after logging
         } finally{
-            boolean matched = m_results.matchMailRecord(mailProcessingRecord);
-            if (!matched) {
+        	MailProcessingRecord matchedAndMergedRecord = m_results.matchMailRecord(mailProcessingRecord);
+            if (matchedAndMergedRecord == null) {
                 if (mailProcessingRecord.getMailId() == null) mailProcessingRecord.setMailId(MailProcessingRecord.getNextId());
                 m_results.addNewMailRecord(mailProcessingRecord);
+            }
+            else {
+            	MailMatchingUtils.validateMail(message, matchedAndMergedRecord);
             }
         }
     }



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org