001: /****************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one *
003: * or more contributor license agreements. See the NOTICE file *
004: * distributed with this work for additional information *
005: * regarding copyright ownership. The ASF licenses this file *
006: * to you under the Apache License, Version 2.0 (the *
007: * "License"); you may not use this file except in compliance *
008: * with the License. You may obtain a copy of the License at *
009: * *
010: * http://www.apache.org/licenses/LICENSE-2.0 *
011: * *
012: * Unless required by applicable law or agreed to in writing, *
013: * software distributed under the License is distributed on an *
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015: * KIND, either express or implied. See the License for the *
016: * specific language governing permissions and limitations *
017: * under the License. *
018: ****************************************************************/package org.apache.james.transport.mailets.listservcommands;
019:
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.james.services.UsersRepository;
023: import org.apache.james.transport.mailets.ICommandListservManager;
024: import org.apache.james.util.XMLResources;
025: import org.apache.mailet.Mail;
026: import org.apache.mailet.MailAddress;
027:
028: import javax.mail.MessagingException;
029: import java.util.Properties;
030:
031: /**
032: * Subscribe handles the subscribe command.
033: * It is configured by:
034: * <pre><command name="subscribe" class="Subscribe"/></pre>
035: *
036: * <br />
037: * <br />
038: *
039: * It uses the formatted text-based resources for its return mail body:
040: * <ul>
041: * <li>subscribe
042: * </ul>
043: *
044: * <br />
045: * <br />
046: * After formatting the text, the message is delivered with {@link #sendStandardReply}
047: *
048: * Note, prior to formatting and sending any text, the user is checked to see if they
049: * are already subscribed to this list. If not, they will be sent a confirmation mail to
050: * be processed by {@link SubscribeConfirm}
051: *
052: * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
053: * @since 2.2.0
054: * @see SubscribeConfirm
055: */
056: public class Subscribe extends BaseCommand {
057:
058: //For resources
059: protected XMLResources xmlResources;
060:
061: public void init(ICommandListservManager commandListservManager,
062: Configuration configuration) throws ConfigurationException {
063: super .init(commandListservManager, configuration);
064: xmlResources = initXMLResources(new String[] { "subscribe" })[0];
065: }
066:
067: /**
068: * After ensuring that the user isn't already subscribed, confirmation mail
069: * will be sent to be processed by {@link SubscribeConfirm}.
070: *
071: * @param mail
072: * @throws MessagingException
073: */
074: public void onCommand(Mail mail) throws MessagingException {
075: if (checkSubscriptionStatus(mail)) {
076: //send confirmation mail
077: Properties props = getStandardProperties();
078: props.put("SENDER_ADDR", mail.getSender().toString());
079:
080: String confirmationMail = xmlResources.getString("text",
081: props);
082: String subject = xmlResources.getString(
083: "confirm.subscribe.subject", props);
084: String replyAddress = xmlResources.getString(
085: "confirm.subscribe.address", props);
086:
087: sendStandardReply(mail, subject, confirmationMail,
088: replyAddress);
089: }
090: }
091:
092: /**
093: * Checks to see if this user is already subscribed, if so return false and send a message
094: * @param mail
095: * @return false if the user is subscribed, true otherwise
096: * @throws MessagingException
097: */
098: protected boolean checkSubscriptionStatus(Mail mail)
099: throws MessagingException {
100: MailAddress mailAddress = mail.getSender();
101: UsersRepository usersRepository = getUsersRepository();
102: if (usersRepository.contains(mailAddress.toString())) {
103: getCommandListservManager().onError(
104: mail,
105: "Invalid request",
106: xmlResources.getString("already.subscribed",
107: getStandardProperties()));
108: return false;
109: }
110: return true;
111: }
112: }
|