View Javadoc

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