Author: sgoeschl
Date: Sat Oct 23 22:04:31 2010
New Revision: 1026696
URL: http://svn.apache.org/viewvc?rev=1026696&view=rev
Log:
[EMAIL-98] Encoding and folding of headers should not be left to the user
Modified:
commons/proper/email/trunk/src/changes/changes.xml
commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailTest.java
Modified: commons/proper/email/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/changes/changes.xml?rev=1026696&r1=1026695&r2=1026696&view=diff
==============================================================================
--- commons/proper/email/trunk/src/changes/changes.xml (original)
+++ commons/proper/email/trunk/src/changes/changes.xml Sat Oct 23 22:04:31 2010
@@ -23,6 +23,9 @@
<body>
<release version="1.3-SNAPSHOT" date="as in SVN">
+ <action dev="sgoeschl" type="fix" issue="EMAIL-98" date="2010-10-23"
due-to="Mario Daepp">
+ Encoding and folding of headers is now done by commons-email.
+ </action>
<action dev="sgoeschl" type="fix" issue="EMAIL-100" date="2010-10-15"
due-to="David Parks">
The default connection timeout is set to a reasonable default value of
60 seconds.
</action>
Modified: commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java?rev=1026696&r1=1026695&r2=1026696&view=diff
==============================================================================
--- commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java
(original)
+++ commons/proper/email/trunk/src/java/org/apache/commons/mail/Email.java Sat
Oct 23 22:04:31 2010
@@ -37,6 +37,7 @@ import javax.mail.internet.AddressExcept
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
+import javax.mail.internet.MimeUtility;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
@@ -948,27 +949,17 @@ public abstract class Email implements E
*/
public Email setHeaders(Map map)
{
+ this.headers.clear();
+
Iterator iterKeyBad = map.entrySet().iterator();
while (iterKeyBad.hasNext())
{
Map.Entry entry = (Map.Entry) iterKeyBad.next();
- String strName = (String) entry.getKey();
- String strValue = (String) entry.getValue();
-
- if (EmailUtils.isEmpty(strName))
- {
- throw new IllegalArgumentException("name can not be null");
- }
- if (EmailUtils.isEmpty(strValue))
- {
- throw new IllegalArgumentException("value can not be null");
- }
+ String name = (String) entry.getKey();
+ this.headers.put(name, createFoldedHeaderValue(name,
entry.getValue()));
}
- // all is ok, update headers
- this.headers = map;
-
return this;
}
@@ -990,7 +981,7 @@ public abstract class Email implements E
throw new IllegalArgumentException("value can not be null");
}
- this.headers.put(name, value);
+ this.headers.put(name, createFoldedHeaderValue(name, value));
}
/**
@@ -1137,6 +1128,7 @@ public abstract class Email implements E
this.toInternetAddressArray(this.replyList));
}
+
if (this.headers.size() > 0)
{
Iterator iterHeaderKeys = this.headers.keySet().iterator();
@@ -1144,7 +1136,8 @@ public abstract class Email implements E
{
String name = (String) iterHeaderKeys.next();
String value = (String) headers.get(name);
- this.message.addHeader(name, value);
+ String foldedValue = createFoldedHeaderValue(name, value);
+ this.message.addHeader(name, foldedValue);
}
}
@@ -1473,7 +1466,7 @@ public abstract class Email implements E
/**
* Set the socket connection timeout value in milliseconds.
- * Default is infinite timeout.
+ * Default is a 60 second timeout.
*
* @param socketConnectionTimeout the connection timeout
* @return An Email.
@@ -1499,7 +1492,7 @@ public abstract class Email implements E
/**
* Set the socket I/O timeout value in milliseconds.
- * Default is infinite timeout.
+ * Default is 60 second timeout.
*
* @param socketTimeout the socket I/O timeout
* @since 1.2
@@ -1509,4 +1502,37 @@ public abstract class Email implements E
this.socketTimeout = socketTimeout;
return this;
}
+
+
+ /**
+ * Create a folded header value containing 76 character chunks.
+ *
+ * @param name the name of the header
+ * @param value the value of the header
+ * @return the folded header value
+ */
+ private String createFoldedHeaderValue(String name, Object value)
+ {
+ String result;
+
+ if (EmailUtils.isEmpty(name))
+ {
+ throw new IllegalArgumentException("name can not be null");
+ }
+ if (value == null || EmailUtils.isEmpty(value.toString()))
+ {
+ throw new IllegalArgumentException("value can not be null");
+ }
+
+ try
+ {
+ result = MimeUtility.fold(name.length() + 2,
MimeUtility.encodeText(value.toString(), this.charset, null));
+ }
+ catch(UnsupportedEncodingException e)
+ {
+ result = value.toString();
+ }
+
+ return result;
+ }
}
Modified:
commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailTest.java?rev=1026696&r1=1026695&r2=1026696&view=diff
==============================================================================
--- commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailTest.java
(original)
+++ commons/proper/email/trunk/src/test/org/apache/commons/mail/EmailTest.java
Sat Oct 23 22:04:31 2010
@@ -56,7 +56,7 @@ public class EmailTest extends BaseEmail
/** mock for testing */
private MockEmailConcrete email;
-
+
/**
* @param name test name
*/
@@ -1103,9 +1103,6 @@ public class EmailTest extends BaseEmail
/** */
public void testSetHeaders()
{
- // ====================================================================
- // Test Success
- // ====================================================================
Map ht = new Hashtable();
ht.put("X-Priority", "1");
ht.put("Disposition-Notification-To", "[email protected]");
@@ -1117,6 +1114,13 @@ public class EmailTest extends BaseEmail
assertEquals(ht, this.email.getHeaders());
}
+ public void testFoldingHeaders()
+ {
+ this.email.addHeader("X-LongHeader", "1234567890 1234567890 123456789
01234567890 123456789 0123456789 01234567890 01234567890");
+ assertTrue(this.email.getHeaders().size() == 1);
+
assertTrue(this.email.getHeaders().get("X-LongHeader").toString().contains("\r\n"));
+ }
+
/** */
public void testSetHeadersEx()
{