View Javadoc

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