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 rd...@apache.org on 2009/05/13 17:07:20 UTC

svn commit: r774392 - in /james/imap/trunk: mailbox/src/main/java/org/apache/james/imap/mailbox/ processor/src/main/java/org/apache/james/imap/processor/ store/src/main/java/org/apache/james/imap/store/ torque/src/main/java/org/apache/james/mailboxmana...

Author: rdonkin
Date: Wed May 13 15:07:20 2009
New Revision: 774392

URL: http://svn.apache.org/viewvc?rev=774392&view=rev
Log:
Improved API naming and changed semantics so that a session is returned and exceptions thrown when authentication fails. IMAP-84 https://issues.apache.org/jira/browse/IMAP-84

Added:
    james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/BadCredentialsException.java   (with props)
Modified:
    james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/MailboxManager.java
    james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/LoginProcessor.java
    james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java
    james/imap/trunk/torque/src/main/java/org/apache/james/mailboxmanager/torque/TorqueMailboxManager.java

Added: james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/BadCredentialsException.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/BadCredentialsException.java?rev=774392&view=auto
==============================================================================
--- james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/BadCredentialsException.java (added)
+++ james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/BadCredentialsException.java Wed May 13 15:07:20 2009
@@ -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.imap.mailbox;
+
+import org.apache.james.imap.api.display.HumanReadableTextKey;
+
+/**
+ * Indicates that the credentials for this operation were not acceptable.
+ */
+public class BadCredentialsException extends MailboxException {
+
+    private static final long serialVersionUID = -8055692887730696513L;
+
+    public BadCredentialsException() {
+        super(HumanReadableTextKey.INVALID_LOGIN);
+    }
+    
+    public BadCredentialsException(HumanReadableTextKey key) {
+        super(key);
+    }
+
+}

Propchange: james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/BadCredentialsException.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/MailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/MailboxManager.java?rev=774392&r1=774391&r2=774392&view=diff
==============================================================================
--- james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/MailboxManager.java (original)
+++ james/imap/trunk/mailbox/src/main/java/org/apache/james/imap/mailbox/MailboxManager.java Wed May 13 15:07:20 2009
@@ -90,7 +90,7 @@
      * @param mailboxName
      *            name, not null
      * @param mailboxSession the context for this call, not null
-     * @throws MailboxException
+     * @throws MailboxException when creation fails
      */
     void createMailbox(String mailboxName, MailboxSession mailboxSession) throws MailboxException;
 
@@ -104,7 +104,7 @@
      * @param to
      *            new name for the mailbox
      * @param session the context for this call, not nul
-     * @throws MailboxException
+     * @throws MailboxException otherwise
      * @throws MailboxExistsException
      *             when the <code>to</code> mailbox exists
      * @throws MailboxNotFound
@@ -146,23 +146,31 @@
     boolean mailboxExists(String mailboxName, MailboxSession session) throws MailboxException;
 
     /**
-     * Creates a new session.
+     * Creates a new system session.
+     * A system session is intended to be used for programmatic access.
+     * Use {@link #login(String, String, Log)} when accessing this API
+     * from a protocol.
      * @param userName the name of the user whose session is being created
      * @param log context sensitive log
      * @return <code>MailboxSession</code>, not null
+     * @throws BadCredentialsException when system access is not allowed for the given user
+     * @throws MailboxException when the creation fails for other reasons
      */
-    public MailboxSession createSession(String userName, Log log);
+    public MailboxSession createSystemSession(String userName, Log log) throws BadCredentialsException, MailboxException;
 
     /**
      * Autenticates the given user against the given password.
-     * 
+     * When authentic and authorized, a session will be supplied
      * @param userid
      *            user name
      * @param passwd
      *            password supplied
-     * @return true if the user is authenticated
+     * @param log context sensitive log    
+     * @return a <code>MailboxSession</code> when the user is authentic and authorized to access
+     * @throws BadCredentialsException when system access is denighed for the given user
+     * @throws MailboxException when the creation fails for other reasons
      */
