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;
21  
22  import org.apache.avalon.framework.service.ServiceException;
23  import org.apache.avalon.framework.service.ServiceManager;
24  import org.apache.james.Constants;
25  import org.apache.james.services.UsersRepository;
26  import org.apache.james.services.UsersStore;
27  import org.apache.mailet.MailAddress;
28  
29  import javax.mail.internet.ParseException;
30  import java.util.Collection;
31  import java.util.Iterator;
32  import java.util.ArrayList;
33  
34  /***
35   * MailingListServer capability.
36   * 
37   * <p>Requires a configuration element in the config.xml file of the form:
38   * <br>  &lt;mailet match="RecipientIs=LIST-ADDRESS" class="AvalonListserv"&gt;
39   * <br>    &lt;repositoryName&gt;LIST-NAME&lt;/repositoryName&gt;
40   * <br>    &lt;membersonly&gt;[true|false]&lt;/membersonly&gt;
41   * <br>    &lt;attachmentsallowed&gt;[true|false]&lt;/attachmentsallowed&gt;
42   * <br>    &lt;replytolist&gt;[true|false]&lt;/replytolist&gt;
43   * <br>    &lt;autobracket&gt;[true|false]&lt;/autobracket&gt;
44   * <br>    &lt;subjectprefix [xml:space="preserve"]&gt;SUBJECT-PREFIX&lt;/subjectprefix&gt;
45   * <br>  &lt;/mailet&gt;
46   * <p>repositoryName - the name of a user repository configured in the
47   * UsersStore block, e.g.,
48   * <br>  &lt;repository name="list-name" class="org.apache.james.userrepository.ListUsersJdbcRepository" destinationURL="db://maildb/lists/list-name"&gt;
49   * <br>    &lt;sqlFile&gt;file://conf/sqlResources.xml&lt;/sqlFile&gt;
50   * <br>  &lt;/repository&gt;
51   * <p>or
52   * <br>  &lt;repository name="list-name" class="org.apache.james.userrepository.UsersFileRepository"&gt;
53   * <br>    &lt;destination URL="file://var/lists/list-name/"/&gt;
54   * <br>  &lt;/repository&gt;
55   * <p>membersonly - if true only members can post to the list
56   * <p>attachmentsallowed - if false attachments are not allowed
57   * <p>replytolist - if true, replies go back to the list address; if
58   * false they go to the sender.
59   * <p>subjectprefix - a prefix that will be inserted at the front of
60   * the subject.  If autobracketing is disabled (see below), the
61   * xml:space="preserve" attribute can be used to precisely control the
62   * prefix.
63   * <p>autobracket - if true the subject prefix will be rendered as
64   * "[PREFIX] ", if false, the prefix will be used literally.
65   *
66   * @version This is $Revision: 494012 $
67   */
68  public class AvalonListserv extends GenericListserv {
69  
70      /***
71       * Whether only members can post to the list
72       */
73      protected boolean membersOnly = false;
74  
75      /***
76       * Whether attachments can be sent to the list
77       */
78      protected boolean attachmentsAllowed = true;
79  
80      /***
81       * Whether the reply-to header should be set to the list address
82       */
83      protected boolean replyToList = true;
84  
85      /***
86       * A String to prepend to the subject of the message when it
87       * is sent to the list
88       */
89      protected String subjectPrefix = null;
90  
91      /***
92       * Whether the subject prefix should be bracketed with '[' and ']'
93       */
94      protected boolean autoBracket = true;
95  
96      /***
97       * The repository containing the users on this list
98       */
99      private UsersRepository members;
100 
101     /***
102      * Initialize the mailet
103      */
104     public void init() {
105         try {
106             membersOnly = new Boolean(getInitParameter("membersonly")).booleanValue();
107         } catch (Exception e) {
108             // Ignore any exceptions, default to false
109         }
110         try {
111             attachmentsAllowed = new Boolean(getInitParameter("attachmentsallowed")).booleanValue();
112         } catch (Exception e) {
113             // Ignore any exceptions, default to true
114         }
115         try {
116             replyToList = new Boolean(getInitParameter("replytolist")).booleanValue();
117         } catch (Exception e) {
118             // Ignore any exceptions, default to true
119         }
120         subjectPrefix = getInitParameter("subjectprefix");
121 
122         try {
123             autoBracket = new Boolean(getInitParameter("autobracket")).booleanValue();
124         } catch (Exception e) {
125             // Ignore any exceptions, default to true
126         }
127 
128         ServiceManager compMgr = (ServiceManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
129         try {
130             UsersStore usersStore = (UsersStore)compMgr.lookup(UsersStore.ROLE);
131             String repName = getInitParameter("repositoryName");
132 
133             members = (UsersRepository)usersStore.getRepository( repName );
134         } catch (ServiceException cnfe) {
135             log("Failed to retrieve Store component:" + cnfe.getMessage());
136         } catch (Exception e) {
137             log("Failed to retrieve Store component:" + e.getMessage());
138         }
139     }
140 
141     public Collection getMembers() throws ParseException {
142         Collection reply = new ArrayList();
143         for (Iterator it = members.list(); it.hasNext(); ) {
144             String member = it.next().toString();
145             try {
146                 reply.add(new MailAddress(member));
147             }
148             catch(Exception e) {
149                 // Handle an invalid subscriber address by logging it and
150                 // proceeding to the next member.
151                 StringBuffer logBuffer =
152                     new StringBuffer(1024)
153                             .append("Invalid subscriber address: ")
154                             .append(member)
155                             .append(" caused: ")
156                             .append(e.getMessage());
157                 log(logBuffer.toString());
158             }
159         }
160         return reply;
161     }
162 
163     /***
164      * Get whether posting to this list is restricted to list members
165      *
166      * @return whether posting to this list is restricted to list members
167      */
168     public boolean isMembersOnly() {
169         return membersOnly;
170     }
171 
172     /***
173      * Get whether attachments can be sent to this list
174      *
175      * @return whether attachments can be sent to this list
176      */
177     public boolean isAttachmentsAllowed() {
178         return attachmentsAllowed;
179     }
180 
181     /***
182      * Get whether the reply-to header for messages sent to this list
183      * will be replaced with the list address
184      *
185      * @return whether replies to messages posted to this list will go to the entire list
186      */
187     public boolean isReplyToList() {
188         return replyToList;
189     }
190 
191     /***
192      * Get the prefix prepended to the subject line
193      *
194      * @return whether the prefix for subjects on this list will be bracketed.
195      */
196     public String getSubjectPrefix() {
197         return subjectPrefix;
198     }
199 
200     /***
201      * Return whether the prefix for subjects on this list will be bracketed.
202      *
203      * @return whether the prefix for subjects on this list will be bracketed.
204      */
205     public boolean isPrefixAutoBracketed() {
206         return autoBracket;
207     }
208 
209     /***
210      * Return a string describing this mailet.
211      *
212      * @return a string describing this mailet
213      */
214     public String getMailetInfo() {
215         return "AvalonListserv Mailet";
216     }
217 }