Repository: james-project
Updated Branches:
  refs/heads/master fdc98fbcc -> 03d424e9f


JAMES-1700 Don't fail in account creation if user already exists


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/03d424e9
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/03d424e9
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/03d424e9

Branch: refs/heads/master
Commit: 03d424e9f01881764097ea5cfa0eeea25dfbb594
Parents: fdc98fb
Author: Raphael Ouazana <[email protected]>
Authored: Fri Mar 4 14:59:56 2016 +0100
Committer: Raphael Ouazana <[email protected]>
Committed: Tue Mar 8 15:11:43 2016 +0100

----------------------------------------------------------------------
 .../AlreadyExistInUsersRepositoryException.java |  31 +++
 .../james/user/lib/AbstractUsersRepository.java |   3 +-
 server/protocols/jmap/pom.xml                   |   6 +
 .../james/jmap/FirstUserConnectionFilter.java   |   6 +-
 .../jmap/FirstUserConnectionFilterTest.java     |   2 -
 .../FirstUserConnectionFilterThreadTest.java    | 202 +++++++++++++++++++
 6 files changed, 246 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/03d424e9/server/data/data-api/src/main/java/org/apache/james/user/api/AlreadyExistInUsersRepositoryException.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-api/src/main/java/org/apache/james/user/api/AlreadyExistInUsersRepositoryException.java
 
