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