@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 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 MailboxMapper getMailboxMapper(MailboxSession session) throws MailboxException { MailboxMapper mapper = (MailboxMapper) session.getAttributes().get(MAILBOXMAPPER); if (mapper == null) { mapper = createMailboxMapper(session); session.getAttributes().put(MAILBOXMAPPER, mapper); } return mapper; }
private void removeMessageFromMailboxes(MailboxMessage message, Set<MailboxId> mailboxesToRemove, MailboxSession mailboxSession) throws MailboxException { MessageIdMapper messageIdMapper = mailboxSessionMapperFactory.getMessageIdMapper(mailboxSession); MailboxMapper mailboxMapper = mailboxSessionMapperFactory.getMailboxMapper(mailboxSession); SimpleMessageMetaData eventPayload = new SimpleMessageMetaData(message); for (MailboxId mailboxId: mailboxesToRemove) { messageIdMapper.delete(message.getMessageId(), mailboxesToRemove); dispatcher.expunged(mailboxSession, eventPayload, mailboxMapper.findMailboxById(mailboxId)); } }
@Override public Flags getApplicableFlags(MailboxSession session) throws MailboxException { return mapperFactory.getMessageMapper(session) .getApplicableFlag(mailbox); }
@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(); }
private List<MailboxMessage> findRelatedMailboxMessages(MessageId messageId, MailboxSession mailboxSession) throws MailboxException { MessageIdMapper messageIdMapper = mailboxSessionMapperFactory.getMessageIdMapper(mailboxSession); return messageIdMapper.find(ImmutableList.of(messageId), MessageMapper.FetchType.Full) .stream() .filter(hasRightsOn(mailboxSession, Right.Read)) .collect(Guavate.toImmutableList()); }
public MessageIdMapper getMessageIdMapper(MailboxSession session) throws MailboxException { MessageIdMapper mapper = (MessageIdMapper) session.getAttributes().get(MESSAGEIDMAPPER); if (mapper == null) { mapper = createMessageIdMapper(session); session.getAttributes().put(MESSAGEIDMAPPER, mapper); } return mapper; }
public AnnotationMapper getAnnotationMapper(MailboxSession session) throws MailboxException { AnnotationMapper mapper = (AnnotationMapper)session.getAttributes().get(ANNOTATIONMAPPER); if (mapper == null) { mapper = createAnnotationMapper(session); session.getAttributes().put(ANNOTATIONMAPPER, mapper); } return mapper; }
@Override public MessageResultIterator getMessages(MessageRange set, FetchGroup fetchGroup, MailboxSession mailboxSession) throws MailboxException { final MessageMapper messageMapper = mapperFactory.getMessageMapper(mailboxSession); return new StoreMessageResultIterator(messageMapper, mailbox, set, batchSizes, fetchGroup); }
private void addMessageToMailboxes(MailboxMessage mailboxMessage, Set<MailboxId> mailboxIds, MailboxSession mailboxSession) throws MailboxException { MessageIdMapper messageIdMapper = mailboxSessionMapperFactory.getMessageIdMapper(mailboxSession); MailboxMapper mailboxMapper = mailboxSessionMapperFactory.getMailboxMapper(mailboxSession); for (MailboxId mailboxId : mailboxIds) { boolean shouldPreserveFlags = mailboxManager.myRights(mailboxId, mailboxSession).contains(Right.Write); SimpleMailboxMessage copy = SimpleMailboxMessage.from(mailboxMessage) .mailboxId(mailboxId) .flags( FlagsFactory .builder() .flags(mailboxMessage.createFlags()) .filteringFlags( FlagsFilter.builder() .systemFlagFilter(f -> shouldPreserveFlags) .userFlagFilter(f -> shouldPreserveFlags) .build()) .build()) .build(); save(mailboxSession, messageIdMapper, copy); dispatcher.added(mailboxSession, mailboxMapper.findMailboxById(mailboxId), copy); } }
@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)); }
@Override public Result run() { try { MailboxSession session = mailboxManager.createSystemSession("MessageIdReIndexerImpl"); return mailboxSessionMapperFactory.getMessageIdMapper(session) .find(ImmutableList.of(messageId), MessageMapper.FetchType.Full) .stream() .map(mailboxMessage -> reIndex(mailboxMessage, session)) .reduce(Task::combine) .orElse(Result.COMPLETED); } catch (Exception e) { LOGGER.warn("Failed to re-index {}", messageId, e); return Result.PARTIAL; } }
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 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 Iterator<MailboxMessage> retrieveOriginalRows(MessageRange set, MailboxSession session) throws MailboxException { MessageMapper messageMapper = mapperFactory.getMessageMapper(session); return messageMapper.findInMailbox(mailbox, set, FetchType.Full, -1); }
@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(); }
@Override public Set<MessageId> accessibleMessages(Collection<MessageId> messageIds, MailboxSession mailboxSession) throws MailboxException { MessageIdMapper messageIdMapper = mailboxSessionMapperFactory.getMessageIdMapper(mailboxSession); List<MailboxMessage> messageList = messageIdMapper.find(messageIds, MessageMapper.FetchType.Metadata); ImmutableSet<MailboxId> allowedMailboxIds = getAllowedMailboxIds(mailboxSession, messageList, Right.Read); return messageList.stream() .filter(message -> allowedMailboxIds.contains(message.getMailboxId())) .map(MailboxMessage::getMessageId) .collect(Guavate.toImmutableSet()); }
@Override public List<MailboxPath> list(MailboxSession session) throws MailboxException { return mailboxSessionMapperFactory.getMailboxMapper(session) .list() .stream() .map(Mailbox::generateAssociatedPath) .collect(Guavate.toImmutableList()); }
@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); }