Author: sgoeschl
Date: Tue Jul 13 15:35:53 2010
New Revision: 963763

URL: http://svn.apache.org/viewvc?rev=963763&view=rev
Log:
[EMAIL-92] Adding lenient mode to ignore resources which can't be resolved (or 
throwing an EMailException in non-lenient mode)

Modified:
    
commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java
    
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java
    
commons/proper/email/trunk/src/test/org/apache/commons/mail/ImageHtmlEmailTest.java

Modified: 
commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java
URL: 
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java?rev=963763&r1=963762&r2=963763&view=diff
==============================================================================
--- 
commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java 
(original)
+++ 
commons/proper/email/trunk/src/java/org/apache/commons/mail/ImageHtmlEmail.java 
Tue Jul 13 15:35:53 2010
@@ -86,7 +86,7 @@ public class ImageHtmlEmail extends Html
             throw new EmailException("Unable to create URL for current working 
directory", e);
         }
 
-        return setHtmlMsg(htmlMessage, currentWorkingDirectoryUrl);
+        return setHtmlMsg(htmlMessage, currentWorkingDirectoryUrl, false);
     }
 
     /**
@@ -95,11 +95,12 @@ public class ImageHtmlEmail extends Html
      * to resolve relative image resources.
      *
      * @param htmlMessage the HTML message.
-     * @param baseUrl An base URL that is used as starting point for resolving 
images that are embedded in the HTML
-     * @return An HtmlEmail
+     * @param baseUrl an base URL that is used as starting point for resolving 
images that are embedded in the HTML
+     * @param isLenient shall we ignore resources not found or throw an 
exception?
+     * @return a HTML email
      * @throws EmailException creating the email failed
      */
-    public HtmlEmail setHtmlMsg(final String htmlMessage, final URL baseUrl)
+    public HtmlEmail setHtmlMsg(final String htmlMessage, final URL baseUrl, 
boolean isLenient)
             throws EmailException
     {
         // if there is no useful HTML then simply route it through to the 
super class
@@ -109,10 +110,10 @@ public class ImageHtmlEmail extends Html
         }
 
         // replace images
-        String temp = replacePattern(htmlMessage, pattern, baseUrl);
+        String temp = replacePattern(htmlMessage, pattern, baseUrl, isLenient);
 
         // replace scripts
-        temp = replacePattern(temp, scriptPattern, baseUrl);
+        temp = replacePattern(temp, scriptPattern, baseUrl, isLenient);
 
         // finally set the resulting HTML with all images replaced if possible
         return super.setHtmlMsg(temp);
@@ -122,12 +123,13 @@ public class ImageHtmlEmail extends Html
      * Replace the regexp matching resource locations with "cid:..." 
references.
      *
      * @param htmlMessage the HTML message to analyze
-     * @param pattern the repexp to find resources
+     * @param pattern the regular expression to find resources
      * @param baseUrl the starting point for resolving relative resource paths
+     * @param isLenient shall we ignore resources not found or throw an 
exception?
      * @return the HTML message containing "cid" references
      * @throws EmailException creating the email failed
      */
-    private String replacePattern(final String htmlMessage, final Pattern 
pattern, final URL baseUrl)
+    private String replacePattern(final String htmlMessage, final Pattern 
pattern, final URL baseUrl, final boolean isLenient)
             throws EmailException
     {
         DataSource imageDataSource;
@@ -152,8 +154,13 @@ public class ImageHtmlEmail extends Html
             // avoid loading the same data source more than once
             if(dataSourceCache.get(image) == null) 
             {
-                imageDataSource = resolve(baseUrl, image);  
-                dataSourceCache.put(image, imageDataSource);
+                // in lenient mode we might get a 'null' data source if the 
resource was not found
+                imageDataSource = resolve(baseUrl, image, isLenient);
+                
+                if(imageDataSource != null)
+                {
+                    dataSourceCache.put(image, imageDataSource);
+                }
             }
             else
             {
@@ -167,7 +174,7 @@ public class ImageHtmlEmail extends Html
 
                 if(cid == null)
                 {
-                    cid = embed(imageDataSource, imageDataSource.getName());   
 
+                    cid = embed(imageDataSource, imageDataSource.getName());
                     cidCache.put(name, cid);
                 }
                 
@@ -189,14 +196,17 @@ public class ImageHtmlEmail extends Html
 
 
     /**
-     * Resolve a resource location to be embedded into the email.
+     * Resolve a resource location to be embedded into the email. When using
+     * the lenient mode a resource which can't be resolved returns "null".
+     * When using the non-lenient mode an exception would be thrown.
      *
      * @param baseUrl the base url of the resourceLocation
      * @param resourceLocation the location of the resource
-     * @return the data source containing the resource.
+     * @param isLenient shall we ignore resources not found?
+     * @return the data source containing the resource
      * @throws EmailException resolving the resource failed
      */
-    protected DataSource resolve(final URL baseUrl, final String 
resourceLocation)
+    protected DataSource resolve(final URL baseUrl, final String 
resourceLocation, final boolean isLenient)
             throws EmailException 
     {
         DataSource result = null;
@@ -207,13 +217,21 @@ public class ImageHtmlEmail extends Html
             {
                 URL url = URLFactory.createUrl(baseUrl, resourceLocation);
                 result = new URLDataSource(url);
+                result.getInputStream();
             }
 
             return result;
         }
         catch (IOException e)
         {
-            throw new EmailException("Resolving the resourceLocation failed", 
e);
+            if(!isLenient)
+            {
+                throw new EmailException("Resolving the resource failed : " + 
resourceLocation, e);
+            }
+            else
+            {
+                return null;
+            }
         }
     }
 }

