KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > samples > mailets > InstrumentationMailet


1 package org.apache.james.samples.mailets;
2
3 /*
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.io.IOException JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Enumeration JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import javax.mail.Address JavaDoc;
23 import javax.mail.Flags JavaDoc;
24 import javax.mail.Header JavaDoc;
25 import javax.mail.MessagingException JavaDoc;
26 import javax.mail.Flags.Flag;
27 import javax.mail.internet.InternetAddress JavaDoc;
28 import javax.mail.internet.MimeMessage JavaDoc;
29 import javax.mail.internet.NewsAddress JavaDoc;
30 import javax.mail.internet.MimeMessage.RecipientType;
31
32 import org.apache.mailet.Mail;
33 import org.apache.mailet.MailAddress;
34 import org.apache.mailet.Mailet;
35 import org.apache.mailet.MailetConfig;
36 import org.apache.mailet.MailetContext;
37
38 /**
39  * Mailet just prints out the details of a message.
40  * Sometimes Useful for debugging.
41  */

42 public class InstrumentationMailet implements Mailet {
43
44     private MailetConfig config;
45     
46     public void destroy() {
47
48     }
49
50     public String JavaDoc getMailetInfo() {
51         return "Example mailet";
52     }
53
54     public MailetConfig getMailetConfig() {
55         return config;
56     }
57
58     public void init(MailetConfig config) throws MessagingException JavaDoc {
59         this.config = config;
60     }
61
62     public void service(Mail mail) throws MessagingException JavaDoc {
63         MailetContext context = config.getMailetContext();
64         context.log("######## MAIL STARTS");
65         context.log("");
66         
67         MimeMessage JavaDoc message = mail.getMessage();
68         
69         context.log("Mail named: " + mail.getName());
70         
71         for (Iterator JavaDoc it=mail.getAttributeNames(); it.hasNext();) {
72             String JavaDoc attributeName = (String JavaDoc) it.next();
73             context.log("Attribute " + attributeName);
74         }
75         context.log("Message size: " + mail.getMessageSize());
76         context.log("Last updated: " + mail.getLastUpdated());
77         context.log("Remote Address: " + mail.getRemoteAddr());
78         context.log("Remote Host: " + mail.getRemoteHost());
79         context.log("State: " + mail.getState());
80         context.log("Sender host: " + mail.getSender().getHost());
81         context.log("Sender user: " + mail.getSender().getUser());
82         Collection JavaDoc recipients = mail.getRecipients();
83         for (Iterator JavaDoc it = recipients.iterator(); it.hasNext();)
84         {
85             MailAddress address = (MailAddress) it.next();
86             context.log("Recipient: " + address.getUser() + "@" + address.getHost());
87         }
88         
89         context.log("Subject: " + message.getSubject());
90         context.log("MessageID: " + message.getMessageID());
91         context.log("Received: " + message.getReceivedDate());
92         context.log("Sent: " + message.getSentDate());
93         
94         Enumeration JavaDoc allHeadersLines = message.getAllHeaderLines();
95         while(allHeadersLines.hasMoreElements()) {
96             String JavaDoc header = (String JavaDoc) allHeadersLines.nextElement();
97             context.log("Header Line:= " + header);
98         }
99         
100         
101         Enumeration JavaDoc allHeadersEnumeration = message.getAllHeaders();
102         while(allHeadersEnumeration.hasMoreElements()) {
103             Header JavaDoc header = (Header JavaDoc) allHeadersEnumeration.nextElement();
104             context.log("Header: " + header.getName() + "=" + header.getValue());
105         }
106         
107         Address JavaDoc[] to = message.getRecipients(RecipientType.TO);
108         printAddresses(to, "TO: ");
109         Address JavaDoc[] cc = message.getRecipients(RecipientType.CC);
110         printAddresses(cc, "CC: ");
111         Address JavaDoc[] bcc = message.getRecipients(RecipientType.BCC);
112         printAddresses(bcc, "BCC: ");
113
114         
115         Flags JavaDoc flags = message.getFlags();
116         Flag[] systemFlags = flags.getSystemFlags();
117         for (int i=0;i<systemFlags.length;i++) {
118             context.log("System Flag:" + systemFlags[i]);
119         }
120         String JavaDoc[] userFlags = flags.getUserFlags();
121         for (int i=0;i<userFlags.length;i++) {
122             context.log("User flag: " + userFlags[i]);
123         }
124         
125         String JavaDoc mimeType = message.getContentType();
126         context.log("Mime type: " + mimeType);
127         if (mimeType == "text/plain") {
128             try {
129                 Object JavaDoc content = message.getContent();
130                 context.log("Content: " + content);
131             } catch (IOException JavaDoc e) {
132                 e.printStackTrace();
133             }
134             
135         }
136         
137         context.log("");
138         context.log("######## MAIL ENDS");
139     }
140     
141     private final void printAddresses(Address JavaDoc[] addresses, String JavaDoc prefix) {
142         MailetContext context = config.getMailetContext();
143         for (int i=0;i<addresses.length;i++) {
144             if (addresses[i] instanceof InternetAddress JavaDoc) {
145                 InternetAddress JavaDoc address = (InternetAddress JavaDoc) addresses[i];
146                 context.log(prefix + address.getPersonal() + "@" + address.getAddress());
147             } else if (addresses[i] instanceof NewsAddress JavaDoc) {
148                 NewsAddress JavaDoc address = (NewsAddress JavaDoc) addresses[i];
149                 context.log(prefix + address.getNewsgroup() + "@" + address.getHost());
150             }
151         }
152     }
153 }
154
Popular Tags