Repository: james-project Updated Branches: refs/heads/master ff3c9517f -> a4d8c8e00
JAMES-1925 Rename FirstUserConnectionFilter to UserProvisioningFilter Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/dea8f2eb Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/dea8f2eb Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/dea8f2eb Branch: refs/heads/master Commit: dea8f2eb1498612a1ad171e00a43f43edfb38f37 Parents: 4473335 Author: Antoine Duprat <[email protected]> Authored: Tue Feb 7 10:30:45 2017 +0100 Committer: Antoine Duprat <[email protected]> Committed: Thu Feb 9 09:44:21 2017 +0100 ---------------------------------------------------------------------- .../james/jmap/FirstUserConnectionFilter.java | 127 --------- .../java/org/apache/james/jmap/JMAPServer.java | 4 +- .../james/jmap/UserProvisioningFilter.java | 127 +++++++++ .../jmap/FirstUserConnectionFilterTest.java | 59 ---- .../FirstUserConnectionFilterThreadTest.java | 271 ------------------- .../james/jmap/UserProvisioningFilterTest.java | 59 ++++ .../jmap/UserProvisioningFilterThreadTest.java | 271 +++++++++++++++++++ 7 files changed, 459 insertions(+), 459 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/dea8f2eb/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 deleted file mode 100644 index 8de6195..0000000 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/FirstUserConnectionFilter.java +++ /dev/null @@ -1,127 +0,0 @@ -/**************************************************************** - * 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.io.IOException; -import java.util.Optional; -import java.util.UUID; -import java.util.function.Function; - -import javax.inject.Inject; -import javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.FilterConfig; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; - -import org.apache.james.mailbox.MailboxManager; -import org.apache.james.mailbox.MailboxSession; -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; -import org.slf4j.LoggerFactory; - -import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Throwables; -import com.google.common.collect.ImmutableList; - -public class FirstUserConnectionFilter implements Filter { - - private static final ImmutableList<String> DEFAULT_MAILBOXES = ImmutableList.of("INBOX", "Outbox", "Sent", "Trash"); - private static final Logger LOGGER = LoggerFactory.getLogger(FirstUserConnectionFilter.class); - private final UsersRepository usersRepository; - private final MailboxManager mailboxManager; - - @Inject - @VisibleForTesting FirstUserConnectionFilter(UsersRepository usersRepository, MailboxManager mailboxManager) { - this.usersRepository = usersRepository; - this.mailboxManager = mailboxManager; - } - - @Override - public void init(FilterConfig filterConfig) throws ServletException { - } - - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { - Optional<MailboxSession> session = Optional.ofNullable((MailboxSession)request.getAttribute(AuthenticationFilter.MAILBOX_SESSION)); - session.ifPresent(this::createAccountIfNeeded); - chain.doFilter(request, response); - } - - @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); - } - } - - private boolean needsAccountCreation(User user) throws UsersRepositoryException { - return !usersRepository.contains(user.getUserName()); - } - - private void createAccount(User user) throws UsersRepositoryException, BadCredentialsException, MailboxException { - createUser(user); - createDefaultMailboxes(user); - } - - private void createUser(User user) throws UsersRepositoryException { - usersRepository.addUser(user.getUserName(), generatePassword()); - } - - private String generatePassword() { - return UUID.randomUUID().toString(); - } - - private void createDefaultMailboxes(User user) throws BadCredentialsException, MailboxException { - MailboxSession session = mailboxManager.createSystemSession(user.getUserName(), LOGGER); - DEFAULT_MAILBOXES.stream() - .map(toMailboxPath(session)) - .forEach(mailboxPath -> createMailbox(mailboxPath, session)); - } - - private Function<String, MailboxPath> toMailboxPath(MailboxSession session) { - return mailbox -> new MailboxPath(session.getPersonalSpace(), session.getUser().getUserName(), mailbox); - } - - private void createMailbox(MailboxPath mailboxPath, MailboxSession session) { - try { - mailboxManager.createMailbox(mailboxPath, session); - } catch (MailboxException e) { - throw Throwables.propagate(e); - } - } - - @Override - public void destroy() { - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/dea8f2eb/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServer.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServer.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServer.java index e1c3ba5..25242f4 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServer.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPServer.java @@ -40,7 +40,7 @@ public class JMAPServer implements Configurable { @Inject private JMAPServer(JMAPConfiguration jmapConfiguration, AuthenticationServlet authenticationServlet, JMAPServlet jmapServlet, DownloadServlet downloadServlet, UploadServlet uploadServlet, - AuthenticationFilter authenticationFilter, FirstUserConnectionFilter firstUserConnectionFilter) { + AuthenticationFilter authenticationFilter, UserProvisioningFilter userProvisioningFilter) { server = JettyHttpServer.create( configurationBuilderFor(jmapConfiguration) @@ -53,7 +53,7 @@ public class JMAPServer implements Configurable { .with(jmapServlet) .filter(JMAPUrls.JMAP) .with(new AllowAllCrossOriginRequests(bypass(authenticationFilter).on("OPTIONS").only())) - .and(firstUserConnectionFilter) + .and(userProvisioningFilter) .only() .serveAsOneLevelTemplate(JMAPUrls.DOWNLOAD) .with(downloadServlet) http://git-wip-us.apache.org/repos/asf/james-project/blob/dea8f2eb/server/protocols/jmap/src/main/java/org/apache/james/jmap/UserProvisioningFilter.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/UserProvisioningFilter.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/UserProvisioningFilter.java new file mode 100644 index 0000000..6145a79 --- /dev/null +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/UserProvisioningFilter.java @@ -0,0 +1,127 @@ +/**************************************************************** + * 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.io.IOException; +import java.util.Optional; +import java.util.UUID; +import java.util.function.Function; + +import javax.inject.Inject; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; + +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxSession; +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; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Throwables; +import com.google.common.collect.ImmutableList; + +public class UserProvisioningFilter implements Filter { + + private static final ImmutableList<String> DEFAULT_MAILBOXES = ImmutableList.of("INBOX", "Outbox", "Sent", "Trash"); + private static final Logger LOGGER = LoggerFactory.getLogger(UserProvisioningFilter.class); + private final UsersRepository usersRepository; + private final MailboxManager mailboxManager; + + @Inject + @VisibleForTesting UserProvisioningFilter(UsersRepository usersRepository, MailboxManager mailboxManager) { + this.usersRepository = usersRepository; + this.mailboxManager = mailboxManager; + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + Optional<MailboxSession> session = Optional.ofNullable((MailboxSession)request.getAttribute(AuthenticationFilter.MAILBOX_SESSION)); + session.ifPresent(this::createAccountIfNeeded); + chain.doFilter(request, response); + } + + @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); + } + } + + private boolean needsAccountCreation(User user) throws UsersRepositoryException { + return !usersRepository.contains(user.getUserName()); + } + + private void createAccount(User user) throws UsersRepositoryException, BadCredentialsException, MailboxException { + createUser(user); + createDefaultMailboxes(user); + } + + private void createUser(User user) throws UsersRepositoryException { + usersRepository.addUser(user.getUserName(), generatePassword()); + } + + private String generatePassword() { + return UUID.randomUUID().toString(); + } + + private void createDefaultMailboxes(User user) throws BadCredentialsException, MailboxException { + MailboxSession session = mailboxManager.createSystemSession(user.getUserName(), LOGGER); + DEFAULT_MAILBOXES.stream() + .map(toMailboxPath(session)) + .forEach(mailboxPath -> createMailbox(mailboxPath, session)); + } + + private Function<String, MailboxPath> toMailboxPath(MailboxSession session) { + return mailbox -> new MailboxPath(session.getPersonalSpace(), session.getUser().getUserName(), mailbox); + } + + private void createMailbox(MailboxPath mailboxPath, MailboxSession session) { + try { + mailboxManager.createMailbox(mailboxPath, session); + } catch (MailboxException e) { + throw Throwables.propagate(e); + } + } + + @Override + public void destroy() { + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/dea8f2eb/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 deleted file mode 100644 index 540ca0a..0000000 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/**************************************************************** - * 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 static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; - -import java.io.IOException; - -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.apache.james.mailbox.MailboxManager; -import org.apache.james.user.api.UsersRepositoryException; -import org.apache.james.user.lib.mock.InMemoryUsersRepository; -import org.junit.Before; -import org.junit.Test; - -public class FirstUserConnectionFilterTest { - - private FirstUserConnectionFilter sut; - private InMemoryUsersRepository usersRepository; - - @Before - public void setup() { - usersRepository = new InMemoryUsersRepository(); - MailboxManager mailboxManager = mock(MailboxManager.class); - sut = new FirstUserConnectionFilter(usersRepository, mailboxManager); - } - - @Test - public void filterShouldDoNothingOnNullSession() throws IOException, ServletException, UsersRepositoryException { - HttpServletRequest request = mock(HttpServletRequest.class); - HttpServletResponse response = mock(HttpServletResponse.class); - FilterChain chain = mock(FilterChain.class); - sut.doFilter(request, response, chain); - verify(chain).doFilter(request, response); - assertThat(usersRepository.list()).isEmpty(); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/dea8f2eb/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 deleted file mode 100644 index b2fe1d5..0000000 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/FirstUserConnectionFilterThreadTest.java +++ /dev/null @@ -1,271 +0,0 @@ -/**************************************************************** - * 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.EnumSet; -import java.util.List; -import java.util.Set; - -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.mock.MockMailboxSession; -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.MailboxAnnotation; -import org.apache.james.mailbox.model.MailboxAnnotationKey; -import org.apache.james.mailbox.model.MailboxId; -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.MessageId; -import org.apache.james.mailbox.model.MessageRange; -import org.apache.james.mailbox.model.MultimailboxesSearchQuery; -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 MockMailboxSession("username"); - 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 EnumSet<SearchCapabilities> getSupportedSearchCapabilities() { - return EnumSet.noneOf(SearchCapabilities.class); - } - - @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 MessageManager getMailbox(MailboxId mailboxId, 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> copyMessages(MessageRange set, MailboxId from, MailboxId 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 EnumSet<MailboxCapabilities> getSupportedMailboxCapabilities() { - return null; - } - - @Override - public EnumSet<MessageCapabilities> getSupportedMessageCapabilities() { - return null; - } - - @Override - public List<MailboxAnnotation> getAllAnnotations(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { - return null; - } - - @Override - public List<MailboxAnnotation> getAnnotationsByKeys(MailboxPath mailboxPath, MailboxSession session, Set<MailboxAnnotationKey> keys) throws MailboxException { - return null; - } - - @Override - public void updateAnnotations(MailboxPath mailboxPath, MailboxSession session, List<MailboxAnnotation> mailboxAnnotations) throws MailboxException { - - } - - @Override - public boolean hasCapability(MailboxCapabilities capability) { - return false; - } - - @Override - public List<MessageId> search(MultimailboxesSearchQuery expression, MailboxSession session, long limit) throws MailboxException { - return null; - } - - @Override - public List<MailboxAnnotation> getAnnotationsByKeysWithOneDepth(MailboxPath mailboxPath, MailboxSession session, - Set<MailboxAnnotationKey> keys) throws MailboxException { - return null; - } - - @Override - public List<MailboxAnnotation> getAnnotationsByKeysWithAllDepth(MailboxPath mailboxPath, MailboxSession session, - Set<MailboxAnnotationKey> keys) throws MailboxException { - return null; - } - - @Override - public boolean hasChildren(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { - return false; - } - } -} - http://git-wip-us.apache.org/repos/asf/james-project/blob/dea8f2eb/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserProvisioningFilterTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserProvisioningFilterTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserProvisioningFilterTest.java new file mode 100644 index 0000000..ffc37b0 --- /dev/null +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserProvisioningFilterTest.java @@ -0,0 +1,59 @@ +/**************************************************************** + * 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 static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.user.api.UsersRepositoryException; +import org.apache.james.user.lib.mock.InMemoryUsersRepository; +import org.junit.Before; +import org.junit.Test; + +public class UserProvisioningFilterTest { + + private UserProvisioningFilter sut; + private InMemoryUsersRepository usersRepository; + + @Before + public void setup() { + usersRepository = new InMemoryUsersRepository(); + MailboxManager mailboxManager = mock(MailboxManager.class); + sut = new UserProvisioningFilter(usersRepository, mailboxManager); + } + + @Test + public void filterShouldDoNothingOnNullSession() throws IOException, ServletException, UsersRepositoryException { + HttpServletRequest request = mock(HttpServletRequest.class); + HttpServletResponse response = mock(HttpServletResponse.class); + FilterChain chain = mock(FilterChain.class); + sut.doFilter(request, response, chain); + verify(chain).doFilter(request, response); + assertThat(usersRepository.list()).isEmpty(); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/dea8f2eb/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserProvisioningFilterThreadTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserProvisioningFilterThreadTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserProvisioningFilterThreadTest.java new file mode 100644 index 0000000..eb5ec5c --- /dev/null +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/UserProvisioningFilterThreadTest.java @@ -0,0 +1,271 @@ +/**************************************************************** + * 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.EnumSet; +import java.util.List; +import java.util.Set; + +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.mock.MockMailboxSession; +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.MailboxAnnotation; +import org.apache.james.mailbox.model.MailboxAnnotationKey; +import org.apache.james.mailbox.model.MailboxId; +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.MessageId; +import org.apache.james.mailbox.model.MessageRange; +import org.apache.james.mailbox.model.MultimailboxesSearchQuery; +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 UserProvisioningFilterThreadTest { + + private UserProvisioningFilter sut; + private InMemoryUsersRepository usersRepository; + private MailboxSession session; + private MailboxManager mailboxManager; + + @ThreadedBefore + public void before() { + usersRepository = new InMemoryUsersRepository(); + session = new MockMailboxSession("username"); + mailboxManager = new FakeMailboxManager(session) ; + sut = new UserProvisioningFilter(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(), UserProvisioningFilter.class); + } + + private static class FakeMailboxManager implements MailboxManager { + private MailboxSession mailboxSession; + + public FakeMailboxManager(MailboxSession mailboxSession) { + this.mailboxSession = mailboxSession; + } + + @Override + public EnumSet<SearchCapabilities> getSupportedSearchCapabilities() { + return EnumSet.noneOf(SearchCapabilities.class); + } + + @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 MessageManager getMailbox(MailboxId mailboxId, 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> copyMessages(MessageRange set, MailboxId from, MailboxId 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 EnumSet<MailboxCapabilities> getSupportedMailboxCapabilities() { + return null; + } + + @Override + public EnumSet<MessageCapabilities> getSupportedMessageCapabilities() { + return null; + } + + @Override + public List<MailboxAnnotation> getAllAnnotations(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { + return null; + } + + @Override + public List<MailboxAnnotation> getAnnotationsByKeys(MailboxPath mailboxPath, MailboxSession session, Set<MailboxAnnotationKey> keys) throws MailboxException { + return null; + } + + @Override + public void updateAnnotations(MailboxPath mailboxPath, MailboxSession session, List<MailboxAnnotation> mailboxAnnotations) throws MailboxException { + + } + + @Override + public boolean hasCapability(MailboxCapabilities capability) { + return false; + } + + @Override + public List<MessageId> search(MultimailboxesSearchQuery expression, MailboxSession session, long limit) throws MailboxException { + return null; + } + + @Override + public List<MailboxAnnotation> getAnnotationsByKeysWithOneDepth(MailboxPath mailboxPath, MailboxSession session, + Set<MailboxAnnotationKey> keys) throws MailboxException { + return null; + } + + @Override + public List<MailboxAnnotation> getAnnotationsByKeysWithAllDepth(MailboxPath mailboxPath, MailboxSession session, + Set<MailboxAnnotationKey> keys) throws MailboxException { + return null; + } + + @Override + public boolean hasChildren(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { + return false; + } + } +} + --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
