Methods Summary |
---|
protected void | addToHTML(javax.mail.internet.MimePart part)Prepends the content of the MimePart as HTML to the existing footer
// log("Trying to add footer to " + part.getContent().toString());
String contentType = part.getContentType();
String content = (String) part.getContent();
/* 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, contentType);
part.setHeader(RFC2822Headers.CONTENT_TYPE, contentType);
// log("After adding footer: " + part.getContent().toString());
|
protected void | addToText(javax.mail.internet.MimePart part)Prepends the content of the MimePart as text to the existing footer
// log("Trying to add footer to " + part.getContent().toString());
String contentType = part.getContentType();
String content = (String) part.getContent();
if (!content.endsWith("\n")) {
content += "\r\n";
}
content += getFooterText();
part.setContent(content, contentType);
part.setHeader(RFC2822Headers.CONTENT_TYPE, contentType);
// log("After adding footer: " + part.getContent().toString());
|
protected boolean | attachFooter(javax.mail.internet.MimePart part)Attach a footer a MimePart
// log("Content type is " + part.getContentType());
if (part.isMimeType("text/plain")
&& part.getContent() instanceof String) {
addToText(part);
return true;
} else if (part.isMimeType("text/html")
&& part.getContent() instanceof String) {
addToHTML(part);
return true;
} else if (part.isMimeType("multipart/mixed")
|| part.isMimeType("multipart/related")) {
//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);
if (isFooterAttached) {
//We have to do this because of a bug in JavaMail (ref id 4403733)
//http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4403733
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);
}
if (isFooterAttached) {
//We have to do this because of a bug in JavaMail (ref id 4403733)
//http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4403733
part.setContent(multipart);
}
return isFooterAttached;
} else {
//Give up... we won't attach the footer to this MimePart
return false;
}
|
protected abstract java.lang.String | getFooterHTML()This is exposed as a method for easy subclassing to provide alternate ways
to get the footer text. By default, this will take the footer text,
converting the linefeeds to <br> tags.
|
protected abstract java.lang.String | getFooterText()This is exposed as a method for easy subclassing to provide alternate ways
to get the footer text.
|
public void | service(org.apache.mailet.Mail mail)Takes the message and attaches a footer message to it. Right now, it only
supports simple messages. Needs to have additions to make it support
messages with alternate content types or with attachments.
try {
MimeMessage message = mail.getMessage();
if (attachFooter(message)) {
message.saveChanges();
} else {
log("Unable to add footer to mail " + mail.getName());
}
} catch (UnsupportedEncodingException e) {
log("UnsupportedEncoding Unable to add footer to mail "
+ mail.getName());
} catch (IOException ioe) {
throw new MessagingException("Could not read message", ioe);
}
|