Modified: 
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java?rev=963763&r1=963762&r2=963763&view=diff
==============================================================================
--- 
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java 
(original)
+++ 
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailLiveTest.java 
Tue Jul 13 15:35:53 2010
@@ -256,13 +256,14 @@ public class EmailLiveTest extends BaseE
         // use a simple HTML page with one image - please note that the Apache 
logo
         // is defined in CSS and not in HTML.
 
-        String htmlMsg1 = FileUtils.readFileToString(new 
File("./src/test/html/www.apache.org.html"));
+        File htmlFile = new File("./src/test/html/www.apache.org.html");
+        String htmlMsg1 = FileUtils.readFileToString(htmlFile);
 
         ImageHtmlEmail email = new ImageHtmlEmail();
         email.setSubject( "[testImageHtmlEmail] 1.Test: simple html content");
         email.setFrom(EmailConfiguration.TEST_FROM);
         email.setTo(getToList());
-        email.setHtmlMsg(htmlMsg1);
+        email.setHtmlMsg(htmlMsg1, htmlFile.toURI().toURL(), false);
         email.setMailSession(getSession());
 
         EmailUtils.writeMimeMessage( new 
File("./target/test-emails/testImageHtmlEmailLocal.eml"), 
send(email).getMimeMessage());
@@ -282,7 +283,7 @@ public class EmailLiveTest extends BaseE
         email.setSubject( "[testImageHtmlEmail] 2.Test: complex html content");
         email.setFrom(EmailConfiguration.TEST_FROM);
         email.setTo(getToList());
-        email.setHtmlMsg(htmlMsg, url);
+        email.setHtmlMsg(htmlMsg, url, true);
         email.setMailSession(getSession());
 
         EmailUtils.writeMimeMessage( new 
File("./target/test-emails/testImageHtmlEmailRemote.eml"), 
send(email).getMimeMessage());

Modified: 
commons/proper/email/trunk/src/test/org/apache/commons/mail/ImageHtmlEmailTest.java
URL: 
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/org/apache/commons/mail/ImageHtmlEmailTest.java?rev=963763&r1=963762&r2=963763&view=diff
==============================================================================
--- 
commons/proper/email/trunk/src/test/org/apache/commons/mail/ImageHtmlEmailTest.java
 (original)
+++ 
commons/proper/email/trunk/src/test/org/apache/commons/mail/ImageHtmlEmailTest.java
 Tue Jul 13 15:35:53 2010
@@ -89,7 +89,7 @@ public class ImageHtmlEmailTest extends 
                String html = str.toString();
 
                // set the html message
-               email.setHtmlMsg(html, TEST_IMAGE_DIR.toURI().toURL());
+               email.setHtmlMsg(html, TEST_IMAGE_DIR.toURI().toURL(), false);
 
                // set the alternative message
                //email.setTextMsg("Your email client does not support HTML 
messages");
@@ -128,7 +128,7 @@ public class ImageHtmlEmailTest extends 
 
                // set the html message
                try {
-                       email.setHtmlMsg(null, new 
File("/tmp").toURI().toURL());
+                       email.setHtmlMsg(null, new 
File("/tmp").toURI().toURL(), false);
                        fail("Should fail here!");
                } catch (EmailException e) {
                        assertTrue(e.getMessage(), e.getMessage().contains(
@@ -144,7 +144,7 @@ public class ImageHtmlEmailTest extends 
 
                // set the html message
                try {
-                       email.setHtmlMsg("", new File("/tmp").toURI().toURL());
+                       email.setHtmlMsg("", new File("/tmp").toURI().toURL(), 
false);
                        fail("Should fail here!");
                } catch (EmailException e) {
                        assertTrue(e.getMessage(), e.getMessage().contains(
@@ -203,7 +203,7 @@ public class ImageHtmlEmailTest extends 
 
                // set the html message
                email.setHtmlMsg("<html><body><img src=\"" + 
file.getAbsolutePath()
-                               + "\"/></body></html>", new 
File("").toURI().toURL());
+                               + "\"/></body></html>", new 
File("").toURI().toURL(), false);
 
                // send the email
                email.send();


Reply via email to