Author: norman
Date: Thu Nov 16 08:00:34 2006
New Revision: 475785

URL: http://svn.apache.org/viewvc?view=rev&rev=475785
Log:
create an AbstractActionHandler (Not sure this is the right solutin, yet). See 
JAMES-614

Added:
    
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractActionHandler.java
   (with props)
    
james/server/trunk/src/test/org/apache/james/smtpserver/MaxRcptHandlerTest.java 
  (with props)
Modified:
    
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/MaxRcptHandler.java
    
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java

Added: 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractActionHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractActionHandler.java?view=auto&rev=475785
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractActionHandler.java
 (added)
+++ 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractActionHandler.java
 Thu Nov 16 08:00:34 2006
@@ -0,0 +1,161 @@
+/****************************************************************
+ * 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.smtpserver.core.filter.fastfail;
+
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.avalon.framework.logger.AbstractLogEnabled;
+import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.util.junkscore.JunkScore;
+import org.apache.james.util.junkscore.JunkScoreConfigUtil;
+
+/**
+ * TODO: Should we split this class  ?
+ *       Or maybe add a Handler which loads other handlers ?
+ *
+ */
+public abstract class AbstractActionHandler extends AbstractLogEnabled 
implements Configurable {
+    private String action = "reject";
+    private double score = 0;
+
+    /**
+     * @see 
org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
+     */
+    public void configure(Configuration handlerConfiguration) throws 
ConfigurationException {
+        
+        Configuration configAction = 
handlerConfiguration.getChild("action",false);
+        if(configAction != null) {
+            String configString = configAction.getValue();
+            if (configString.startsWith(JunkScoreConfigUtil.JUNKSCORE)) {
+                setAction(JunkScoreConfigUtil.JUNKSCORE);
+                
setScore(JunkScoreConfigUtil.getJunkScore(configAction.getValue()));
+            }
+        }
+        
+    }
+    
+    /**
+     * Set the Action which should be taken if the mail from has no valid 
domain.
+     * Supported are: junkScore and reject
+     * 
+     * @param action the action
+     */
+    public void setAction(String action) {
+        this.action = action.toLowerCase();
+    }
+    
+    /**
+     * Set the score which will get added to the JunkScore object if the 
action is junkScore andt the sender has no valid domain
+     * 
+     * @param score the score
+     */
+    public void setScore(double score) {
+        this.score = score;
+    }
+    
+    /**
+     * Return the configured score
+     * 
+     * @return score
+     */
+    protected double getScore() {
+        return score;
+    }
+    
+    
+    /**
+     * Return the action
+     * 
+     * @return action
+     */
+    protected String getAction() {
+        return action;
+    }
+    
+    /**
+     * Process the checking
+     * 
+     * @param session the SMTPSession
+     */
+    protected void doProcessing(SMTPSession session) {
+        if (check(session)) {
+            if (getAction().equals(JunkScoreConfigUtil.JUNKSCORE)) {
+                getLogger().info(getJunkScoreLogString(session));
+                JunkScore junk = getJunkScore(session);
+                junk.setStoredScore(getScoreName(), getScore());
+                 
+            } else {
+                String response = getResponseString(session);
+                getLogger().info(response);
+                session.writeResponse(response);
+                // After this filter match we should not call any other 
handler!
+                session.setStopHandlerProcessing(true);
+            }
+        } 
+    }
+    
+    /**
+     * All checks must be done in this method
+     * 
+     * @param session the SMTPSession
+     * @return true if the check match
+     */
+    protected abstract boolean check(SMTPSession session);
+    
+    /**
+     * Get the reponseString to return 
+     * 
+     * @param session the SMTPSession
+     * @return responseString
+     */
+    protected abstract String getResponseString(SMTPSession session);
+    
+    /**
+     * Return the LogString if a JunkScore action is used
+     * 
+     * @param session the SMTPSession
+     * @return the LogString
+     */
+    protected abstract String getJunkScoreLogString(SMTPSession session);
+    
+    /**
+     * Return the LogString if a Reject action is used
+     * 
+     * @param the SMTPSession
+     * @return the LogString
+     */
+    protected abstract String getRejectLogString(SMTPSession session);
+    
+    /**
+     * Return the Name which will used to store the JunkScore and get used in 
the headers
+     * @return the name
+     */
+    protected abstract String getScoreName();
+    
+    /**
+     * Return the JunkScore object
+     * 
+     * @return junkScore
+     */
+    protected abstract JunkScore getJunkScore(SMTPSession session);
+}

