FileDocCategorySizeDatePackage
BaseCommand.javaAPI DocApache James 2.3.18417Fri Jan 12 12:56:30 GMT 2007org.apache.james.transport.mailets.listservcommands

BaseCommand

public abstract class BaseCommand extends Object implements IListServCommand
BaseCommand is a convience base class for any class that wishes to implement {@link IListServCommand}. It provides some functions like:
  • {@link #log}
  • {@link #sendStandardReply}
  • {@link #generateMail}


version
CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
since
2.2.0
see
org.apache.james.transport.mailets.CommandListservManager

Fields Summary
protected org.apache.avalon.framework.configuration.Configuration
configuration
protected org.apache.james.transport.mailets.ICommandListservManager
commandListservManager
protected String
commandName
protected org.apache.mailet.MailetContext
mailetContext
Constructors Summary
Methods Summary
protected javax.mail.internet.MimeMessagegenerateMail(java.lang.String destEmailAddr, java.lang.String destDisplayName, java.lang.String fromEmailAddr, java.lang.String fromDisplayName, java.lang.String emailSubject, java.lang.String emailPlainText)
Send mail

param
destEmailAddr the destination email addr: user@server.com
param
destDisplayName the display name
param
fromEmailAddr
param
fromDisplayName
param
emailSubject
param
emailPlainText
throws
Exception

        MimeMessage message = new MimeMessage(Session.getDefaultInstance(System.getProperties(), null));

        InternetAddress[] toAddrs = InternetAddress.parse(destEmailAddr, false);
        toAddrs[0].setPersonal(destDisplayName);
        InternetAddress from = new InternetAddress(fromEmailAddr);
        from.setPersonal(fromDisplayName);

        message.setRecipients(Message.RecipientType.TO, toAddrs);
        message.setFrom(from);
        message.setSubject(emailSubject);
        message.setSentDate(new java.util.Date());

        MimeMultipart msgbody = new MimeMultipart();
        MimeBodyPart html = new MimeBodyPart();
        html.setDataHandler(new DataHandler(new MailDataSource(emailPlainText, "text/plain")));
        msgbody.addBodyPart(html);
        message.setContent(msgbody);
        return message;
    
protected org.apache.james.transport.mailets.ICommandListservManagergetCommandListservManager()
The list serv manager

return
{@link ICommandListservManager}

        return commandListservManager;
    
public java.lang.StringgetCommandName()
The name of this command

see
IListServCommand#getCommandName

        return commandName;
    
protected org.apache.avalon.framework.configuration.ConfigurationgetConfiguration()

see
Configuration

        return configuration;
    
protected org.apache.mailet.MailetContextgetMailetContext()
The current mailet context

return
{@link MailetContext}

        return mailetContext;
    
protected java.util.PropertiesgetStandardProperties()
Use this to get standard properties for future calls to {@link org.apache.james.util.XMLResources}

return
properties with the "LIST_NAME" and the "DOMAIN_NAME" properties

        return commandListservManager.getStandardProperties();
    
protected org.apache.james.services.UsersRepositorygetUsersRepository()

see
ICommandListservManager#getUsersRepository

        return commandListservManager.getUsersRepository();
    
public voidinit(org.apache.james.transport.mailets.ICommandListservManager commandListservManager, org.apache.avalon.framework.configuration.Configuration configuration)
Perform any required initialization

param
configuration
throws
ConfigurationException

        this.commandListservManager = commandListservManager;
        this.configuration = configuration;
        commandName = configuration.getAttribute("name");
        mailetContext = this.commandListservManager.getMailetConfig().getMailetContext();
        log("Initialized listserv command: [" + commandName + ", " + getClass().getName() + "]");
    
protected org.apache.james.util.XMLResources[]initXMLResources(java.lang.String[] names)

        return commandListservManager.initXMLResources(names);
    
protected voidlog(java.lang.String message)
Writes the specified message to a mailet log file, prepended by the mailet's name.

param
message - a String specifying the message to be written to the log file

        StringBuffer logBuffer =
                new StringBuffer(256)
                .append(getCommandName())
                .append(": ")
                .append(message);
        mailetContext.log(logBuffer.toString());
    
protected voidlog(java.lang.String message, java.lang.Throwable t)
Writes an explanatory message and a stack trace for a given Throwable exception to the mailet log file, prepended by the mailet's name.

param
message - a String that describes the error or exception
param
t - the java.lang.Throwable error or exception

        StringBuffer logBuffer =
                new StringBuffer(256)
                .append(getCommandName())
                .append(": ")
                .append(message);
        mailetContext.log(logBuffer.toString(), t);
    
protected voidsendStandardReply(org.apache.mailet.Mail origMail, java.lang.String subject, java.lang.String message, java.lang.String replyAddress)
Produces a standard response replyAddress to the sender

param
origMail
param
subject
param
message
param
replyAddress an optional custom replyAddress address
throws
MessagingException
see
#generateMail
see
MailetContext#sendMail

        MailAddress senderAddress = origMail.getSender();
        try {
            MimeMessage mimeMessage = generateMail(senderAddress.toString(),
                    senderAddress.getUser(),
                    getCommandListservManager().getListOwner(),
                    getCommandListservManager().getListName(true),
                    subject,
                    message);
            if (replyAddress != null) {
                mimeMessage.setHeader(RFC2822Headers.REPLY_TO, replyAddress);
            }

            getMailetContext().sendMail(mimeMessage);
        } catch (Exception e) {
            throw new MessagingException(e.getMessage(), e);
        }