diff options
author | Shawn F. Cook | 2011-06-10 23:00:49 +0000 |
---|---|---|
committer | Ryan D. Brooks | 2011-06-10 23:00:49 +0000 |
commit | f87c1f344defb77e247aba1acbeb621ef7d999f7 (patch) | |
tree | c46a249cf3a822b9411e63dc1586067445db7731 | |
parent | bf75f71ec74cd3e6771f7b6769460a8655fd0a1e (diff) | |
download | org.eclipse.osee-f87c1f344defb77e247aba1acbeb621ef7d999f7.tar.gz org.eclipse.osee-f87c1f344defb77e247aba1acbeb621ef7d999f7.tar.xz org.eclipse.osee-f87c1f344defb77e247aba1acbeb621ef7d999f7.zip |
refinement[bgz_349095]: Improve text parsing and ASCII encoding when creating mail messages
-rw-r--r-- | plugins/org.eclipse.osee.mail.test/src/org/eclipse/osee/mail/MailUtilsContainsAnyTest.java | 7 | ||||
-rw-r--r-- | plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java | 40 |
2 files changed, 44 insertions, 3 deletions
diff --git a/plugins/org.eclipse.osee.mail.test/src/org/eclipse/osee/mail/MailUtilsContainsAnyTest.java b/plugins/org.eclipse.osee.mail.test/src/org/eclipse/osee/mail/MailUtilsContainsAnyTest.java index dbd7321ec4f..8051376e86a 100644 --- a/plugins/org.eclipse.osee.mail.test/src/org/eclipse/osee/mail/MailUtilsContainsAnyTest.java +++ b/plugins/org.eclipse.osee.mail.test/src/org/eclipse/osee/mail/MailUtilsContainsAnyTest.java @@ -7,6 +7,7 @@ package org.eclipse.osee.mail; import java.util.ArrayList; import java.util.Collection; +import org.junit.Assert; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @@ -28,6 +29,12 @@ public class MailUtilsContainsAnyTest { this.indexOfFirstInstance = indexOfFirstInstance; } + @org.junit.Test + public void testContainsAny() { + int ret = MailUtils.containsAny(testStr, chars); + Assert.assertEquals(indexOfFirstInstance, ret); + } + @Parameters public static Collection<Object[]> getData() { Collection<Object[]> data = new ArrayList<Object[]>(); diff --git a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java index c85a34c0c24..8c12ac75474 100644 --- a/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java +++ b/plugins/org.eclipse.osee.mail/src/org/eclipse/osee/mail/MailUtils.java @@ -47,17 +47,51 @@ public final class MailUtils { //The String.format can handle the '%' character. public static DataSource createFromString(String name, String message, Object... args) { - String data = String.format(message, args); + //The '%' has special meaning as a format specifier to the + // String::format() function. Because of this we replace + // the '%' with its unicode representation. + String msgWReplacedChar = message.replace("%", "\\u0025"); + String data = String.format(msgWReplacedChar, args); StringDataSource dataSource = new StringDataSource(name, data); dataSource.setCharset("UTF-8"); dataSource.setContentType("text/plain"); return dataSource; } + //returns -1 if searchChars are NOT found. + // Else it returns the index (>=0) into str where the first searchChar is found. + public static int containsAny(String str, char[] searchChars) { + if (str == null || str.length() == 0 || searchChars == null || searchChars.length == 0) { + return -1; + } + for (int i = 0; i < str.length(); i++) { + char ch = str.charAt(i); + for (int j = 0; j < searchChars.length; j++) { + if (searchChars[j] == ch) { + return i; + } + } + } + return -1; + } + public static DataSource createOutlookEvent(String location, String event, Date date, String startTime, String endTime) { OutlookCalendarEvent calendarEvent = new OutlookCalendarEvent(location, event, date, startTime, endTime); - String attachmentName = String.format("%s.vcs", event); - return createFromString(calendarEvent.getEvent(), attachmentName); + + //The event string is used as a file name and therefore must be + // validated and its contents restricted to valid filename characters. + // Invalid Windows characters based on - http://msdn.microsoft.com/en-us/library/aa365247%28v=vs.85%29.aspx#naming_conventions + char[] illegalChars_Windows = {'<', '>', ':', '\"', '/', '\\', '|', '?', '*'}; + int charIndex = containsAny(event, illegalChars_Windows); + if (charIndex >= 0) { + System.out.println("Illegal Windows character found in event string: " + event.substring(0, charIndex + 1) + "<--"); + return null; + } + + StringBuilder strbld = new StringBuilder(event); + strbld.append(".vcs"); + //System.out.println("event:" + event + " strbld:" + strbld.toString()); + return createFromString(strbld.toString(), calendarEvent.getEvent()); } public static DataSource createFromHtml(final String name, String htmlData) throws MessagingException { |