MAILBOX-355 Move SystemMailboxesProvider to Mailbox-Api
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/3412083e Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/3412083e Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/3412083e Branch: refs/heads/master Commit: 3412083e85f8845e1f30a496b63ea526fc438442 Parents: 5b05b94 Author: Benoit Tellier <btell...@linagora.com> Authored: Tue Dec 4 12:05:17 2018 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Wed Dec 5 16:34:25 2018 +0700 ---------------------------------------------------------------------- .../james/mailbox/SystemMailboxesProvider.java | 34 +++++++++ .../exception/MailboxRoleNotFoundException.java | 36 +++++++++ .../store/SystemMailboxesProviderImpl.java | 79 ++++++++++++++++++++ .../store/SystemMailboxesProviderImplTest.java | 74 ++++++++++++++++++ server/container/guice/protocols/jmap/pom.xml | 4 + .../java/org/apache/james/jmap/JMAPModule.java | 4 +- .../MailboxRoleNotFoundException.java | 36 --------- .../james/jmap/methods/SendMDNProcessor.java | 2 +- .../methods/SetMessagesCreationProcessor.java | 2 +- .../methods/SetMessagesUpdateProcessor.java | 2 +- .../james/jmap/send/PostDequeueDecorator.java | 4 +- .../jmap/send/PostDequeueDecoratorFactory.java | 2 +- .../jmap/utils/SystemMailboxesProvider.java | 37 --------- .../jmap/utils/SystemMailboxesProviderImpl.java | 78 ------------------- .../SetMessagesCreationProcessorTest.java | 2 +- .../methods/SetMessagesUpdateProcessorTest.java | 2 +- .../jmap/send/PostDequeueDecoratorTest.java | 4 +- .../utils/SystemMailboxesProviderImplTest.java | 74 ------------------ 18 files changed, 239 insertions(+), 237 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/mailbox/api/src/main/java/org/apache/james/mailbox/SystemMailboxesProvider.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/SystemMailboxesProvider.java b/mailbox/api/src/main/java/org/apache/james/mailbox/SystemMailboxesProvider.java new file mode 100644 index 0000000..2b2dd9f --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/SystemMailboxesProvider.java @@ -0,0 +1,34 @@ +/**************************************************************** + * 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.mailbox; + +import java.util.stream.Stream; + +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.exception.MailboxRoleNotFoundException; + +public interface SystemMailboxesProvider { + Stream<MessageManager> getMailboxByRole(Role aRole, MailboxSession session) throws MailboxException; + + default MessageManager findMailbox(Role role, MailboxSession session) throws MailboxException { + return getMailboxByRole(role, session).findAny() + .orElseThrow(() -> new MailboxRoleNotFoundException(role)); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/mailbox/api/src/main/java/org/apache/james/mailbox/exception/MailboxRoleNotFoundException.java ---------------------------------------------------------------------- diff --git a/mailbox/api/src/main/java/org/apache/james/mailbox/exception/MailboxRoleNotFoundException.java b/mailbox/api/src/main/java/org/apache/james/mailbox/exception/MailboxRoleNotFoundException.java new file mode 100644 index 0000000..f92f879 --- /dev/null +++ b/mailbox/api/src/main/java/org/apache/james/mailbox/exception/MailboxRoleNotFoundException.java @@ -0,0 +1,36 @@ +/**************************************************************** + * 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.mailbox.exception; + +import org.apache.james.mailbox.Role; + +public class MailboxRoleNotFoundException extends RuntimeException { + + private final Role role; + + public MailboxRoleNotFoundException(Role role) { + super(String.format("Could not find any mailbox with role '%s'", role.serialize())); + this.role = role; + } + + public Role getRole() { + return role; + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/mailbox/store/src/main/java/org/apache/james/mailbox/store/SystemMailboxesProviderImpl.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/main/java/org/apache/james/mailbox/store/SystemMailboxesProviderImpl.java b/mailbox/store/src/main/java/org/apache/james/mailbox/store/SystemMailboxesProviderImpl.java new file mode 100644 index 0000000..0045d70 --- /dev/null +++ b/mailbox/store/src/main/java/org/apache/james/mailbox/store/SystemMailboxesProviderImpl.java @@ -0,0 +1,79 @@ +/**************************************************************** + * 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.mailbox.store; + +import java.util.stream.Stream; + +import javax.inject.Inject; + +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MessageManager; +import org.apache.james.mailbox.Role; +import org.apache.james.mailbox.SystemMailboxesProvider; +import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.exception.MailboxNotFoundException; +import org.apache.james.mailbox.model.MailboxMetaData; +import org.apache.james.mailbox.model.MailboxPath; +import org.apache.james.mailbox.model.search.MailboxQuery; +import org.apache.james.mailbox.model.search.PrefixedWildcard; + +import com.github.fge.lambdas.Throwing; +import com.github.fge.lambdas.functions.ThrowingFunction; +import com.google.common.annotations.VisibleForTesting; + +public class SystemMailboxesProviderImpl implements SystemMailboxesProvider { + + private final MailboxManager mailboxManager; + + @Inject + @VisibleForTesting + public SystemMailboxesProviderImpl(MailboxManager mailboxManager) { + this.mailboxManager = mailboxManager; + } + + @Override + public Stream<MessageManager> getMailboxByRole(Role aRole, MailboxSession session) throws MailboxException { + MailboxPath mailboxPath = MailboxPath.forUser(session.getUser().getUserName(), aRole.getDefaultMailbox()); + try { + return Stream.of(mailboxManager.getMailbox(mailboxPath, session)); + } catch (MailboxNotFoundException e) { + return searchMessageManagerByMailboxRole(aRole, session); + } + } + + private boolean hasRole(Role aRole, MailboxPath mailBoxPath) { + return Role.from(mailBoxPath.getName()) + .map(aRole::equals) + .orElse(false); + } + + private Stream<MessageManager> searchMessageManagerByMailboxRole(Role aRole, MailboxSession session) throws MailboxException { + ThrowingFunction<MailboxPath, MessageManager> loadMailbox = path -> mailboxManager.getMailbox(path, session); + MailboxQuery mailboxQuery = MailboxQuery.privateMailboxesBuilder(session) + .expression(new PrefixedWildcard(aRole.getDefaultMailbox())) + .build(); + return mailboxManager.search(mailboxQuery, session) + .stream() + .map(MailboxMetaData::getPath) + .filter(path -> hasRole(aRole, path)) + .map(Throwing.function(loadMailbox).sneakyThrow()); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/mailbox/store/src/test/java/org/apache/james/mailbox/store/SystemMailboxesProviderImplTest.java ---------------------------------------------------------------------- diff --git a/mailbox/store/src/test/java/org/apache/james/mailbox/store/SystemMailboxesProviderImplTest.java b/mailbox/store/src/test/java/org/apache/james/mailbox/store/SystemMailboxesProviderImplTest.java new file mode 100644 index 0000000..f100a27 --- /dev/null +++ b/mailbox/store/src/test/java/org/apache/james/mailbox/store/SystemMailboxesProviderImplTest.java @@ -0,0 +1,74 @@ +/**************************************************************** + * 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.mailbox.store; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.james.mailbox.MailboxManager; +import org.apache.james.mailbox.MailboxSession; +import org.apache.james.mailbox.MessageManager; +import org.apache.james.mailbox.Role; +import org.apache.james.mailbox.exception.MailboxNotFoundException; +import org.apache.james.mailbox.fixture.MailboxFixture; +import org.apache.james.mailbox.mock.MockMailboxSession; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +public class SystemMailboxesProviderImplTest { + + private MailboxSession mailboxSession = new MockMailboxSession(MailboxFixture.ALICE); + private SystemMailboxesProviderImpl systemMailboxProvider; + + private MailboxManager mailboxManager; + + private MessageManager inboxMessageManager; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Before + public void setUp() throws Exception { + mailboxManager = mock(MailboxManager.class); + inboxMessageManager = mock(MessageManager.class); + + systemMailboxProvider = new SystemMailboxesProviderImpl(mailboxManager); + } + + @Test + public void getMailboxByRoleShouldReturnEmptyWhenNoMailbox() throws Exception { + when(mailboxManager.getMailbox(eq(MailboxFixture.INBOX_ALICE), eq(mailboxSession))).thenThrow(MailboxNotFoundException.class); + + assertThat(systemMailboxProvider.getMailboxByRole(Role.INBOX, mailboxSession)).isEmpty(); + } + + @Test + public void getMailboxByRoleShouldReturnMailboxByRole() throws Exception { + when(mailboxManager.getMailbox(eq(MailboxFixture.INBOX_ALICE), eq(mailboxSession))).thenReturn(inboxMessageManager); + + assertThat(systemMailboxProvider.getMailboxByRole(Role.INBOX, mailboxSession)) + .hasSize(1) + .containsOnly(inboxMessageManager); + } +} http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/container/guice/protocols/jmap/pom.xml ---------------------------------------------------------------------- diff --git a/server/container/guice/protocols/jmap/pom.xml b/server/container/guice/protocols/jmap/pom.xml index 2d4191e..f870ff8 100644 --- a/server/container/guice/protocols/jmap/pom.xml +++ b/server/container/guice/protocols/jmap/pom.xml @@ -43,6 +43,10 @@ </dependency> <dependency> <groupId>${james.groupId}</groupId> + <artifactId>apache-james-mailbox-store</artifactId> + </dependency> + <dependency> + <groupId>${james.groupId}</groupId> <artifactId>james-server-guice-common</artifactId> </dependency> <dependency> http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java ---------------------------------------------------------------------- diff --git a/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java b/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java index 6ce551e..c15bf7b 100644 --- a/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java +++ b/server/container/guice/protocols/jmap/src/main/java/org/apache/james/jmap/JMAPModule.java @@ -36,13 +36,13 @@ import org.apache.james.jmap.methods.RequestHandler; import org.apache.james.jmap.send.PostDequeueDecoratorFactory; import org.apache.james.jmap.utils.HtmlTextExtractor; import org.apache.james.jmap.utils.JsoupHtmlTextExtractor; -import org.apache.james.jmap.utils.SystemMailboxesProvider; -import org.apache.james.jmap.utils.SystemMailboxesProviderImpl; import org.apache.james.jwt.JwtConfiguration; import org.apache.james.lifecycle.api.Configurable; import org.apache.james.mailbox.MailboxListener; import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxManager.SearchCapabilities; +import org.apache.james.mailbox.SystemMailboxesProvider; +import org.apache.james.mailbox.store.SystemMailboxesProviderImpl; import org.apache.james.modules.server.CamelMailetContainerModule; import org.apache.james.queue.api.MailQueueItemDecoratorFactory; import org.apache.james.server.core.configuration.FileConfigurationProvider; http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/MailboxRoleNotFoundException.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/MailboxRoleNotFoundException.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/MailboxRoleNotFoundException.java deleted file mode 100644 index 89d63a1..0000000 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/exceptions/MailboxRoleNotFoundException.java +++ /dev/null @@ -1,36 +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.exceptions; - -import org.apache.james.mailbox.Role; - -public class MailboxRoleNotFoundException extends RuntimeException { - - private final Role role; - - public MailboxRoleNotFoundException(Role role) { - super(String.format("Could not find any mailbox with role '%s'", role.serialize())); - this.role = role; - } - - public Role getRole() { - return role; - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SendMDNProcessor.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SendMDNProcessor.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SendMDNProcessor.java index b828900..7f71259 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SendMDNProcessor.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SendMDNProcessor.java @@ -37,11 +37,11 @@ import org.apache.james.jmap.model.MessageFactory; import org.apache.james.jmap.model.SetError; import org.apache.james.jmap.model.SetMessagesRequest; import org.apache.james.jmap.model.SetMessagesResponse; -import org.apache.james.jmap.utils.SystemMailboxesProvider; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageIdManager; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.Role; +import org.apache.james.mailbox.SystemMailboxesProvider; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.OverQuotaException; import org.apache.james.mailbox.model.Attachment; http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java index 090dfed..f4f30ef 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesCreationProcessor.java @@ -49,11 +49,11 @@ import org.apache.james.jmap.model.SetMessagesError; import org.apache.james.jmap.model.SetMessagesRequest; import org.apache.james.jmap.model.SetMessagesResponse; import org.apache.james.jmap.model.SetMessagesResponse.Builder; -import org.apache.james.jmap.utils.SystemMailboxesProvider; import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.Role; +import org.apache.james.mailbox.SystemMailboxesProvider; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.MailboxNotFoundException; import org.apache.james.mailbox.exception.OverQuotaException; http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessor.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessor.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessor.java index fd7f17b..f183f9e 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessor.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessor.java @@ -46,12 +46,12 @@ import org.apache.james.jmap.model.SetMessagesRequest; import org.apache.james.jmap.model.SetMessagesResponse; import org.apache.james.jmap.model.UpdateMessagePatch; import org.apache.james.jmap.utils.KeywordsCombiner; -import org.apache.james.jmap.utils.SystemMailboxesProvider; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageIdManager; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.MessageManager.FlagsUpdateMode; import org.apache.james.mailbox.Role; +import org.apache.james.mailbox.SystemMailboxesProvider; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.MailboxNotFoundException; import org.apache.james.mailbox.exception.OverQuotaException; http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecorator.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecorator.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecorator.java index 814ba8c..a42af65 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecorator.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecorator.java @@ -24,15 +24,15 @@ import java.util.List; import javax.mail.Flags; import javax.mail.Flags.Flag; -import org.apache.james.jmap.exceptions.MailboxRoleNotFoundException; import org.apache.james.jmap.send.exception.MailShouldBeInOutboxException; -import org.apache.james.jmap.utils.SystemMailboxesProvider; import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageIdManager; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.Role; +import org.apache.james.mailbox.SystemMailboxesProvider; import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.exception.MailboxRoleNotFoundException; import org.apache.james.mailbox.model.FetchGroupImpl; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MessageId; http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecoratorFactory.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecoratorFactory.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecoratorFactory.java index 5114fe8..ee8128e 100644 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecoratorFactory.java +++ b/server/protocols/jmap/src/main/java/org/apache/james/jmap/send/PostDequeueDecoratorFactory.java @@ -21,9 +21,9 @@ package org.apache.james.jmap.send; import javax.inject.Inject; -import org.apache.james.jmap.utils.SystemMailboxesProvider; import org.apache.james.mailbox.MailboxManager; import org.apache.james.mailbox.MessageIdManager; +import org.apache.james.mailbox.SystemMailboxesProvider; import org.apache.james.mailbox.model.MessageId.Factory; import org.apache.james.queue.api.MailQueue.MailQueueItem; import org.apache.james.queue.api.MailQueueItemDecoratorFactory; http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/SystemMailboxesProvider.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/SystemMailboxesProvider.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/SystemMailboxesProvider.java deleted file mode 100644 index 57e41bf..0000000 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/SystemMailboxesProvider.java +++ /dev/null @@ -1,37 +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.utils; - -import java.util.stream.Stream; - -import org.apache.james.jmap.exceptions.MailboxRoleNotFoundException; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.MessageManager; -import org.apache.james.mailbox.Role; -import org.apache.james.mailbox.exception.MailboxException; - -public interface SystemMailboxesProvider { - Stream<MessageManager> getMailboxByRole(Role aRole, MailboxSession session) throws MailboxException; - - default MessageManager findMailbox(Role role, MailboxSession session) throws MailboxException { - return getMailboxByRole(role, session).findAny() - .orElseThrow(() -> new MailboxRoleNotFoundException(role)); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/SystemMailboxesProviderImpl.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/SystemMailboxesProviderImpl.java b/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/SystemMailboxesProviderImpl.java deleted file mode 100644 index 50bd057..0000000 --- a/server/protocols/jmap/src/main/java/org/apache/james/jmap/utils/SystemMailboxesProviderImpl.java +++ /dev/null @@ -1,78 +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.utils; - -import java.util.stream.Stream; - -import javax.inject.Inject; - -import org.apache.james.mailbox.MailboxManager; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.MessageManager; -import org.apache.james.mailbox.Role; -import org.apache.james.mailbox.exception.MailboxException; -import org.apache.james.mailbox.exception.MailboxNotFoundException; -import org.apache.james.mailbox.model.MailboxMetaData; -import org.apache.james.mailbox.model.MailboxPath; -import org.apache.james.mailbox.model.search.MailboxQuery; -import org.apache.james.mailbox.model.search.PrefixedWildcard; - -import com.github.fge.lambdas.Throwing; -import com.github.fge.lambdas.functions.ThrowingFunction; -import com.google.common.annotations.VisibleForTesting; - -public class SystemMailboxesProviderImpl implements SystemMailboxesProvider { - - private final MailboxManager mailboxManager; - - @Inject - @VisibleForTesting - public SystemMailboxesProviderImpl(MailboxManager mailboxManager) { - this.mailboxManager = mailboxManager; - } - - @Override - public Stream<MessageManager> getMailboxByRole(Role aRole, MailboxSession session) throws MailboxException { - MailboxPath mailboxPath = MailboxPath.forUser(session.getUser().getUserName(), aRole.getDefaultMailbox()); - try { - return Stream.of(mailboxManager.getMailbox(mailboxPath, session)); - } catch (MailboxNotFoundException e) { - return searchMessageManagerByMailboxRole(aRole, session); - } - } - - private boolean hasRole(Role aRole, MailboxPath mailBoxPath) { - return Role.from(mailBoxPath.getName()) - .map(aRole::equals) - .orElse(false); - } - - private Stream<MessageManager> searchMessageManagerByMailboxRole(Role aRole, MailboxSession session) throws MailboxException { - ThrowingFunction<MailboxPath, MessageManager> loadMailbox = path -> mailboxManager.getMailbox(path, session); - MailboxQuery mailboxQuery = MailboxQuery.privateMailboxesBuilder(session) - .expression(new PrefixedWildcard(aRole.getDefaultMailbox())) - .build(); - return mailboxManager.search(mailboxQuery, session) - .stream() - .map(MailboxMetaData::getPath) - .filter(path -> hasRole(aRole, path)) - .map(Throwing.function(loadMailbox).sneakyThrow()); - } -} http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java index 0c93184..b93ed0f 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesCreationProcessorTest.java @@ -46,7 +46,6 @@ import org.apache.james.jmap.send.MailFactory; import org.apache.james.jmap.send.MailMetadata; import org.apache.james.jmap.send.MailSpool; import org.apache.james.jmap.utils.HtmlTextExtractor; -import org.apache.james.jmap.utils.SystemMailboxesProvider; import org.apache.james.mailbox.AttachmentManager; import org.apache.james.mailbox.BlobManager; import org.apache.james.mailbox.MailboxManager; @@ -55,6 +54,7 @@ import org.apache.james.mailbox.MessageIdManager; import org.apache.james.mailbox.MessageManager; import org.apache.james.mailbox.MessageUid; import org.apache.james.mailbox.Role; +import org.apache.james.mailbox.SystemMailboxesProvider; import org.apache.james.mailbox.exception.MailboxException; import org.apache.james.mailbox.exception.MailboxNotFoundException; import org.apache.james.mailbox.inmemory.InMemoryId; http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessorTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessorTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessorTest.java index 8fde32d..13c2989 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessorTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/methods/SetMessagesUpdateProcessorTest.java @@ -28,8 +28,8 @@ import org.apache.james.jmap.model.MessageProperties; import org.apache.james.jmap.model.SetMessagesRequest; import org.apache.james.jmap.model.SetMessagesResponse; import org.apache.james.jmap.model.UpdateMessagePatch; -import org.apache.james.jmap.utils.SystemMailboxesProvider; import org.apache.james.mailbox.MessageIdManager; +import org.apache.james.mailbox.SystemMailboxesProvider; import org.apache.james.mailbox.model.MailboxId; import org.apache.james.mailbox.model.MessageId; import org.apache.james.mailbox.model.TestMessageId; http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/test/java/org/apache/james/jmap/send/PostDequeueDecoratorTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/send/PostDequeueDecoratorTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/send/PostDequeueDecoratorTest.java index 9bdec79..80affe6 100644 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/send/PostDequeueDecoratorTest.java +++ b/server/protocols/jmap/src/test/java/org/apache/james/jmap/send/PostDequeueDecoratorTest.java @@ -32,8 +32,6 @@ import java.nio.charset.StandardCharsets; import javax.mail.Flags; import javax.mail.Flags.Flag; -import org.apache.james.jmap.exceptions.MailboxRoleNotFoundException; -import org.apache.james.jmap.utils.SystemMailboxesProviderImpl; import org.apache.james.mailbox.DefaultMailboxes; import org.apache.james.mailbox.MailboxSession; import org.apache.james.mailbox.MessageIdManager; @@ -42,6 +40,7 @@ import org.apache.james.mailbox.MessageManager.AppendCommand; import org.apache.james.mailbox.MessageUid; import org.apache.james.mailbox.acl.GroupMembershipResolver; import org.apache.james.mailbox.exception.MailboxException; +import org.apache.james.mailbox.exception.MailboxRoleNotFoundException; import org.apache.james.mailbox.inmemory.InMemoryMessageId; import org.apache.james.mailbox.inmemory.manager.InMemoryIntegrationResources; import org.apache.james.mailbox.model.ComposedMessageId; @@ -52,6 +51,7 @@ import org.apache.james.mailbox.model.MessageRange; import org.apache.james.mailbox.model.MessageResult; import org.apache.james.mailbox.model.MessageResultIterator; import org.apache.james.mailbox.store.StoreMailboxManager; +import org.apache.james.mailbox.store.SystemMailboxesProviderImpl; import org.apache.james.mime4j.dom.Message; import org.apache.james.queue.api.MailQueue; import org.apache.james.queue.api.MailQueue.MailQueueItem; http://git-wip-us.apache.org/repos/asf/james-project/blob/3412083e/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/SystemMailboxesProviderImplTest.java ---------------------------------------------------------------------- diff --git a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/SystemMailboxesProviderImplTest.java b/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/SystemMailboxesProviderImplTest.java deleted file mode 100644 index 04dbaa0..0000000 --- a/server/protocols/jmap/src/test/java/org/apache/james/jmap/utils/SystemMailboxesProviderImplTest.java +++ /dev/null @@ -1,74 +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.utils; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.apache.james.mailbox.MailboxManager; -import org.apache.james.mailbox.MailboxSession; -import org.apache.james.mailbox.MessageManager; -import org.apache.james.mailbox.Role; -import org.apache.james.mailbox.exception.MailboxNotFoundException; -import org.apache.james.mailbox.fixture.MailboxFixture; -import org.apache.james.mailbox.mock.MockMailboxSession; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; - -public class SystemMailboxesProviderImplTest { - - private MailboxSession mailboxSession = new MockMailboxSession(MailboxFixture.ALICE); - private SystemMailboxesProviderImpl systemMailboxProvider; - - private MailboxManager mailboxManager; - - private MessageManager inboxMessageManager; - - @Rule - public ExpectedException expectedException = ExpectedException.none(); - - @Before - public void setUp() throws Exception { - mailboxManager = mock(MailboxManager.class); - inboxMessageManager = mock(MessageManager.class); - - systemMailboxProvider = new SystemMailboxesProviderImpl(mailboxManager); - } - - @Test - public void getMailboxByRoleShouldReturnEmptyWhenNoMailbox() throws Exception { - when(mailboxManager.getMailbox(eq(MailboxFixture.INBOX_ALICE), eq(mailboxSession))).thenThrow(MailboxNotFoundException.class); - - assertThat(systemMailboxProvider.getMailboxByRole(Role.INBOX, mailboxSession)).isEmpty(); - } - - @Test - public void getMailboxByRoleShouldReturnMailboxByRole() throws Exception { - when(mailboxManager.getMailbox(eq(MailboxFixture.INBOX_ALICE), eq(mailboxSession))).thenReturn(inboxMessageManager); - - assertThat(systemMailboxProvider.getMailboxByRole(Role.INBOX, mailboxSession)) - .hasSize(1) - .containsOnly(inboxMessageManager); - } -} --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org