1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38
39
40
41 public class LogMessage extends GenericMailet {
42
43
44
45
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
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
65 }
66 }
67
68
69
70
71
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
103
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
116
117
118
119 public String getMailetInfo() {
120 return "LogHeaders Mailet";
121 }
122 }