1 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 ; 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 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 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 80 public String getCommandName() { 81 return commandName; 82 } 83 84 87 protected Configuration getConfiguration() { 88 return configuration; 89 } 90 91 95 protected ICommandListservManager getCommandListservManager() { 96 return commandListservManager; 97 } 98 99 103 protected MailetContext getMailetContext() { 104 return mailetContext; 105 } 106 107 110 protected UsersRepository getUsersRepository() { 111 return commandListservManager.getUsersRepository(); 112 } 113 114 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 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 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 179 protected Properties getStandardProperties() { 180 return commandListservManager.getStandardProperties(); 181 } 182 183 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 } 224 | Popular Tags |