@Override public MessageId getMessageId() { return message.getMessageId(); }
@Override public int compare(MailboxMessage mailboxMessage1, MailboxMessage mailboxMessage2) { return mailboxMessage1.getMessageId() .serialize() .compareToIgnoreCase(mailboxMessage2.getMessageId().serialize()); } }
public static String getSerializedMessageIdIfSupportedByUnderlyingStorageOrNull(MailboxMessage mailboxMessage) { MessageId messageId = mailboxMessage.getMessageId(); if (messageId != null) { return messageId.serialize(); } return null; }
private MailboxMessage addUidAndModseq(MailboxMessage message, CassandraId mailboxId) throws MailboxException { CompletableFuture<Optional<MessageUid>> uidFuture = uidProvider.nextUid(mailboxId); CompletableFuture<Optional<Long>> modseqFuture = modSeqProvider.nextModSeq(mailboxId); CompletableFuture.allOf(uidFuture, modseqFuture).join(); message.setUid(uidFuture.join() .orElseThrow(() -> new MailboxException("Can not find a UID to save " + message.getMessageId() + " in " + mailboxId))); message.setModSeq(modseqFuture.join() .orElseThrow(() -> new MailboxException("Can not find a MODSEQ to save " + message.getMessageId() + " in " + mailboxId))); return message; }
@Override protected void storeAttachment(final MailboxMessage message, final List<MessageAttachment> messageAttachments, final MailboxSession session) throws MailboxException { mapperFactory.getAttachmentMapper(session) .storeAttachmentsForMessage( messageAttachments.stream() .map(MessageAttachment::getAttachment) .collect(Guavate.toImmutableList()), message.getMessageId()); } }
@Override protected void storeAttachment(final MailboxMessage message, final List<MessageAttachment> messageAttachments, final MailboxSession session) throws MailboxException { mapperFactory.getAttachmentMapper(session) .storeAttachmentsForMessage( messageAttachments.stream() .map(MessageAttachment::getAttachment) .collect(Guavate.toImmutableList()), message.getMessageId()); }
@Override public List<MailboxMessage> find(Collection<MessageId> messageIds, MessageMapper.FetchType fetchType) { try { return mailboxMapper.list() .stream() .flatMap(Throwing.function(mailbox -> ImmutableList.copyOf( messageMapper.findInMailbox(mailbox, MessageRange.all(), fetchType, UNLIMITED)) .stream())) .filter(message -> messageIds.contains(message.getMessageId())) .collect(Guavate.toImmutableList()); } catch (MailboxException e) { throw new RuntimeException(e); } }
@Override public void copyInMailbox(MailboxMessage mailboxMessage) throws MailboxException { boolean isAlreadyInMailbox = findMailboxes(mailboxMessage.getMessageId()).contains(mailboxMessage.getMailboxId()); if (!isAlreadyInMailbox) { save(mailboxMessage); } }
@Override public Iterator<SimpleMessageSearchIndex.SearchResult> iterator() { ImmutableList.Builder<MailboxMessage> builder = ImmutableList.builder(); while (messages.hasNext()) { MailboxMessage m = messages.next(); try { if (isMatch(m)) { builder.add(m); } } catch (MailboxException e) { LOGGER.error("Unable to search message {}", m.getUid(), e); } } return builder.build() .stream() .sorted(CombinedComparator.create(query.getSorts())) .map(mailboxMessage -> new SimpleMessageSearchIndex.SearchResult( Optional.of(mailboxMessage.getMessageId()), mailboxMessage.getMailboxId(), mailboxMessage.getUid())) .iterator(); }
public SimpleMessageMetaData(MailboxMessage message) { this(message.getUid(), message.getModSeq(), message.createFlags(), message.getFullContentOctets(), message.getInternalDate(), message.getMessageId()); }
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)); } }
public UnloadedMessageResult(MailboxMessage message, MailboxException exception) { super(); internalDate = message.getInternalDate(); size = message.getFullContentOctets(); uid = message.getUid(); flags = message.createFlags(); modSeq = message.getModSeq(); mailboxId = message.getMailboxId(); messageId = message.getMessageId(); this.exception = exception; }
private CompletableFuture<Void> insertIds(MailboxMessage message, CassandraId mailboxId) { ComposedMessageIdWithMetaData composedMessageIdWithMetaData = ComposedMessageIdWithMetaData.builder() .composedMessageId(new ComposedMessageId(mailboxId, message.getMessageId(), message.getUid())) .flags(message.createFlags()) .modSeq(message.getModSeq()) .build(); return CompletableFuture.allOf(messageIdDAO.insert(composedMessageIdWithMetaData), imapUidDAO.insert(composedMessageIdWithMetaData)); }
private BoundStatement boundWriteStatement(MailboxMessage message, Pair<BlobId, BlobId> pair) { CassandraMessageId messageId = (CassandraMessageId) message.getMessageId(); return insert.bind() .setUUID(MESSAGE_ID, messageId.get()) .setTimestamp(INTERNAL_DATE, message.getInternalDate()) .setInt(BODY_START_OCTET, (int) (message.getHeaderOctets())) .setLong(FULL_CONTENT_OCTETS, message.getFullContentOctets()) .setLong(BODY_OCTECTS, message.getBodyOctets()) .setString(BODY_CONTENT, pair.getRight().asString()) .setString(HEADER_CONTENT, pair.getLeft().asString()) .setLong(TEXTUAL_LINE_COUNT, Optional.ofNullable(message.getTextualLineCount()).orElse(DEFAULT_LONG_VALUE)) .setList(PROPERTIES, buildPropertiesUdt(message)) .setList(ATTACHMENTS, buildAttachmentUdt(message)); }
@Test public void textShouldContainsToWhenTo() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("To: First to <user@james.org>\nTo: Second to <user2@james.org>".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("First to user@james.org Second to user2@james.org"); }
@Test public void textShouldContainsBccWhenBcc() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("Bcc: First bcc <user@james.org>\nBcc: Second bcc <user2@james.org>".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("Second bcc user2@james.org First bcc user@james.org"); }
@Test public void textShouldContainsBodyWhenBody() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("\nMy body".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("My body"); }
@Test public void textShouldContainsFromWhenFrom() throws Exception { MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(new ByteArrayInputStream("From: First user <user@james.org>\nFrom: Second user <user2@james.org>".getBytes())); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.NO) .build(); assertThat(indexableMessage.getText()).isEqualTo("Second user user2@james.org First user user@james.org"); }
private ComposedMessageIdWithMetaData createMetadataFor(MailboxMessage mailboxMessage) { ComposedMessageId composedMessageId = new ComposedMessageId( mailboxMessage.getMailboxId(), mailboxMessage.getMessageId(), mailboxMessage.getUid()); return ComposedMessageIdWithMetaData.builder() .composedMessageId(composedMessageId) .flags(mailboxMessage.createFlags()) .modSeq(mailboxMessage.getModSeq()) .build(); }
public static Builder fromWithoutAttachments(MailboxMessage original) throws MailboxException { PropertyBuilder propertyBuilder = new PropertyBuilder(original.getProperties()); propertyBuilder.setTextualLineCount(original.getTextualLineCount()); return builder() .bodyStartOctet(Ints.checkedCast(original.getFullContentOctets() - original.getBodyOctets())) .content(copyFullContent(original)) .messageId(original.getMessageId()) .internalDate(original.getInternalDate()) .size(original.getFullContentOctets()) .flags(original.createFlags()) .propertyBuilder(propertyBuilder); }