KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > transport > mailets > AvalonListserv


1 /***********************************************************************
2  * Copyright (c) 2000-2004 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;
19
20 import org.apache.avalon.framework.component.ComponentException;
21 import org.apache.avalon.framework.component.ComponentManager;
22 import org.apache.james.Constants;
23 import org.apache.james.services.UsersRepository;
24 import org.apache.james.services.UsersStore;
25 import org.apache.mailet.MailAddress;
26
27 import javax.mail.internet.ParseException JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.ArrayList JavaDoc;
31
32 /**
33  * MailingListServer capability.
34  *
35  * <p>Requires a configuration element in the config.xml file of the form:
36  * <br> &lt;mailet match="RecipientIs=LIST-ADDRESS" class="AvalonListserv"&gt;
37  * <br> &lt;repositoryName&gt;LIST-NAME&lt;/repositoryName&gt;
38  * <br> &lt;membersonly&gt;[true|false]&lt;/membersonly&gt;
39  * <br> &lt;attachmentsallowed&gt;[true|false]&lt;/attachmentsallowed&gt;
40  * <br> &lt;replytolist&gt;[true|false]&lt;/replytolist&gt;
41  * <br> &lt;autobracket&gt;[true|false]&lt;/autobracket&gt;
42  * <br> &lt;subjectprefix [xml:space="preserve"]&gt;SUBJECT-PREFIX&lt;/subjectprefix&gt;
43  * <br> &lt;/mailet&gt;
44  * <p>repositoryName - the name of a user repository configured in the
45  * UsersStore block, e.g.,
46  * <br> &lt;repository name="list-name" class="org.apache.james.userrepository.ListUsersJdbcRepository" destinationURL="db://maildb/lists/list-name"&gt;
47  * <br> &lt;sqlFile&gt;file://conf/sqlResources.xml&lt;/sqlFile&gt;
48  * <br> &lt;/repository&gt;
49  * <p>or
50  * <br> &lt;repository name="list-name" class="org.apache.james.userrepository.UsersFileRepository"&gt;
51  * <br> &lt;destination URL="file://var/lists/list-name/"/&gt;
52  * <br> &lt;/repository&gt;
53  * <p>membersonly - if true only members can post to the list
54  * <p>attachmentsallowed - if false attachments are not allowed
55  * <p>replytolist - if true, replies go back to the list address; if
56  * false they go to the sender.
57  * <p>subjectprefix - a prefix that will be inserted at the front of
58  * the subject. If autobracketing is disabled (see below), the
59  * xml:space="preserve" attribute can be used to precisely control the
60  * prefix.
61  * <p>autobracket - if true the subject prefix will be rendered as
62  * "[PREFIX] ", if false, the prefix will be used literally.
63  *
64  * @version This is $Revision: 1.7.4.4 $
65  */

66 public class AvalonListserv extends GenericListserv {
67
68     /**
69      * Whether only members can post to the list
70      */

71     protected boolean membersOnly = false;
72
73     /**
74      * Whether attachments can be sent to the list
75      */

76     protected boolean attachmentsAllowed = true;
77
78     /**
79      * Whether the reply-to header should be set to the list address
80      */

81     protected boolean replyToList = true;
82
83     /**
84      * A String to prepend to the subject of the message when it
85      * is sent to the list
86      */

87     protected String JavaDoc subjectPrefix = null;
88
89     /**
90      * Whether the subject prefix should be bracketed with '[' and ']'
91      */

92     protected boolean autoBracket = true;
93
94     /**
95      * The repository containing the users on this list
96      */

97     private UsersRepository members;
98
99     /**
100      * Initialize the mailet
101      */

102     public void init() {
103         try {
104             membersOnly = new Boolean JavaDoc(getInitParameter("membersonly")).booleanValue();
105         } catch (Exception JavaDoc e) {
106             // Ignore any exceptions, default to false
107
}
108         try {
109             attachmentsAllowed = new Boolean JavaDoc(getInitParameter("attachmentsallowed")).booleanValue();
110         } catch (Exception JavaDoc e) {
111             // Ignore any exceptions, default to true
112
}
113         try {
114             replyToList = new Boolean JavaDoc(getInitParameter("replytolist")).booleanValue();
115         } catch (Exception JavaDoc e) {
116             // Ignore any exceptions, default to true
117
}
118         subjectPrefix = getInitParameter("subjectprefix");
119
120         try {
121             autoBracket = new Boolean JavaDoc(getInitParameter("autobracket")).booleanValue();
122         } catch (Exception JavaDoc e) {
123             // Ignore any exceptions, default to true
124
}
125
126         ComponentManager compMgr = (ComponentManager)getMailetContext().getAttribute(Constants.AVALON_COMPONENT_MANAGER);
127         try {
128             UsersStore usersStore = (UsersStore)compMgr.lookup("org.apache.james.services.UsersStore");
129             String JavaDoc repName = getInitParameter("repositoryName");
130
131             members = (UsersRepository)usersStore.getRepository( repName );
132         } catch (ComponentException cnfe) {
133             log("Failed to retrieve Store component:" + cnfe.getMessage());
134         } catch (Exception JavaDoc e) {
135             log("Failed to retrieve Store component:" + e.getMessage());
136         }
137     }
138
139     public Collection JavaDoc getMembers() throws ParseException JavaDoc {
140         Collection JavaDoc reply = new ArrayList JavaDoc();
141         for (Iterator JavaDoc it = members.list(); it.hasNext(); ) {
142             String JavaDoc member = it.next().toString();
143             try {
144                 reply.add(new MailAddress(member));
145             }
146             catch(Exception JavaDoc e) {
147                 // Handle an invalid subscriber address by logging it and
148
// proceeding to the next member.
149
StringBuffer JavaDoc logBuffer =
150                     new StringBuffer JavaDoc(1024)
151                             .append("Invalid subscriber address: ")
152                             .append(member)
153                             .append(" caused: ")
154                             .append(e.getMessage());
155                 log(logBuffer.toString());
156             }
157         }
158         return reply;
159     }
160
161     /**
162      * Get whether posting to this list is restricted to list members
163      *
164      * @return whether posting to this list is restricted to list members
165      */

166     public boolean isMembersOnly() {
167         return membersOnly;
168     }
169
170     /**
171      * Get whether attachments can be sent to this list
172      *
173      * @return whether attachments can be sent to this list
174      */

175     public boolean isAttachmentsAllowed() {
176         return attachmentsAllowed;
177     }
178
179     /**
180      * Get whether the reply-to header for messages sent to this list
181      * will be replaced with the list address
182      *
183      * @return whether replies to messages posted to this list will go to the entire list
184      */

185     public boolean isReplyToList() {
186         return replyToList;
187     }
188
189     /**
190      * Get the prefix prepended to the subject line
191      *
192      * @return whether the prefix for subjects on this list will be bracketed.
193      */

194     public String JavaDoc getSubjectPrefix() {
195         return subjectPrefix;
196     }
197
198     /**
199      * Return whether the prefix for subjects on this list will be bracketed.
200      *
201      * @return whether the prefix for subjects on this list will be bracketed.
202      */

203     public boolean isPrefixAutoBracketed() {
204         return autoBracket;
205     }
206
207     /**
208      * Return a string describing this mailet.
209      *
210      * @return a string describing this mailet
211      */

212     public String JavaDoc getMailetInfo() {
213         return "AvalonListserv Mailet";
214     }
215 }
216
Popular Tags