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  
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.MessagingException;
29  
30  /**
31   * An abstract implementation of a listserv manager.  This mailet reads the
32   * address to find the command.
33   */
34  public abstract class GenericListservManager extends GenericMailet {
35  
36      /**
37       * Adds an address to the listserv.  Returns whether command was
38       * successful.
39       */
40      public abstract boolean addAddress(MailAddress address);
41  
42      /**
43       * Removes an address from the listserv.  Returns whether command
44       * was successful.
45       */
46      public abstract boolean removeAddress(MailAddress address);
47  
48  
49      /**
50       * Indicates whether an address already exists on the listserv. Returns
51       * whether the address exists.
52       */
53      public abstract boolean existsAddress(MailAddress address);
54  
55      /**
56       * Processes the message.  Checks which command was sent based on the
57       * recipient address, and does the appropriate action.
58       */
59      public final void service(Mail mail) throws MessagingException {
60          if (mail.getRecipients().size() != 1) {
61              getMailetContext().bounce(mail, "You can only send one command at a time to this listserv manager.");
62              return;
63          }
64          MailAddress address = (MailAddress)mail.getRecipients().iterator().next();
65          if (address.getUser().endsWith("-off")) {
66              if (existsAddress(mail.getSender())) {
67                  if (removeAddress(mail.getSender())) {
68                      getMailetContext().bounce(mail, "Successfully removed from listserv.");
69                  } else {
70                      getMailetContext().bounce(mail, "Unable to remove you from listserv for some reason.");
71                  }
72              } else {
73                  getMailetContext().bounce(mail, "You are not subscribed to this listserv.");
74              }
75          } else if (address.getUser().endsWith("-on")) {
76              if (existsAddress(mail.getSender())) {
77                  getMailetContext().bounce(mail, "You are already subscribed to this listserv.");
78              } else {
79                  if (addAddress(mail.getSender())) {
80                      getMailetContext().bounce(mail, "Successfully added to listserv.");
81                  } else {
82                      getMailetContext().bounce(mail, "Unable to add you to the listserv for some reason");
83                  }
84              }
85          } else {
86              getMailetContext().bounce(mail, "Could not understand the command you sent to this listserv manager.\r\n"
87                                        + "Valid commands are <listserv>-on@domain.com and <listserv>-off@domain.com");
88          }
89          //Kill the command message
90          mail.setState(Mail.GHOST);
91      }
92  }