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