org.apache.james.mime4j.stream.Field Java Examples

The following examples show how to use org.apache.james.mime4j.stream.Field. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldAddMultivaluedHeadersWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage messageHavingInReplyTo = CreationMessage.builder()
            .from(DraftEmailer.builder().name("sender").build())
            .headers(ImmutableMap.of("FIRST", "first value\nsecond value"))
            .mailboxIds(ImmutableList.of("dead-beef-1337"))
            .subject("subject")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), messageHavingInReplyTo), ImmutableList.of(), session);

    // Then
    assertThat(result.getHeader().getFields("FIRST")).extracting(Field::getBody)
        .containsOnly("first value", "second value");
}
 
Example #2
Source File: MessageStoreImplRepositoryTestUtil.java    From sling-samples with Apache License 2.0 6 votes vote down vote up
static String getResourcePath(Message msg, MessageStoreImpl store) {
    final Header hdr = msg.getHeader();
    final String listIdRaw = hdr.getField("List-Id").getBody();
    final String listId = listIdRaw.substring(1, listIdRaw.length()-1); // remove < and >

    String msgId;
    final Field msgIdField = hdr.getField("Message-ID");
    if (msgIdField != null) {
        msgId = msgIdField.getBody();
        msgId = msgId.substring(1, msgId.length()-1);
    } else {
        msgId = Integer.toHexString(hdr.getField("Date").hashCode());
    }
    msgId = makeJcrFriendly(msgId);

    String subject = null;
    final Field subjectField = hdr.getField("Subject");
    if (subjectField != null) {
        subject = subjectField.getBody();
    }

    String threadPath = store.threadKeyGen.getThreadKey(subject);
    String path = store.archivePath + getDomainNodeName(listId) + "/" + getListNodeName(listId) +
            "/" + threadPath + "/" + msgId;
    return path;
}
 
Example #3
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldFilterGeneratedHeadersRegardlessOfCaseWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    String joesEmail = "[email protected]";
    CreationMessage messageHavingInReplyTo = CreationMessage.builder()
            .from(DraftEmailer.builder().email(joesEmail).name("joe").build())
            .headers(ImmutableMap.of("frOM", "[email protected]", "VALID", "valid header value"))
            .mailboxIds(ImmutableList.of("dead-beef-1337"))
            .subject("subject")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), messageHavingInReplyTo), ImmutableList.of(), session);

    // Then
    assertThat(result.getFrom()).extracting(Mailbox::getAddress)
        .allMatch(f -> f.equals(joesEmail));
    assertThat(result.getHeader().getFields("VALID")).extracting(Field::getBody)
        .containsOnly("valid header value");
    assertThat(result.getHeader().getFields("From")).extracting(Field::getBody)
        .containsOnly("joe <[email protected]>");
}
 
Example #4
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldFilterGeneratedHeadersWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    String joesEmail = "[email protected]";
    CreationMessage messageHavingInReplyTo = CreationMessage.builder()
            .from(DraftEmailer.builder().email(joesEmail).name("joe").build())
            .headers(ImmutableMap.of("From", "[email protected]", "VALID", "valid header value"))
            .mailboxIds(ImmutableList.of("dead-beef-1337"))
            .subject("subject")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), messageHavingInReplyTo), ImmutableList.of(), session);

    // Then
    assertThat(result.getFrom()).extracting(Mailbox::getAddress)
        .allMatch(f -> f.equals(joesEmail));
    assertThat(result.getHeader().getFields("VALID")).extracting(Field::getBody)
        .containsOnly("valid header value");
    assertThat(result.getHeader().getFields("From")).extracting(Field::getBody)
        .containsOnly("joe <[email protected]>");
}
 
Example #5
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldAddHeadersWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage messageHavingInReplyTo = CreationMessage.builder()
            .from(DraftEmailer.builder().name("sender").build())
            .headers(ImmutableMap.of("FIRST", "first value", "SECOND", "second value"))
            .mailboxIds(ImmutableList.of("dead-beef-1337"))
            .subject("subject")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), messageHavingInReplyTo), ImmutableList.of(), session);

    // Then
    assertThat(result.getHeader().getFields("FIRST")).extracting(Field::getBody)
            .containsOnly("first value");
    assertThat(result.getHeader().getFields("SECOND")).extracting(Field::getBody)
        .containsOnly("second value");
}
 
