Source Code Cross Referenced for AbstractAddFooter.java in  » Net » james-2.3.1 » org » apache » james » transport » mailets » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » james 2.3.1 » org.apache.james.transport.mailets 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 &lt;br&gt; tags.
186:             *
187:             * @return the HTML version of the footer text
188:             */
189:            protected abstract String getFooterHTML();
190:
191:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.