Skip to content

Commit

Permalink
[#1897] Added method play.mvc.Mailer.attachInlineEmbed(DataSource
Browse files Browse the repository at this point in the history
dataSource, String name)
  • Loading branch information
tazmaniax authored and xael-fry committed Jan 13, 2015
1 parent 92d518c commit 3d8c9ce
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions framework/src/play/mvc/Mailer.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,25 @@ public static void attachDataSource(DataSource dataSource, String name, String d
attachDataSource(dataSource, name, description, EmailAttachment.ATTACHMENT);
}

public static String attachInlineEmbed(DataSource dataSource, String name) {
HashMap<String, Object> map = infos.get();
if (map == null) {
throw new UnexpectedException("Mailer not instrumented ?");
}

InlineImage ii = new InlineImage(dataSource);

Map<String, InlineImage> inlineEmbeds = (Map<String, InlineImage>) map.get("inlineEmbeds");
if (inlineEmbeds == null) {
inlineEmbeds = new HashMap<String, InlineImage>();
map.put("inlineEmbeds", inlineEmbeds);
}

inlineEmbeds.put(name, ii);
infos.set(map);

return "cid:" + ii.cid;
}

public static void setContentType(String contentType) {
HashMap<String, Object> map = infos.get();
Expand Down Expand Up @@ -170,9 +189,13 @@ private static class InlineImage {
/** <code>DataSource</code> for the content */
private final DataSource dataSource;

public InlineImage(DataSource dataSource) {
this(null, dataSource);
}

public InlineImage(String cid, DataSource dataSource) {
super();
this.cid = cid;
this.cid = cid != null ? cid : RandomStringUtils.randomAlphabetic(HtmlEmail.CID_LENGTH).toLowerCase();
this.dataSource = dataSource;
}

Expand Down Expand Up @@ -247,8 +270,7 @@ public static String getEmbedddedSrc(String urlString, String name) {
try {
url = new URL(urlString);
} catch (MalformedURLException e1) {
throw new UnexpectedException(
"Invalid URL '" + urlString + "'", e1);
throw new UnexpectedException("Invalid URL '" + urlString + "'", e1);
}

if (name == null || name.isEmpty()) {
Expand All @@ -262,7 +284,7 @@ public static String getEmbedddedSrc(String urlString, String name) {

dataSource = url.getProtocol().equals("file") ? new VirtualFileDataSource(
url.getFile()) : new URLDataSource(url);
}else{
} else {
dataSource = new VirtualFileDataSource(img);
}

Expand Down Expand Up @@ -309,18 +331,7 @@ public static String getEmbedddedSrc(String urlString, String name) {
IOUtils.closeQuietly(is);
}

String cid = RandomStringUtils.randomAlphabetic(HtmlEmail.CID_LENGTH)
.toLowerCase();
InlineImage ii = new InlineImage(cid, dataSource);

if (inlineEmbeds == null) {
inlineEmbeds = new HashMap<String, InlineImage>();
map.put("inlineEmbeds", inlineEmbeds);
}
inlineEmbeds.put(name, ii);
infos.set(map);

return "cid:" + cid;
return attachInlineEmbed(dataSource, name);
}

/**
Expand Down

0 comments on commit 3d8c9ce

Please sign in to comment.