View Javadoc

1   /****************************************************************
2    * Licensed to the Apache Software Foundation (ASF) under one   *
3    * or more contributor license agreements.  See the NOTICE file *
4    * distributed with this work for additional information        *
5    * regarding copyright ownership.  The ASF licenses this file   *
6    * to you under the Apache License, Version 2.0 (the            *
7    * "License"); you may not use this file except in compliance   *
8    * with the License.  You may obtain a copy of the License at   *
9    *                                                              *
10   *   http://www.apache.org/licenses/LICENSE-2.0                 *
11   *                                                              *
12   * Unless required by applicable law or agreed to in writing,   *
13   * software distributed under the License is distributed on an  *
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
15   * KIND, either express or implied.  See the License for the    *
16   * specific language governing permissions and limitations      *
17   * under the License.                                           *
18   ****************************************************************/
19  
20  
21  
22  package org.apache.james.transport.mailets.listservcommands;
23  
24  import org.apache.avalon.framework.configuration.Configuration;
25  import org.apache.avalon.framework.configuration.ConfigurationException;
26  import org.apache.james.api.user.UsersRepository;
27  import org.apache.james.transport.mailets.ICommandListservManager;
28  import org.apache.james.util.XMLResources;
29  import org.apache.mailet.Mail;
30  
31  import javax.mail.MessagingException;
32  import java.util.Iterator;
33  import java.util.Properties;
34  
35  /**
36   * Info handles the info command.
37   * It is configured by:
38   * <pre>&lt;command name="info" class="Info"/&gt;</pre>
39   *
40   * <br />
41   * <br />
42   *
43   * It uses the formatted text-based resources for its return mail body:
44   * <ul>
45   *  <li>header
46   *  <li>info
47   *  <li>admincommands
48   * </ul>
49   *
50   * <br />
51   * <br />
52   * After formatting the text, the message is delivered with {@link #sendStandardReply(Mail, String, String, String)}
53   *
54   * Todo: make displaying the current member list optional
55   *
56   * @version CVS $Revision: 684466 $ $Date: 2008-08-10 12:42:08 +0100 (Sun, 10 Aug 2008) $
57   * @since 2.2.0
58   */
59  public class Info extends BaseCommand {
60  
61      //For resources
62      protected XMLResources[] xmlResources;
63  
64      protected static final int HEADER = 0;
65      protected static final int INFO = 1;
66      protected static final int ADMIN_COMMANDS = 2;
67  
68      public void init(ICommandListservManager commandListservManager, Configuration configuration) throws ConfigurationException {
69          super.init(commandListservManager, configuration);
70          xmlResources = initXMLResources(new String[]{"header", "info", "admincommands"});
71      }
72  
73      /**
74       * Process the info command using the following text resources:
75       * <ul>
76       *  <li>{@link #HEADER}
77       *  <li>{@link #INFO}
78       *  <li>{@link #ADMIN_COMMANDS}
79       * </ul>
80       *
81       * @param mail
82       */
83      public void onCommand(Mail mail) throws MessagingException {
84          //send info mail
85          Properties props = getStandardProperties();
86          props.put("MEMBER_LIST", getMemberList());
87  
88          StringBuffer plainTextMessage = new StringBuffer();
89          String header = xmlResources[HEADER].getString("text", props);
90          plainTextMessage.append(header);
91  
92          String infoMail = xmlResources[INFO].getString("text", props);
93          plainTextMessage.append(infoMail);
94  
95          String adminCommands = xmlResources[ADMIN_COMMANDS].getString("text", props);
96          plainTextMessage.append(adminCommands);
97  
98          String subject = xmlResources[INFO].getString("info.subject", props);
99  
100         sendStandardReply(mail, subject, plainTextMessage.toString(), null);
101     }
102 
103     /**
104      * Retrieve the current member list
105      * @return the formatted member list
106      *
107      * @see BaseCommand#getUsersRepository
108      */
109     protected String getMemberList() {
110 
111         StringBuffer buffer = new StringBuffer(0x1000);
112         buffer.append("\r\n");
113         UsersRepository usersRepository = getUsersRepository();
114         for (Iterator it = usersRepository.list(); it.hasNext();) {
115             String userName = (String) it.next();
116             buffer.append("    ").append(userName);
117             buffer.append("\r\n");
118         }
119 
120         return buffer.toString();
121     }
122 }