Search in sources :

Example 1 with Message

use of org.apache.james.mime4j.dom.Message in project gerrit by GerritCodeReview.

the class RawMailParser method parse.

/**
   * Parses a MailMessage from a string.
   *
   * @param raw String as received over the wire
   * @return Parsed MailMessage
   * @throws MailParsingException
   */
public static MailMessage parse(String raw) throws MailParsingException {
    MailMessage.Builder messageBuilder = MailMessage.builder();
    messageBuilder.rawContentUTF(raw);
    Message mimeMessage;
    try {
        MessageBuilder builder = new DefaultMessageBuilder();
        mimeMessage = builder.parseMessage(new ByteArrayInputStream(raw.getBytes(UTF_8)));
    } catch (IOException | MimeException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    // Add general headers
    if (mimeMessage.getMessageId() != null) {
        messageBuilder.id(mimeMessage.getMessageId());
    }
    if (mimeMessage.getSubject() != null) {
        messageBuilder.subject(mimeMessage.getSubject());
    }
    messageBuilder.dateReceived(new DateTime(mimeMessage.getDate()));
    // Add From, To and Cc
    if (mimeMessage.getFrom() != null && mimeMessage.getFrom().size() > 0) {
        Mailbox from = mimeMessage.getFrom().get(0);
        messageBuilder.from(new Address(from.getName(), from.getAddress()));
    }
    if (mimeMessage.getTo() != null) {
        for (Mailbox m : mimeMessage.getTo().flatten()) {
            messageBuilder.addTo(new Address(m.getName(), m.getAddress()));
        }
    }
    if (mimeMessage.getCc() != null) {
        for (Mailbox m : mimeMessage.getCc().flatten()) {
            messageBuilder.addCc(new Address(m.getName(), m.getAddress()));
        }
    }
    // Add additional headers
    mimeMessage.getHeader().getFields().stream().filter(f -> !MAIN_HEADERS.contains(f.getName().toLowerCase())).forEach(f -> messageBuilder.addAdditionalHeader(f.getName() + ": " + f.getBody()));
    // Add text and html body parts
    StringBuilder textBuilder = new StringBuilder();
    StringBuilder htmlBuilder = new StringBuilder();
    try {
        handleMimePart(mimeMessage, textBuilder, htmlBuilder);
    } catch (IOException e) {
        throw new MailParsingException("Can't parse email", e);
    }
    messageBuilder.textContent(Strings.emptyToNull(textBuilder.toString()));
    messageBuilder.htmlContent(Strings.emptyToNull(htmlBuilder.toString()));
    try {
        // required attributes are missing, so that the caller doesn't fall over.
        return messageBuilder.build();
    } catch (IllegalStateException e) {
        throw new MailParsingException("Missing required attributes after email was parsed", e);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) UTF_8(java.nio.charset.StandardCharsets.UTF_8) Message(org.apache.james.mime4j.dom.Message) MimeException(org.apache.james.mime4j.MimeException) DateTime(org.joda.time.DateTime) IOException(java.io.IOException) TextBody(org.apache.james.mime4j.dom.TextBody) Ints(com.google.common.primitives.Ints) InputStreamReader(java.io.InputStreamReader) Multipart(org.apache.james.mime4j.dom.Multipart) Strings(com.google.common.base.Strings) Address(com.google.gerrit.server.mail.Address) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) ImmutableList(com.google.common.collect.ImmutableList) ByteArrayInputStream(java.io.ByteArrayInputStream) CharStreams(com.google.common.io.CharStreams) Entity(org.apache.james.mime4j.dom.Entity) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) Message(org.apache.james.mime4j.dom.Message) Address(com.google.gerrit.server.mail.Address) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) IOException(java.io.IOException) DateTime(org.joda.time.DateTime) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Mailbox(org.apache.james.mime4j.dom.address.Mailbox) ByteArrayInputStream(java.io.ByteArrayInputStream) MimeException(org.apache.james.mime4j.MimeException)

Example 2 with Message

use of org.apache.james.mime4j.dom.Message in project sling by apache.

the class Mime4jMboxParserImplCountTest method testParse.

@Test
public void testParse() throws IOException {
    Iterator<Message> iter = parser.parse(new FileInputStream(new File(TU.TEST_FOLDER, filePath)));
    int cnt = 0;
    Set<Message> set = new HashSet<Message>();
    while (iter.hasNext()) {
        Message message = (Message) iter.next();
        cnt++;
        set.add(message);
    }
    assertEquals("Expecting correct number of messages parsed", expectedMessagesCount, cnt);
    assertEquals("Expecting all messages unique", expectedMessagesCount, set.size());
}
Also used : Message(org.apache.james.mime4j.dom.Message) File(java.io.File) FileInputStream(java.io.FileInputStream) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Message

