Subject: refinement[bgz_349095]: Improve text parsing and ASCII encoding when creating mail messages

From: Shawn F. Cook
Date: Fri, 10 Jun 2011 16:00:49 -0700

---
 .../src/org/eclipse/osee/mail/MailUtils.java       | 40 ++++++++++++++++++++--
 1 file changed, 37 insertions(+), 3 deletions(-)

(limited to 'plugins/org.eclipse.osee.mail')

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 {
-- 
cgit v1.2.3