@Override public boolean mailboxExists(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { try { final MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); mapper.findMailboxByPath(mailboxPath); return true; } catch (MailboxNotFoundException e) { return false; } }
private List<Mailbox> retrieveMailboxes(QuotaRoot quotaRoot, MailboxSession session) throws MailboxException { List<MailboxPath> paths = quotaRootResolver.retrieveAssociatedMailboxes(quotaRoot, session); final MailboxMapper mapper = factory.getMailboxMapper(session); return Lists.transform(paths, mailboxPath -> { try { return mapper.findMailboxByPath(mailboxPath); } catch (MailboxException e) { throw new RuntimeException(e); } }); }
@Override public MailboxACL listRights(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailbox = mapper.findMailboxByPath(mailboxPath); return mailbox.getACL(); }
@Override public Rfc4314Rights myRights(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailbox = mapper.findMailboxByPath(mailboxPath); return myRights(mailbox, session); }
@Override public MessageManager getMailbox(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { final MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailboxRow = mapper.findMailboxByPath(mailboxPath); if (mailboxRow == null) { LOGGER.info("Mailbox '{}' not found.", mailboxPath); throw new MailboxNotFoundException(mailboxPath); } else { LOGGER.debug("Loaded mailbox {}", mailboxPath); return createMessageManager(mailboxRow, session); } }
@Override public boolean hasChildren(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailbox = mapper.findMailboxByPath(mailboxPath); return mapper.hasChildren(mailbox, session.getPathDelimiter()); } }
@Override public Rfc4314Rights[] listRights(MailboxPath mailboxPath, EntryKey key, MailboxSession session) throws MailboxException { MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailbox = mapper.findMailboxByPath(mailboxPath); return aclResolver.listRights(key, groupMembershipResolver, mailbox.getUser(), !GROUP_FOLDER); }
private Task.Result reIndex(MailboxPath path, MailboxSession mailboxSession, ReprocessingContext reprocessingContext) throws MailboxException { LOGGER.info("Intend to reindex {}", path.asString()); Mailbox mailbox = mailboxSessionMapperFactory.getMailboxMapper(mailboxSession).findMailboxByPath(path); messageSearchIndex.deleteAll(mailboxSession, mailbox); try { return Iterators.toStream( mailboxSessionMapperFactory.getMessageMapper(mailboxSession) .findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Metadata, NO_LIMIT)) .map(MailboxMessage::getUid) .map(uid -> handleMessageReIndexing(mailboxSession, mailbox, uid)) .peek(reprocessingContext::updateAccordingToReprocessingResult) .reduce(Task::combine) .orElse(Task.Result.COMPLETED); } finally { LOGGER.info("Finish to reindex {}", path.asString()); } }
@Override public MailboxId getMailboxId(String namespace, String user, String name) { MailboxSession mailboxSession = null; try { mailboxSession = mailboxManager.createSystemSession(user); MailboxMapper mailboxMapper = mailboxMapperFactory.getMailboxMapper(mailboxSession); return mailboxMapper.findMailboxByPath(new MailboxPath(namespace, user, name)).getMailboxId(); } catch (MailboxException e) { throw new RuntimeException(e); } finally { closeSession(mailboxSession); } }
Task.Result handleMessageReIndexing(MailboxPath path, MessageUid uid) throws MailboxException { MailboxSession mailboxSession = mailboxManager.createSystemSession(path.getUser()); Mailbox mailbox = mailboxSessionMapperFactory.getMailboxMapper(mailboxSession) .findMailboxByPath(path); return handleMessageReIndexing(mailboxSession, mailbox, uid); }
public MailboxId checkThenGetMailboxId(MailboxPath path, MailboxSession session) throws MailboxException { MailboxMapper mailboxMapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailbox = mailboxMapper.findMailboxByPath(path); if (!rightManager.hasRight(mailbox, Right.Read, session)) { throw new InsufficientRightsException("Not enough rights on " + path); } return mailbox.getMailboxId(); }
@Override public void applyRightsCommand(MailboxPath mailboxPath, ACLCommand mailboxACLCommand, MailboxSession session) throws MailboxException { assertSharesBelongsToUserDomain(mailboxPath.getUser(), mailboxACLCommand); MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailbox = mapper.findMailboxByPath(mailboxPath); ACLDiff aclDiff = mapper.updateACL(mailbox, mailboxACLCommand); dispatcher.aclUpdated(session, mailboxPath, aclDiff); }
@Override public Mailbox createMailbox(MailboxPath mailboxPath, MailboxSession session) throws MailboxException { cassandraMailboxManager.createMailbox(mailboxPath, session); return mapperFactory.getMailboxMapper(session).findMailboxByPath(mailboxPath); }
@Override public void setRights(MailboxPath mailboxPath, MailboxACL mailboxACL, MailboxSession session) throws MailboxException { assertSharesBelongsToUserDomain(mailboxPath.getUser(), mailboxACL.getEntries()); MailboxMapper mapper = mailboxSessionMapperFactory.getMailboxMapper(session); Mailbox mailbox = mapper.findMailboxByPath(mailboxPath); setRights(mailboxACL, mapper, mailbox, session); }
@Test public void createMailboxShouldCreateAMailbox() throws Exception { mailboxManagerManagement.createMailbox(MailboxConstants.USER_NAMESPACE, USER, "name"); assertThat(mapperFactory.createMailboxMapper(session).list()).hasSize(1); assertThat(mapperFactory.createMailboxMapper(session).findMailboxByPath(MailboxPath.forUser(USER, "name"))).isNotNull(); }
@Test public void test() throws Exception { final MockMailboxSession mockMailboxSession = new MockMailboxSession("re-indexing"); when(mailboxManager.createSystemSession(any(String.class))) .thenReturn(mockMailboxSession); final MessageMapper messageMapper = mock(MessageMapper.class); final MailboxMapper mailboxMapper = mock(MailboxMapper.class); when(mailboxSessionMapperFactory.getMessageMapper(any(MailboxSession.class))) .thenReturn(messageMapper); when(mailboxSessionMapperFactory.getMailboxMapper(any(MailboxSession.class))) .thenReturn(mailboxMapper); final MailboxMessage message = new MessageBuilder().build(); final SimpleMailbox mailbox = new SimpleMailbox(INBOX, 42); mailbox.setMailboxId(message.getMailboxId()); when(mailboxMapper.findMailboxByPath(INBOX)).thenReturn(mailbox); when(messageMapper.findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Full, LIMIT)) .thenReturn(Lists.newArrayList(message).iterator()); reIndexer.reIndex(INBOX); verify(mailboxManager).createSystemSession(any(String.class)); verify(mailboxSessionMapperFactory).getMailboxMapper(mockMailboxSession); verify(mailboxSessionMapperFactory).getMessageMapper(mockMailboxSession); verify(mailboxMapper).findMailboxByPath(INBOX); verify(messageMapper).findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Full, LIMIT); verify(mailboxManager).addListener(eq(INBOX), any(MailboxListener.class), any(MailboxSession.class)); verify(mailboxManager).removeListener(eq(INBOX), any(MailboxListener.class), any(MailboxSession.class)); verify(messageSearchIndex).add(any(MailboxSession.class), eq(mailbox), eq(message)); verify(messageSearchIndex).deleteAll(any(MailboxSession.class), eq(mailbox)); verifyNoMoreInteractions(mailboxMapper, mailboxSessionMapperFactory, messageSearchIndex, messageMapper, mailboxMapper); }
.thenReturn(user1MailboxMapper); Mailbox user1Mailbox = mock(Mailbox.class); when(user1MailboxMapper.findMailboxByPath(user1MailboxPath)) .thenReturn(user1Mailbox); MessageMapper user1MessageMapper = mock(MessageMapper.class);
@Override public void deleteMailbox(final MailboxPath mailboxPath, final MailboxSession session) throws MailboxException { LOGGER.info("deleteMailbox {}", mailboxPath); assertIsOwner(session, mailboxPath); MailboxMapper mailboxMapper = mailboxSessionMapperFactory.getMailboxMapper(session); MessageMapper messageMapper = mailboxSessionMapperFactory.getMessageMapper(session); mailboxMapper.execute((Mapper.Transaction<Mailbox>) () -> { Mailbox mailbox = mailboxMapper.findMailboxByPath(mailboxPath); if (mailbox == null) { throw new MailboxNotFoundException(mailboxPath); } QuotaRoot quotaRoot = quotaRootResolver.getQuotaRoot(mailboxPath); long messageCount = messageMapper.countMessagesInMailbox(mailbox); long totalSize = Iterators.toStream(messageMapper.findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Metadata, -1)) .mapToLong(Message::getFullContentOctets) .sum(); // We need to create a copy of the mailbox as maybe we can not refer to the real // mailbox once we remove it SimpleMailbox m = new SimpleMailbox(mailbox); mailboxMapper.delete(mailbox); dispatcher.mailboxDeleted(session, mailbox, quotaRoot, QuotaCount.count(messageCount), QuotaSize.size(totalSize)); return m; }); }
private void doRenameMailbox(MailboxPath from, MailboxPath to, MailboxSession session, MailboxMapper mapper) throws MailboxException { // TODO put this into a serilizable transaction Mailbox mailbox = Optional.ofNullable(mapper.findMailboxByPath(from)) .orElseThrow(() -> new MailboxNotFoundException(from)); mailbox.setNamespace(to.getNamespace()); mailbox.setUser(to.getUser()); mailbox.setName(to.getName()); mapper.save(mailbox); dispatcher.mailboxRenamed(session, from, mailbox); // rename submailboxes MailboxPath children = new MailboxPath(from.getNamespace(), from.getUser(), from.getName() + getDelimiter() + "%"); locker.executeWithLock(session, children, (LockAwareExecution<Void>) () -> { List<Mailbox> subMailboxes = mapper.findMailboxWithPathLike(children); for (Mailbox sub : subMailboxes) { String subOriginalName = sub.getName(); String subNewName = to.getName() + subOriginalName.substring(from.getName().length()); MailboxPath fromPath = new MailboxPath(children, subOriginalName); sub.setName(subNewName); mapper.save(sub); dispatcher.mailboxRenamed(session, fromPath, sub); LOGGER.debug("Rename mailbox sub-mailbox {} to {}", subOriginalName, subNewName); } return null; }, true); }