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 no...@apache.org on 2012/01/11 16:02:28 UTC

svn commit: r1230080 - in /james/protocols/trunk/pop3/src: main/java/org/apache/james/protocols/pop3/ main/java/org/apache/james/protocols/pop3/core/ main/java/org/apache/james/protocols/pop3/mailbox/ test/java/org/apache/james/protocols/pop3/

Author: norman
Date: Wed Jan 11 15:02:27 2012
New Revision: 1230080

URL: http://svn.apache.org/viewvc?rev=1230080&view=rev
Log:
Remove MailboxFactory and make PassCmdHandler abstract. See PROTOCOLS-86

Added:
    james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3StartTlsResponse.java
    james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java
Removed:
    james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/core/PassCmdHandler.java
    james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/mailbox/MailboxFactory.java
Modified:
    james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3ProtocolHandlerChain.java
    james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/mailbox/AbstractMailbox.java
    james/protocols/trunk/pop3/src/test/java/org/apache/james/protocols/pop3/POP3ServerTest.java

Modified: james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3ProtocolHandlerChain.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3ProtocolHandlerChain.java?rev=1230080&r1=1230079&r2=1230080&view=diff
==============================================================================
--- james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3ProtocolHandlerChain.java (original)
+++ james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3ProtocolHandlerChain.java Wed Jan 11 15:02:27 2012
@@ -30,7 +30,7 @@ import org.apache.james.protocols.pop3.c
 import org.apache.james.protocols.pop3.core.DeleCmdHandler;
 import org.apache.james.protocols.pop3.core.ListCmdHandler;
 import org.apache.james.protocols.pop3.core.NoopCmdHandler;
-import org.apache.james.protocols.pop3.core.PassCmdHandler;
+import org.apache.james.protocols.pop3.core.AbstractPassCmdHandler;
 import org.apache.james.protocols.pop3.core.QuitCmdHandler;
 import org.apache.james.protocols.pop3.core.RetrCmdHandler;
 import org.apache.james.protocols.pop3.core.RsetCmdHandler;
@@ -41,7 +41,6 @@ import org.apache.james.protocols.pop3.c
 import org.apache.james.protocols.pop3.core.UnknownCmdHandler;
 import org.apache.james.protocols.pop3.core.UserCmdHandler;
 import org.apache.james.protocols.pop3.core.WelcomeMessageHandler;
-import org.apache.james.protocols.pop3.mailbox.MailboxFactory;
 
 /**
  * {@link ProtocolHandlerChainImpl} which allows to add the default handlers which are needed to server POP3.
@@ -55,24 +54,24 @@ public class POP3ProtocolHandlerChain ex
     }
 
     /**
-     * The {@link MailboxFactory} to use. If a <code>not null</code> {@link MailboxFactory} is given, the {@link POP3ProtocolHandlerChain}
+     * The {@link AbstractPassCmdHandler} to use. If a <code>not null</code> {@link AbstractPassCmdHandler} is given, the {@link POP3ProtocolHandlerChain}
      * will add all default handlers
      * 
-     * @param mailboxFactory
+     * @param passHandler
      * @throws WiringException 
      */