Example #6
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldAddHeaderWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage messageHavingInReplyTo = CreationMessage.builder()
            .from(DraftEmailer.builder().name("sender").build())
            .headers(ImmutableMap.of("FIRST", "first value"))
            .mailboxIds(ImmutableList.of("dead-beef-1337"))
            .subject("subject")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), messageHavingInReplyTo), ImmutableList.of(), session);

    // Then
    assertThat(result.getHeader().getFields("FIRST")).extracting(Field::getBody)
            .containsOnly("first value");
}
 
Example #7
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldGenerateMessageIdWhenSenderWithoutDomain() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage message = CreationMessage.builder()
            .from(DraftEmailer.builder().email("sender").build())
            .mailboxIds(ImmutableList.of("dead-beef-1337"))
            .subject("subject")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), message), ImmutableList.of(), session);

    // Then
    assertThat(result.getHeader().getFields("Message-ID")).extracting(Field::getBody)
            .isNotNull();
}
 
Example #8
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldGenerateMessageId() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    CreationMessage message = CreationMessage.builder()
            .mailboxIds(ImmutableList.of("dead-beef-1337"))
            .subject("subject")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), message), ImmutableList.of(), session);

    // Then
    assertThat(result.getHeader().getFields("Message-ID")).extracting(Field::getBody)
            .isNotNull();
}
 
Example #9
Source File: MIMEMessageConverterTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
void convertToMimeShouldAddInReplyToHeaderWhenProvided() {
    // Given
    MIMEMessageConverter sut = new MIMEMessageConverter(attachmentContentLoader);

    String matchingMessageId = "unique-message-id";
    CreationMessage messageHavingInReplyTo = CreationMessage.builder()
            .from(DraftEmailer.builder().name("sender").build())
            .inReplyToMessageId(matchingMessageId)
            .mailboxIds(ImmutableList.of("dead-beef-1337"))
            .subject("subject")
            .build();

    // When
    Message result = sut.convertToMime(new ValueWithId.CreationMessageEntry(
            CreationMessageId.of("user|mailbox|1"), messageHavingInReplyTo), ImmutableList.of(), session);

    // Then
    assertThat(result.getHeader().getFields("In-Reply-To")).extracting(Field::getBody)
            .containsOnly(matchingMessageId);
}
 
Example #10
Source File: MboxReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
/** Process a single body part */
private boolean processBody(JCas jCas, Body body, String sourceUri) throws IOException {
  if (body instanceof TextBody) {
    // Process plain text body
    processTextBody(jCas, (TextBody) body);

    // Add fields from parent
    for (Field f : body.getParent().getHeader().getFields()) {
      addMetadata(jCas, f.getName(), f.getBody());
    }

    // Set up document annotation - this is done by the content extractor in other cases
    DocumentAnnotation doc = UimaSupport.getDocumentAnnotation(jCas);
    doc.setSourceUri(sourceUri);
    doc.setTimestamp(System.currentTimeMillis());
  } else if (body instanceof BinaryBody) {
    processBinaryBody(jCas, (BinaryBody) body, sourceUri);
  } else if (body instanceof Multipart) {
    // Multipart message, so recurse
    Multipart mp = (Multipart) body;
    return processMultipart(jCas, mp, sourceUri);
  } else {
    // No body processed
    return false;
  }

  return true;
}
 
Example #11
Source File: MessageViewFactory.java    From james-project with Apache License 2.0 5 votes vote down vote up
static ImmutableMap<String, String> toHeaderMap(List<Field> fields) {
    Function<Map.Entry<String, Collection<Field>>, String> bodyConcatenator = fieldListEntry -> fieldListEntry.getValue()
        .stream()
        .map(Field::getBody)
        .map(MimeUtil::unscrambleHeaderValue)
        .collect(Collectors.toList())
        .stream()
        .collect(Collectors.joining(JMAP_MULTIVALUED_FIELD_DELIMITER));

    return Multimaps.index(fields, Field::getName)
        .asMap()
        .entrySet()
        .stream()
        .collect(Guavate.toImmutableMap(Map.Entry::getKey, bodyConcatenator));
}
 
