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   * Subscribe handles the subscribe-confirm command.
37   * It is configured by:
38   * <pre>&lt;command name="subscribe-confirm" class="SubscribeConfirm"/&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>subscribe-confirm
46   *  <li>admincommands
47   * </ul>
48   *
49   * <br />
50   * <br />
51   * After formatting the text, the message is delivered with {@link #sendStandardReply(Mail, String, String, String)}
52   *
53   * <br />
54   * <br />
55   * This command basically sends the welcome message and adds the user to the mailing list.
56   *
57   * @version CVS $Revision: 684466 $ $Date: 2008-08-10 12:42:08 +0100 (Sun, 10 Aug 2008) $
58   * @since 2.2.0
59   * @see Subscribe
60   */
61  public class SubscribeConfirm extends BaseCommand {
62  
63      //For resources
64      protected XMLResources[] xmlResources;
65  
66      protected static final int SUBSCRIBE_CONFIRM = 0;
67      protected static final int ADMIN_COMMANDS = 1;
68  
69      public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
70          super.init(commandListservManager, configuration);
71          xmlResources = initXMLResources(new String[]{"subscribeConfirm", "admincommands"});
72      }
73  
74      /**
75       * After ensuring that the user isn't already subscribed, add the user to the
76       * mailing list, and send a welcome message.
77       *
78       * <br />
79       * <br />
80       *
81       * It uses the formatted text-based resources for its return mail body:
82       * <ul>
83       *  <li>{@link #SUBSCRIBE_CONFIRM}
84       *  <li>{@link #ADMIN_COMMANDS}
85       * </ul>
86       *
87       * @param mail
88       * @throws MessagingException
89       */
90      public void onCommand(Mail mail) throws MessagingException {
91          if (checkSubscriptionStatus(mail)) {
92              getUsersRepository().addUser(mail.getSender().toString(), "");
93  
94              //send mail
95              Properties props = getStandardProperties();
96              props.put("SENDER_ADDR", mail.getSender().toString());
97  
98              String confirmationMail = xmlResources[SUBSCRIBE_CONFIRM].getString("text", props);
99              String adminCommands = xmlResources[ADMIN_COMMANDS].getString("text", props);
100             String subject = xmlResources[SUBSCRIBE_CONFIRM].getString("welcome.subscribe.address", props);
101 
102             sendStandardReply(mail, subject, confirmationMail + adminCommands, null);
103         }
104     }
105 
106     /**
107      * Checks to see if this user is already subscribed, if so return false and send a message
108      * @param mail
109      * @return false if the user is subscribed, true otherwise
110      * @throws MessagingException
111      */
112     protected boolean checkSubscriptionStatus(Mail mail) throws MessagingException {
113         MailAddress mailAddress = mail.getSender();
114         UsersRepository usersRepository = getUsersRepository();
115         if (usersRepository.contains(mailAddress.toString())) {
116             getCommandListservManager().onError(mail,
117                     "Invalid request",
118                     xmlResources[SUBSCRIBE_CONFIRM].getString("already.subscribed", getStandardProperties()));
119             return false;
120         }
121         return true;
122     }
123 }