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  package org.apache.james.transport.mailets;
21  
22  import java.util.Enumeration;
23  import javax.mail.MessagingException;
24  import javax.mail.internet.MimeMessage;
25  
26  import org.apache.james.core.MailImpl;
27  import org.apache.mailet.GenericMailet;
28  import org.apache.mailet.Mail;
29  
30  import java.io.InputStream;
31  import java.lang.StringBuffer;
32  
33  /***
34   * Logs Message Headers and/or Body.
35   * If the "passThrough" in confs is true the mail will be left untouched in
36   * the pipe. If false will be destroyed.  Default is true.
37   *
38   * @version This is $Revision: 1.8.4.2 $
39   */
40  public class LogMessage extends GenericMailet {
41  
42      /***
43       * Whether this mailet should allow mails to be processed by additional mailets
44       * or mark it as finished.
45       */
46      private boolean passThrough = true;
47      private boolean headers = true;
48      private boolean body = true;
49      private int bodyMax = 0;
50      private String comment = null;
51  
52      /***
53       * Initialize the mailet, loading configuration information.
54       */
55      public void init() {
56          try {
57              passThrough = (getInitParameter("passThrough") == null) ? true : new Boolean(getInitParameter("passThrough")).booleanValue();
58              headers = (getInitParameter("headers") == null) ? true : new Boolean(getInitParameter("headers")).booleanValue();
59              body = (getInitParameter("body") == null) ? true : new Boolean(getInitParameter("body")).booleanValue();
60              bodyMax = (getInitParameter("maxBody") == null) ? 0 : Integer.parseInt(getInitParameter("maxBody"));
61              comment = getInitParameter("comment");
62          } catch (Exception e) {
63              // Ignore exception, default to true
64          }
65      }
66  
67      /***
68       * Log a particular message
69       *
70       * @param mail the mail to process
71       */
72      public void service(Mail genericmail) {
73          MailImpl mail = (MailImpl)genericmail;
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 }