01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/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.internet.MimeMessage;
25:
26: /**
27: * Returns the current time for the mail server. Sample configuration:
28: * <mailet match="RecipientIs=time@cadenza.lokitech.com" class="ServerTime">
29: * </mailet>
30: *
31: */
32: public class ServerTime extends GenericMailet {
33: /**
34: * Sends a message back to the sender indicating what time the server thinks it is.
35: *
36: * @param mail the mail being processed
37: *
38: * @throws MessagingException if an error is encountered while formulating the reply message
39: */
40: public void service(Mail mail) throws javax.mail.MessagingException {
41: MimeMessage response = (MimeMessage) mail.getMessage().reply(
42: false);
43: response.setSubject("The time is now...");
44: StringBuffer textBuffer = new StringBuffer(128).append(
45: "This mail server thinks it's ").append(
46: (new java.util.Date()).toString()).append(".");
47: response.setText(textBuffer.toString());
48:
49: // Someone manually checking the server time by hand may send
50: // an formatted message, lacking From and To headers. If the
51: // response fields are null, try setting them from the SMTP
52: // MAIL FROM/RCPT TO commands used to send the inquiry.
53:
54: if (response.getFrom() == null) {
55: response.setFrom(((MailAddress) mail.getRecipients()
56: .iterator().next()).toInternetAddress());
57: }
58:
59: if (response.getAllRecipients() == null) {
60: response.setRecipients(MimeMessage.RecipientType.TO, mail
61: .getSender().toString());
62: }
63:
64: response.saveChanges();
65: getMailetContext().sendMail(response);
66: }
67:
68: /**
69: * Return a string describing this mailet.
70: *
71: * @return a string describing this mailet
72: */
73: public String getMailetInfo() {
74: return "ServerTime Mailet";
75: }
76: }
|