View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 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.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(false);
42          response.setSubject("The time is now...");
43          StringBuffer textBuffer =
44              new StringBuffer(128)
45                      .append("This mail server thinks it's ")
46                      .append((new java.util.Date()).toString())
47                      .append(".");
48          response.setText(textBuffer.toString());
49  
50          // Someone manually checking the server time by hand may send
51          // an formatted message, lacking From and To headers.  If the
52          // response fields are null, try setting them from the SMTP
53          // MAIL FROM/RCPT TO commands used to send the inquiry.
54  
55          if (response.getFrom() == null) {
56              response.setFrom(((MailAddress)mail.getRecipients().iterator().next()).toInternetAddress());
57          }
58  
59          if (response.getAllRecipients() == null) {
60              response.setRecipients(MimeMessage.RecipientType.TO, mail.getSender().toString());
61          }
62  
63          response.saveChanges();
64          getMailetContext().sendMail(response);
65      }
66  
67      /***
68       * Return a string describing this mailet.
69       *
70       * @return a string describing this mailet
71       */
72      public String getMailetInfo() {
73          return "ServerTime Mailet";
74      }
75  }
76