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: }
|