-    boolean isAuthentic(String userid, String passwd);
+    MailboxSession login(String userid, String passwd, Log log) throws BadCredentialsException, MailboxException;
 
     /**
      * Subscribes the user to the given mailbox.

Modified: james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/LoginProcessor.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/LoginProcessor.java?rev=774392&r1=774391&r2=774392&view=diff
==============================================================================
--- james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/LoginProcessor.java (original)
+++ james/imap/trunk/processor/src/main/java/org/apache/james/imap/processor/LoginProcessor.java Wed May 13 15:07:20 2009
@@ -26,6 +26,7 @@
 import org.apache.james.imap.api.message.response.StatusResponseFactory;
 import org.apache.james.imap.api.process.ImapProcessor;
 import org.apache.james.imap.api.process.ImapSession;
+import org.apache.james.imap.mailbox.BadCredentialsException;
 import org.apache.james.imap.mailbox.MailboxException;
 import org.apache.james.imap.mailbox.MailboxExistsException;
 import org.apache.james.imap.mailbox.MailboxManager;
@@ -63,9 +64,9 @@
             final String userid = request.getUserid();
             final String passwd = request.getPassword();
             final MailboxManager mailboxManager = getMailboxManager();
-            if (mailboxManager.isAuthentic(userid, passwd)) {
+            try {
+                final MailboxSession mailboxSession = mailboxManager.login(userid, passwd, session.getLog());
                 session.authenticated();
-                final MailboxSession mailboxSession = mailboxManager.createSession(userid, session.getLog());
                 session.setAttribute(
                         ImapSessionUtils.MAILBOX_SESSION_ATTRIBUTE_SESSION_KEY,
                         mailboxSession);
@@ -81,7 +82,7 @@
                     }
                 }
                 okComplete(command, tag, responder);
-            } else {
+            } catch (BadCredentialsException e) {
                 final Integer currentNumberOfFailures = (Integer) session
                         .getAttribute(ATTRIBUTE_NUMBER_OF_FAILURES);
                 final int failures;
@@ -103,8 +104,7 @@
             }
         } catch (MailboxException e) {
             session.getLog().debug("Login failed", e);
-            final HumanReadableTextKey displayTextKey = HumanReadableTextKey.INVALID_LOGIN;
-            no(command, tag, responder, displayTextKey);
+            no(command, tag, responder, e.getKey());
         }
     }
 }

Modified: james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java?rev=774392&r1=774391&r2=774392&view=diff
==============================================================================
--- james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java (original)
+++ james/imap/trunk/store/src/main/java/org/apache/james/imap/store/StoreMailboxManager.java Wed May 13 15:07:20 2009
@@ -30,6 +30,7 @@
 import org.apache.commons.logging.Log;
 import org.apache.james.imap.api.AbstractLogEnabled;
 import org.apache.james.imap.api.display.HumanReadableTextKey;
+import org.apache.james.imap.mailbox.BadCredentialsException;
 import org.apache.james.imap.mailbox.MailboxException;
 import org.apache.james.imap.mailbox.MailboxExistsException;
 import org.apache.james.imap.mailbox.MailboxListener;
@@ -299,7 +300,12 @@
         mailboxes.clear();
     }
 
-    public MailboxSession createSession(String userName, Log log) {
+    public MailboxSession createSystemSession(String userName, Log log) {
+        return createSession(userName, log);
+    }
+
+
+    private SimpleMailboxSession createSession(String userName, Log log) {
         return new SimpleMailboxSession(random.nextLong(), userName, log, delimiter);
     }
 
@@ -312,7 +318,7 @@
         return result;
     }
 
-    public boolean isAuthentic(String userid, String passwd) {
+    public boolean login(String userid, String passwd) {
         return authenticator.isAuthentic(userid, passwd);
     }
 
@@ -334,4 +340,15 @@
         final StoreMailbox mailbox = doGetMailbox(mailboxName);
         mailbox.addListener(listener);
     }
+
+
+    public MailboxSession login(String userid, String passwd, Log log) throws BadCredentialsException, MailboxException {
+        if (login(userid, passwd)) {
+            return createSession(userid, log);
+        } else {
+            throw new BadCredentialsException();
+        }
+    }
+    
+    
 }

Modified: james/imap/trunk/torque/src/main/java/org/apache/james/mailboxmanager/torque/TorqueMailboxManager.java
URL: http://svn.apache.org/viewvc/james/imap/trunk/torque/src/main/java/org/apache/james/mailboxmanager/torque/TorqueMailboxManager.java?rev=774392&r1=774391&r2=774392&view=diff
==============================================================================
--- james/imap/trunk/torque/src/main/java/org/apache/james/mailboxmanager/torque/TorqueMailboxManager.java (original)
+++ james/imap/trunk/torque/src/main/java/org/apache/james/mailboxmanager/torque/TorqueMailboxManager.java Wed May 13 15:07:20 2009
@@ -32,6 +32,7 @@
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.logging.impl.SimpleLog;
 import org.apache.james.imap.api.display.HumanReadableTextKey;
+import org.apache.james.imap.mailbox.BadCredentialsException;
 import org.apache.james.imap.mailbox.Mailbox;
 import org.apache.james.imap.mailbox.MailboxException;
 import org.apache.james.imap.mailbox.MailboxExistsException;
@@ -350,7 +351,11 @@
         return log;
     }
 
-    public MailboxSession createSession(String userName, Log log) {
+    public MailboxSession createSystemSession(String userName, Log log) {
+        return createSession(userName, log);
+    }
+
+    private TorqueMailboxSession createSession(String userName, Log log) {
         return new TorqueMailboxSession(random.nextLong(), log, userName, delimiter);
     }
 
@@ -363,7 +368,7 @@
         return result;
     }
 
-    public boolean isAuthentic(String userid, String passwd) {
+    public boolean login(String userid, String passwd) {
         return userManager.isAuthentic(userid, passwd);
     }
 
@@ -386,4 +391,12 @@
         mailbox.addListener(listener);
     }
 
+    public MailboxSession login(String userid, String passwd, Log log) throws BadCredentialsException, MailboxException {
+        if (login(userid, passwd)) {
+            return createSession(userid, log);
+        } else {
+            throw new BadCredentialsException();
+        }
+    }
+
 }



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