KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > listservcommands > SubscribeConfirm


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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 JavaDoc;
29 import java.util.Properties JavaDoc;
30
31 /**
32  * Subscribe handles the subscribe-confirm command.
33  * It is configured by:
34  * <pre>&lt;command name="subscribe-confirm" class="SubscribeConfirm"/&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>subscribe-confirm
42  * <li>admincommands
43  * </ul>
44  *
45  * <br />
46  * <br />
47  * After formatting the text, the message is delivered with {@link #sendStandardReply}
48  *
49  * <br />
50  * <br />
51  * This command basically sends the welcome message and adds the user to the mailing list.
52  *
53  * @version CVS $Revision: 1.1.2.3 $ $Date: 2004/03/15 03:54:20 $
54  * @since 2.2.0
55  * @see Subscribe
56  */

57 public class SubscribeConfirm extends BaseCommand {
58
59     //For resources
60
protected XMLResources[] xmlResources;
61
62     protected static final int SUBSCRIBE_CONFIRM = 0;
63     protected static final int ADMIN_COMMANDS = 1;
64
65     public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
66         super.init(commandListservManager, configuration);
67         xmlResources = initXMLResources(new String JavaDoc[]{"subscribeConfirm", "admincommands"});
68     }
69
70     /**
71      * After ensuring that the user isn't already subscribed, add the user to the
72      * mailing list, and send a welcome message.
73      *
74      * <br />
75      * <br />
76      *
77      * It uses the formatted text-based resources for its return mail body:
78      * <ul>
79      * <li>{@link #SUBSCRIBE_CONFIRM}
80      * <li>{@link #ADMIN_COMMANDS}
81      * </ul>
82      *
83      * @param mail
84      * @throws MessagingException
85      */

86     public void onCommand(Mail mail) throws MessagingException JavaDoc {
87         if (checkSubscriptionStatus(mail)) {
88             getUsersRepository().addUser(mail.getSender().toString(), "");
89
90             //send mail
91
Properties JavaDoc props = getStandardProperties();
92             props.put("SENDER_ADDR", mail.getSender().toString());
93
94             String JavaDoc confirmationMail = xmlResources[SUBSCRIBE_CONFIRM].getString("text", props);
95             String JavaDoc adminCommands = xmlResources[ADMIN_COMMANDS].getString("text", props);
96             String JavaDoc subject = xmlResources[SUBSCRIBE_CONFIRM].getString("welcome.subscribe.address", props);
97
98             sendStandardReply(mail, subject, confirmationMail + adminCommands, null);
99         }
100     }
101
102     /**
103      * Checks to see if this user is already subscribed, if so return false and send a message
104      * @param mail
105      * @return false if the user is subscribed, true otherwise
106      * @throws MessagingException
107      */

108     protected boolean checkSubscriptionStatus(Mail mail) throws MessagingException JavaDoc {
109         MailAddress mailAddress = mail.getSender();
110         UsersRepository usersRepository = getUsersRepository();
111         if (usersRepository.contains(mailAddress.toString())) {
112             getCommandListservManager().onError(mail,
113                     "Invalid request",
114                     xmlResources[SUBSCRIBE_CONFIRM].getString("already.subscribed", getStandardProperties()));
115             return false;
116         }
117         return true;
118     }
119 }
120
Popular Tags