@VisibleForTesting Optional<MailboxPath> getMailboxPathFromId(Optional<MailboxId> mailboxId, MailboxSession mailboxSession) { FunctionChainer<MailboxId, Optional<MailboxPath>> fromMailboxIdToMailboxPath = Throwing.function(id -> { try { return Optional.of(mailboxManager.getMailbox(id, mailboxSession).getMailboxPath()); } catch (MailboxNotFoundException e) { return Optional.empty(); } }); return mailboxId .flatMap(fromMailboxIdToMailboxPath.sneakyThrow()); }
private void assertOwned(MailboxSession mailboxSession, MessageManager parent) throws MailboxException { if (!parent.getMailboxPath().belongsTo(mailboxSession)) { throw new MailboxNotOwnedException(); } }
private void addStorageDirective(MailboxId mailboxId) { try { MailboxSession mailboxSession = mailboxManager.createSystemSession(user.asString()); MessageManager messageManager = mailboxManager.getMailbox(mailboxId, mailboxSession); String mailboxName = messageManager.getMailboxPath().getName(); String attributeNameForUser = DELIVERY_PATH_PREFIX + user.asString(); mail.setAttribute(attributeNameForUser, mailboxName); } catch (MailboxNotFoundException e) { LOGGER.info("Mailbox {} does not exist, but it was mentioned in a JMAP filtering rule", mailboxId, e); } catch (Exception e) { LOGGER.error("Unexpected failure while resolving mailbox name for {}", mailboxId, e); } } }
@Test public void assertIsUserOwnerOfMailboxesShouldThrowWhenRetrievingMailboxPathFails() throws Exception { InMemoryId mailboxId = InMemoryId.of(6789); MessageManager mailbox = mock(MessageManager.class); when(mockedMailboxManager.getMailbox(mailboxId, session)) .thenReturn(mailbox); when(mockedMailboxIdFactory.fromString(mailboxId.serialize())) .thenReturn(mailboxId); when(mailbox.getMailboxPath()) .thenThrow(new MailboxException()); assertThatThrownBy(() -> sut.assertIsUserOwnerOfMailboxes(ImmutableList.of(mailboxId), session)); }
private MailboxPath computeMailboxPathWithNewParentId(MailboxPath originMailboxPath, MailboxId parentMailboxId, MailboxSession mailboxSession) throws MailboxException { MailboxPath newParentMailboxPath = mailboxManager.getMailbox(parentMailboxId, mailboxSession).getMailboxPath(); String lastName = getCurrentMailboxName(originMailboxPath, mailboxSession); return new MailboxPath(originMailboxPath, newParentMailboxPath.getName() + mailboxSession.getPathDelimiter() + lastName); }
@Test public void assertIsUserOwnerOfMailboxesShouldThrowWhenUserIsNotTheOwnerOfTheMailbox() throws Exception { InMemoryId mailboxId = InMemoryId.of(6789); MessageManager mailbox = mock(MessageManager.class); when(mockedMailboxManager.getMailbox(mailboxId, session)) .thenReturn(mailbox); when(mockedMailboxIdFactory.fromString(mailboxId.serialize())) .thenReturn(mailboxId); when(mailbox.getMailboxPath()) .thenReturn(MailboxPath.forUser("otheruser@example.com", mailboxId.serialize())); assertThatThrownBy(() -> sut.assertIsUserOwnerOfMailboxes(ImmutableList.of(mailboxId), session)) .isInstanceOf(MailboxNotOwnedException.class); }
@Test public void assertIsUserOwnerOfMailboxesShouldNotThrowWhenUserIsTheOwnerOfTheMailbox() throws Exception { InMemoryId mailboxId = InMemoryId.of(6789); MessageManager mailbox = mock(MessageManager.class); when(mockedMailboxManager.getMailbox(mailboxId, session)) .thenReturn(mailbox); when(mockedMailboxIdFactory.fromString(mailboxId.serialize())) .thenReturn(mailboxId); when(mailbox.getMailboxPath()) .thenReturn(MailboxPath.forUser(USER, mailboxId.serialize())); sut.assertIsUserOwnerOfMailboxes(ImmutableList.of(mailboxId), session); }
preconditions(mailbox, mailboxSession); MailboxPath mailboxPath = mailboxManager.getMailbox(mailbox.getId(), mailboxSession).getMailboxPath(); mailboxManager.deleteMailbox(mailboxPath, mailboxSession); subscriptionManager.unsubscribe(mailboxSession, mailboxPath.getName());
when(outbox.getMailboxPath()).thenReturn(MailboxPath.forUser(USER, OUTBOX)); when(drafts.getMailboxPath()).thenReturn(MailboxPath.forUser(USER, DRAFTS)); optionalOutbox = Optional.of(outbox); optionalDrafts = Optional.of(drafts);
private void updateMailbox(Mailbox mailbox, MailboxUpdateRequest updateRequest, MailboxSession mailboxSession) throws MailboxException { MailboxPath originMailboxPath = mailboxManager.getMailbox(mailbox.getId(), mailboxSession).getMailboxPath(); MailboxPath destinationMailboxPath = computeNewMailboxPath(mailbox, originMailboxPath, updateRequest, mailboxSession); if (updateRequest.getSharedWith().isPresent()) { mailboxManager.setRights(originMailboxPath, updateRequest.getSharedWith() .get() .removeEntriesFor(Username.forMailboxPath(originMailboxPath)) .toMailboxAcl(), mailboxSession); } if (!originMailboxPath.equals(destinationMailboxPath)) { mailboxManager.renameMailbox(originMailboxPath, destinationMailboxPath, mailboxSession); subscriptionManager.unsubscribe(mailboxSession, originMailboxPath.getName()); subscriptionManager.subscribe(mailboxSession, destinationMailboxPath.getName()); } }
private Mailbox fromMessageManager(MessageManager messageManager, Optional<List<MailboxMetaData>> userMailboxesMetadata, MailboxSession mailboxSession) throws MailboxException { MailboxPath mailboxPath = messageManager.getMailboxPath(); boolean isOwner = mailboxPath.belongsTo(mailboxSession); Optional<Role> role = Role.from(mailboxPath.getName()); MailboxCounters mailboxCounters = messageManager.getMailboxCounters(mailboxSession); MessageManager.MetaData metaData = messageManager.getMetaData(NO_RESET_RECENT, mailboxSession, MessageManager.MetaData.FetchGroup.NO_COUNT); Rights rights = Rights.fromACL(metaData.getACL()) .removeEntriesFor(Username.forMailboxPath(mailboxPath)); Username username = Username.fromSession(mailboxSession); Quotas quotas = getQuotas(mailboxPath); return Mailbox.builder() .id(messageManager.getId()) .name(getName(mailboxPath, mailboxSession)) .parentId(getParentIdFromMailboxPath(mailboxPath, userMailboxesMetadata, mailboxSession).orElse(null)) .role(role) .unreadMessages(mailboxCounters.getUnseen()) .totalMessages(mailboxCounters.getCount()) .sortOrder(SortOrder.getSortOrder(role)) .sharedWith(rights) .mayAddItems(rights.mayAddItems(username).orElse(isOwner)) .mayCreateChild(rights.mayCreateChild(username).orElse(isOwner)) .mayDelete(rights.mayDelete(username).orElse(isOwner)) .mayReadItems(rights.mayReadItems(username).orElse(isOwner)) .mayRemoveItems(rights.mayRemoveItems(username).orElse(isOwner)) .mayRename(rights.mayRename(username).orElse(isOwner)) .namespace(getNamespace(mailboxPath, isOwner)) .quotas(quotas) .build(); }