001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.mailets;
019:
020: import org.apache.mailet.GenericMailet;
021: import org.apache.mailet.Mail;
022: import org.apache.mailet.RFC2822Headers;
023:
024: import javax.mail.MessagingException;
025: import javax.mail.internet.MimeBodyPart;
026: import javax.mail.internet.MimeMessage;
027: import javax.mail.internet.MimeMultipart;
028: import javax.mail.internet.MimePart;
029:
030: import java.io.IOException;
031: import java.io.UnsupportedEncodingException;
032:
033: /**
034: * An abstract implementation of a mailet that add a Footer to an email
035: */
036: public abstract class AbstractAddFooter extends GenericMailet {
037:
038: /**
039: * Takes the message and attaches a footer message to it. Right now, it only
040: * supports simple messages. Needs to have additions to make it support
041: * messages with alternate content types or with attachments.
042: *
043: * @param mail the mail being processed
044: *
045: * @throws MessagingException if an error arises during message processing
046: */
047: public void service(Mail mail) throws MessagingException {
048: try {
049: MimeMessage message = mail.getMessage();
050:
051: if (attachFooter(message)) {
052: message.saveChanges();
053: } else {
054: log("Unable to add footer to mail " + mail.getName());
055: }
056: } catch (UnsupportedEncodingException e) {
057: log("UnsupportedEncoding Unable to add footer to mail "
058: + mail.getName());
059: } catch (IOException ioe) {
060: throw new MessagingException("Could not read message", ioe);
061: }
062: }
063:
064: /**
065: * Prepends the content of the MimePart as text to the existing footer
066: *
067: * @param part the MimePart to attach
068: *
069: * @throws MessagingException
070: * @throws IOException
071: */
072: protected void addToText(MimePart part) throws MessagingException,
073: IOException {
074: // log("Trying to add footer to " + part.getContent().toString());
075: String contentType = part.getContentType();
076: String content = (String) part.getContent();
077:
078: if (!content.endsWith("\n")) {
079: content += "\r\n";
080: }
081: content += getFooterText();
082:
083: part.setContent(content, contentType);
084: part.setHeader(RFC2822Headers.CONTENT_TYPE, contentType);
085: // log("After adding footer: " + part.getContent().toString());
086: }
087:
088: /**
089: * Prepends the content of the MimePart as HTML to the existing footer
090: *
091: * @param part the MimePart to attach
092: *
093: * @throws MessagingException
094: * @throws IOException
095: */
096: protected void addToHTML(MimePart part) throws MessagingException,
097: IOException {
098: // log("Trying to add footer to " + part.getContent().toString());
099: String contentType = part.getContentType();
100: String content = (String) part.getContent();
101:
102: /* This HTML part may have a closing <BODY> tag. If so, we
103: * want to insert out footer immediately prior to that tag.
104: */
105: int index = content.lastIndexOf("</body>");
106: if (index == -1)
107: index = content.lastIndexOf("</BODY>");
108: String insert = "<br>" + getFooterHTML();
109: content = index == -1 ? content + insert : content.substring(0,
110: index)
111: + insert + content.substring(index);
112:
113: part.setContent(content, contentType);
114: part.setHeader(RFC2822Headers.CONTENT_TYPE, contentType);
115: // log("After adding footer: " + part.getContent().toString());
116: }
117:
118: /**
119: * Attach a footer a MimePart
120: *
121: * @param part the MimePart to which the footer is to be attached
122: *
123: * @return whether a footer was successfully attached
124: * @throws MessagingException
125: * @throws IOException
126: */
127: protected boolean attachFooter(MimePart part)
128: throws MessagingException, IOException {
129: // log("Content type is " + part.getContentType());
130: if (part.isMimeType("text/plain")
131: && part.getContent() instanceof String) {
132: addToText(part);
133: return true;
134: } else if (part.isMimeType("text/html")
135: && part.getContent() instanceof String) {
136: addToHTML(part);
137: return true;
138: } else if (part.isMimeType("multipart/mixed")
139: || part.isMimeType("multipart/related")) {
140: //Find the first body part, and determine what to do then.
141: MimeMultipart multipart = (MimeMultipart) part.getContent();
142: MimeBodyPart firstPart = (MimeBodyPart) multipart
143: .getBodyPart(0);
144: boolean isFooterAttached = attachFooter(firstPart);
145: if (isFooterAttached) {
146: //We have to do this because of a bug in JavaMail (ref id 4403733)
147: //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4403733
148: part.setContent(multipart);
149: }
150: return isFooterAttached;
151: } else if (part.isMimeType("multipart/alternative")) {
152: MimeMultipart multipart = (MimeMultipart) part.getContent();
153: int count = multipart.getCount();
154: // log("number of alternatives = " + count);
155: boolean isFooterAttached = false;
156: for (int index = 0; index < count; index++) {
157: // log("processing alternative #" + index);
158: MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart
159: .getBodyPart(index);
160: isFooterAttached |= attachFooter(mimeBodyPart);
161: }
162: if (isFooterAttached) {
163: //We have to do this because of a bug in JavaMail (ref id 4403733)
164: //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4403733
165: part.setContent(multipart);
166: }
167: return isFooterAttached;
168: } else {
169: //Give up... we won't attach the footer to this MimePart
170: return false;
171: }
172: }
173:
174: /**
175: * This is exposed as a method for easy subclassing to provide alternate ways
176: * to get the footer text.
177: *
178: * @return the footer text
179: */
180: protected abstract String getFooterText();
181:
182: /**
183: * This is exposed as a method for easy subclassing to provide alternate ways
184: * to get the footer text. By default, this will take the footer text,
185: * converting the linefeeds to <br> tags.
186: *
187: * @return the HTML version of the footer text
188: */
189: protected abstract String getFooterHTML();
190:
191: }
|