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