@Override public long getMessageCount(MailboxSession mailboxSession) throws MailboxException { return mapperFactory.getMessageMapper(mailboxSession).countMessagesInMailbox(getMailboxEntity()); }
public CurrentQuotas recalculateCurrentQuotas(QuotaRoot quotaRoot, MailboxSession session) throws MailboxException { List<Mailbox> mailboxes = retrieveMailboxes(quotaRoot, session); MessageMapper mapper = factory.getMessageMapper(session); long messagesSizes = 0; long messageCount = 0; for (Mailbox mailbox : mailboxes) { Iterator<MailboxMessage> messages = mapper.findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Metadata, -1); messageCount += mapper.countMessagesInMailbox(mailbox); while (messages.hasNext()) { messagesSizes += messages.next().getFullContentOctets(); } } return new CurrentQuotas(messageCount, messagesSizes); }
@Test public void importEmlFileToMailboxShouldImportEmlFileToGivenMailbox() throws Exception { Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY); mapperFactory.createMailboxMapper(session).save(mailbox); String emlpath = ClassLoader.getSystemResource("eml/frnog.eml").getFile(); mailboxManagerManagement.importEmlFileToMailbox(MailboxConstants.USER_NAMESPACE, USER, "name", emlpath); assertThat(mapperFactory.getMessageMapper(session).countMessagesInMailbox(mailbox)).isEqualTo(1); Iterator<MailboxMessage> iterator = mapperFactory.getMessageMapper(session).findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Full, LIMIT); MailboxMessage mailboxMessage = iterator.next(); assertThat(IOUtils.toString(new FileInputStream(new File(emlpath)), StandardCharsets.UTF_8)) .isEqualTo(IOUtils.toString(mailboxMessage.getFullContent(), StandardCharsets.UTF_8)); }
@Test public void importEmlFileToMailboxShouldNotImportEmlFileWithWrongPathToGivenMailbox() throws Exception { Mailbox mailbox = new SimpleMailbox(MailboxPath.forUser(USER, "name"), UID_VALIDITY); mapperFactory.createMailboxMapper(session).save(mailbox); String emlpath = ClassLoader.getSystemResource("eml/frnog.eml").getFile(); mailboxManagerManagement.importEmlFileToMailbox(MailboxConstants.USER_NAMESPACE, USER, "name", "wrong_path" + emlpath); assertThat(mapperFactory.getMessageMapper(session).countMessagesInMailbox(mailbox)).isEqualTo(0); Iterator<MailboxMessage> iterator = mapperFactory.getMessageMapper(session).findInMailbox(mailbox, MessageRange.all(), MessageMapper.FetchType.Full, LIMIT); assertThat(iterator.hasNext()).isFalse(); }
@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; }); }