FileDocCategorySizeDatePackage
LogMessage.javaAPI DocApache James 2.3.14779Fri Jan 12 12:56:30 GMT 2007org.apache.james.transport.mailets

LogMessage

public class LogMessage extends org.apache.mailet.GenericMailet
Logs Message Headers and/or Body. If the "passThrough" in confs is true the mail will be left untouched in the pipe. If false will be destroyed. Default is true.
version
This is $Revision: 1.8.4.2 $

Fields Summary
private boolean
passThrough
Whether this mailet should allow mails to be processed by additional mailets or mark it as finished.
private boolean
headers
private boolean
body
private int
bodyMax
private String
comment
Constructors Summary
Methods Summary
public java.lang.StringgetMailetInfo()
Return a string describing this mailet.

return
a string describing this mailet

        return "LogHeaders Mailet";
    
private java.lang.StringgetMessageHeaders(javax.mail.internet.MimeMessage message)
Utility method for obtaining a string representation of a Message's headers

        Enumeration heads = message.getAllHeaderLines();
        StringBuffer headBuffer = new StringBuffer(1024).append("\n");
        while(heads.hasMoreElements()) {
            headBuffer.append(heads.nextElement().toString()).append("\n");
        }
        return headBuffer.toString();
    
public voidinit()
Initialize the mailet, loading configuration information.


               
       
        try {
            passThrough = (getInitParameter("passThrough") == null) ? true : new Boolean(getInitParameter("passThrough")).booleanValue();
            headers = (getInitParameter("headers") == null) ? true : new Boolean(getInitParameter("headers")).booleanValue();
            body = (getInitParameter("body") == null) ? true : new Boolean(getInitParameter("body")).booleanValue();
            bodyMax = (getInitParameter("maxBody") == null) ? 0 : Integer.parseInt(getInitParameter("maxBody"));
            comment = getInitParameter("comment");
        } catch (Exception e) {
            // Ignore exception, default to true
        }
    
public voidservice(org.apache.mailet.Mail genericmail)
Log a particular message

param
mail the mail to process

        MailImpl mail = (MailImpl)genericmail;
        log(new StringBuffer(160).append("Logging mail ").append(mail.getName()).toString());
        if (comment != null) log(comment);
        try {
            if (headers) log(getMessageHeaders(mail.getMessage()));
            if (body) {
                int len = bodyMax > 0 ? bodyMax : mail.getMessage().getSize();
                StringBuffer text = new StringBuffer(len);
                InputStream is = mail.getMessage().getRawInputStream();
                byte[] buf = new byte[1024];
                int read = 0;
                while (text.length() < len && (read = is.read(buf)) > -1) {
                    text.append(new String(buf, 0, Math.min(read, len - text.length())));
                }
                log(text.toString());
            }
        }
        catch (MessagingException e) {
            log("Error logging message.", e);
        }
        catch (java.io.IOException e) {
            log("Error logging message.", e);
        }
        if (!passThrough) {
            mail.setState(Mail.GHOST);
        }