Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JAMES-1818 Introduce MailboxFactory to simplify MailboxUtils
- Loading branch information
Raphael Ouazana
committed
Sep 5, 2016
1 parent
268f001
commit 7ecb7ef
Showing
12 changed files
with
317 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
server/protocols/jmap/src/main/java/org/apache/james/jmap/model/MailboxFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/**************************************************************** | ||
* 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.model; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import javax.inject.Inject; | ||
|
||
import org.apache.james.jmap.model.mailbox.Mailbox; | ||
import org.apache.james.jmap.model.mailbox.Role; | ||
import org.apache.james.jmap.model.mailbox.SortOrder; | ||
import org.apache.james.mailbox.MailboxManager; | ||
import org.apache.james.mailbox.MailboxSession; | ||
import org.apache.james.mailbox.MessageManager; | ||
import org.apache.james.mailbox.exception.MailboxException; | ||
import org.apache.james.mailbox.model.MailboxId; | ||
import org.apache.james.mailbox.model.MailboxPath; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Splitter; | ||
|
||
public class MailboxFactory { | ||
private static final boolean DONT_RESET_RECENT = false; | ||
private static final Logger LOGGER = LoggerFactory.getLogger(MailboxFactory.class); | ||
|
||
private final MailboxManager mailboxManager; | ||
|
||
@Inject | ||
public MailboxFactory(MailboxManager mailboxManager) { | ||
this.mailboxManager = mailboxManager; | ||
} | ||
|
||
public Optional<Mailbox> fromMailboxPath(MailboxPath mailboxPath, MailboxSession mailboxSession) { | ||
try { | ||
MessageManager mailbox = mailboxManager.getMailbox(mailboxPath, mailboxSession); | ||
return fromMessageManager(mailbox, mailboxSession); | ||
} catch (MailboxException e) { | ||
LOGGER.warn("Cannot find mailbox for: " + mailboxPath.getName(), e); | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
public Optional<Mailbox> fromMailboxId(MailboxId mailboxId, MailboxSession mailboxSession) { | ||
try { | ||
MessageManager mailbox = mailboxManager.getMailbox(mailboxId, mailboxSession); | ||
return fromMessageManager(mailbox, mailboxSession); | ||
} catch (MailboxException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
private Optional<Mailbox> fromMessageManager(MessageManager messageManager, MailboxSession mailboxSession) throws MailboxException { | ||
MailboxPath mailboxPath = messageManager.getMailboxPath(); | ||
Optional<Role> role = Role.from(mailboxPath.getName()); | ||
MessageManager.MetaData mailboxMetaData = getMailboxMetaData(messageManager, mailboxSession); | ||
return Optional.ofNullable(Mailbox.builder() | ||
.id(messageManager.getId()) | ||
.name(getName(mailboxPath, mailboxSession)) | ||
.parentId(getParentIdFromMailboxPath(mailboxPath, mailboxSession).orElse(null)) | ||
.role(role) | ||
.unreadMessages(mailboxMetaData.getUnseenCount()) | ||
.totalMessages(mailboxMetaData.getMessageCount()) | ||
.sortOrder(SortOrder.getSortOrder(role)) | ||
.build()); | ||
} | ||
|
||
private MessageManager.MetaData getMailboxMetaData(MessageManager messageManager, MailboxSession mailboxSession) throws MailboxException { | ||
return messageManager.getMetaData(DONT_RESET_RECENT, mailboxSession, MessageManager.MetaData.FetchGroup.UNSEEN_COUNT); | ||
} | ||
|
||
@VisibleForTesting String getName(MailboxPath mailboxPath, MailboxSession mailboxSession) { | ||
String name = mailboxPath.getName(); | ||
if (name.contains(String.valueOf(mailboxSession.getPathDelimiter()))) { | ||
List<String> levels = Splitter.on(mailboxSession.getPathDelimiter()).splitToList(name); | ||
return levels.get(levels.size() - 1); | ||
} | ||
return name; | ||
} | ||
|
||
@VisibleForTesting Optional<MailboxId> getParentIdFromMailboxPath(MailboxPath mailboxPath, MailboxSession mailboxSession) throws MailboxException { | ||
List<MailboxPath> levels = mailboxPath.getHierarchyLevels(mailboxSession.getPathDelimiter()); | ||
if (levels.size() <= 1) { | ||
return Optional.empty(); | ||
} | ||
MailboxPath parent = levels.get(levels.size() - 2); | ||
return Optional.of(getMailboxId(parent, mailboxSession)); | ||
} | ||
|
||
private MailboxId getMailboxId(MailboxPath mailboxPath, MailboxSession mailboxSession) throws MailboxException { | ||
return mailboxManager.getMailbox(mailboxPath, mailboxSession) | ||
.getId(); | ||
} | ||
} |
Oops, something went wrong.