KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.mailet.GenericMailet;
21 import org.apache.mailet.Mail;
22 import org.apache.mailet.MailAddress;
23
24 import javax.mail.Address JavaDoc;
25 import javax.mail.internet.InternetAddress JavaDoc;
26 import javax.mail.internet.MimeMessage JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Set JavaDoc;
29
30 /**
31  * Returns the current time for the mail server. Sample configuration:
32  * <mailet match="RecipientIs=time@cadenza.lokitech.com" class="ServerTime">
33  * </mailet>
34  *
35  */

36 public class ServerTime extends GenericMailet {
37     /**
38      * Sends a message back to the sender indicating what time the server thinks it is.
39      *
40      * @param mail the mail being processed
41      *
42      * @throws MessagingException if an error is encountered while formulating the reply message
43      */

44     public void service(Mail mail) throws javax.mail.MessagingException JavaDoc {
45         MimeMessage JavaDoc response = (MimeMessage JavaDoc)mail.getMessage().reply(false);
46         response.setSubject("The time is now...");
47         StringBuffer JavaDoc textBuffer =
48             new StringBuffer JavaDoc(128)
49                     .append("This mail server thinks it's ")
50                     .append((new java.util.Date JavaDoc()).toString())
51                     .append(".");
52         response.setText(textBuffer.toString());
53
54         // Someone manually checking the server time by hand may send
55
// an formatted message, lacking From and To headers. If the
56
// response fields are null, try setting them from the SMTP
57
// MAIL FROM/RCPT TO commands used to send the inquiry.
58

59         if (response.getFrom() == null) {
60             response.setFrom(((MailAddress)mail.getRecipients().iterator().next()).toInternetAddress());
61         }
62
63         if (response.getAllRecipients() == null) {
64             response.setRecipients(MimeMessage.RecipientType.TO, mail.getSender().toString());
65         }
66
67         response.saveChanges();
68         getMailetContext().sendMail(response);
69     }
70
71     /**
72      * Return a string describing this mailet.
73      *
74      * @return a string describing this mailet
75      */

76     public String JavaDoc getMailetInfo() {
77         return "ServerTime Mailet";
78     }
79 }
80
81
Popular Tags