[seam-commits] Seam SVN: r8563 - in branches/Seam_2_0_FP: examples/mail/src/org/jboss/seam/example/mail/test and 10 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Mon Aug 4 10:15:41 EDT 2008


Author: manaRH
Date: 2008-08-04 10:15:41 -0400 (Mon, 04 Aug 2008)
New Revision: 8563

Added:
   branches/Seam_2_0_FP/examples/mail/src/org/jboss/seam/example/mail/test/sanitization.xhtml
   branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/Header.java
   branches/Seam_2_0_FP/src/test/mail/
   branches/Seam_2_0_FP/src/test/mail/unit/
   branches/Seam_2_0_FP/src/test/mail/unit/org/
   branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/
   branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/
   branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/
   branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/
   branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/unit/
   branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/unit/HeaderTest.java
   branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/unit/testng.xml
Modified:
   branches/Seam_2_0_FP/build/mail.pom.xml
   branches/Seam_2_0_FP/examples/mail/src/org/jboss/seam/example/mail/test/MailTest.java
   branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/AddressComponent.java
   branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIAttachment.java
   branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIBody.java
   branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIHeader.java
   branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIMessage.java
   branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UISubject.java
Log:
Backport of JBSEAM-3071

Modified: branches/Seam_2_0_FP/build/mail.pom.xml
===================================================================
--- branches/Seam_2_0_FP/build/mail.pom.xml	2008-08-04 01:22:44 UTC (rev 8562)
+++ branches/Seam_2_0_FP/build/mail.pom.xml	2008-08-04 14:15:41 UTC (rev 8563)
@@ -59,6 +59,19 @@
       <artifactId>el-api</artifactId>
       <scope>provided</scope>
     </dependency>
+    
+    <dependency>
+      <groupId>org.testng</groupId>
+      <artifactId>testng</artifactId>
+      <version>5.6</version>
+      <optional>true</optional>
+      <exclusions>
+        <exclusion>
+          <groupId>junit</groupId>
+          <artifactId>junit</artifactId>
+        </exclusion>
+      </exclusions>
+    </dependency>
 
   </dependencies>
 

Modified: branches/Seam_2_0_FP/examples/mail/src/org/jboss/seam/example/mail/test/MailTest.java
===================================================================
--- branches/Seam_2_0_FP/examples/mail/src/org/jboss/seam/example/mail/test/MailTest.java	2008-08-04 01:22:44 UTC (rev 8562)
+++ branches/Seam_2_0_FP/examples/mail/src/org/jboss/seam/example/mail/test/MailTest.java	2008-08-04 14:15:41 UTC (rev 8563)
@@ -528,6 +528,39 @@
     }
     
     @Test
