KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > LogHeaders


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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 JavaDoc;
21 import javax.mail.MessagingException JavaDoc;
22 import javax.mail.internet.MimeMessage JavaDoc;
23
24 import org.apache.james.core.MailImpl;
25 import org.apache.mailet.GenericMailet;
26 import org.apache.mailet.Mail;
27
28 /**
29  * Logs Message Headers.
30  * If the "passThrough" in confs is true the mail will be left untouched in
31  * the pipe. If false will be destroyed. Default is true.
32  *
33  * @version This is $Revision: 1.8.4.2 $
34  */

35 public class LogHeaders extends GenericMailet {
36
37     /**
38      * Whether this mailet should allow mails to be processed by additional mailets
39      * or mark it as finished.
40      */

41     private boolean passThrough = true;
42
43     /**
44      * Initialize the mailet, loading configuration information.
45      */

46     public void init() {
47         try {
48             passThrough = (getInitParameter("passThrough") == null) ? true : new Boolean JavaDoc(getInitParameter("debug")).booleanValue();
49         } catch (Exception JavaDoc e) {
50             // Ignore exception, default to true
51
}
52     }
53
54     /**
55      * Log a particular message
56      *
57      * @param mail the mail to process
58      */

59     public void service(Mail genericmail) {
60         MailImpl mail = (MailImpl)genericmail;
61         log(new StringBuffer JavaDoc(160).append("Logging mail ").append(mail.getName()).toString());
62         try {
63             log(getMessageHeaders(mail.getMessage()));
64         }
65         catch (MessagingException JavaDoc e) {
66             log("Error logging headers.");
67         }
68         if (!passThrough) {
69             mail.setState(Mail.GHOST);
70         }
71     }
72
73     /**
74      * Utility method for obtaining a string representation of a
75      * Message's headers
76      */

77     private String JavaDoc getMessageHeaders(MimeMessage JavaDoc message) throws MessagingException JavaDoc {
78         Enumeration JavaDoc heads = message.getAllHeaderLines();
79         StringBuffer JavaDoc headBuffer = new StringBuffer JavaDoc(1024).append("\n");
80         while(heads.hasMoreElements()) {
81             headBuffer.append(heads.nextElement().toString()).append("\n");
82         }
83         return headBuffer.toString();
84     }
85
86     /**
87      * Return a string describing this mailet.
88      *
89      * @return a string describing this mailet
90      */

91     public String JavaDoc getMailetInfo() {
92         return "LogHeaders Mailet";
93     }
94 }
95
Popular Tags