-    public POP3ProtocolHandlerChain(MailboxFactory mailboxFactory) throws WiringException {
-        if (mailboxFactory != null) {
-            addAll(initDefaultHandlers(mailboxFactory));      
+    public POP3ProtocolHandlerChain(AbstractPassCmdHandler passHandler) throws WiringException {
+        if (passHandler != null) {
+            addAll(initDefaultHandlers(passHandler));      
             wireExtensibleHandlers();
         }
     }
     
-    protected List<ProtocolHandler> initDefaultHandlers(MailboxFactory mailboxFactory) {
+    protected List<ProtocolHandler> initDefaultHandlers(AbstractPassCmdHandler passHandler) {
         List<ProtocolHandler> handlers = new ArrayList<ProtocolHandler>();
         handlers.add(new CapaCmdHandler());
         handlers.add(new UserCmdHandler());
-        handlers.add(new PassCmdHandler(mailboxFactory));
+        handlers.add(passHandler);
         handlers.add(new ListCmdHandler());
         handlers.add(new UidlCmdHandler());
         handlers.add(new RsetCmdHandler());

Added: james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3StartTlsResponse.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3StartTlsResponse.java?rev=1230080&view=auto
==============================================================================
--- james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3StartTlsResponse.java (added)
+++ james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/POP3StartTlsResponse.java Wed Jan 11 15:02:27 2012
@@ -0,0 +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.protocols.pop3;
+
+import org.apache.james.protocols.api.StartTlsResponse;
+
+/**
+ * Special sub-type of {@link POP3Response} which will trigger the start of TLS after the response was written to the client
+ * 
+ *
+ */
+public class POP3StartTlsResponse extends POP3Response implements StartTlsResponse{
+
+    public POP3StartTlsResponse(String code, CharSequence description) {
+        super(code, description);
+    }
+
+    public POP3StartTlsResponse(String code) {
+        super(code);
+    }
+
+}

Added: james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java?rev=1230080&view=auto
==============================================================================
--- james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java (added)
+++ james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/core/AbstractPassCmdHandler.java Wed Jan 11 15:02:27 2012
@@ -0,0 +1,92 @@
+/****************************************************************
+ * 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.protocols.pop3.core;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+
+import org.apache.james.protocols.api.Request;
+import org.apache.james.protocols.api.Response;
+import org.apache.james.protocols.pop3.POP3Response;
+import org.apache.james.protocols.pop3.POP3Session;
+import org.apache.james.protocols.pop3.mailbox.Mailbox;
+
+/**
+ * Handles PASS commands.
+ */
+public abstract class AbstractPassCmdHandler extends RsetCmdHandler {
+    private static final Collection<String> COMMANDS = Collections.unmodifiableCollection(Arrays.asList("PASS"));
+    private static final Response UNEXPECTED_ERROR = new POP3Response(POP3Response.ERR_RESPONSE, "Unexpected error accessing mailbox").immutable();
+    private static final Response AUTH_FAILED = new POP3Response(POP3Response.ERR_RESPONSE, "Authentication failed.").immutable();
+
+    /**
+     * Handler method called upon receipt of a PASS command. Reads in and
+     * validates the password.
+     */
+    public Response onCommand(POP3Session session, Request request) {
+        String parameters = request.getArgument();
+        POP3Response response = null;
+        if (session.getHandlerState() == POP3Session.AUTHENTICATION_USERSET && parameters != null) {
+            String passArg = parameters;
+            try {
+                Mailbox mailbox = auth(session, passArg);
+                if (mailbox != null) {
+                    session.setUserMailbox(mailbox);
+                    stat(session);
+
+                    StringBuilder responseBuffer = new StringBuilder(64).append("Welcome ").append(session.getUser());
+                    response = new POP3Response(POP3Response.OK_RESPONSE, responseBuffer.toString());
+                    session.setHandlerState(POP3Session.TRANSACTION);
+                } else {
+                    session.setHandlerState(POP3Session.AUTHENTICATION_READY);
+                    return AUTH_FAILED;
+                }
+            } catch (Exception e) {
+                session.getLogger().error("Unexpected error accessing mailbox for " + session.getUser(), e);
+                session.setHandlerState(POP3Session.AUTHENTICATION_READY);
+                return UNEXPECTED_ERROR;
+            }
+        } else {
+            session.setHandlerState(POP3Session.AUTHENTICATION_READY);
+            return AUTH_FAILED;
+        }
+
+        return response;
+    }
+
+    /**
+     * @see org.apache.james.protocols.api.handler.CommandHandler#getImplCommands()
+     */
+    public Collection<String> getImplCommands() {
+        return COMMANDS;
+    }
+
+    /**
+     * Authenticate a {@link POP3Session} and returns the {@link Mailbox} for it. If it can not get authenticated it will return <code>null</code>.
+     * 
+     * @param session
+     * @param password
+     * @return mailbox
+     * 
+     */
+    protected abstract Mailbox auth(POP3Session session, String password) throws Exception;
+}

Modified: james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/mailbox/AbstractMailbox.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/mailbox/AbstractMailbox.java?rev=1230080&r1=1230079&r2=1230080&view=diff
==============================================================================
--- james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/mailbox/AbstractMailbox.java (original)
+++ james/protocols/trunk/pop3/src/main/java/org/apache/james/protocols/pop3/mailbox/AbstractMailbox.java Wed Jan 11 15:02:27 2012
@@ -21,8 +21,14 @@ package org.apache.james.protocols.pop3.
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.SequenceInputStream;
 
+import org.apache.james.protocols.api.CombinedInputStream;
+
+/**
+ * A {@link Mailbox} implementation which use a {@link CombinedInputStream} over {@link #getMessageHeaders(long)}  and {@link #getMessageBody(long)} to return the full message.
+ * 
+ *
+ */
 public abstract class AbstractMailbox implements Mailbox {
 
     /*
@@ -31,13 +37,12 @@ public abstract class AbstractMailbox im
      * @see org.apache.james.protocols.pop3.mailbox.Mailbox#getMessage(long)
      */
     public InputStream getMessage(long uid) throws IOException {
-        return new SequenceInputStream(getMessageHeaders(uid), getMessageBody(uid));
+        return new CombinedInputStream(getMessageHeaders(uid), getMessageBody(uid));
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.apache.james.protocols.pop3.mailbox.Mailbox#close()
+
+    /**
+     * Does nothing
      */
     public void close() throws IOException {
         // do nothing

Modified: james/protocols/trunk/pop3/src/test/java/org/apache/james/protocols/pop3/POP3ServerTest.java
URL: http://svn.apache.org/viewvc/james/protocols/trunk/pop3/src/test/java/org/apache/james/protocols/pop3/POP3ServerTest.java?rev=1230080&r1=1230079&r2=1230080&view=diff
==============================================================================
--- james/protocols/trunk/pop3/src/test/java/org/apache/james/protocols/pop3/POP3ServerTest.java (original)
+++ james/protocols/trunk/pop3/src/test/java/org/apache/james/protocols/pop3/POP3ServerTest.java Wed Jan 11 15:02:27 2012
@@ -41,8 +41,8 @@ import org.apache.commons.net.pop3.POP3C
 import org.apache.commons.net.pop3.POP3MessageInfo;
 import org.apache.james.protocols.api.handler.WiringException;
 import org.apache.james.protocols.netty.NettyServer;
+import org.apache.james.protocols.pop3.core.AbstractPassCmdHandler;
 import org.apache.james.protocols.pop3.mailbox.Mailbox;
-import org.apache.james.protocols.pop3.mailbox.MailboxFactory;
 import org.apache.james.protocols.pop3.mailbox.MessageMetaData;
 
 import org.junit.Test;
@@ -52,8 +52,8 @@ public class POP3ServerTest {
     private static final Message MESSAGE1 = new Message("Subject: test\r\nX-Header: value\r\n", "My Body\r\n");
     private static final Message MESSAGE2 = new Message("Subject: test2\r\nX-Header: value2\r\n", "My Body with a DOT.\r\n.\r\n");
 
-    private POP3Protocol createProtocol(MailboxFactory factory) throws WiringException {
-        return new POP3Protocol(new POP3ProtocolHandlerChain(factory), new POP3Configuration(), new MockLogger());
+    private POP3Protocol createProtocol(AbstractPassCmdHandler handler) throws WiringException {
+        return new POP3Protocol(new POP3ProtocolHandlerChain(handler), new POP3Configuration(), new MockLogger());
     }
     @Test
     public void testInvalidAuth() throws Exception {
@@ -61,7 +61,7 @@ public class POP3ServerTest {
         
         NettyServer server = null;
         try {
-            server = new NettyServer(createProtocol(new MockMailboxFactory()));
+            server = new NettyServer(createProtocol(new TestPassCmdHandler()));
             server.setListenAddresses(address);
             server.bind();
             
@@ -87,10 +87,10 @@ public class POP3ServerTest {
         NettyServer server = null;
         try {
             String identifier = "id";
-            MockMailboxFactory factory = new MockMailboxFactory();
+            TestPassCmdHandler handler = new TestPassCmdHandler();
             
-            factory.add("valid", new MockMailbox(identifier));
-            server = new NettyServer(createProtocol(factory));
+            handler.add("valid", new MockMailbox(identifier));
+            server = new NettyServer(createProtocol(handler));
             server.setListenAddresses(address);
             server.bind();
             
@@ -120,10 +120,10 @@ public class POP3ServerTest {
         NettyServer server = null;
         try {
             String identifier = "id";
-            MockMailboxFactory factory = new MockMailboxFactory();
+            TestPassCmdHandler handler = new TestPassCmdHandler();
             
-            factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2));
-            server = new NettyServer(createProtocol(factory));
+            handler.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2));
+            server = new NettyServer(createProtocol(handler));
             server.setListenAddresses(address);
             server.bind();
             
@@ -178,7 +178,7 @@ public class POP3ServerTest {
         NettyServer server = null;
         try {
             String identifier = "id";
-            MockMailboxFactory factory = new MockMailboxFactory();
+            TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2));
             server = new NettyServer(createProtocol(factory));
@@ -222,7 +222,7 @@ public class POP3ServerTest {
         NettyServer server = null;
         try {
             String identifier = "id";
-            MockMailboxFactory factory = new MockMailboxFactory();
+            TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2));
             server = new NettyServer(createProtocol(factory));
@@ -269,7 +269,7 @@ public class POP3ServerTest {
         NettyServer server = null;
         try {
             String identifier = "id";
-            MockMailboxFactory factory = new MockMailboxFactory();
+            TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2));
             server = new NettyServer(createProtocol(factory));
@@ -323,7 +323,7 @@ public class POP3ServerTest {
         NettyServer server = null;
         try {
             String identifier = "id";
-            MockMailboxFactory factory = new MockMailboxFactory();
+            TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier));
             server = new NettyServer(createProtocol(factory));
@@ -352,7 +352,7 @@ public class POP3ServerTest {
         NettyServer server = null;
         try {
             String identifier = "id";
-            MockMailboxFactory factory = new MockMailboxFactory();
+            TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1));
             server = new NettyServer(createProtocol(factory));
@@ -388,7 +388,7 @@ public class POP3ServerTest {
         NettyServer server = null;
         try {
             String identifier = "id";
-            MockMailboxFactory factory = new MockMailboxFactory();
+            TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2));
             server = new NettyServer(createProtocol(factory));
@@ -419,7 +419,7 @@ public class POP3ServerTest {
         NettyServer server = null;
         try {
             String identifier = "id";
-            MockMailboxFactory factory = new MockMailboxFactory();
+            TestPassCmdHandler factory = new TestPassCmdHandler();
             
             factory.add("valid", new MockMailbox(identifier, MESSAGE1, MESSAGE2));
             server = new NettyServer(createProtocol(factory));
@@ -497,15 +497,18 @@ public class POP3ServerTest {
         
     }
     
-    private final class MockMailboxFactory implements MailboxFactory {
+    private final class TestPassCmdHandler extends AbstractPassCmdHandler {
         private final Map<String, Mailbox> mailboxes = new HashMap<String, Mailbox>();
        
         public void add(String username, Mailbox mailbox) {
             mailboxes.put(username, mailbox);
         }
-        public Mailbox getMailbox(POP3Session session, String password) throws IOException {
+
+        @Override
+        protected Mailbox auth(POP3Session session, String password) {
             return mailboxes.get(session.getUser());
         }
+
         
     }
     



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