Example #12
Source File: MessageViewFactory.java    From james-project with Apache License 2.0 5 votes vote down vote up
static String getHeaderValue(org.apache.james.mime4j.dom.Message message, String header) {
    Field field = message.getHeader().getField(header);
    if (field == null) {
        return null;
    }
    return field.getBody();
}
 
Example #13
Source File: MessageParser.java    From james-project with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private <U extends ParsedField> Optional<U> castField(Field field, Class<U> clazz) {
    if (field == null || !clazz.isInstance(field)) {
        return Optional.empty();
    }
    return Optional.of((U) field);
}
 
Example #14
Source File: HeaderCollection.java    From james-project with Apache License 2.0 5 votes vote down vote up
public Builder add(Field field) {
    Preconditions.checkNotNull(field);
    String headerName = field.getName().toLowerCase(Locale.US);
    String rawHeaderValue = field.getBody();
    String sanitizedValue = MimeUtil.unscrambleHeaderValue(rawHeaderValue);

    headers.add(new Header(headerName, sanitizedValue));

    handleSpecificHeader(headerName, sanitizedValue, rawHeaderValue);
    return this;
}
 
Example #15
Source File: EnvelopeParser.java    From NioImapClient with Apache License 2.0 5 votes vote down vote up
public static Envelope parseHeader(Header header) {
  Map<String, String> envelopeFields = header.getFields().stream()
      .filter(f -> EnvelopeField.NAME_INDEX.containsKey(f.getName().toLowerCase()))
      .collect(Collectors.groupingBy(field -> field.getName().toLowerCase(),
          Collectors.mapping(Field::getBody, Collectors.joining(","))));

  Envelope.Builder envelope = new Envelope.Builder();

  String dateString = envelopeFields.get(EnvelopeField.DATE.getFieldName());
  List<ImapAddress> fromAddress = emailAddressesFromStringList(envelopeFields.get(EnvelopeField.FROM.getFieldName()));
  envelope.setDateString(dateString)
      .setSubject(envelopeFields.get(EnvelopeField.SUBJECT.getFieldName()))
      .setFrom(fromAddress)
      .setSender(emailAddressesFromStringList(envelopeFields.get(EnvelopeField.SENDER.getFieldName()), fromAddress))
      .setReplyTo(emailAddressesFromStringList(envelopeFields.get(EnvelopeField.REPLY_TO.getFieldName()), fromAddress))
      .setTo(emailAddressesFromStringList(envelopeFields.get(EnvelopeField.TO.getFieldName())))
      .setCc(emailAddressesFromStringList(envelopeFields.get(EnvelopeField.CC.getFieldName())))
      .setBcc(emailAddressesFromStringList(envelopeFields.get(EnvelopeField.BCC.getFieldName())))
      .setInReplyTo(envelopeFields.get(EnvelopeField.IN_REPLY_TO.getFieldName()))
      .setMessageId(envelopeFields.get(EnvelopeField.MESSAGE_ID.getFieldName()));

  try {
    if (!Strings.isNullOrEmpty(dateString)) {
      envelope.setDate(parseDate(dateString));
    }
  } catch (DateTimeParseException e) {
    LOGGER.debug("Failed to parse date {}", header.getField("date").getBody(), e);
  }
  return envelope.build();
}
 
Example #16
Source File: MboxReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
/** Process a multipart body part */
private boolean processMultipart(JCas jCas, Multipart mp, String sourceUri) throws IOException {
  boolean doneBody = false;

  for (Entity e : mp.getBodyParts()) {
    if (e.getFilename() != null) {
      // Part has a filename, and is therefore an attachment
      String extension = FilenameUtils.getExtension(e.getFilename()).toLowerCase();
      if (ignoreExtensionsList.contains(extension)) {
        getMonitor().info("Skipping attachment {}", e.getFilename());
        continue;
      }

      attachments.put(sourceUri + "/" + e.getFilename(), e.getBody());
    } else if (!doneBody) {
      // Part has no filename, and we've not already processed a part to use as a body
      processBody(jCas, e.getBody(), sourceUri);

      // Add metadata
      for (Field f : e.getParent().getHeader().getFields()) {
        addMetadata(jCas, f.getName(), f.getBody());
      }

      doneBody = true;
    }
  }

  return doneBody;
}
 
