/***********************************************************************
 * Copyright (c) 2000-2006 The Apache Software Foundation.             *
 * All rights reserved.                                                *
 * ------------------------------------------------------------------- *
 * Licensed under the Apache License, Version 2.0 (the "License"); you *
 * may not use this file except in compliance with the License. You    *
 * may obtain a copy of the License at:                                *
 *                                                                     *
 *     http://www.apache.org/licenses/LICENSE-2.0                      *
 *                                                                     *
 * Unless required by applicable law or agreed to in writing, software *
 * distributed under the License is distributed on an "AS IS" BASIS,   *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
 * implied.  See the License for the specific language governing       *
 * permissions and limitations under the License.                      *
 ***********************************************************************/

package org.apache.james.transport.mailets;

import java.util.Enumeration;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.james.core.MailImpl;
import org.apache.mailet.GenericMailet;
import org.apache.mailet.Mail;

/**
 * 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 $
 */
public class LogMessage extends GenericMailet {

    /**
     * Whether this mailet should allow mails to be processed by additional mailets
     * or mark it as finished.
     */
    private boolean passThrough = true;
    private boolean headers = true;
    private boolean body = true;
    private int bodyMax = 0;

    /**
     * Initialize the mailet, loading configuration information.
     */
    public void init() {
        try {
            passThrough = (getInitParameter("passThrough") == null) ? true : new Boolean(getInitParameter("passThrough")).booleanValue();
            headers = (getInitParameter("headers") == null) ? true : new Boolean(getInitParameter("headers")).booleanValue();
            passThrough = (getInitParameter("body") == null) ? true : new Boolean(getInitParameter("body")).booleanValue();
	    bodyMax = (getInitParameter("maxBody") == null) ? 0 : Integer.parseInt(getInitParameter("maxBody"));
        } catch (Exception e) {
            // Ignore exception, default to true
        }
    }

    /**
     * Log a particular message
     *
     * @param mail the mail to process
     */
    public void service(Mail genericmail) {
        MailImpl mail = (MailImpl)genericmail;
        log(new StringBuffer(160).append("Logging mail ").append(mail.getName()).toString());
        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];
		while (text.length() < len && read = is.read(buf) > -1) {
		    text.append(buf, 0, Math.min(read, len - text.length()));
		}
		log(text.toString());
	    }
        }
        catch (MessagingException e) {
            log("Error logging headers.");
        }
        if (!passThrough) {
            mail.setState(Mail.GHOST);
        }
    }

    /**
     * Utility method for obtaining a string representation of a
     * Message's headers
     */
    private String getMessageHeaders(MimeMessage message) throws MessagingException {
        Enumeration heads = message.getAllHeaderLines();
        StringBuffer headBuffer = new StringBuffer(1024).append("\n");
        while(heads.hasMoreElements()) {
            headBuffer.append(heads.nextElement().toString()).append("\n");
        }
        return headBuffer.toString();
    }

    /**
     * Return a string describing this mailet.
     *
     * @return a string describing this mailet
     */
    public String getMailetInfo() {
        return "LogHeaders Mailet";
    }
}

@@ -1428,16 +1428,76 @@
 .Mail;%0A%0A
+import java.io.InputStream;%0Aimport java.lang.StringBuffer;%0A%0A
 /**%0A * L
@@ -2539,17 +2539,20 @@
 alue();%0A
-%09
+    
     body
@@ -3131,18 +3131,24 @@
 body) %7B%0A
-%09%09
+        
 int len 
@@ -3202,18 +3202,24 @@
 Size();%0A
-%09%09
+        
 StringBu
@@ -3253,18 +3253,24 @@
 r(len);%0A
-%09%09
+        
 InputStr
@@ -3317,18 +3317,24 @@
 ream();%0A
-%09%09
+        
 byte%5B%5D b
@@ -3354,18 +3354,46 @@
 %5B1024%5D;%0A
-%09%09
+        int read = 0;%0A        
 while (t
@@ -3414,16 +3414,17 @@
  len && 
+(
 read = i
@@ -3434,16 +3434,17 @@
 ead(buf)
+)
  %3E -1) %7B
@@ -3444,18 +3444,24 @@
 %3E -1) %7B%0A
-%09%09
+        
     text
@@ -3468,16 +3468,27 @@
 .append(
+new String(
 buf, 0, 
@@ -3527,16 +3527,29 @@
 ()))
+)
 ;%0A
-%09%09%7D%0A%09%09
+        %7D%0A        
 log(
@@ -3570,9 +3570,12 @@
 ));%0A
-%09
+    
     
@@ -3660,17 +3660,116 @@
 ing 
-headers.%22
+message.%22, e);%0A        %7D%0A        catch (java.io.IOException e) %7B%0A            log(%22Error logging message.%22, e
 );%0A 

Fix compile errors.