Author: norman
Date: Wed Jan 27 16:46:00 2010
New Revision: 903727

URL: http://svn.apache.org/viewvc?rev=903727&view=rev
Log:
Export DefaultVirtualUserTable through VirtualUserTableStore API + refactor 
ValidRcptHandler to only check in UsersRepository + VirtualUserTable for users 
(JAMES-919)

Modified:
    
james/server/trunk/avalon-user-function/src/main/java/org/apache/james/core/VirtualUserTableStoreImpl.java
    
james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/integration/ValidRcptHandler.java
    
james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java

Modified: 
james/server/trunk/avalon-user-function/src/main/java/org/apache/james/core/VirtualUserTableStoreImpl.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/avalon-user-function/src/main/java/org/apache/james/core/VirtualUserTableStoreImpl.java?rev=903727&r1=903726&r2=903727&view=diff
==============================================================================
--- 
james/server/trunk/avalon-user-function/src/main/java/org/apache/james/core/VirtualUserTableStoreImpl.java
 (original)
+++ 
james/server/trunk/avalon-user-function/src/main/java/org/apache/james/core/VirtualUserTableStoreImpl.java
 Wed Jan 27 16:46:00 2010
@@ -36,6 +36,9 @@
     implements VirtualUserTableStore {
 
 
+    private String defaultName;
+
+
     /** 
      * Get the repository, if any, whose name corresponds to
      * the argument parameter
@@ -45,6 +48,9 @@
      * @return the VirtualUserTable corresponding to the name parameter
      */
     public VirtualUserTable getTable(String name) {
+        if (name == null || name.trim().equals("")) {
+            name = defaultName;
+        }
         VirtualUserTable response = getObject(name);
         if ((response == null) && (getLogger().isWarnEnabled())) {
             getLogger().warn("No virtualUserTable called: " + name);
@@ -53,6 +59,15 @@
     }
 
     /**
+     * Set the default VirtualUserTable which will get returned when no name 
is given or the name is empty
+     * 
+     * @param defaultName
+     */
+    public void setDefaultTable(String defaultName) {
+        this.defaultName = defaultName;
+    }
+    
+    /**
      * @see org.apache.james.core.AbstractAvalonStore#getStoreName()
      */
     public String getStoreName() {

Modified: 
james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/integration/ValidRcptHandler.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/integration/ValidRcptHandler.java?rev=903727&r1=903726&r2=903727&view=diff
==============================================================================
--- 
james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/integration/ValidRcptHandler.java
 (original)
+++ 
james/server/trunk/smtpserver-function/src/main/java/org/apache/james/smtpserver/integration/ValidRcptHandler.java
 Wed Jan 27 16:46:00 2010
@@ -19,9 +19,7 @@
 
 package org.apache.james.smtpserver.integration;
 
-import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 
 import javax.annotation.PostConstruct;
 import javax.annotation.Resource;
@@ -33,13 +31,10 @@
 import org.apache.james.api.vut.VirtualUserTable;
 import org.apache.james.api.vut.VirtualUserTableStore;
 import org.apache.james.lifecycle.Configurable;
+import org.apache.james.services.MailServer;
 import org.apache.james.smtpserver.protocol.SMTPSession;
 import 
org.apache.james.smtpserver.protocol.core.fastfail.AbstractValidRcptHandler;
 import org.apache.mailet.MailAddress;
-import org.apache.oro.text.regex.MalformedPatternException;
-import org.apache.oro.text.regex.Pattern;
-import org.apache.oro.text.regex.Perl5Compiler;
-import org.apache.oro.text.regex.Perl5Matcher;
 
 /**
  * Handler which reject invalid recipients
@@ -50,14 +45,13 @@
        private UsersRepository users;
 
        private VirtualUserTableStore tableStore;
-
-       private Collection<String> recipients = new ArrayList<String>();
-       private Collection<String> domains = new ArrayList<String>();
-       private Collection<Pattern> regex = new ArrayList<Pattern>();
-       private boolean vut = true;
        private VirtualUserTable table;
        private String tableName = null;
 
+    private boolean vut = true;
+
+    private MailServer mailServer;
+
        /**
         * Gets the users repository.
         * 
@@ -98,6 +92,11 @@
                this.tableStore = tableStore;
        }
        
+       @Resource(name = "mailServer")
+       public void setMailServer(MailServer mailServer) {
+           this.mailServer = mailServer;
+       }
+       
        @PostConstruct
        public void init() throws Exception{
                loadTable();
@@ -106,70 +105,14 @@
        /**
         * @see 
org.apache.james.lifecycle.Configurable#configure(org.apache.commons.configuration.Configuration)
         */
-       @SuppressWarnings("unchecked")
        public void configure(HierarchicalConfiguration config) throws 
ConfigurationException {
-               setValidRecipients(config.getList("validRecipients"));
-               setValidDomains(config.getList("validDomains"));
-               try {
-                       setValidRegex(config.getList("validRegexPattern"));
-               } catch (MalformedPatternException mpe) {
-                       throw new ConfigurationException("Malformed pattern: ", 
mpe);
-               }
                
setVirtualUserTableSupport(config.getBoolean("enableVirtualUserTable",
                                true));
                setTableName(config.getString("table", null));
        }
 
-       /**
-        * Set the valid recipients.
-        * 
-        * @param recip
-        *            The valid recipients. Commaseperated list
-        */
-       public void setValidRecipients(Collection<String> recips) {
-               Iterator<String> recipsIt = recips.iterator();
-               while (recipsIt.hasNext()) {
-                       String recipient = recipsIt.next();
-                       recipients.add(recipient);
-               }
-       }
-
-       /**
-        * Set the domains for which every rcpt will be accepted.
-        * 
-        * @param dom
-        *            The valid domains. Comma seperated list
-        */
-       public void setValidDomains(Collection<String> doms) {
-               Iterator<String> domsIt = doms.iterator();
-
-               while (domsIt.hasNext()) {
-                       String domain = domsIt.next().toLowerCase();
-                       domains.add(domain);
-               }
-       }
-
-       /**
-        * 
-        * @param reg
-        * @throws MalformedPatternException
-        */
-       public void setValidRegex(Collection<String> regs)
-                       throws MalformedPatternException {
-               Perl5Compiler compiler = new Perl5Compiler();
-
-               Iterator<String> regsIt = regs.iterator();
-               while (regsIt.hasNext()) {
-                       String patternString = regsIt.next();
-                       Pattern pattern = compiler.compile(patternString,
-                                       Perl5Compiler.READ_ONLY_MASK);
-                       regex.add(pattern);
-
-               }
-       }
-
        public void setVirtualUserTableSupport(boolean vut) {
-               this.vut = vut;
+               this.vut  = vut;
        }
 
        public void setTableName(String tableName) {
@@ -177,15 +120,7 @@
        }
 
        private void loadTable() throws Exception {
-               if (this.tableName == null || this.tableName.equals("")) {
-                       this.tableName = VirtualUserTableStore.DEFAULT_TABLE;
-               }
-               if (tableStore != null) {
-                       table = tableStore.getTable(this.tableName);
-               }
-               if (table == null) {
-                       throw new Exception("Unable to find VirtualUserTable 
with name " + tableName);
-               }
+        table = tableStore.getTable(this.tableName);   
        }
 
 
@@ -193,9 +128,11 @@
        protected boolean isValidRecipient(SMTPSession session,
                        MailAddress recipient) {
 
-               if (users.contains(recipient.getLocalPart()) == true
-                               || 
recipients.contains(recipient.toString().toLowerCase())
-                               || 
domains.contains(recipient.getDomain().toLowerCase())) {
+           if (mailServer.isLocalServer(recipient.getDomain()) == false) {
+               return false;
+           }
+           
+               if (users.contains(recipient.getLocalPart()) == true) {
                        return true;
                } else {
                        if (vut == true) {
@@ -211,17 +148,6 @@
                                }
                        }
 
-                       if (!regex.isEmpty()) {
-                               Iterator<Pattern> reg = regex.iterator();
-                               Perl5Matcher matcher = new Perl5Matcher();
-
-                               while (reg.hasNext()) {
-                                       if 
(matcher.matches(recipient.toString(), reg.next())) {
-                                               // regex match
-                                               return true;
-                                       }
-                               }
-                       }
                        return false;
                }
        }

Modified: 
james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
URL: 
http://svn.apache.org/viewvc/james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java?rev=903727&r1=903726&r2=903727&view=diff
==============================================================================
--- 
james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
 (original)
+++ 
james/server/trunk/smtpserver-function/src/test/java/org/apache/james/smtpserver/ValidRcptHandlerTest.java
 Wed Jan 27 16:46:00 2010
@@ -22,17 +22,23 @@
 
 package org.apache.james.smtpserver;
 
+import java.io.InputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
 
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+
 import junit.framework.TestCase;
 
 import org.apache.james.api.user.UsersRepository;
 import org.apache.james.api.vut.ErrorMappingException;
 import org.apache.james.api.vut.VirtualUserTable;
 import org.apache.james.api.vut.VirtualUserTableStore;
+import org.apache.james.services.MailRepository;
+import org.apache.james.services.MailServer;
 import org.apache.james.smtpserver.integration.ValidRcptHandler;
 import org.apache.james.smtpserver.protocol.BaseFakeSMTPSession;
 import org.apache.james.smtpserver.protocol.SMTPConfiguration;
@@ -40,11 +46,12 @@
 import org.apache.james.smtpserver.protocol.hook.HookReturnCode;
 import org.apache.james.test.mock.james.MockVirtualUserTableStore;
 import org.apache.james.userrepository.MockUsersRepository;
+import org.apache.mailet.Mail;
 import org.apache.mailet.MailAddress;
-import org.apache.oro.text.regex.MalformedPatternException;
 
 public class ValidRcptHandlerTest extends TestCase {
     
+    private final static String VALID_DOMAIN = "localhost";
     private final static String VALID_USER = "postmaster";
     private final static String INVALID_USER = "invalid";
     private final static String USER1 = "user1";
@@ -60,6 +67,63 @@
         handler = new ValidRcptHandler();
         handler.setUsers(users);
         handler.setTableStore(setUpVirtualUserTableStore());
+        handler.setMailServer(new MailServer() {
+
+            public boolean addUser(String userName, String password) {
+                // TODO Auto-generated method stub
+                return false;
+            }
+
+            public String getDefaultDomain() {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            public String getHelloName() {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            public String getId() {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            public MailRepository getUserInbox(String userName) {
+                // TODO Auto-generated method stub
+                return null;
+            }
+
+            public boolean isLocalServer(String serverName) {
+                return serverName.equals(VALID_DOMAIN);
+            }
+
+            public void sendMail(MailAddress sender, Collection<MailAddress> 
recipients, MimeMessage msg) throws MessagingException {
+                // TODO Auto-generated method stub
+                
+            }
+
+            public void sendMail(MailAddress sender, Collection<MailAddress> 
recipients, InputStream msg) throws MessagingException {
+                // TODO Auto-generated method stub
+                
+            }
+
+            public void sendMail(Mail mail) throws MessagingException {
+                // TODO Auto-generated method stub
+                
+            }
+
+            public void sendMail(MimeMessage message) throws 
MessagingException {
+                // TODO Auto-generated method stub
+                
+            }
+
+            public boolean supportVirtualHosting() {
+                // TODO Auto-generated method stub
+                return false;
+            }
+            
+        });
         handler.init();
     }
 
@@ -92,8 +156,14 @@
                 return mappings;
             }
         };
-        final MockVirtualUserTableStore store = new 
MockVirtualUserTableStore();
-        store.tableStore.put(VirtualUserTableStore.DEFAULT_TABLE, table);
+        final MockVirtualUserTableStore store = new 
MockVirtualUserTableStore() {
+
+            @Override
+            public VirtualUserTable getTable(String name) {
+                return table;
+            }
+            
+        };
         return store;
     }
     
@@ -168,65 +238,7 @@
         
         assertEquals("Not rejected",rCode,HookReturnCode.DECLINED);
     }
-    
-    public void testNotRejectValidUserRecipient() throws Exception {
-        String recipient = "re...@domain";
-        ArrayList<String> list = new ArrayList<String>();
-        list.add(recipient);
-        MailAddress mailAddress = new MailAddress(recipient);
-        SMTPSession session = 
setupMockedSMTPSession(setupMockedSMTPConfiguration(),mailAddress,false);
-    
-        handler.setValidRecipients(list);
-
-        int rCode = handler.doRcpt(session, null, mailAddress).getResult();
-        
-        assertEquals("Not rejected",rCode,HookReturnCode.DECLINED);
-    }
-    
-    public void testNotRejectValidUserDomain() throws Exception {
-        String domain = "domain";
-        String recipient = "recip@" + domain;
-        ArrayList<String> list = new ArrayList<String>();
-        list.add(domain);
-        MailAddress mailAddress = new MailAddress(recipient);
-        SMTPSession session = 
setupMockedSMTPSession(setupMockedSMTPConfiguration(),mailAddress,false);
-    
-        handler.setValidDomains(list);
-
-        int rCode = handler.doRcpt(session, null, mailAddress).getResult();
-        
-        assertEquals("Not rejected",rCode,HookReturnCode.DECLINED);
-    }
-    
-    public void testNotRejectValidUserRegex() throws Exception {
-        String domain = "domain";
-        String recipient = "recip@" + domain;
-        ArrayList<String> list = new ArrayList<String>();
-        list.add("reci.*");
-        MailAddress mailAddress = new MailAddress(recipient);
-        SMTPSession session = 
setupMockedSMTPSession(setupMockedSMTPConfiguration(),mailAddress,false);
-    
-        handler.setValidRegex(list);
-
-        int rCode = handler.doRcpt(session, null, mailAddress).getResult();
-        
-        assertEquals("Not rejected",rCode,HookReturnCode.DECLINED);
-    }
-    
-    public void testInvalidRegex() throws Exception{
-        boolean exception = false;
-        
-        ArrayList<String> list = new ArrayList<String>();
-        list.add("(.*");
-        try {
-            handler.setValidRegex(list);
-        } catch (MalformedPatternException e) {
-            exception = true;
-        }
-
-        assertTrue("Invalid Config",exception);
-    }
-    
+  
     public void testHasAddressMapping() throws Exception {
         MailAddress mailAddress = new MailAddress(USER1 + "@localhost");
         SMTPSession session = 
setupMockedSMTPSession(setupMockedSMTPConfiguration(),mailAddress,false);



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to