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