You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by no...@apache.org on 2006/07/08 18:26:25 UTC

svn commit: r420161 - /james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java

Author: noel
Date: Sat Jul  8 09:26:24 2006
New Revision: 420161

URL: http://svn.apache.org/viewvc?rev=420161&view=rev
Log:
Initial version of a Mailet to log headers and/or messages for debugging purposes.

Added:
    james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java   (with props)

Added: james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java
URL: http://svn.apache.org/viewvc/james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java?rev=420161&view=auto
==============================================================================
--- james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java (added)
+++ james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java Sat Jul  8 09:26:24 2006
@@ -0,0 +1,110 @@
+/***********************************************************************
+ * 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";
+    }
+}

Propchange: james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java
------------------------------------------------------------------------------
    svn:eol-style = native



---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Re: Custom Mailets and JAMES v2.3

Posted by Stefano Bagnara <ap...@bago.org>.
Noel J. Bergman wrote:
> Stefano Bagnara wrote:
> 
>>  I can try to give some hints to some of the problems that could be
>> caused by MimeMessageWrapper and the CopyOnWriteProxy.
> 
> Is there something that we need to document generically for all of the users who do have custom mailets?
> 
> 	--- Noel

At a first thought I think that MimeMessageWrapper should never be used 
directly. Uses of previous MimeMessageWrapper have been replaced with 
MimeMessageCopyOnWriteProxy.

Now we keep the MimeMessage shared much more than before, maybe this 
will make bad usages to show up.

Stefano


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Custom Mailets and JAMES v2.3

Posted by "Noel J. Bergman" <no...@devtech.com>.
Stefano Bagnara wrote:

>  I can try to give some hints to some of the problems that could be
> caused by MimeMessageWrapper and the CopyOnWriteProxy.

Is there something that we need to document generically for all of the users who do have custom mailets?

	--- Noel


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Re: svn commit: r420161 - /james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java

Posted by Stefano Bagnara <ap...@bago.org>.
Noel J. Bergman wrote:
>> I rushed through this right now [...]
> 
> Just committed a version that at least compiles.  IM'ing Vincenzo as he works through debugging the server issues (again, he has no e-mail access).
> 
> 	--- Noel

Ask him informations on his configuration.
If I know something more of "server issue" I can try to give some hints 
to some of the problems that could be caused by MimeMessageWrapper and 
the CopyOnWriteProxy.

Stefano


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


RE: svn commit: r420161 - /james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java

Posted by "Noel J. Bergman" <no...@devtech.com>.
> I rushed through this right now [...]

Just committed a version that at least compiles.  IM'ing Vincenzo as he works through debugging the server issues (again, he has no e-mail access).

	--- Noel


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


RE: svn commit: r420161 - /james/server/trunk/src/java/org/apache/james/transport/mailets/LogMessage.java

Posted by "Noel J. Bergman" <no...@devtech.com>.
This is not really tested.  I modified an existing test mailet of mine to do more logging, and committed it as we had discussed at ApacheCon.  I rushed through this right now because Vincenzo is having problems with JAMES v2.3 on his production server, can't send mail at the moment, and should benefit from this in debugging.

	--- Noel


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org