Author: dkulp Date: Fri Jul 8 20:18:54 2011 New Revision: 1144484 URL: http://svn.apache.org/viewvc?rev=1144484&view=rev Log: Merged revisions 1134714,1134911 via svnmerge from https://svn.apache.org/repos/asf/camel/trunk
........ r1134714 | davsclaus | 2011-06-11 13:55:35 -0400 (Sat, 11 Jun 2011) | 1 line CAMEL-4007: Added test for mail file attachments with non ascii names. ........ r1134911 | davsclaus | 2011-06-12 07:01:23 -0400 (Sun, 12 Jun 2011) | 1 line CAMEL-4083: Fixed mail binding may drop details from content-type. Improved parsing and configuration of charset in content-type header. ........ Added: camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/DetermineContentTypeIssueTest.java - copied unchanged from r1134911, camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/DetermineContentTypeIssueTest.java camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentsUmlautIssueTest.java - copied unchanged from r1134911, camel/trunk/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentsUmlautIssueTest.java Modified: camel/branches/camel-2.7.x/ (props changed) camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java Propchange: camel/branches/camel-2.7.x/ ------------------------------------------------------------------------------ --- svn:mergeinfo (original) +++ svn:mergeinfo Fri Jul 8 20:18:54 2011 @@ -1 +1 @@ -/camel/trunk:1083696,1083723-1083724,1084150,1085277,1085543,1085549,1085905,1085909,1086165,1086231,1087005,1087276,1087612,1087620,1087856,1088583,1088916-1088917,1089275,1089348,1090166,1090204,1090564,1090960-1090969,1091082,1091518,1091771,1091799,1092034,1092068,1092577,1092667,1093978,1093980,1093999,1094123,1094147,1094156,1095405,1095469,1095471,1095475-1095476,1096346,1096736,1097761,1097909,1097912,1097978,1098032,1098628,1098630,1099228,1099417,1100975,1102162,1102177,1102181,1104076,1124497,1127744,1127988,1128315,1128970,1131411,1132961,1134252,1134260,1134404,1134501,1134626,1134681,1135223,1135364,1136290,1138285,1139163,1140096-1140102,1141783,1143925,1144248,1144324 +/camel/trunk:1083696,1083723-1083724,1084150,1085277,1085543,1085549,1085905,1085909,1086165,1086231,1087005,1087276,1087612,1087620,1087856,1088583,1088916-1088917,1089275,1089348,1090166,1090204,1090564,1090960-1090969,1091082,1091518,1091771,1091799,1092034,1092068,1092577,1092667,1093978,1093980,1093999,1094123,1094147,1094156,1095405,1095469,1095471,1095475-1095476,1096346,1096736,1097761,1097909,1097912,1097978,1098032,1098628,1098630,1099228,1099417,1100975,1102162,1102177,1102181,1104076,1124497,1127744,1127988,1128315,1128970,1131411,1132961,1134252,1134260,1134404,1134501,1134626,1134681,1134714-1134911,1135223,1135364,1136290,1138285,1139163,1140096-1140102,1141783,1143925,1144248,1144324 Propchange: camel/branches/camel-2.7.x/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java?rev=1144484&r1=1144483&r2=1144484&view=diff ============================================================================== --- camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java (original) +++ camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailBinding.java Fri Jul 8 20:18:54 2011 @@ -125,21 +125,16 @@ public class MailBinding { contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); } - // fix content type to include a space after semi colon if missing + // fix content-type to have space after semi colons, otherwise some mail servers will choke if (contentType != null && contentType.contains(";")) { - String before = ObjectHelper.before(contentType, ";"); - String charset = determineCharSet(configuration, exchange); - - if (before != null && charset == null) { - contentType = before.trim(); - } else if (before != null && charset != null) { - contentType = before.trim() + "; charset=" + charset; - } + contentType = MailUtils.padContentType(contentType); } - - // set the charset if exchange has the charset name as fall back - if (contentType != null && !contentType.contains(";") && IOConverter.getCharsetName(exchange, false) != null) { - contentType = contentType.trim() + "; charset=" + IOConverter.getCharsetName(exchange, false); + + if (contentType != null) { + // no charset in content-type, then try to see if we can determine one + String charset = determineCharSet(configuration, exchange); + // must replace charset, even with null in case its an unsupported charset + contentType = MailUtils.replaceCharSet(contentType, charset); } if (LOG.isTraceEnabled()) { @@ -159,37 +154,30 @@ public class MailBinding { contentType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class); } - // fix content type to include a space after semi colon if missing - if (contentType != null && contentType.contains(";")) { - String after = ObjectHelper.after(contentType, ";"); - - // after is the charset lets see if its given and a valid charset - if (after != null) { - String charset = IOConverter.normalizeCharset(ObjectHelper.after(after, "=")); - if (charset != null) { - boolean supported; - try { - supported = Charset.isSupported(charset); - } catch (IllegalCharsetNameException e) { - supported = false; - } - if (supported) { - return charset; - } else if (!configuration.isIgnoreUnsupportedCharset()) { - return charset; - } else if (configuration.isIgnoreUnsupportedCharset()) { - LOG.warn("Charset: " + charset + " is not supported, will fallback to use platform default instead."); - return null; - } + // look for charset + String charset = MailUtils.getCharSetFromContentType(contentType); + if (charset != null) { + charset = IOConverter.normalizeCharset(charset); + if (charset != null) { + boolean supported; + try { + supported = Charset.isSupported(charset); + } catch (IllegalCharsetNameException e) { + supported = false; + } + if (supported) { + return charset; + } else if (!configuration.isIgnoreUnsupportedCharset()) { + return charset; + } else if (configuration.isIgnoreUnsupportedCharset()) { + LOG.warn("Charset: " + charset + " is not supported and cannot be used as charset in Content-Type header."); + return null; } } } - + // Using the charset header of exchange as a fall back return IOConverter.getCharsetName(exchange, false); - - - } protected String populateContentOnMimeMessage(MimeMessage part, MailConfiguration configuration, Exchange exchange) Modified: camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java?rev=1144484&r1=1144483&r2=1144484&view=diff ============================================================================== --- camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java (original) +++ camel/branches/camel-2.7.x/components/camel-mail/src/main/java/org/apache/camel/component/mail/MailUtils.java Fri Jul 8 20:18:54 2011 @@ -18,11 +18,12 @@ package org.apache.camel.component.mail; import java.text.DateFormat; import java.util.Date; - import javax.mail.Address; import javax.mail.Message; import javax.mail.MessagingException; +import org.apache.camel.util.ObjectHelper; + /** * Mail utility class. * <p> @@ -137,4 +138,94 @@ public final class MailUtils { return message.toString(); } } + + /** + * Pads the content-type so it has a space after semi colon that separate pairs. + * <p/> + * This is needed as some mail servers will choke otherwise + * + * @param contentType the content type + * @return the padded content type + */ + public static String padContentType(String contentType) { + StringBuilder sb = new StringBuilder(); + String[] parts = contentType.split(";"); + for (int i = 0; i < parts.length; i++) { + String part = parts[i]; + if (ObjectHelper.isNotEmpty(part)) { + part = part.trim(); + sb.append(part); + if (i < parts.length - 1) { + sb.append("; "); + } + } + } + return sb.toString(); + } + + /** + * Replaces the charset in the content-type + * + * @param contentType the content-type + * @param charset the charset to replace, can be <tt>null</tt> to remove charset + * @return the content-type with replaced charset + */ + public static String replaceCharSet(String contentType, String charset) { + boolean replaced = false; + StringBuilder sb = new StringBuilder(); + String[] parts = contentType.split(";"); + for (int i = 0; i < parts.length; i++) { + String part = parts[i]; + part = part.trim(); + if (!part.startsWith("charset")) { + part = part.trim(); + if (sb.length() > 0) { + sb.append("; "); + } + sb.append(part); + } else if (charset != null) { + // replace with new charset + if (sb.length() > 0) { + sb.append("; "); + } + sb.append("charset="); + sb.append(charset); + replaced = true; + } + } + + // if we did not replace any existing charset, then append new charset at the end + if (!replaced && charset != null) { + if (sb.length() > 0) { + sb.append("; "); + } + sb.append("charset="); + sb.append(charset); + } + + return sb.toString(); + } + + /** + * Gets the charset from the content-type + * + * @param contentType the content-type + * @return the charset, or <tt>null</tt> if no charset existed + */ + public static String getCharSetFromContentType(String contentType) { + if (contentType == null) { + return null; + } + + String[] parts = contentType.split(";"); + for (int i = 0; i < parts.length; i++) { + String part = parts[i]; + part = part.trim(); + if (part.startsWith("charset")) { + return ObjectHelper.after(part, "charset="); + } + } + return null; + } + } Modified: camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java?rev=1144484&r1=1144483&r2=1144484&view=diff ============================================================================== --- camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java (original) +++ camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailAttachmentTest.java Fri Jul 8 20:18:54 2011 @@ -37,7 +37,7 @@ import org.jvnet.mock_javamail.Mailbox; public class MailAttachmentTest extends CamelTestSupport { @Test - public void testSendAndRecieveMailWithAttachments() throws Exception { + public void testSendAndReceiveMailWithAttachments() throws Exception { // clear mailbox Mailbox.clearAll(); @@ -81,6 +81,7 @@ public class MailAttachmentTest extends assertNotNull("The logo should be there", handler); assertEquals("image/jpeg; name=logo.jpeg", handler.getContentType()); + assertEquals("Handler name should be the file name", "logo.jpeg", handler.getName()); producer.stop(); } Modified: camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java URL: http://svn.apache.org/viewvc/camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java?rev=1144484&r1=1144483&r2=1144484&view=diff ============================================================================== --- camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java (original) +++ camel/branches/camel-2.7.x/components/camel-mail/src/test/java/org/apache/camel/component/mail/MailProducerUnsupportedCharsetTest.java Fri Jul 8 20:18:54 2011 @@ -23,6 +23,7 @@ import java.util.Map; import org.apache.camel.RuntimeCamelException; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.converter.IOConverter; import org.apache.camel.test.junit4.CamelTestSupport; import org.junit.Test; import org.jvnet.mock_javamail.Mailbox;