KickJava   Java API By Example, From Geeks To Geeks.

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


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.services.UsersRepository;
23 import org.apache.james.transport.mailets.ICommandListservManager;
24 import org.apache.james.util.XMLResources;
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 command.
33  * It is configured by:
34  * <pre>&lt;command name="subscribe" class="Subscribe"/&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
42  * </ul>
43  *
44  * <br />
45  * <br />
46  * After formatting the text, the message is delivered with {@link #sendStandardReply}
47  *
48  * Note, prior to formatting and sending any text, the user is checked to see if they
49  * are already subscribed to this list. If not, they will be sent a confirmation mail to
50  * be processed by {@link SubscribeConfirm}
51  *
52  * @version CVS $Revision: 1.1.2.3 $ $Date: 2004/03/15 03:54:20 $
53  * @since 2.2.0
54  * @see SubscribeConfirm
55  */

56 public class Subscribe extends BaseCommand {
57
58     //For resources
59
protected XMLResources xmlResources;
60
61
62
63     public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
64         super.init(commandListservManager, configuration);
65         xmlResources = initXMLResources(new String JavaDoc[]{"subscribe"})[0];
66     }
67
68     /**
69      * After ensuring that the user isn't already subscribed, confirmation mail
70      * will be sent to be processed by {@link SubscribeConfirm}.
71      *
72      * @param mail
73      * @throws MessagingException
74      */

75     public void onCommand(Mail mail) throws MessagingException JavaDoc {
76         if (checkSubscriptionStatus(mail)) {
77             //send confirmation mail
78
Properties JavaDoc props = getStandardProperties();
79             props.put("SENDER_ADDR", mail.getSender().toString());
80
81             String JavaDoc confirmationMail = xmlResources.getString("text", props);
82             String JavaDoc subject = xmlResources.getString("confirm.subscribe.subject", props);
83             String JavaDoc replyAddress = xmlResources.getString("confirm.subscribe.address", props);
84
85             sendStandardReply(mail, subject, confirmationMail, replyAddress);
86         }
87     }
88
89     /**
90      * Checks to see if this user is already subscribed, if so return false and send a message
91      * @param mail
92      * @return false if the user is subscribed, true otherwise
93      * @throws MessagingException
94      */

95     protected boolean checkSubscriptionStatus(Mail mail) throws MessagingException JavaDoc {
96         MailAddress mailAddress = mail.getSender();
97         UsersRepository usersRepository = getUsersRepository();
98         if (usersRepository.contains(mailAddress.toString())) {
99             getCommandListservManager().onError(mail,
100                     "Invalid request",
101                     xmlResources.getString("already.subscribed", getStandardProperties()));
102             return false;
103         }
104         return true;
105     }
106 }
107
Popular Tags