Author: norman Date: Fri Jun 2 05:39:27 2006 New Revision: 411144 URL: http://svn.apache.org/viewvc?rev=411144&view=rev Log: Let it deal with encoded subjects
Modified: james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java Modified: james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java?rev=411144&r1=411143&r2=411144&view=diff ============================================================================== --- james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java (original) +++ james/server/trunk/src/java/org/apache/james/transport/mailets/AddSubjectPrefix.java Fri Jun 2 05:39:27 2006 @@ -22,6 +22,7 @@ import org.apache.mailet.GenericMailet; import org.apache.mailet.Mail; +import org.apache.mailet.RFC2822Headers; /** * Add an prefix (tag) to the subject of a message <br> @@ -60,15 +61,88 @@ * if an error arises during message processing */ public void service(Mail mail) throws MessagingException { + String newSubject = null; MimeMessage m = mail.getMessage(); String subject = m.getSubject(); if (subject != null) { - m.setSubject(subjectPrefix + " " + subject); + // m.setSubject(subjectPrefix + " " + subject); + newSubject = subjectPrefix + " " + m.getSubject().toString(); + } else { - m.setSubject(subjectPrefix); + newSubject = subjectPrefix; + } + + /* + * Get sure to use the right encoding when add the subjectPrefix.. + * otherwise we get problems with some special chars + */ + String rawSubject = m.getHeader(RFC2822Headers.SUBJECT, null); + String mimeCharset = determineMailHeaderEncodingCharset(rawSubject); + if (mimeCharset == null) { // most likely ASCII + // it uses the system charset or the value of the + // mail.mime.charset property if set + m.setSubject(newSubject); + return; + } else { // original charset determined + String javaCharset = javax.mail.internet.MimeUtility + .javaCharset(mimeCharset); + try { + m.setSubject(newSubject, javaCharset); + } catch (MessagingException e) { + // known, but unsupported encoding + // this should be logged, the admin may setup a more i18n + // capable JRE, but the log API cannot be accessed from here + // if (charset != null) log(charset + + // " charset unsupported by the JRE, email subject may be + // damaged"); + m.setSubject(newSubject); // recover + } } + m.saveChanges(); + } + + public String getMailetInfo() { + return "AddSubjectPrefix Mailet"; + } + + /** + * It attempts to determine the charset used to encode an "unstructured" RFC + * 822 header (like Subject). The encoding is specified in RFC 2047. If it + * cannot determine or the the text is not encoded then it returns null. + * + * Here is an example raw text: Subject: + * =?iso-8859-2?Q?leg=FAjabb_pr=F3ba_l=F5elemmel?= + * + * TODO: Should we include this in a util class ? + * + * @param rawText + * the raw (not decoded) value of the header. Null means that the + * header was not present (in this case it always return null). + * @return the MIME charset name or null if no encoding applied + */ + static private String determineMailHeaderEncodingCharset(String rawText) { + if (rawText == null) + return null; + int iEncodingPrefix = rawText.indexOf("=?"); + if (iEncodingPrefix == -1) + return null; + int iCharsetBegin = iEncodingPrefix + 2; + int iSecondQuestionMark = rawText.indexOf('?', iCharsetBegin); + if (iSecondQuestionMark == -1) + return null; + // safety checks + if (iSecondQuestionMark == iCharsetBegin) + return null; // empty charset? impossible + int iThirdQuestionMark = rawText.indexOf('?', iSecondQuestionMark + 1); + if (iThirdQuestionMark == -1) + return null; // there must be one after encoding + if (-1 == rawText.indexOf("?=", iThirdQuestionMark + 1)) + return null; // closing tag + String mimeCharset = rawText.substring(iCharsetBegin, + iSecondQuestionMark); + return mimeCharset; } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]