Skip to content

Commit

Permalink
#202: fixed concurrent modification exception when moving invalid emb…
Browse files Browse the repository at this point in the history
…edded images to regular attachments list (big thanks to @nedashley!)
  • Loading branch information
bbottema committed Apr 24, 2019
1 parent 405d3f7 commit e3d9069
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
Expand Down Expand Up @@ -447,20 +448,21 @@ public static String parseMessageId(@Nonnull final MimeMessage mimeMessage) {
}
}

private static void moveInvalidEmbeddedResourcesToAttachments(ParsedMimeMessageComponents parsedComponents) {
static void moveInvalidEmbeddedResourcesToAttachments(ParsedMimeMessageComponents parsedComponents) {
final String htmlContent = parsedComponents.htmlContent;
for (Map.Entry<String, DataSource> cidEntry : parsedComponents.cidMap.entrySet()) {
for(Iterator<Map.Entry<String, DataSource>> it = parsedComponents.cidMap.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<String, DataSource> cidEntry = it.next();
String cid = extractCID(cidEntry.getKey());
if (htmlContent == null || !htmlContent.contains("cid:" + cid)) {
parsedComponents.attachmentList.put(cid, cidEntry.getValue());
parsedComponents.cidMap.remove(cidEntry.getKey());
it.remove();
}
}
}

public static class ParsedMimeMessageComponents {
private final Map<String, DataSource> attachmentList = new TreeMap<>();
private final Map<String, DataSource> cidMap = new TreeMap<>();
final Map<String, DataSource> attachmentList = new TreeMap<>();
final Map<String, DataSource> cidMap = new TreeMap<>();
private final Map<String, Object> headers = new HashMap<>();
private final List<InternetAddress> toAddresses = new ArrayList<>();
private final List<InternetAddress> ccAddresses = new ArrayList<>();
Expand All @@ -473,7 +475,7 @@ public static class ParsedMimeMessageComponents {
private InternetAddress returnReceiptTo;
private InternetAddress bounceToAddress;
private String plainContent;
private String htmlContent;
String htmlContent;

public String getMessageId() {
return messageId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.simplejavamail.converter.internal.mimemessage;

import org.junit.Test;
import org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.ParsedMimeMessageComponents;

import javax.mail.util.ByteArrayDataSource;
import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.simplejavamail.converter.internal.mimemessage.MimeMessageParser.moveInvalidEmbeddedResourcesToAttachments;

public class MimeMessageParserTest {
@Test
public void testMoveInvalidEmbeddedResourcesToAttachments_NoHtmlNoInvalid() throws IOException {
ParsedMimeMessageComponents parsedComponents = new ParsedMimeMessageComponents();
parsedComponents.cidMap.put("moo1", new ByteArrayDataSource("moomoo", "text/plain"));
parsedComponents.cidMap.put("moo2", new ByteArrayDataSource("moomoo", "text/plain"));
moveInvalidEmbeddedResourcesToAttachments(parsedComponents);

assertThat(parsedComponents.cidMap).isEmpty();
assertThat(parsedComponents.attachmentList).containsOnlyKeys("moo1","moo2");
}
@Test
public void testMoveInvalidEmbeddedResourcesToAttachments_HtmlButNoInvalid() throws IOException {
ParsedMimeMessageComponents parsedComponents = new ParsedMimeMessageComponents();
parsedComponents.htmlContent = "blah moo1 blah html";
parsedComponents.cidMap.put("moo1", new ByteArrayDataSource("moomoo", "text/plain"));
parsedComponents.cidMap.put("moo2", new ByteArrayDataSource("moomoo", "text/plain"));
moveInvalidEmbeddedResourcesToAttachments(parsedComponents);

assertThat(parsedComponents.cidMap).isEmpty();
assertThat(parsedComponents.attachmentList).containsOnlyKeys("moo1","moo2");
}

@Test
public void testMoveInvalidEmbeddedResourcesToAttachments_Invalid() throws IOException {
ParsedMimeMessageComponents parsedComponents = new ParsedMimeMessageComponents();
parsedComponents.htmlContent = "blah cid:moo1 blah html";
parsedComponents.cidMap.put("moo1", new ByteArrayDataSource("moomoo", "text/plain"));
parsedComponents.cidMap.put("moo2", new ByteArrayDataSource("moomoo", "text/plain"));
moveInvalidEmbeddedResourcesToAttachments(parsedComponents);

assertThat(parsedComponents.cidMap).containsOnlyKeys("moo1");
assertThat(parsedComponents.attachmentList).containsOnlyKeys("moo2");
}
}

0 comments on commit e3d9069

Please sign in to comment.