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
21
22 package org.apache.james.transport.mailets;
23
24 import org.apache.mailet.base.GenericMailet;
25 import org.apache.mailet.Mail;
26 import org.apache.mailet.MailAddress;
27
28 import javax.mail.internet.MimeMessage;
29
30 /**
31 * Returns the current time for the mail server. Sample configuration:
32 * <pre><code>
33 * <mailet match="RecipientIs=time@cadenza.lokitech.com" class="ServerTime">
34 * </mailet>
35 * </code></pre>
36 *
37 */
38 public class ServerTime extends GenericMailet {
39 /**
40 * Sends a message back to the sender indicating what time the server thinks it is.
41 *
42 * @param mail the mail being processed
43 *
44 * @throws javax.mail.MessagingException if an error is encountered while formulating the reply message
45 */
46 public void service(Mail mail) throws javax.mail.MessagingException {
47 MimeMessage response = (MimeMessage)mail.getMessage().reply(false);
48 response.setSubject("The time is now...");
49 StringBuffer textBuffer =
50 new StringBuffer(128)
51 .append("This mail server thinks it's ")
52 .append((new java.util.Date()).toString())
53 .append(".");
54 response.setText(textBuffer.toString());
55
56 // Someone manually checking the server time by hand may send
57 // an formatted message, lacking From and To headers. If the
58 // response fields are null, try setting them from the SMTP
59 // MAIL FROM/RCPT TO commands used to send the inquiry.
60
61 if (response.getFrom() == null) {
62 response.setFrom(((MailAddress)mail.getRecipients().iterator().next()).toInternetAddress());
63 }
64
65 if (response.getAllRecipients() == null) {
66 response.setRecipients(MimeMessage.RecipientType.TO, mail.getSender().toString());
67 }
68
69 response.saveChanges();
70 getMailetContext().sendMail(response);
71 }
72
73 /**
74 * Return a string describing this mailet.
75 *
76 * @return a string describing this mailet
77 */
78 public String getMailetInfo() {
79 return "ServerTime Mailet";
80 }
81 }
82