KickJava   Java API By Example, From Geeks To Geeks.

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


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  * UnSubscribe handles the unsubscribe command.
33  * It is configured by:
34  * <pre>&lt;command name="unsubscribe" class="UnSubscribe"/&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>unsubscribe
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 that they
49  * are currently subscribed to this list. If so, they will be sent a confirmation mail to
50  * be processed by {@link UnSubscribeConfirm}
51  *
52  * @version CVS $Revision: 1.1.2.3 $ $Date: 2004/03/15 03:54:20 $
53  * @since 2.2.0
54  * @see UnSubscribeConfirm
55  */

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

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

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