b/server/data/data-api/src/main/java/org/apache/james/user/api/AlreadyExistInUsersRepositoryException.java
new file mode 100644
index 0000000..b507f9e
--- /dev/null
+++ 
b/server/data/data-api/src/main/java/org/apache/james/user/api/AlreadyExistInUsersRepositoryException.java
@@ -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.user.api;
+
+public class AlreadyExistInUsersRepositoryException extends 
UsersRepositoryException {
+
+    public AlreadyExistInUsersRepositoryException(String msg, Throwable t) {
+        super(msg, t);
+    }
+
+    public AlreadyExistInUsersRepositoryException(String msg) {
+        super(msg);
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/03d424e9/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
----------------------------------------------------------------------
diff --git 
a/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
 
b/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
index c95b7f6..b3988ab 100644
--- 
a/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
+++ 
b/server/data/data-library/src/main/java/org/apache/james/user/lib/AbstractUsersRepository.java
@@ -27,6 +27,7 @@ import org.apache.james.domainlist.api.DomainList;
 import org.apache.james.domainlist.api.DomainListException;
 import org.apache.james.lifecycle.api.Configurable;
 import org.apache.james.lifecycle.api.LogEnabled;
+import org.apache.james.user.api.AlreadyExistInUsersRepositoryException;
 import org.apache.james.user.api.UsersRepository;
 import org.apache.james.user.api.UsersRepositoryException;
 import org.slf4j.Logger;
@@ -106,7 +107,7 @@ public abstract class AbstractUsersRepository implements 
UsersRepository, LogEna
             isValidUsername(username);
             doAddUser(username, password);
         } else {
-            throw new UsersRepositoryException("User with username " + 
username + " already exist!");
+            throw new AlreadyExistInUsersRepositoryException("User with 
username " + username + " already exists!");
         }
 
     }

http://git-wip-us.apache.org/repos/asf/james-project/blob/03d424e9/server/protocols/jmap/pom.xml
----------------------------------------------------------------------
diff --git a/server/protocols/jmap/pom.xml b/server/protocols/jmap/pom.xml
index 2f31da7..1745231 100644
--- a/server/protocols/jmap/pom.xml
+++ b/server/protocols/jmap/pom.xml
@@ -273,6 +273,12 @@
                     <scope>test</scope>
                 </dependency>
                 <dependency>
+                    <groupId>com.googlecode.thread-weaver</groupId>
+                    <artifactId>threadweaver</artifactId>
+                    <version>0.2</version>
+                    <scope>test</scope>
+                </dependency>
+                <dependency>
                     <groupId>junit</groupId>
                     <artifactId>junit</artifactId>
                     <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/james-project/blob/03d424e9/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java
 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java
index 9a44db5..f7354f7 100644
--- 
a/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java
+++ 
b/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java
@@ -37,6 +37,7 @@ import org.apache.james.mailbox.MailboxSession.User;
 import org.apache.james.mailbox.exception.BadCredentialsException;
 import org.apache.james.mailbox.exception.MailboxException;
 import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.user.api.AlreadyExistInUsersRepositoryException;
 import org.apache.james.user.api.UsersRepository;
 import org.apache.james.user.api.UsersRepositoryException;
 import org.slf4j.Logger;
@@ -70,12 +71,15 @@ public class FirstUserConnectionFilter implements Filter {
         chain.doFilter(request, response);
     }
     
-    private void createAccountIfNeeded(MailboxSession session) {
+    @VisibleForTesting
+    void createAccountIfNeeded(MailboxSession session) {
         try {
             User user = session.getUser();
             if (needsAccountCreation(user)) {
                 createAccount(user);
             }
+        } catch (AlreadyExistInUsersRepositoryException e) {
+            // Ignore
         } catch (UsersRepositoryException|MailboxException e) {
             throw Throwables.propagate(e);
         }

http://git-wip-us.apache.org/repos/asf/james-project/blob/03d424e9/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java
 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java
index 2da6c0c..540ca0a 100644
--- 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java
+++ 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java
@@ -56,6 +56,4 @@ public class FirstUserConnectionFilterTest {
         verify(chain).doFilter(request, response);
         assertThat(usersRepository.list()).isEmpty();
     }
-    
 }
-

http://git-wip-us.apache.org/repos/asf/james-project/blob/03d424e9/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java
----------------------------------------------------------------------
diff --git 
a/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java
 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java
new file mode 100644
index 0000000..3918c91
--- /dev/null
+++ 
b/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java
@@ -0,0 +1,202 @@
+/****************************************************************
+ * 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.jmap;
+
+import java.util.List;
+
+import org.apache.james.mailbox.MailboxListener;
+import org.apache.james.mailbox.MailboxManager;
+import org.apache.james.mailbox.MailboxSession;
+import org.apache.james.mailbox.MessageManager;
+import org.apache.james.mailbox.exception.BadCredentialsException;
+import org.apache.james.mailbox.exception.MailboxException;
+import org.apache.james.mailbox.model.MailboxACL.MailboxACLCommand;
+import org.apache.james.mailbox.model.MailboxACL.MailboxACLEntryKey;
+import org.apache.james.mailbox.model.MailboxACL.MailboxACLRight;
+import org.apache.james.mailbox.model.MailboxACL.MailboxACLRights;
+import org.apache.james.mailbox.model.MailboxMetaData;
+import org.apache.james.mailbox.model.MailboxPath;
+import org.apache.james.mailbox.model.MailboxQuery;
+import org.apache.james.mailbox.model.MessageRange;
+import org.apache.james.mailbox.store.SimpleMailboxSession;
+import org.apache.james.user.lib.mock.InMemoryUsersRepository;
+import org.junit.Test;
+import org.slf4j.Logger;
+
+import com.google.testing.threadtester.AnnotatedTestRunner;
+import com.google.testing.threadtester.ThreadedAfter;
+import com.google.testing.threadtester.ThreadedBefore;
+import com.google.testing.threadtester.ThreadedMain;
+import com.google.testing.threadtester.ThreadedSecondary;
+
+public class FirstUserConnectionFilterThreadTest {
+
+    private FirstUserConnectionFilter sut;
+    private InMemoryUsersRepository usersRepository;
+    private MailboxSession session;
+    private MailboxManager mailboxManager;
+
+    @ThreadedBefore
+    public void before() {
+        usersRepository = new InMemoryUsersRepository();
+        session = new SimpleMailboxSession(0, "username", null, null, null, 
':', null);
+        mailboxManager = new FakeMailboxManager(session) ;
+        sut = new FirstUserConnectionFilter(usersRepository, mailboxManager);
+    }
+    
+    @ThreadedMain
+    public void mainThread() {
+        sut.createAccountIfNeeded(session);
+    }
+    
+    @ThreadedSecondary
+    public void secondThread() {
+        sut.createAccountIfNeeded(session);
+    }
+    
+    @ThreadedAfter
+    public void after() {
+        // Exception is thrown if test fails
+    }
+    
+    @Test
+    public void testConcurrentAccessToFilterShouldNotThrow() {
+        AnnotatedTestRunner runner = new AnnotatedTestRunner();
+        runner.runTests(this.getClass(), FirstUserConnectionFilter.class);
+    }
+    
+    private static class FakeMailboxManager implements MailboxManager {
+        private MailboxSession mailboxSession;
+
+        public FakeMailboxManager(MailboxSession mailboxSession) {
+            this.mailboxSession = mailboxSession;
+        }
+
+        @Override
+        public void startProcessingRequest(MailboxSession session) {
+        }
+
+        @Override
+        public void endProcessingRequest(MailboxSession session) {
+        }
+
+        @Override
+        public void addListener(MailboxPath mailboxPath, MailboxListener 
listener, MailboxSession session) throws MailboxException {
+        }
+
+        @Override
+        public void removeListener(MailboxPath mailboxPath, MailboxListener 
listner, MailboxSession session) throws MailboxException {
+        }
+
+        @Override
+        public void addGlobalListener(MailboxListener listener, MailboxSession 
session) throws MailboxException {
+        }
+
+        @Override
+        public void removeGlobalListener(MailboxListener listner, 
MailboxSession session) throws MailboxException {
+        }
+
+        @Override
+        public char getDelimiter() {
+            return 0;
+        }
+
+        @Override
+        public MessageManager getMailbox(MailboxPath mailboxPath, 
MailboxSession session) throws MailboxException {
+            return null;
+        }
+
+        @Override
+        public void createMailbox(MailboxPath mailboxPath, MailboxSession 
mailboxSession) throws MailboxException {
+        }
+
+        @Override
+        public void deleteMailbox(MailboxPath mailboxPath, MailboxSession 
session) throws MailboxException {
+        }
+
+        @Override
+        public void renameMailbox(MailboxPath from, MailboxPath to, 
MailboxSession session) throws MailboxException {
+        }
+
+        @Override
+        public List<MessageRange> copyMessages(MessageRange set, MailboxPath 
from, MailboxPath to, MailboxSession session) throws MailboxException {
+            return null;
+        }
+
+        @Override
+        public List<MessageRange> moveMessages(MessageRange set, MailboxPath 
from, MailboxPath to, MailboxSession session) throws MailboxException {
+            return null;
+        }
+
+        @Override
+        public List<MailboxMetaData> search(MailboxQuery expression, 
MailboxSession session) throws MailboxException {
+            return null;
+        }
+
+        @Override
+        public boolean mailboxExists(MailboxPath mailboxPath, MailboxSession 
session) throws MailboxException {
+            return false;
+        }
+
+        @Override
+        public MailboxSession createSystemSession(String userName, Logger log) 
throws BadCredentialsException, MailboxException {
+            return mailboxSession;
+        }
+
+        @Override
+        public MailboxSession login(String userid, String passwd, Logger log) 
throws BadCredentialsException, MailboxException {
+            return null;
+        }
+
+        @Override
+        public void logout(MailboxSession session, boolean force) throws 
MailboxException {
+        }
+
+        @Override
+        public boolean hasRight(MailboxPath mailboxPath, MailboxACLRight 
right, MailboxSession session) throws MailboxException {
+            return false;
+        }
+
+        @Override
+        public MailboxACLRights myRights(MailboxPath mailboxPath, 
MailboxSession session) throws MailboxException {
+            return null;
+        }
+
+        @Override
+        public MailboxACLRights[] listRigths(MailboxPath mailboxPath, 
MailboxACLEntryKey identifier, MailboxSession session) throws MailboxException {
+            return null;
+        }
+
+        @Override
+        public void setRights(MailboxPath mailboxPath, MailboxACLCommand 
mailboxACLCommand, MailboxSession session) throws MailboxException {
+        }
+
+        @Override
+        public List<MailboxPath> list(MailboxSession session) throws 
MailboxException {
+            return null;
+        }
+
+        @Override
+        public List<Capabilities> getSupportedCapabilities() {
+            return null;
+        }
+        
+    }
+}
+


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

Reply via email to