Propchange: 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/AbstractActionHandler.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/MaxRcptHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/MaxRcptHandler.java?view=diff&rev=475785&r1=475784&r2=475785
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/MaxRcptHandler.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/MaxRcptHandler.java
 Thu Nov 16 08:00:34 2006
@@ -27,12 +27,12 @@
 import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.james.smtpserver.CommandHandler;
 import org.apache.james.smtpserver.SMTPSession;
+import org.apache.james.util.junkscore.JunkScore;
 import org.apache.james.util.mail.dsn.DSNStatus;
 
-public class MaxRcptHandler extends AbstractLogEnabled implements
+public class MaxRcptHandler extends AbstractActionHandler implements
         CommandHandler, Configurable {
 
     private int maxRcpt = 0;
@@ -50,6 +50,8 @@
             throw new ConfigurationException(
                     "Please set the maxRcpt configuration value");
         }
+        
+        super.configure(handlerConfiguration);
     }
 
     /**
@@ -66,23 +68,7 @@
      * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession)
      */
     public void onCommand(SMTPSession session) {
-        String responseString = null;
-        int rcptCount = 0;
-
-        rcptCount = session.getRcptCount() + 1;
-
-        // check if the max recipients has reached
-        if (rcptCount > maxRcpt) {
-            responseString = "452 "
-                    + DSNStatus.getStatus(DSNStatus.NETWORK,
-                            DSNStatus.DELIVERY_TOO_MANY_REC)
-                    + " Requested action not taken: max recipients reached";
-            session.writeResponse(responseString);
-            getLogger().error(responseString);
-
-            // After this filter match we should not call any other handler!
-            session.setStopHandlerProcessing(true);
-        }
+        doProcessing(session);
     }
     
     /**
@@ -93,6 +79,53 @@
         implCommands.add("RCPT");
         
         return implCommands;
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#check(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected boolean check(SMTPSession session) {
+        // check if the max recipients has reached
+        return ((session.getRcptCount() + 1) > maxRcpt);
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getJunkScoreLogString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getJunkScoreLogString(SMTPSession session) {
+        return "Maximum recipients of " + maxRcpt + " reached. Add JunkScore: 
" +getScore();
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getRejectLogString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getRejectLogString(SMTPSession session) {
+        return getResponseString(session);
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getResponseString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getResponseString(SMTPSession session) {
+        String responseString = "452 "
+            + DSNStatus.getStatus(DSNStatus.NETWORK,
+                    DSNStatus.DELIVERY_TOO_MANY_REC)
+            + " Requested action not taken: max recipients reached";
+        return responseString;
+    }
+    
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getScoreName()
+     */
+    protected String getScoreName() {
+        return "MaxRcptCheck";
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getJunkScore()
+     */
+    protected JunkScore getJunkScore(SMTPSession session) {
+        return (JunkScore) session.getState().get(JunkScore.JUNK_SCORE);
     }
 
 }

Modified: 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java?view=diff&rev=475785&r1=475784&r2=475785
==============================================================================
--- 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
 (original)
+++ 
james/server/trunk/src/java/org/apache/james/smtpserver/core/filter/fastfail/ValidSenderDomainHandler.java
 Thu Nov 16 08:00:34 2006
@@ -27,7 +27,6 @@
 import org.apache.avalon.framework.configuration.Configurable;
 import org.apache.avalon.framework.configuration.Configuration;
 import org.apache.avalon.framework.configuration.ConfigurationException;
-import org.apache.avalon.framework.logger.AbstractLogEnabled;
 import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
 import org.apache.avalon.framework.service.Serviceable;
@@ -35,21 +34,17 @@
 import org.apache.james.smtpserver.CommandHandler;
 import org.apache.james.smtpserver.SMTPSession;
 import org.apache.james.util.junkscore.JunkScore;
