View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 The Apache Software Foundation.             *
3    * All rights reserved.                                                *
4    * ------------------------------------------------------------------- *
5    * Licensed under the Apache License, Version 2.0 (the "License"); you *
6    * may not use this file except in compliance with the License. You    *
7    * may obtain a copy of the License at:                                *
8    *                                                                     *
9    *     http://www.apache.org/licenses/LICENSE-2.0                      *
10   *                                                                     *
11   * Unless required by applicable law or agreed to in writing, software *
12   * distributed under the License is distributed on an "AS IS" BASIS,   *
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or     *
14   * implied.  See the License for the specific language governing       *
15   * permissions and limitations under the License.                      *
16   ***********************************************************************/
17  
18  package org.apache.james.transport.mailets;
19  
20  import java.util.Enumeration;
21  import javax.mail.MessagingException;
22  import javax.mail.internet.MimeMessage;
23  
24  import org.apache.james.core.MailImpl;
25  import org.apache.mailet.GenericMailet;
26  import org.apache.mailet.Mail;
27  
28  import java.io.InputStream;
29  import java.lang.StringBuffer;
30  
31  /***
32   * Logs Message Headers and/or Body.
33   * If the "passThrough" in confs is true the mail will be left untouched in
34   * the pipe. If false will be destroyed.  Default is true.
35   *
36   * @version This is $Revision: 1.8.4.2 $
37   */
38  public class LogMessage extends GenericMailet {
39  
40      /***
41       * Whether this mailet should allow mails to be processed by additional mailets
42       * or mark it as finished.
43       */
44      private boolean passThrough = true;
45      private boolean headers = true;
46      private boolean body = true;
47      private int bodyMax = 0;
48      private String comment = null;
49  
50      /***
51       * Initialize the mailet, loading configuration information.
52       */
53      public void init() {
54          try {
55              passThrough = (getInitParameter("passThrough") == null) ? true : new Boolean(getInitParameter("passThrough")).booleanValue();
56              headers = (getInitParameter("headers") == null) ? true : new Boolean(getInitParameter("headers")).booleanValue();
57              body = (getInitParameter("body") == null) ? true : new Boolean(getInitParameter("body")).booleanValue();
58              bodyMax = (getInitParameter("maxBody") == null) ? 0 : Integer.parseInt(getInitParameter("maxBody"));
59              comment = getInitParameter("comment");
60          } catch (Exception e) {
61              // Ignore exception, default to true
62          }
63      }
64  
65      /***
66       * Log a particular message
67       *
68       * @param mail the mail to process
69       */
70      public void service(Mail genericmail) {
71          MailImpl mail = (MailImpl)genericmail;
72          log(new StringBuffer(160).append("Logging mail ").append(mail.getName()).toString());
73          if (comment != null) log(comment);
74          try {
75              if (headers) log(getMessageHeaders(mail.getMessage()));
76              if (body) {
77                  int len = bodyMax > 0 ? bodyMax : mail.getMessage().getSize();
78                  StringBuffer text = new StringBuffer(len);
79                  InputStream is = mail.getMessage().getRawInputStream();
80                  byte[] buf = new byte[1024];
81                  int read = 0;
82                  while (text.length() < len && (read = is.read(buf)) > -1) {
83                      text.append(new String(buf, 0, Math.min(read, len - text.length())));
84                  }
85                  log(text.toString());
86              }
87          }
88          catch (MessagingException e) {
89              log("Error logging message.", e);
90          }
91          catch (java.io.IOException e) {
92              log("Error logging message.", e);
93          }
94          if (!passThrough) {
95              mail.setState(Mail.GHOST);
96          }
97      }
98  
99      /***
100      * Utility method for obtaining a string representation of a
101      * Message's headers
102      */
103     private String getMessageHeaders(MimeMessage message) throws MessagingException {
104         Enumeration heads = message.getAllHeaderLines();
105         StringBuffer headBuffer = new StringBuffer(1024).append("\n");
106         while(heads.hasMoreElements()) {
107             headBuffer.append(heads.nextElement().toString()).append("\n");
108         }
109         return headBuffer.toString();
110     }
111 
112     /***
113      * Return a string describing this mailet.
114      *
115      * @return a string describing this mailet
116      */
117     public String getMailetInfo() {
118         return "LogHeaders Mailet";
119     }
120 }