You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2003/03/10 21:42:16 UTC
cvs commit: jakarta-james/src/java/org/apache/james/transport/mailets AddFooter.java
noel 2003/03/10 12:42:16
Modified: src/java/org/apache/james/transport/mailets Tag:
branch_2_1_fcs AddFooter.java
Log:
Corrected text/html handling. Added fixes for multipart handling. Left in considerable debugging code (commented out) for the time being to assist in debugging.
Revision Changes Path
No revision
No revision
1.4.4.3 +77 -29 jakarta-james/src/java/org/apache/james/transport/mailets/AddFooter.java
Index: AddFooter.java
===================================================================
RCS file: /home/cvs/jakarta-james/src/java/org/apache/james/transport/mailets/AddFooter.java,v
retrieving revision 1.4.4.2
retrieving revision 1.4.4.3
diff -u -r1.4.4.2 -r1.4.4.3
--- AddFooter.java 8 Mar 2003 21:54:08 -0000 1.4.4.2
+++ AddFooter.java 10 Mar 2003 20:42:16 -0000 1.4.4.3
@@ -58,6 +58,7 @@
package org.apache.james.transport.mailets;
+import org.apache.james.core.MailImpl;
import org.apache.mailet.GenericMailet;
import org.apache.mailet.Mail;
@@ -99,29 +100,39 @@
public void service(Mail mail) throws MessagingException {
try {
MimeMessage message = mail.getMessage();
+// log("Trying to add footer to mail " + ((MailImpl)mail).getName());
+ if (attachFooter(message)) {
+ message.saveChanges();
+// log("Message after saving: " + message.getContent().toString());
+ /*
+ java.io.ByteArrayOutputStream bodyOs = new java.io.ByteArrayOutputStream(512);
+ java.io.OutputStream bos;
+ java.io.InputStream bis;
+ try {
+ bis = message.getRawInputStream();
+ bos = bodyOs;
+ log("Using getRawInputStream()");
+ } catch(javax.mail.MessagingException me) {
+ bos = javax.mail.internet.MimeUtility.encode(bodyOs, message.getEncoding());
+ bis = message.getInputStream();
+ log("Using getInputStream()");
+ }
- //I want to modify the right message body
- if (message.isMimeType("text/plain")) {
- //This is a straight text message... just append the single part normally
- addToText(message);
- } else if (message.isMimeType("multipart/mixed")) {
- //Find the first body part, and determine what to do then.
- MimeMultipart multipart = (MimeMultipart)message.getContent();
- MimeBodyPart part = (MimeBodyPart)multipart.getBodyPart(0);
- attachFooter(part);
- //We have to do this because of a bug in JavaMail (ref id 4404733)
- message.setContent(multipart);
- } else if (message.isMimeType("multipart/alternative")) {
- //Find the HTML and text message types and add to each
- MimeMultipart multipart = (MimeMultipart)message.getContent();
- for (int i = 0; i < multipart.getCount(); i++) {
- MimeBodyPart part = (MimeBodyPart)multipart.getBodyPart(i);
- attachFooter(part);
+ try {
+ byte[] block = new byte[1024];
+ int read = 0;
+ while ((read = bis.read(block)) > -1) {
+ bos.write(block, 0, read);
+ }
+ bos.flush();
+ }
+ finally {
+ org.apache.avalon.excalibur.io.IOUtil.shutdownStream(bis);
}
- //We have to do this because of a bug in JavaMail (ref id 4404733)
- message.setContent(multipart);
+ log("Message from stream: " + bodyOs.toString());
+ */
} else {
- //Give up... we won't attach the footer to this message
+ log("Unable to add footer to mail " + ((MailImpl)mail).getName());
}
} catch (IOException ioe) {
throw new MessagingException("Could not read message", ioe);
@@ -177,16 +188,18 @@
*
* @param part the MimePart to attach
*
- * @throws MessagingException
+ * @throws MessagingException
* @throws IOException
*/
protected void addToText(MimePart part) throws MessagingException, IOException {
+// log("Trying to add footer to " + part.getContent().toString());
String content = part.getContent().toString();
if (!content.endsWith("\n")) {
content += "\r\n";
}
content += getFooterText();
part.setText(content);
+// log("After adding footer: " + part.getContent().toString());
}
/**
@@ -194,31 +207,66 @@
*
* @param part the MimePart to attach
*
- * @throws MessagingException
+ * @throws MessagingException
* @throws IOException
*/
protected void addToHTML(MimePart part) throws MessagingException, IOException {
+// log("Trying to add footer to " + part.getContent().toString());
String content = part.getContent().toString();
- content += "<br>" + getFooterHTML();
+
+ /* This HTML part may have a closing <BODY> tag. If so, we
+ * want to insert out footer immediately prior to that tag.
+ */
+ int index = content.lastIndexOf("</body>");
+ if (index == -1) index = content.lastIndexOf("</BODY>");
+ String insert = "<br>" + getFooterHTML();
+ content = index == -1 ? content + insert : content.substring(0, index) + insert + content.substring(index);
+
part.setContent(content, part.getContentType());
+// log("After adding footer: " + part.getContent().toString());
}
/**
- * Attaches a MimePart as an appropriate footer
+ * Attach a footer a MimePart
*
- * @param part the MimePart to attach
+ * @param part the MimePart to which the footer is to be attached
*
- * @throws MessagingException
+ * @return whether a footer was successfully attached
+ * @throws MessagingException
* @throws IOException
*/
- protected void attachFooter(MimePart part) throws MessagingException, IOException {
+ protected boolean attachFooter(MimePart part) throws MessagingException, IOException {
+// log("Content type is " + part.getContentType());
if (part.isMimeType("text/plain")) {
addToText(part);
+ return true;
} else if (part.isMimeType("text/html")) {
addToHTML(part);
+ return true;
+ } else if (part.isMimeType("multipart/mixed")) {
+ //Find the first body part, and determine what to do then.
+ MimeMultipart multipart = (MimeMultipart)part.getContent();
+ MimeBodyPart firstPart = (MimeBodyPart)multipart.getBodyPart(0);
+ boolean isFooterAttached = attachFooter(firstPart);
+ //We have to do this because of a bug in JavaMail (ref id 4404733)
+ part.setContent(multipart);
+ return isFooterAttached;
+ } else if (part.isMimeType("multipart/alternative")) {
+ MimeMultipart multipart = (MimeMultipart)part.getContent();
+ int count = multipart.getCount();
+// log("number of alternatives = " + count);
+ boolean isFooterAttached = false;
+ for (int index = 0; index < count; index++) {
+// log("processing alternative #" + index);
+ MimeBodyPart mimeBodyPart = (MimeBodyPart)multipart.getBodyPart(index);
+ isFooterAttached |= attachFooter(mimeBodyPart);
+ }
+ //We have to do this because of a bug in JavaMail (ref id 4404733)
+ part.setContent(multipart);
+ return isFooterAttached;
} else {
- System.err.println(part.getContentType());
+ //Give up... we won't attach the footer to this MimePart
+ return false;
}
- //Give up... we won't attach the footer to this message
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: james-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: james-dev-help@jakarta.apache.org