View Javadoc

1   /************************************************************************
2    * Copyright (c) 2000-2006 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  
27  import javax.mail.MessagingException;
28  import java.util.Iterator;
29  import java.util.Properties;
30  
31  /***
32   * Info handles the info command.
33   * It is configured by:
34   * <pre>&lt;command name="info" class="Info"/&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>header
42   *  <li>info
43   *  <li>admincommands
44   * </ul>
45   *
46   * <br />
47   * <br />
48   * After formatting the text, the message is delivered with {@link #sendStandardReply}
49   *
50   * Todo: make displaying the current member list optional
51   *
52   * @version CVS $Revision: 365582 $ $Date: 2006-01-03 08:51:21 +0000 (mar, 03 gen 2006) $
53   * @since 2.2.0
54   */
55  public class Info extends BaseCommand {
56  
57      //For resources
58      protected XMLResources[] xmlResources;
59  
60      protected static final int HEADER = 0;
61      protected static final int INFO = 1;
62      protected static final int ADMIN_COMMANDS = 2;
63  
64      public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
65          super.init(commandListservManager, configuration);
66          xmlResources = initXMLResources(new String[]{"header", "info", "admincommands"});
67      }
68  
69      /***
70       * Process the info command using the following text resources:
71       * <ul>
72       *  <li>{@link #HEADER}
73       *  <li>{@link #INFO}
74       *  <li>{@link #ADMIN_COMMANDS}
75       * </ul>
76       *
77       * @param mail
78       */
79      public void onCommand(Mail mail) throws MessagingException {
80          //send info mail
81          Properties props = getStandardProperties();
82          props.put("MEMBER_LIST", getMemberList());
83  
84          StringBuffer plainTextMessage = new StringBuffer();
85          String header = xmlResources[HEADER].getString("text", props);
86          plainTextMessage.append(header);
87  
88          String infoMail = xmlResources[INFO].getString("text", props);
89          plainTextMessage.append(infoMail);
90  
91          String adminCommands = xmlResources[ADMIN_COMMANDS].getString("text", props);
92          plainTextMessage.append(adminCommands);
93  
94          String subject = xmlResources[INFO].getString("info.subject", props);
95  
96          sendStandardReply(mail, subject, plainTextMessage.toString(), null);
97      }
98  
99      /***
100      * Retrieve the current member list
101      * @return the formatted member list
102      *
103      * @see #getUsersRepository
104      */
105     protected String getMemberList() {
106 
107         StringBuffer buffer = new StringBuffer(0x1000);
108         buffer.append("\r\n");
109         UsersRepository usersRepository = getUsersRepository();
110         for (Iterator it = usersRepository.list(); it.hasNext();) {
111             String userName = (String) it.next();
112             buffer.append("    ").append(userName);
113             buffer.append("\r\n");
114         }
115 
116         return buffer.toString();
117     }
118 }