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