aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLucien Hansen2013-06-26 05:16:23 (EDT)
committerKen Lee2013-06-26 05:30:53 (EDT)
commit05f333871bcc8579b468d984803473effce5322a (patch)
tree9972723f162ad8bc10ae1adbc7747b52e89c7a43
parentcfb0cc0ca5c40e6b2aa11829b0b7ff1a25425b0c (diff)
downloadorg.eclipse.scout.rt-05f333871bcc8579b468d984803473effce5322a.zip
org.eclipse.scout.rt-05f333871bcc8579b468d984803473effce5322a.tar.gz
org.eclipse.scout.rt-05f333871bcc8579b468d984803473effce5322a.tar.bz2
Bug 410780 - MailUtility: extract mailMessage from word archiverefs/changes/69/14069/2
https://bugs.eclipse.org/bugs/show_bug.cgi?id=410780 (cherry picked from commit 6ffe1d0099fec7f3d3e4aab806e4a9fbf16c7fa0) Change-Id: I137e3b268dd7e099ed97916c4d6d51d3793f0b95 Reviewed-on: https://git.eclipse.org/r/14069 Tested-by: Hudson CI Reviewed-by: Ken Lee <kle@bsiag.com> IP-Clean: Ken Lee <kle@bsiag.com>
-rw-r--r--org.eclipse.scout.commons/src/org/eclipse/scout/commons/MailUtility.java50
1 files changed, 34 insertions, 16 deletions
diff --git a/org.eclipse.scout.commons/src/org/eclipse/scout/commons/MailUtility.java b/org.eclipse.scout.commons/src/org/eclipse/scout/commons/MailUtility.java
index 0019b86..fdd02cb 100644
--- a/org.eclipse.scout.commons/src/org/eclipse/scout/commons/MailUtility.java
+++ b/org.eclipse.scout.commons/src/org/eclipse/scout/commons/MailUtility.java
@@ -13,11 +13,9 @@ package org.eclipse.scout.commons;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
-import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
@@ -366,16 +364,14 @@ public final class MailUtility {
String txt = null;
File plainTextFile = new File(dir, simpleName + "." + fileType);
if (plainTextFile.exists() && plainTextFile.canRead()) {
- Reader reader = new FileReader(plainTextFile);
- txt = IOUtility.getContent(reader);
- reader.close();
+ txt = getContentFromFile(plainTextFile, "UTF-8");
}
return txt;
}
/**
* Create {@link MimeMessage} from plain text fields.
- *
+ *
* @rn aho, 19.01.2009
*/
public static MimeMessage createMimeMessage(String[] toRecipients, String sender, String subject, String bodyTextPlain, DataSource[] attachements) throws ProcessingException {
@@ -384,7 +380,7 @@ public final class MailUtility {
/**
* Create {@link MimeMessage} from plain text fields.
- *
+ *
* @rn aho, 19.01.2009
*/
public static MimeMessage createMimeMessage(String[] toRecipients, String[] ccRecipients, String[] bccRecipients, String sender, String subject, String bodyTextPlain, DataSource[] attachements) throws ProcessingException {
@@ -453,9 +449,7 @@ public final class MailUtility {
String plainTextMessage = null;
boolean hasPlainText = false;
if (plainTextFile.exists()) {
- Reader reader = new FileReader(plainTextFile);
- plainTextMessage = IOUtility.getContent(new InputStreamReader(new FileInputStream(plainTextFile), "UTF-8"));
- reader.close();
+ plainTextMessage = getContentFromFile(plainTextFile, "UTF-8");
hasPlainText = StringUtility.hasText(plainTextMessage);
}
@@ -484,9 +478,7 @@ public final class MailUtility {
String htmlMessage = null;
boolean hasHtml = false;
if (htmlFile.exists()) {
- Reader reader = new FileReader(htmlFile);
- htmlMessage = IOUtility.getContent(new InputStreamReader(new FileInputStream(htmlFile), "UTF-8"));
- reader.close();
+ htmlMessage = getContentFromFile(htmlFile, "UTF-8");
// replace directory entry
// replace all paths to the 'files directory' with the root directory
htmlMessage = htmlMessage.replaceAll("\"" + folderName + "/", "\"cid:");
@@ -556,14 +548,40 @@ public final class MailUtility {
}
return mimeMessage;
}
- catch (IOException e) {
- throw new ProcessingException("Error occured while accessing files", e);
- }
catch (MessagingException e) {
throw new ProcessingException("Error occured while creating MIME-message", e);
}
}
+ /**
+ * Reads the content of a file in the specified encoding (charset-name) e.g. "UTF-8"
+ * If no encoding is provided, the system default encoding is used
+ */
+ String getContentFromFile(File htmlFile, String encoding) throws ProcessingException {
+ try {
+ FileInputStream in = null;
+ String content = null;
+ try {
+ in = new FileInputStream(htmlFile);
+ if (StringUtility.hasText(encoding)) {
+ content = IOUtility.getContent(new InputStreamReader(in, encoding));
+ }
+ else {
+ content = IOUtility.getContent(new InputStreamReader(in));
+ }
+ }
+ finally {
+ if (in != null) {
+ in.close();
+ }
+ }
+ return content;
+ }
+ catch (IOException e) {
+ throw new ProcessingException(e.getMessage(), e);
+ }
+ }
+
private String removeWordTags(String htmlMessage) {
// remove special/unused files
htmlMessage = htmlMessage.replaceAll("<link rel=File-List href=\"cid:filelist.xml\">", "");