-import org.apache.james.util.junkscore.JunkScoreConfigUtil;
 import org.apache.james.util.mail.dsn.DSNStatus;
 import org.apache.mailet.MailAddress;
 
 public class ValidSenderDomainHandler
-    extends AbstractLogEnabled
+    extends AbstractActionHandler
     implements CommandHandler, Configurable, Serviceable {
     
     private boolean checkAuthClients = false;
     
     private DNSServer dnsServer = null;
 
-    private String action = "reject";
-
-    private double score = 0;
     
     /**
      * @see 
org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
@@ -61,15 +56,7 @@
             setCheckAuthClients(configRelay.getValueAsBoolean(false));
         }
         
-        Configuration configAction = 
handlerConfiguration.getChild("action",false);
-        if(configAction != null) {
-            String configString = configAction.getValue();
-            if (configString.startsWith(JunkScoreConfigUtil.JUNKSCORE)) {
-                setAction(JunkScoreConfigUtil.JUNKSCORE);
-                
setScore(JunkScoreConfigUtil.getJunkScore(configAction.getValue()));
-            }
-        }
-        
+        super.configure(handlerConfiguration);
     }
     
     /**
@@ -98,50 +85,16 @@
     }
     
     /**
-     * Set the Action which should be taken if the mail from has no valid 
domain.
-     * Supported are: junkScore and reject
-     * 
-     * @param action the action
-     */
-    public void setAction(String action) {
-        this.action = action.toLowerCase();
-    }
-    
-    /**
-     * Set the score which will get added to the JunkScore object if the 
action is junkScore andt the sender has no valid domain
-     * 
-     * @param score the score
-     */
-    public void setScore(double score) {
-        this.score = score;
-    }
-    
-
-    /**
      * @see org.apache.james.smtpserver.CommandHandler#onCommand(SMTPSession)
      */
     public void onCommand(SMTPSession session) {
-        boolean match = checkRBL(session);
-        if (match == true) {
-        MailAddress senderAddress = (MailAddress) 
session.getState().get(SMTPSession.SENDER);
-
-            if (action.equals(JunkScoreConfigUtil.JUNKSCORE)) {
-                String response = "Sender " + senderAddress + " contains a 
domain with no valid MX records. Add Junkscore: " + score;
-                getLogger().info(response);
-                JunkScore junk = (JunkScore) 
session.getState().get(JunkScore.JUNK_SCORE);
-                junk.setStoredScore("ValidSenderDomainCheck", score);
-                 
-            } else {
-                String response = "501 
"+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+ " 
sender " + senderAddress + " contains a domain with no valid MX records";
-                getLogger().info(response);
-                session.writeResponse(response);
-                // After this filter match we should not call any other 
handler!
-                session.setStopHandlerProcessing(true);
-            }
-        } 
+        doProcessing(session);
     }
     
-    private boolean checkRBL(SMTPSession session) {
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#check(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected boolean check(SMTPSession session) {
         MailAddress senderAddress = (MailAddress) 
session.getState().get(SMTPSession.SENDER);
             
         // null sender so return
@@ -171,5 +124,44 @@
         implCommands.add("MAIL");
         
         return implCommands;
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getJunkScoreLogString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getJunkScoreLogString(SMTPSession session) {
+       MailAddress senderAddress = (MailAddress) 
session.getState().get(SMTPSession.SENDER);
+        String response = "Sender " + senderAddress + " contains a domain with 
no valid MX records. Add Junkscore: " + getScore();
+        return response;
+    }
+    
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getRejectLogString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getRejectLogString(SMTPSession session) {
+        return getResponseString(session);
+    }
+    
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getResponseString(org.apache.james.smtpserver.SMTPSession)
+     */
+    protected String getResponseString(SMTPSession session) {
+        MailAddress senderAddress = (MailAddress) 
session.getState().get(SMTPSession.SENDER);
+        String response = "501 
"+DSNStatus.getStatus(DSNStatus.PERMANENT,DSNStatus.ADDRESS_SYNTAX_SENDER)+ " 
sender " + senderAddress + " contains a domain with no valid MX records";
+        return response;
+    }
+
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getScoreName()
+     */
+    protected String getScoreName() {
+        return "ValidSenderDomainCheck";
+    }
+    
+    /**
+     * @see 
org.apache.james.smtpserver.core.filter.fastfail.AbstractActionHandler#getJunkScore()
+     */
+    protected JunkScore getJunkScore(SMTPSession session) {
+        return (JunkScore) session.getState().get(JunkScore.JUNK_SCORE);
     }
 }

Added: 
james/server/trunk/src/test/org/apache/james/smtpserver/MaxRcptHandlerTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/src/test/org/apache/james/smtpserver/MaxRcptHandlerTest.java?view=auto&rev=475785
==============================================================================
--- 
james/server/trunk/src/test/org/apache/james/smtpserver/MaxRcptHandlerTest.java 
(added)
+++ 
james/server/trunk/src/test/org/apache/james/smtpserver/MaxRcptHandlerTest.java 
Thu Nov 16 08:00:34 2006
@@ -0,0 +1,121 @@
+/****************************************************************
+ * 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.smtpserver;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.TestCase;
+
+import org.apache.avalon.framework.container.ContainerUtil;
+import org.apache.james.smtpserver.core.filter.fastfail.MaxRcptHandler;
+import org.apache.james.test.mock.avalon.MockLogger;
+import org.apache.james.util.junkscore.JunkScore;
+import org.apache.james.util.junkscore.JunkScoreImpl;
+
+
+
+public class MaxRcptHandlerTest extends TestCase{
+    
+    private String response;
+    
+    private SMTPSession setupMockedSession(final int rcptCount) {
+        SMTPSession session = new AbstractSMTPSession() {
+            HashMap state = new HashMap();
+            boolean processing = false;
+            
+            public Map getState() {
+                return state;
+            }
+            
+            public boolean isRelayingAllowed() {
+                return false;
+            }
+            
+            public void writeResponse(String resp) {
+                response = resp;
+            }
+            
+            public void setStopHandlerProcessing(boolean processing) {
+                this.processing = processing;
+            }
+            
+            public boolean getStopHandlerProcessing() {
+                return processing;
+            }
+            
+            public int getRcptCount() {
+                return rcptCount;
+            }
+            
+        };
+        return session;
+    }
+    
+    public void testRejectMaxRcpt() {
+        SMTPSession session = setupMockedSession(3);
+        MaxRcptHandler handler = new MaxRcptHandler();
+    
+        ContainerUtil.enableLogging(handler,new MockLogger());
+    
+        handler.setAction("reject");
+        handler.setMaxRcpt(2);
+        handler.onCommand(session);
+    
+        assertNotNull("Rejected.. To many recipients", response);
+        assertTrue("Reject.. Stop 
processing",session.getStopHandlerProcessing());
+    }
+    
+    public void testAddScoreMaxRcpt() {
+        SMTPSession session = setupMockedSession(3);
+        session.getState().put(JunkScore.JUNK_SCORE, new JunkScoreImpl());
+    
+        MaxRcptHandler handler = new MaxRcptHandler();
+    
+        ContainerUtil.enableLogging(handler,new MockLogger());
+    
+        handler.setAction("junkScore");
+        handler.setScore(20);
+        handler.setMaxRcpt(2);
+        handler.onCommand(session);
+    
+        assertNull("Not Rejected.. we use junkScore action", response);
+        assertFalse("Not Rejected.. we use junkScore 
action",session.getStopHandlerProcessing());
+        assertEquals("Get Score", ((JunkScore) 
session.getState().get(JunkScore.JUNK_SCORE)).getStoredScore("MaxRcptCheck"),20.0,0d);
+    }
+    
+    public void testNotRejectMaxRcpt() {
+        SMTPSession session = setupMockedSession(3);
+        MaxRcptHandler handler = new MaxRcptHandler();
+    
+        ContainerUtil.enableLogging(handler,new MockLogger());
+    
+        handler.setAction("reject");
+        handler.setMaxRcpt(4);
+        handler.onCommand(session);
+    
+        assertNull("Not Rejected..", response);
+        assertFalse("Not stop processing",session.getStopHandlerProcessing());
+    }
+
+}

Propchange: 
james/server/trunk/src/test/org/apache/james/smtpserver/MaxRcptHandlerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to