+    public void testSanitization() throws Exception
+    {
+        
+        new FacesRequest()
+        {
+            @Override
+            protected void updateModelValues() throws Exception
+            {
+                
+            }
+            
+            @Override
+            protected void invokeApplication() throws Exception
+            { 
+                Contexts.getEventContext().set("name", "Pete\nMuir");   
+                MimeMessage renderedMessage = getRenderedMailMessage("/org/jboss/seam/example/mail/test/sanitization.xhtml");
+                assert "Try out".equals(renderedMessage.getSubject());
+                InternetAddress to = (InternetAddress) renderedMessage.getAllRecipients()[0];
+                assert to.getAddress().equals("peter at email.tld");
+                assert to.getPersonal().equals("Pete");
+                assert renderedMessage.getFrom().length == 1;
+                assert renderedMessage.getFrom()[0] instanceof InternetAddress;
+                InternetAddress from = (InternetAddress) renderedMessage.getFrom()[0];
+                assert from.getAddress().equals("peter at example.com");
+                assert from.getPersonal().equals("Pete");
+                assert renderedMessage.getHeader("Pete") != null;
+                assert renderedMessage.getHeader("Pete").length == 1;
+                assert "roll".equals(renderedMessage.getHeader("Pete")[0]);
+            }
+        }.run();
+    }
+    
+    @Test
     public void testTemplating() throws Exception
     {
         

Added: branches/Seam_2_0_FP/examples/mail/src/org/jboss/seam/example/mail/test/sanitization.xhtml
===================================================================
--- branches/Seam_2_0_FP/examples/mail/src/org/jboss/seam/example/mail/test/sanitization.xhtml	                        (rev 0)
+++ branches/Seam_2_0_FP/examples/mail/src/org/jboss/seam/example/mail/test/sanitization.xhtml	2008-08-04 14:15:41 UTC (rev 8563)
@@ -0,0 +1,14 @@
+<m:message xmlns="http://www.w3.org/1999/xhtml"
+           xmlns:m="http://jboss.com/products/seam/mail"
+           xmlns:h="http://java.sun.com/jsf/html">
+    
+          <m:from name="#{name}">peter at example.com
+foo</m:from>
+          <m:to name="#{name}">peter at email.tld
+bar</m:to>
+          <m:subject>Try out
+Seam!</m:subject>
+          <m:header name="#{name}">roll
+over</m:header>
+          <m:body>A mail message to test sanitization</m:body>
+</m:message>
\ No newline at end of file


Property changes on: branches/Seam_2_0_FP/examples/mail/src/org/jboss/seam/example/mail/test/sanitization.xhtml
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/AddressComponent.java
===================================================================
--- branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/AddressComponent.java	2008-08-04 01:22:44 UTC (rev 8562)
+++ branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/AddressComponent.java	2008-08-04 14:15:41 UTC (rev 8563)
@@ -25,15 +25,16 @@
    protected InternetAddress getInternetAddress(FacesContext facesContext) throws IOException, AddressException 
    {
       InternetAddress address = new InternetAddress();
-      address.setAddress(getAddress() != null ? getAddress() : encode(facesContext));
+      address.setAddress(new Header(getAddress() != null ? getAddress() : encode(facesContext)).getSanitizedValue());
       String charset = findMessage().getCharset();
+      String personal = new Header(getName()).getSanitizedValue();
       if (charset == null)
       {
-         address.setPersonal(getName());
+         address.setPersonal(personal);
       }
       else
       {
-         address.setPersonal(getName(), charset);
+         address.setPersonal(personal, charset);
       }
       address.validate();
       return address;
@@ -70,7 +71,7 @@
     */
    public String getName()
    {
-      if (address == null)
+      if (name == null)
       {
          return getString("name");
       } 

Added: branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/Header.java
===================================================================
--- branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/Header.java	                        (rev 0)
+++ branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/Header.java	2008-08-04 14:15:41 UTC (rev 8563)
@@ -0,0 +1,82 @@
+package org.jboss.seam.mail.ui;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.StringReader;
+
+import javax.mail.internet.MimeUtility;
+
+public class Header
+{
+   
+   private final String name;
+   private final String value;
+   
+   private String sanitizedName;
+   private String sanitizedValue;
+   
+   public Header(String name, String value)
+   {
+      this.name = name;
+      this.value = value;
+   }
+   
+   public Header(String value)
+   {
+      this.value = value;
+      this.name = null;
+   }
+   
+   public String getSanitizedName()
+   {
+      if (sanitizedName == null && name != null)
+      {
+         try
+         {
+            sanitizedName = sanitize(name);
+         }
+         catch (IOException e)
+         {
+            throw new IllegalStateException("Error santizing Header name " + name, e);
+         }
+      }
+      return sanitizedName;
+   }
+   
+   public String getSanitizedValue()
+   {
+      if (sanitizedValue == null && value != null)
+      {
+         try
+         {
+            sanitizedValue = sanitizeValue(value);
+         }
+         catch (IOException e)
+         {
+            throw new IllegalStateException("Error santizing Header " + name + " value " + value, e);
+         }
+      }
+      return sanitizedValue;
+   }
+   
+   /**
+    * Remove any line feed/new line characters
+    * @throws IOException 
+    */
+   public static String sanitize(String value) throws IOException
+   {
+      BufferedReader  reader = new BufferedReader(new StringReader(value));
+      return reader.readLine();
+   }
+   
+   /**
+    * Remove any line feed/new line characters from a (possibly) folded header
+    * @throws IOException 
+    */
+   public static String sanitizeValue(String value) throws IOException
+   {
+      // The user might have folded the header (stupid SMTP idiocy)
+      return sanitize(MimeUtility.unfold(value));
+   }
+
+}


Property changes on: branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/Header.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain

Modified: branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIAttachment.java
===================================================================
--- branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIAttachment.java	2008-08-04 01:22:44 UTC (rev 8562)
+++ branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIAttachment.java	2008-08-04 14:15:41 UTC (rev 8563)
@@ -163,10 +163,10 @@
             MimeBodyPart attachment = new MimeBodyPart();
             // Need to manually set the contentid
             String contentId = RandomStringUtils.randomAlphabetic(20).toLowerCase();
-            attachment.setContentID("<" + contentId + ">");
+            attachment.setContentID(new Header("<" + contentId + ">").getSanitizedValue());
             attachment.setDataHandler(new DataHandler(ds));
-            attachment.setFileName(getName(ds.getName()));
-            attachment.setDisposition(getDisposition());
+            attachment.setFileName(new Header(getName(ds.getName())).getSanitizedValue());
+            attachment.setDisposition(new Header(getDisposition()).getSanitizedValue());
             super.getRootMultipart().addBodyPart(attachment);
             if (getStatus() != null)
             {

Modified: branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIBody.java
===================================================================
--- branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIBody.java	2008-08-04 01:22:44 UTC (rev 8562)
+++ branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIBody.java	2008-08-04 14:15:41 UTC (rev 8563)
@@ -88,13 +88,13 @@
             throws MessagingException
    {
       MimeBodyPart bodyPart = new MimeBodyPart();
-      bodyPart.setDisposition("inline");
+      bodyPart.setDisposition(new Header("inline").getSanitizedValue());
       String charset = findMessage().getCharset();
       if ( charset != null) 
       {
          //bodyPart.setContent(body, "text/plain; charset="
          //      + charset + "; format=flowed");
-         bodyPart.setText(body, charset);
+         bodyPart.setText(body, new Header(charset).getSanitizedValue());
       } 
       else 
       {
@@ -107,16 +107,16 @@
             throws MessagingException
    {
       MimeBodyPart bodyPart = new MimeBodyPart();
-      bodyPart.setDisposition("inline");
+      bodyPart.setDisposition(new Header("inline").getSanitizedValue());
       String charset = findMessage().getCharset();
       if ( charset != null) 
       {
-         bodyPart.setContent(body, "text/html; charset="
-                  + charset);
+         bodyPart.setContent(body, new Header("text/html; charset="
+               + charset).getSanitizedValue());
       } 
       else 
       {
-         bodyPart.setContent(body, "text/html");
+         bodyPart.setContent(body, new Header("text/html").getSanitizedValue());
       }
       
       return bodyPart;

Modified: branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIHeader.java
===================================================================
--- branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIHeader.java	2008-08-04 01:22:44 UTC (rev 8562)
+++ branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIHeader.java	2008-08-04 14:15:41 UTC (rev 8563)
@@ -17,11 +17,16 @@
    {
       try
       {
-         if (getValue() != null) {
-            findMimeMessage().addHeader(getName(), getValue());
-         } else {
-            findMimeMessage().addHeader(getName(), encode(facesContext));
+         if (getValue() != null) 
+         {
+            Header header = new Header(getName(), getValue());
+            findMimeMessage().addHeader(header.getSanitizedName(), header.getSanitizedValue());
          }
+         else 
+         {
+            Header header = new Header(getName(), encode(facesContext));
+            findMimeMessage().addHeader(header.getSanitizedName(), header.getSanitizedValue());
+         }
       }
       catch (MessagingException e)
       {

Modified: branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIMessage.java
===================================================================
--- branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIMessage.java	2008-08-04 01:22:44 UTC (rev 8562)
+++ branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UIMessage.java	2008-08-04 14:15:41 UTC (rev 8563)
@@ -108,7 +108,8 @@
             }
             if (getPrecedence() != null)
             {
-               getMimeMessage().addHeader("Precedence", getPrecedence());
+               Header header = new Header("Precedence", getPrecedence());
+               getMimeMessage().addHeader(header.getSanitizedName(), header.getSanitizedValue());
             }
          }
          catch (MessagingException e)
@@ -131,8 +132,9 @@
          if (isRequestReadReceipt() && getMimeMessage().getFrom() != null
                   && getMimeMessage().getFrom().length == 1)
          {
-            getMimeMessage().addHeader("Disposition-Notification-To",
-                     getMimeMessage().getFrom()[0].toString());
+            Header header = new Header("Disposition-Notification-To",
+                  getMimeMessage().getFrom()[0].toString());
+            getMimeMessage().addHeader(header.getSanitizedName(), header.getSanitizedValue());
          }
          // Do the send manually, Transport.send gets the wrong transport
          getMimeMessage().saveChanges();

Modified: branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UISubject.java
===================================================================
--- branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UISubject.java	2008-08-04 01:22:44 UTC (rev 8562)
+++ branches/Seam_2_0_FP/src/mail/org/jboss/seam/mail/ui/UISubject.java	2008-08-04 14:15:41 UTC (rev 8563)
@@ -22,11 +22,11 @@
          String charset = findMessage().getCharset();
          if (charset == null)
          {
-            findMimeMessage().setSubject(subject);
+            findMimeMessage().setSubject(new Header(subject).getSanitizedValue());
          }
          else
          {
-            findMimeMessage().setSubject(subject, charset);
+            findMimeMessage().setSubject(new Header(subject).getSanitizedValue(), charset);
          }
       }
       catch (MessagingException e)

Added: branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/unit/HeaderTest.java
===================================================================
--- branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/unit/HeaderTest.java	                        (rev 0)
+++ branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/unit/HeaderTest.java	2008-08-04 14:15:41 UTC (rev 8563)
@@ -0,0 +1,105 @@
+package org.jboss.seam.test.mail.unit;
+
+import org.jboss.seam.mail.ui.Header;
+import org.testng.annotations.Test;
+
+public class HeaderTest
+{
+   @Test
+   public void testHeader()
+   {
+      String name = "foo";
+      String value = "bar";
+      
+      Header header = new Header(name, value);
+      
+      assert header.getSanitizedName().equals(name);
+      assert header.getSanitizedValue().equals(value);
+   }
+   
+   @Test
+   public void testHeaderWithLineFeed()
+   {
+      String name = "foo\nnewline";
+      String value = "bar\nnewline";
+      
+      Header header = new Header(name, value);
+      
+      assert !header.getSanitizedName().equals(name);
+      assert !header.getSanitizedValue().equals(value);
+      
+      assert "foo".equals(header.getSanitizedName());
+      assert "bar".equals(header.getSanitizedValue());
+   }
+   
+   @Test
+   public void testHeaderWithCarrigeReturnLineBreak()
+   {
+      String name = "foo\r\nnewline";
+      String value = "bar\r\nnewline";
+      
+      Header header = new Header(name, value);
+      
+      assert !header.getSanitizedName().equals(name);
+      assert !header.getSanitizedValue().equals(value);
+      
+      assert "foo".equals(header.getSanitizedName());
+      assert "bar".equals(header.getSanitizedValue());
+   }
+   
+   @Test
+   public void testHeaderWithCarriageReturn()
+   {
+      String name = "foo\rnewline";
+      String value = "bar\rnewline";
+      
+      Header header = new Header(name, value);
+      
+      assert !header.getSanitizedName().equals(name);
+      assert !header.getSanitizedValue().equals(value);
+      
+      assert "foo".equals(header.getSanitizedName());
+      assert "bar".equals(header.getSanitizedValue());
+   }
+   
+   @Test
+   public void testHeaderWithFolding1()
+   {
+      String name = "header";
+      String value = "\"Joe & J. Harvey\" <ddd @ Org>,\n  JJV @ BBN";
+      String sanitizedValue = "\"Joe & J. Harvey\" <ddd @ Org>, JJV @ BBN";
+      
+      Header header = new Header(name, value);
+      
+      assert header.getSanitizedName().equals(name);
+      assert header.getSanitizedValue().equals(sanitizedValue);
+      
+   }
+   
+   @Test
+   public void testHeaderWithFolding2()
+   {
+      String name = "header";
+      String value = "\"Joe & J. Harvey\"\n <ddd @ Org>, JJV\n @ BBN";
+      String sanitizedValue = "\"Joe & J. Harvey\" <ddd @ Org>, JJV @ BBN";
+      
+      Header header = new Header(name, value);
+      
+      assert header.getSanitizedName().equals(name);
+      assert header.getSanitizedValue().equals(sanitizedValue);
+      
+   }
+   
+   @Test
+   public void testHeaderWithFolding3()
+   {
+      String name = "header";
+      String value = "\"Joe &\n  J. Harvey\" <ddd @ Org>, JJV @ BBN";
+      String sanitizedValue = "\"Joe & J. Harvey\" <ddd @ Org>, JJV @ BBN";
+      Header header = new Header(name, value);
+      
+      assert header.getSanitizedName().equals(name);
+      assert header.getSanitizedValue().equals(sanitizedValue);
+      
+   }
+}

Added: branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/unit/testng.xml
===================================================================
--- branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/unit/testng.xml	                        (rev 0)
+++ branches/Seam_2_0_FP/src/test/mail/unit/org/jboss/seam/test/mail/unit/testng.xml	2008-08-04 14:15:41 UTC (rev 8563)
@@ -0,0 +1,9 @@
+<!DOCTYPE suite SYSTEM "http://beust.com/testng/testng-1.0.dtd" >
+
+<suite name="Seam Mail Unit Testsuite" verbose="2" parallel="false">
+   <test name="Seam Unit Tests: Mail">
+     <classes>
+       <class name="org.jboss.seam.test.mail.unit.HeaderTest"/>
+     </classes>
+   </test>
+</suite>




More information about the seam-commits mailing list