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.listservcommands;
19  
20  import org.apache.avalon.framework.configuration.Configuration;
21  import org.apache.avalon.framework.configuration.ConfigurationException;
22  import org.apache.james.transport.mailets.ICommandListservManager;
23  import org.apache.james.util.XMLResources;
24  import org.apache.james.services.UsersRepository;
25  import org.apache.mailet.Mail;
26  import org.apache.mailet.MailAddress;
27  
28  import javax.mail.MessagingException;
29  import java.util.Properties;
30  
31  /***
32   * UnSubscribeConfirm handles the unsubscribe-confirm command.
33   * It is configured by:
34   * <pre>&lt;command name="unsubscribe-confirm" class="UnSubscribeConfirm"/&gt;</pre>
35   *
36   * <br />
37   * <br />
38   *
39   * It uses the formatted text-based resources for its return mail body:
40   * <ul>
41   *  <li>unsubscribe-confirm
42   * </ul>
43   *
44   * <br />
45   * <br />
46   * After formatting the text, the message is delivered with {@link #sendStandardReply}
47   *
48   * <br />
49   * <br />
50   * This command basically sends a goodbye message and removes the user from the mailing list.
51   *
52   * @version CVS $Revision: 365582 $ $Date: 2006-01-03 08:51:21 +0000 (mar, 03 gen 2006) $
53   * @since 2.2.0
54   * @see UnSubscribe
55   */
56  public class UnSubscribeConfirm extends BaseCommand {
57  
58      //For resources
59      protected XMLResources xmlResources;
60  
61      public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
62          super.init(commandListservManager, configuration);
63          xmlResources = initXMLResources(new String[]{"unsubscribeConfirm"})[0];
64      }
65  
66      /***
67       * After ensuring that the user is currently subscribed, remove the user to the
68       * mailing list, and send a goodbye message.
69       *
70       * @param mail
71       * @throws MessagingException
72       */
73      public void onCommand(Mail mail) throws MessagingException {
74          if (checkSubscriptionStatus(mail)) {
75              getUsersRepository().removeUser(mail.getSender().toString());
76  
77              //send mail
78              Properties props = getStandardProperties();
79              props.put("SENDER_ADDR", mail.getSender().toString());
80  
81              String confirmationMail = xmlResources.getString("text", props);
82              String subject = xmlResources.getString("goodbye.subscribe.address", props);
83  
84              sendStandardReply(mail, subject, confirmationMail, null);
85          }
86      }
87  
88      /***
89       * Checks to see that this user is already subscribed, if not return false and send a message
90       * @param mail
91       * @return false if the user isn't subscribed, true otherwise
92       * @throws MessagingException
93       */
94      protected boolean checkSubscriptionStatus(Mail mail) throws MessagingException {
95          MailAddress mailAddress = mail.getSender();
96          UsersRepository usersRepository = getUsersRepository();
97          if (!usersRepository.contains(mailAddress.toString())) {
98              getCommandListservManager().onError(mail,
99                      "Invalid request",
100                     xmlResources.getString("not.subscribed", getStandardProperties()));
101             return false;
102         }
103         return true;
104     }
105 }