Example #17
Source File: EmailVerificationTest.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
private void assertGettingHeader(String header)
        throws MimeException, IOException {

    Email email = getParsedSimpleGmail();
    Header parsedHeader = email.getHeader();

    Field from = parsedHeader.getField(header);
    Assert.assertEquals(header, from.getName());
}
 
Example #18
Source File: Email.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
public String getFromEmailHeaderValue(){
	Field from = header.getField("From");	
	if (from != null) {			
		return from.getBody();
	}
	return null;
}
 
Example #19
Source File: Email.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
public String getCCEmailHeaderValue(){
	Field cc = header.getField("Cc");	
	if (cc != null) {
		return cc.getBody();
	}
	return null;
}
 
Example #20
Source File: Email.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
public String getToEmailHeaderValue() {
	Field to = header.getField("To");
	if (to != null) {
		return to.getBody();
	}
	return null;
}
 
Example #21
Source File: Email.java    From email-mime-parser with Apache License 2.0 5 votes vote down vote up
public String getEmailSubject(){
	Field subjectField = header.getField("Subject");	
	if (subjectField != null) {
		Field decodedSubjectField = new CustomUnstructuredFieldImpl(subjectField,DecodeMonitor.SILENT);
		return ((CustomUnstructuredFieldImpl)decodedSubjectField).getValue();
	}
	return null;
}
 
Example #22
Source File: MessageStoreImplAttachmentsTest.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
private void assertSaveMessageWithAttachments(Message msg, int num) throws IOException {
    store.save(msg);

    List<BodyPart> attList = new LinkedList<BodyPart>();
    MessageStoreImpl.recursiveMultipartProcessing((Multipart) msg.getBody(), new StringBuilder(), new StringBuilder(), false, attList); 
    @SuppressWarnings("unchecked")
    Queue<BodyPart> attachmentsMsg = (Queue<BodyPart>) attList;
    assertTrue("No attachments found", attachmentsMsg.size() > 0);
    assertEquals("", num, attachmentsMsg.size());
    
    final Resource r = resolver.getResource(getResourcePath(msg, store));
    assertNotNull("Expecting non-null Resource", r);
    for (Resource aRes : r.getChildren()) {
        final ModifiableValueMap aMap = aRes.adaptTo(ModifiableValueMap.class);
        BodyPart aMsg = attachmentsMsg.poll();
        assertNotNull("JCR contains more attachments", aMsg);

        for (Field f : aMsg.getHeader().getFields()) {
            String name = f.getName();
            assertEquals("Field "+name+" is different", (aMap.get(name, String.class)), f.getBody());
        }
        
        if (aMsg.getBody() instanceof TextBody) {
            assertEquals("Content is not the same", MessageStoreImpl.getTextPart(aMsg), aMap.get(CONTENT, String.class));
        } else if (aMsg.getBody() instanceof BinaryBody) {
            assertEquals("Content is not the same", getBinPart(aMsg), aMap.get(CONTENT, String.class));
        } else {
            fail("Unknown type of attachment body");
        }
    }
    assertEquals("Message contains more attachments", attachmentsMsg.poll(), null);
}
 
Example #23
Source File: MessageStoreImpl.java    From sling-samples with Apache License 2.0 5 votes vote down vote up
private static void parseHeaderToProps(Header hdr, Map<String, Object> props) {
    Set<String> processed = new HashSet<String>();
    for (Field f : hdr.getFields()) {
        String name = f.getName();
        if (!processed.contains(name)) {
            processed.add(name);
            String value = "";
            List<Field> fields = hdr.getFields(name);
            for (Field fl : fields) {
                value += fl.getBody()+FIELD_SEPARATOR;
            }
            props.put(name, value.substring(0, value.length()-FIELD_SEPARATOR.length()));
        }
    }
}
 