use of org.apache.james.mime4j.dom.Message in project sling by apache.

the class Mime4jMboxParserImplTest method testMboxParsing.

@Test
public void testMboxParsing() throws IOException {
    Iterator<Message> iter = parser.parse(new FileInputStream(new File(TU.TEST_FOLDER, WRONGBODY_MBOX)));
    boolean fail = true;
    int i = 1;
    while (iter.hasNext()) {
        final Message message = iter.next();
        File bodyFile = new File(TU.TEST_FOLDER, specialPathFromFilePath(WRONGBODY_MBOX, "_bodyOf" + i, "txt"));
        if (bodyFile.exists()) {
            final String actual = getPlainBody(message);
            final String expected = readTextFile(bodyFile);
            assertEquals("Body #" + i, expected, actual);
            fail = false;
        }
        i++;
    }
    if (fail) {
        fail("No file with expected body.");
    }
}
Also used : Message(org.apache.james.mime4j.dom.Message) File(java.io.File) MessageStoreImplRepositoryTestUtil.readTextFile(org.apache.sling.mailarchiveserver.impl.MessageStoreImplRepositoryTestUtil.readTextFile) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 4 with Message

use of org.apache.james.mime4j.dom.Message in project Gargoyle by callakrsos.

the class MimeToHtmlAdapter method getContent.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.kyj.fx.voeditor.visual.framework.word.AbstractMimeAdapter#getContent(
	 * )
	 */
@Override
public String getContent() {
    StringBuilder sb = new StringBuilder();
    try {
        final MessageBuilder builder = new DefaultMessageBuilder();
        Message message = builder.parseMessage(new ByteArrayInputStream(this.content));
        Body body = ((Entity) message).getBody();
        append(sb, body);
    } catch (MimeException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) StringBuilder(scala.collection.mutable.StringBuilder) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Message(org.apache.james.mime4j.dom.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody)

Example 5 with Message

use of org.apache.james.mime4j.dom.Message in project Gargoyle by callakrsos.

the class MimeToHtmlWordAdapter method getContent.

/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.kyj.fx.voeditor.visual.framework.word.AbstractMimeAdapter#getContent(
	 * )
	 */
@Override
public String getContent() {
    StringBuilder sb = new StringBuilder();
    try {
        final MessageBuilder builder = new DefaultMessageBuilder();
        Message message = builder.parseMessage(new ByteArrayInputStream(this.content));
        Body body = ((Entity) message).getBody();
        append(sb, body);
    } catch (MimeException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return sb.toString();
}
Also used : Entity(org.apache.james.mime4j.dom.Entity) StringBuilder(scala.collection.mutable.StringBuilder) MessageBuilder(org.apache.james.mime4j.dom.MessageBuilder) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) Message(org.apache.james.mime4j.dom.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultMessageBuilder(org.apache.james.mime4j.message.DefaultMessageBuilder) MimeException(org.apache.james.mime4j.MimeException) IOException(java.io.IOException) BinaryBody(org.apache.james.mime4j.dom.BinaryBody) Body(org.apache.james.mime4j.dom.Body) TextBody(org.apache.james.mime4j.dom.TextBody)

Aggregations

Message (org.apache.james.mime4j.dom.Message)9 MessageBuilder (org.apache.james.mime4j.dom.MessageBuilder)5 DefaultMessageBuilder (org.apache.james.mime4j.message.DefaultMessageBuilder)5 Entity (org.apache.james.mime4j.dom.Entity)4 TextBody (org.apache.james.mime4j.dom.TextBody)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 IOException (java.io.IOException)3 MimeException (org.apache.james.mime4j.MimeException)3 BinaryBody (org.apache.james.mime4j.dom.BinaryBody)3 Body (org.apache.james.mime4j.dom.Body)3 MessageStoreImplRepositoryTestUtil.readTextFile (org.apache.sling.mailarchiveserver.impl.MessageStoreImplRepositoryTestUtil.readTextFile)2 Test (org.junit.Test)2 StringBuilder (scala.collection.mutable.StringBuilder)2 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 CharStreams (com.google.common.io.CharStreams)1 Ints (com.google.common.primitives.Ints)1