View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 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.mailet.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;
31  import javax.mail.Message;
32  import javax.mail.MessagingException;
33  import javax.mail.Session;
34  import javax.mail.internet.InternetAddress;
35  import javax.mail.internet.MimeBodyPart;
36  import javax.mail.internet.MimeMessage;
37  import javax.mail.internet.MimeMultipart;
38  import java.util.Properties;
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: 365582 $ $Date: 2006-01-03 08:51:21 +0000 (mar, 03 gen 2006) $
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 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 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 message) {
121         StringBuffer logBuffer =
122                 new StringBuffer(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 message, Throwable t) {
137         StringBuffer logBuffer =
138                 new StringBuffer(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 subject, String message, String replyAddress) throws MessagingException {
157         MailAddress senderAddress = origMail.getSender();
158         try {
159             MimeMessage 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 e) {
171             throw new MessagingException(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 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 generateMail(String destEmailAddr,
195                                        String destDisplayName,
196                                        String fromEmailAddr,
197                                        String fromDisplayName,
198                                        String emailSubject,
199                                        String emailPlainText) throws Exception {
200         MimeMessage message = new MimeMessage(Session.getDefaultInstance(System.getProperties(), null));
201 
202         InternetAddress[] toAddrs = InternetAddress.parse(destEmailAddr, false);
203         toAddrs[0].setPersonal(destDisplayName);
204         InternetAddress from = new InternetAddress(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());
211 
212         MimeMultipart msgbody = new MimeMultipart();
213         MimeBodyPart html = new MimeBodyPart();
214         html.setDataHandler(new DataHandler(new MailDataSource(emailPlainText, "text/plain")));
215         msgbody.addBodyPart(html);
216         message.setContent(msgbody);
217         return message;
218     }
219 
220     protected XMLResources[] initXMLResources(String[] names) throws ConfigurationException {
221         return commandListservManager.initXMLResources(names);
222     }
223 }