Example #24
Source File: RootMimePartContainerBuilder.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public MimePartContainerBuilder addToHeaders(Field field) {
    LOGGER.warn("Trying to add headers to the Root MimePart container");
    return this;
}
 
Example #25
Source File: MboxHandler.java    From SnowGraph with Apache License 2.0 4 votes vote down vote up
@Override
public void field(Field fieldData) throws MimeException {
    if (fieldData.toString().startsWith("Message-ID:") || fieldData.toString().startsWith("Message-Id:")) {
        mailInfo.id = fieldData.toString().substring(11).trim();
    } else if (fieldData.toString().startsWith("Subject:")) {
        mailInfo.subject = fieldData.toString().substring(8).trim();
    } else if (fieldData.toString().startsWith("In-Reply-To:")) {
        mailInfo.replyTo = fieldData.toString().substring(12).trim();
        if (mailInfo.replyTo.length() > 0)
            mailReplyMap.put(mailInfo.id, mailInfo.replyTo);
    } else if (fieldData.toString().startsWith("From:")) {
        mailInfo.from = fieldData.toString().substring(5).trim();
        Pair<String, String> senderPair = MailUtil.extractMailNameAndAddress(mailInfo.from);
        if (senderPair != null) {
            mailInfo.senderName = senderPair.getLeft();
            mailInfo.senderMail = senderPair.getRight();
        } else {// has no mail address, e.g., from="undisclosed-recipients:;"
            mailInfo.senderName = mailInfo.senderMail = mailInfo.from;
        }
    } else if (fieldData.toString().startsWith("To:")) {
        mailInfo.to = fieldData.toString().substring(3).trim();

        List<Pair<String, String>> mailPairs = MailUtil.extractMultiMailNameAndAddress(mailInfo.to);
        int mailNum = mailPairs.size();
        if (mailNum > 0) {
            mailInfo.receiverNames = new String[mailNum];
            mailInfo.receiverMails = new String[mailNum];

            for (int i = 0; i < mailNum; i++) {
                Pair<String, String> mailPair = mailPairs.get(i);
                mailInfo.receiverNames[i] = mailPair.getLeft();
                mailInfo.receiverMails[i] = mailPair.getRight();
            }
        } else {// has no mail address
            mailInfo.receiverNames = new String[]{};
            mailInfo.receiverMails = new String[]{};
        }
    } else if (fieldData.toString().startsWith("Date:")) {
        mailInfo.date = fieldData.toString().substring(5).trim();
    }
}
 
Example #26
Source File: MimePart.java    From james-project with Apache License 2.0 4 votes vote down vote up
@Override
public Builder addToHeaders(Field field) {
    headerCollectionBuilder.add(field);
    return this;
}
 
Example #27
Source File: MessageContentExtractorTest.java    From holdmail with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldHandleField() {

    Field fieldMock = mock(Field.class);
    when(fieldMock.getName()).thenReturn("fName");
    when(fieldMock.getBody()).thenReturn("fBody");

    MessageContentExtractor extractor = new MessageContentExtractor();

    extractor.startHeader(); // initialize a 'nextPotentialPart'
    extractor.field(fieldMock);

    MessageContentPart nextPart = extractor.getNextPotentialPart();
    assertThat(nextPart.getHeaders().get("fName")).isEqualTo(new HeaderValue("fBody"));

}
 
Example #28
Source File: CustomUnstructuredFieldImpl.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
public UnstructuredField parse(final Field rawField, final DecodeMonitor monitor) {
    return new CustomUnstructuredFieldImpl(rawField, monitor);
}
 
Example #29
Source File: CustomUnstructuredFieldImpl.java    From email-mime-parser with Apache License 2.0 4 votes vote down vote up
public CustomUnstructuredFieldImpl(Field rawField, DecodeMonitor monitor) {
    super(rawField, monitor);
}
 
Example #30
Source File: MessageContentExtractorTest.java    From holdmail with Apache License 2.0 3 votes vote down vote up
@Test
public void shouldNotHandleFieldIfNoPart() {

    MessageContentExtractor extractor = new MessageContentExtractor();

    extractor.field(mock(Field.class));
    assertThat(extractor.getNextPotentialPart()).isNull();

}