Source Code Cross Referenced for BaseCommand.java in  » Net » james-2.3.1 » org » apache » james » transport » mailets » listservcommands » 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.listservcommands 
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.listservcommands;
019:
020:        import org.apache.avalon.framework.configuration.Configuration;
021:        import org.apache.avalon.framework.configuration.ConfigurationException;
022:        import org.apache.james.services.UsersRepository;
023:        import org.apache.james.transport.mailets.ICommandListservManager;
024:        import org.apache.mailet.RFC2822Headers;
025:        import org.apache.james.util.XMLResources;
026:        import org.apache.mailet.Mail;
027:        import org.apache.mailet.MailAddress;
028:        import org.apache.mailet.MailetContext;
029:
030:        import javax.activation.DataHandler;
031:        import javax.mail.Message;
032:        import javax.mail.MessagingException;
033:        import javax.mail.Session;
034:        import javax.mail.internet.InternetAddress;
035:        import javax.mail.internet.MimeBodyPart;
036:        import javax.mail.internet.MimeMessage;
037:        import javax.mail.internet.MimeMultipart;
038:        import java.util.Properties;
039:
040:        /**
041:         * BaseCommand is a convience base class for any class that wishes to implement {@link IListServCommand}.
042:         * It provides some functions like:
043:         * <ul>
044:         *  <li>{@link #log}
045:         *  <li>{@link #sendStandardReply}
046:         *  <li>{@link #generateMail}
047:         * </ul>
048:         *
049:         * <br />
050:         * <br />
051:         *
052:         * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
053:         * @since 2.2.0
054:         * @see org.apache.james.transport.mailets.CommandListservManager
055:         */
056:        public abstract class BaseCommand implements  IListServCommand {
057:
058:            protected Configuration configuration;
059:            protected ICommandListservManager commandListservManager;
060:            protected String commandName;
061:            protected MailetContext mailetContext;
062:
063:            /**
064:             * Perform any required initialization
065:             * @param configuration
066:             * @throws ConfigurationException
067:             */
068:            public void init(ICommandListservManager commandListservManager,
069:                    Configuration configuration) throws ConfigurationException {
070:                this .commandListservManager = commandListservManager;
071:                this .configuration = configuration;
072:                commandName = configuration.getAttribute("name");
073:                mailetContext = this .commandListservManager.getMailetConfig()
074:                        .getMailetContext();
075:                log("Initialized listserv command: [" + commandName + ", "
076:                        + getClass().getName() + "]");
077:            }
078:
079:            /**
080:             * The name of this command
081:             * @see IListServCommand#getCommandName
082:             */
083:            public String getCommandName() {
084:                return commandName;
085:            }
086:
087:            /**
088:             * @see Configuration
089:             */
090:            protected Configuration getConfiguration() {
091:                return configuration;
092:            }
093:
094:            /**
095:             * The list serv manager
096:             * @return {@link ICommandListservManager}
097:             */
098:            protected ICommandListservManager getCommandListservManager() {
099:                return commandListservManager;
100:            }
101:
102:            /**
103:             * The current mailet context
104:             * @return {@link MailetContext}
105:             */
106:            protected MailetContext getMailetContext() {
107:                return mailetContext;
108:            }
109:
110:            /**
111:             * @see ICommandListservManager#getUsersRepository
112:             */
113:            protected UsersRepository getUsersRepository() {
114:                return commandListservManager.getUsersRepository();
115:            }
116:
117:            /**
118:             * Writes the specified message to a mailet log file, prepended by
119:             * the mailet's name.
120:             *
121:             * @param message - a String specifying the message to be written to the log file
122:             */
123:            protected void log(String message) {
124:                StringBuffer logBuffer = new StringBuffer(256).append(
125:                        getCommandName()).append(": ").append(message);
126:                mailetContext.log(logBuffer.toString());
127:            }
128:
129:            /**
130:             * Writes an explanatory message and a stack trace for a given Throwable
131:             * exception to the mailet log file, prepended by the mailet's name.
132:             *
133:             * @param message - a String that describes the error or exception
134:             * @param t - the java.lang.Throwable error or exception
135:             */
136:            protected void log(String message, Throwable t) {
137:                StringBuffer logBuffer = new StringBuffer(256).append(
138:                        getCommandName()).append(": ").append(message);
139:                mailetContext.log(logBuffer.toString(), t);
140:            }
141:
142:            /**
143:             * Produces a standard response replyAddress to the sender
144:             * @param origMail
145:             * @param subject
146:             * @param message
147:             * @param replyAddress an optional custom replyAddress address
148:             * @throws MessagingException
149:             *
150:             * @see #generateMail
151:             * @see MailetContext#sendMail
152:             */
153:            protected void sendStandardReply(Mail origMail, String subject,
154:                    String message, String replyAddress)
155:                    throws MessagingException {
156:                MailAddress senderAddress = origMail.getSender();
157:                try {
158:                    MimeMessage mimeMessage = generateMail(senderAddress
159:                            .toString(), senderAddress.getUser(),
160:                            getCommandListservManager().getListOwner(),
161:                            getCommandListservManager().getListName(true),
162:                            subject, message);
163:                    if (replyAddress != null) {
164:                        mimeMessage.setHeader(RFC2822Headers.REPLY_TO,
165:                                replyAddress);
166:                    }
167:
168:                    getMailetContext().sendMail(mimeMessage);
169:                } catch (Exception e) {
170:                    throw new MessagingException(e.getMessage(), e);
171:                }
172:            }
173:
174:            /**
175:             * Use this to get standard properties for future calls to {@link org.apache.james.util.XMLResources}
176:             * @return properties with the "LIST_NAME" and the "DOMAIN_NAME" properties
177:             */
178:            protected Properties getStandardProperties() {
179:                return commandListservManager.getStandardProperties();
180:            }
181:
182:            /**
183:             * Send mail
184:             *
185:             * @param destEmailAddr the destination email addr: user@server.com
186:             * @param destDisplayName the display name
187:             * @param fromEmailAddr
188:             * @param fromDisplayName
189:             * @param emailSubject
190:             * @param emailPlainText
191:             * @throws Exception
192:             */
193:            protected MimeMessage generateMail(String destEmailAddr,
194:                    String destDisplayName, String fromEmailAddr,
195:                    String fromDisplayName, String emailSubject,
196:                    String emailPlainText) throws Exception {
197:                MimeMessage message = new MimeMessage(Session
198:                        .getDefaultInstance(System.getProperties(), null));
199:
200:                InternetAddress[] toAddrs = InternetAddress.parse(
201:                        destEmailAddr, false);
202:                toAddrs[0].setPersonal(destDisplayName);
203:                InternetAddress from = new InternetAddress(fromEmailAddr);
204:                from.setPersonal(fromDisplayName);
205:
206:                message.setRecipients(Message.RecipientType.TO, toAddrs);
207:                message.setFrom(from);
208:                message.setSubject(emailSubject);
209:                message.setSentDate(new java.util.Date());
210:
211:                MimeMultipart msgbody = new MimeMultipart();
212:                MimeBodyPart html = new MimeBodyPart();
213:                html.setDataHandler(new DataHandler(new MailDataSource(
214:                        emailPlainText, "text/plain")));
215:                msgbody.addBodyPart(html);
216:                message.setContent(msgbody);
217:                return message;
218:            }
219:
220:            protected XMLResources[] initXMLResources(String[] names)
221:                    throws ConfigurationException {
222:                return commandListservManager.initXMLResources(names);
223:            }
224:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.