KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > listservcommands > BaseCommand


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.transport.mailets.listservcommands;
19
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.james.services.UsersRepository;
23 import org.apache.james.transport.mailets.ICommandListservManager;
24 import org.apache.james.util.RFC2822Headers;
25 import org.apache.james.util.XMLResources;
26 import org.apache.mailet.Mail;
27 import org.apache.mailet.MailAddress;
28 import org.apache.mailet.MailetContext;
29
30 import javax.activation.DataHandler JavaDoc;
31 import javax.mail.Message JavaDoc;
32 import javax.mail.MessagingException JavaDoc;
33 import javax.mail.Session JavaDoc;
34 import javax.mail.internet.InternetAddress JavaDoc;
35 import javax.mail.internet.MimeBodyPart JavaDoc;
36 import javax.mail.internet.MimeMessage JavaDoc;
37 import javax.mail.internet.MimeMultipart JavaDoc;
38 import java.util.Properties JavaDoc;
39
40 /**
41  * BaseCommand is a convience base class for any class that wishes to implement {@link IListServCommand}.
42  * It provides some functions like:
43  * <ul>
44  * <li>{@link #log}
45  * <li>{@link #sendStandardReply}
46  * <li>{@link #generateMail}
47  * </ul>
48  *
49  * <br />
50  * <br />
51  *
52  * @version CVS $Revision: 1.1.2.4 $ $Date: 2004/04/16 20:59:53 $
53  * @since 2.2.0
54  * @see org.apache.james.transport.mailets.CommandListservManager
55  */

56 public abstract class BaseCommand implements IListServCommand {
57
58     protected Configuration configuration;
59     protected ICommandListservManager commandListservManager;
60     protected String JavaDoc commandName;
61     protected MailetContext mailetContext;
62
63     /**
64      * Perform any required initialization
65      * @param configuration
66      * @throws ConfigurationException
67      */

68     public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
69         this.commandListservManager = commandListservManager;
70         this.configuration = configuration;
71         commandName = configuration.getAttribute("name");
72         mailetContext = this.commandListservManager.getMailetConfig().getMailetContext();
73         log("Initialized listserv command: [" + commandName + ", " + getClass().getName() + "]");
74     }
75
76     /**
77      * The name of this command
78      * @see IListServCommand#getCommandName
79      */

80     public String JavaDoc getCommandName() {
81         return commandName;
82     }
83
84     /**
85      * @see Configuration
86      */

87     protected Configuration getConfiguration() {
88         return configuration;
89     }
90
91     /**
92      * The list serv manager
93      * @return {@link ICommandListservManager}
94      */

95     protected ICommandListservManager getCommandListservManager() {
96         return commandListservManager;
97     }
98
99     /**
100      * The current mailet context
101      * @return {@link MailetContext}
102      */

103     protected MailetContext getMailetContext() {
104         return mailetContext;
105     }
106
107     /**
108      * @see ICommandListservManager#getUsersRepository
109      */

110     protected UsersRepository getUsersRepository() {
111         return commandListservManager.getUsersRepository();
112     }
113
114     /**
115      * Writes the specified message to a mailet log file, prepended by
116      * the mailet's name.
117      *
118      * @param message - a String specifying the message to be written to the log file
119      */

120     protected void log(String JavaDoc message) {
121         StringBuffer JavaDoc logBuffer =
122                 new StringBuffer JavaDoc(256)
123                 .append(getCommandName())
124                 .append(": ")
125                 .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 JavaDoc message, Throwable JavaDoc t) {
137         StringBuffer JavaDoc logBuffer =
138                 new StringBuffer JavaDoc(256)
139                 .append(getCommandName())
140                 .append(": ")
141                 .append(message);
142         mailetContext.log(logBuffer.toString(), t);
143     }
144
145     /**
146      * Produces a standard response replyAddress to the sender
147      * @param origMail
148      * @param subject
149      * @param message
150      * @param replyAddress an optional custom replyAddress address
151      * @throws MessagingException
152      *
153      * @see #generateMail
154      * @see MailetContext#sendMail
155      */

156     protected void sendStandardReply(Mail origMail, String JavaDoc subject, String JavaDoc message, String JavaDoc replyAddress) throws MessagingException JavaDoc {
157         MailAddress senderAddress = origMail.getSender();
158         try {
159             MimeMessage JavaDoc mimeMessage = generateMail(senderAddress.toString(),
160                     senderAddress.getUser(),
161                     getCommandListservManager().getListOwner(),
162                     getCommandListservManager().getListName(true),
163                     subject,
164                     message);
165             if (replyAddress != null) {
166                 mimeMessage.setHeader(RFC2822Headers.REPLY_TO, replyAddress);
167             }
168
169             getMailetContext().sendMail(mimeMessage);
170         } catch (Exception JavaDoc e) {
171             throw new MessagingException JavaDoc(e.getMessage(), e);
172         }
173     }
174
175     /**
176      * Use this to get standard properties for future calls to {@link org.apache.james.util.XMLResources}
177      * @return properties with the "LIST_NAME" and the "DOMAIN_NAME" properties
178      */

179     protected Properties JavaDoc getStandardProperties() {
180         return commandListservManager.getStandardProperties();
181     }
182
183     /**
184      * Send mail
185      *
186      * @param destEmailAddr the destination email addr: user@server.com
187      * @param destDisplayName the display name
188      * @param fromEmailAddr
189      * @param fromDisplayName
190      * @param emailSubject
191      * @param emailPlainText
192      * @throws Exception
193      */

194     protected MimeMessage JavaDoc generateMail(String JavaDoc destEmailAddr,
195                                        String JavaDoc destDisplayName,
196                                        String JavaDoc fromEmailAddr,
197                                        String JavaDoc fromDisplayName,
198                                        String JavaDoc emailSubject,
199                                        String JavaDoc emailPlainText) throws Exception JavaDoc {
200         MimeMessage JavaDoc message = new MimeMessage JavaDoc(Session.getDefaultInstance(System.getProperties(), null));
201
202         InternetAddress JavaDoc[] toAddrs = InternetAddress.parse(destEmailAddr, false);
203         toAddrs[0].setPersonal(destDisplayName);
204         InternetAddress JavaDoc from = new InternetAddress JavaDoc(fromEmailAddr);
205         from.setPersonal(fromDisplayName);
206
207         message.setRecipients(Message.RecipientType.TO, toAddrs);
208         message.setFrom(from);
209         message.setSubject(emailSubject);
210         message.setSentDate(new java.util.Date JavaDoc());
211
212         MimeMultipart JavaDoc msgbody = new MimeMultipart JavaDoc();
213         MimeBodyPart JavaDoc html = new MimeBodyPart JavaDoc();
214         html.setDataHandler(new DataHandler JavaDoc(new MailDataSource(emailPlainText, "text/plain")));
215         msgbody.addBodyPart(html);
216         message.setContent(msgbody);
217         return message;
218     }
219
220     protected XMLResources[] initXMLResources(String JavaDoc[] names) throws ConfigurationException {
221         return commandListservManager.initXMLResources(names);
222     }
223 }
224
Popular Tags