private boolean bodyContains(String value, MailboxMessage message) throws IOException, MimeException { final InputStream input = message.getFullContent(); return isInMessage(value, input, false); }
private boolean messageContains(String value, MailboxMessage message) throws IOException, MimeException { final InputStream input = message.getFullContent(); return isInMessage(value, input, true); }
private static SharedByteArrayInputStream copyFullContent(MailboxMessage original) throws MailboxException { try { return new SharedByteArrayInputStream(IOUtils.toByteArray(original.getFullContent())); } catch (IOException e) { throw new MailboxException("Unable to parse message", e); } }
/** * Create a copy of the given message */ public JPAStreamingMailboxMessage(JPAMailbox mailbox, MessageUid uid, long modSeq, MailboxMessage message) throws MailboxException { super(mailbox, uid, modSeq, message); try { this.content = new SharedByteArrayInputStream(IOUtils.toByteArray(message.getFullContent())); this.header = getHeaderContent(); this.body = getBodyContent(); } catch (IOException e) { throw new MailboxException("Unable to parse message",e); } }
private static PartContentBuilder build(int[] path, MailboxMessage message) throws IOException, MimeException { final InputStream stream = message.getFullContent(); PartContentBuilder result = new PartContentBuilder(); result.parse(stream); try { for (int next : path) { result.to(next); } } catch (PartContentBuilder.PartNotFoundException e) { // Missing parts should return zero sized content // See http://markmail.org/message/2jconrj7scvdi5dj result.markEmpty(); } return result; }
@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 shouldHandleCorrectlyMessageIdHavingSerializeMethodThatReturnNull() throws Exception { MessageId invalidMessageIdThatReturnNull = mock(MessageId.class); when(invalidMessageIdThatReturnNull.serialize()) .thenReturn(null); // When MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(invalidMessageIdThatReturnNull); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/bodyMakeTikaToFail.eml")); 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(textExtractor) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.YES) .build(); // Then assertThat(indexableMessage.getMessageId()).isNull(); }
@Test public void textShouldContainsCcWhenCc() 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("Cc: First cc <user@james.org>\nCc: Second cc <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 cc user@james.org Second cc user2@james.org"); }
@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"); }
@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 textShouldContainsSubjectsWhenSubjects() 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("Subject: subject1\nSubject: subject2".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("subject1 subject2"); }
@Test public void shouldHandleCorrectlyNullMessageId() throws Exception { // When MailboxMessage mailboxMessage = mock(MailboxMessage.class); TestId mailboxId = TestId.of(1); when(mailboxMessage.getMailboxId()) .thenReturn(mailboxId); when(mailboxMessage.getMessageId()) .thenReturn(null); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/bodyMakeTikaToFail.eml")); 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(textExtractor) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.YES) .build(); // Then assertThat(indexableMessage.getMessageId()).isNull(); } }
@Test public void messageShouldBeIndexedEvenIfTikaParserThrowsAnError() throws Exception { //Given 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(ClassLoader.getSystemResourceAsStream("eml/bodyMakeTikaToFail.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); // When IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(textExtractor) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.YES) .build(); // Then assertThat(indexableMessage.getText()).contains("subject should be parsed"); }
@Test public void hasAttachmentsShouldReturnFalseWhenPropertyIsPresentButFalse() throws IOException { //Given 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(ClassLoader.getSystemResourceAsStream("eml/mailWithHeaders.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); when(mailboxMessage.getProperties()) .thenReturn(ImmutableList.of(new SimpleProperty(PropertyBuilder.JAMES_INTERNALS, PropertyBuilder.HAS_ATTACHMENT, "false"))); // When 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(); // Then assertThat(indexableMessage.getHasAttachment()).isFalse(); }
@Test public void textShouldBeEmptyWhenNoMatchingHeaders() 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("".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()).isEmpty(); }
@Test public void attachmentsShouldNotBeenIndexedWhenAsked() throws Exception { //Given 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(ClassLoader.getSystemResourceAsStream("eml/mailWithHeaders.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); // When 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(); // Then assertThat(indexableMessage.getAttachments()).isEmpty(); }
when(mailboxMessage.getMessageId()) .thenReturn(InMemoryMessageId.of(42)); when(mailboxMessage.getFullContent()) .thenReturn(ClassLoader.getSystemResourceAsStream("eml/mailWithHeaders.eml")); when(mailboxMessage.createFlags())
@Test public void attachmentsShouldBeenIndexedWhenAsked() throws Exception { //Given 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(ClassLoader.getSystemResourceAsStream("eml/emailWith3Attachments.eml")); when(mailboxMessage.createFlags()) .thenReturn(new Flags()); when(mailboxMessage.getUid()) .thenReturn(MESSAGE_UID); // When IndexableMessage indexableMessage = IndexableMessage.builder() .message(mailboxMessage) .users(ImmutableList.of(new MockMailboxSession("username").getUser())) .extractor(new DefaultTextExtractor()) .zoneId(ZoneId.of("Europe/Paris")) .indexAttachments(IndexAttachments.YES) .build(); // Then assertThat(indexableMessage.getAttachments()).isNotEmpty(); }