KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > business > mail > server > ScriptMailServer


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: ScriptMailServer.java,v $
31  * Revision 1.3 2005/04/10 18:47:43 colinmacleod
32  * Changed i tag to em and b tag to strong.
33  *
34  * Revision 1.2 2005/04/09 17:20:01 colinmacleod
35  * Changed copyright text to GPL v2 explicitly.
36  *
37  * Revision 1.1.1.1 2005/03/10 17:51:20 colinmacleod
38  * Restructured ivata op around Hibernate/PicoContainer.
39  * Renamed ivata groupware.
40  *
41  * Revision 1.8 2004/11/12 18:16:08 colinmacleod
42  * Ordered imports.
43  *
44  * Revision 1.7 2004/11/12 15:57:25 colinmacleod
45  * Removed dependencies on SSLEXT.
46  * Moved Persistence classes to ivata masks.
47  *
48  * Revision 1.6 2004/11/03 16:18:09 colinmacleod
49  * Changed return type of getUserAliases to List.
50  *
51  * Revision 1.5 2004/07/18 21:59:15 colinmacleod
52  * Removed Person from User - now you need to use addressbook/persistence manager to find the person (makes the app run faster.)
53  *
54  * Revision 1.4 2004/07/13 19:48:12 colinmacleod
55  * Moved project to POJOs from EJBs.
56  * Applied PicoContainer to services layer (replacing session EJBs).
57  * Applied Hibernate to persistence layer (replacing entity EJBs).
58  *
59  * Revision 1.3 2004/03/21 21:16:39 colinmacleod
60  * Shortened name to ivata op.
61  *
62  * Revision 1.2 2004/02/01 22:07:32 colinmacleod
63  * Added full names to author tags
64  *
65  * Revision 1.1.1.1 2004/01/27 20:59:57 colinmacleod
66  * Moved ivata openportal to SourceForge..
67  *
68  * Revision 1.5 2004/01/12 14:02:23 jano
69  * fixing bugs
70  *
71  * Revision 1.4 2003/10/28 13:27:51 jano
72  * Committed webmail,
73  * still fixing compile and building openGroupware project
74  *
75  * Revision 1.3 2003/10/23 21:31:05 colin
76  * Fixed singletons; removed double-checked locking.
77  *
78  * Revision 1.2 2003/10/15 14:11:33 colin
79  * fixing for XDoclet
80  *
81  * Revision 1.2 2003/02/27 17:24:25 peter
82  * getFolderModified - script return value trimmed
83  *
84  * Revision 1.1 2003/02/24 19:09:24 colin
85  * moved to business
86  * Revision 1.9 2003/02/04 17:39:22 colin
87  * copyright notice
88  * Revision 1.8 2002/11/19 14:38:29 colin
89  * ed getFolderModified
90  * Revision 1.7 2002/11/05 16:13:29 jano
91  * we need to read setting in echa exec method,
92  * Revision 1.6 2002/10/24 15:50:08 jano
93  * filing bug with spaces in exec and SUDO
94  * Revision 1.5 2002/10/23 09:18:08 jano
95  * there is a new method for generating SystemUserName
96  * Revision 1.4 2002/09/09 11:03:08 colin
97  * made session classes serializable
98  * Revision 1.3 2002/09/09 08:44:52 colin
99  * made new mail session class to abstract the non-serializable
100  * * JavaMail session
101  * Revision 1.2 2002/08/30 14:20:36 peter
102  * fixed the previous state
103  * Revision 1.2 2002/08/11 11:42:03 colin
104  * made initialization use path directly rather than the
105  * settings
106  * Revision 1.1 2002/07/26 13:10:18 colin
107  * first version of mail server support
108  * user rights currently not implemented
109  * -----------------------------------------------------------------------------
110  */

111 package com.ivata.groupware.business.mail.server;
112
113 import java.util.Collection JavaDoc;
114 import java.util.Iterator JavaDoc;
115 import java.util.List JavaDoc;
116
117 import com.ivata.groupware.admin.script.ScriptExecutor;
118 import com.ivata.groupware.admin.security.server.SecuritySession;
119 import com.ivata.groupware.admin.setting.Settings;
120 import com.ivata.groupware.business.addressbook.AddressBook;
121 import com.ivata.mask.util.CollectionHandling;
122 import com.ivata.mask.util.SystemException;
123
124
125 /**
126  * <p>This implementation of the mail server uses scripts
127  * to perform each of the actions.</p>
128  *
129  * <p>The scripts must have the same name as each of the
130  * methods and use the same parameters. The location of the
131  * scripts within the system is set using the system setting
132  * 'pathScriptMailServer'.</p>
133  *
134  * <p>For each script, a return value of <code>0</code> is taken
135  * as success. Any other value results in a {@link
136  * GroupwareException GroupwareException} being thrown, with
137  * the contents of the standard error stream as its message.</p>
138  *
139  * @since 2002-07-20
140  * @author Colin MacLeod
141  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
142  * @version $Revision: 1.3 $
143  */

144 public class ScriptMailServer extends JavaMailServer implements MailServer {
145     String JavaDoc[] environmentVariables = null;
146     private ScriptExecutor scriptExecutor;
147     /**
148      * <p>Stores the sole instance of this class.</p>
149      */

150     String JavaDoc scriptPath;
151
152     private Settings settings;
153     /**
154      * This prefix is appended to user names to get the underlying system
155      * equivalent.
156      */

157     private String JavaDoc userNamePrefix;
158     AddressBook addressBook;
159
160     /**
161      * <p>Create a script mail server.</p>
162      */

163     public ScriptMailServer(final ScriptExecutor scriptExecutor,
164             final String JavaDoc userNamePrefix,
165             final AddressBook addressBook,
166             final Settings settings
167             ) {
168         super(addressBook, settings);
169         this.scriptExecutor = scriptExecutor;
170         this.settings = settings;
171         this.userNamePrefix = userNamePrefix;
172         this.addressBook = addressBook;
173    }
174
175     /**
176      * <p>Add a new user to the system. </p>
177      *
178      * <p>This method calls the <code>addUser</code> script.</p>
179      *
180      * @param userName it's userName in System
181      * @param fileAs it's file as of user
182      * @throws GroupwareException containing the content of the standard
183      * error stream, if the script returns non-zero.
184      */

185     public void addUser(final SecuritySession securitySession,
186             final String JavaDoc userName,
187             final String JavaDoc fileAs)
188             throws SystemException {
189         String JavaDoc[] argv = new String JavaDoc[2];
190
191         argv[0] = getSystemUserName(securitySession, userName);
192         argv[1] = fileAs;
193
194         scriptExecutor.exec("addUser", argv);
195         argv = new String JavaDoc[3];
196
197         argv[0] = getSystemUserName(securitySession, userName);
198         argv[1] = getSystemUserName(securitySession,
199                 securitySession.getUser().getName());
200         argv[2] = getSystemUserName(securitySession,
201                 securitySession.getPassword());
202         scriptExecutor.exec("makeMaildir", argv);
203     }
204
205     /**
206      * <p>Gets the contents of mailing list with the name provided.</p>
207      *
208      * <p>This method calls the script called <code>getList</code>.</p>
209      *
210      * @param name the name of the list to be retrieved.
211      * @return a <code>Collection</code> containing
212      * <code>UserLocal</code>
213      * instances of each of the users in the list.
214      * @throws GroupwareException containing the content of the standard
215      * error stream, if the script returns non-zero.
216      */

217     public final Collection JavaDoc getList(final SecuritySession securitySession,
218             final String JavaDoc name)
219         throws SystemException {
220         String JavaDoc[] argv = new String JavaDoc[1];
221
222         argv[0] = name;
223
224         return CollectionHandling.convertFromLines(scriptExecutor.exec("getList", argv));
225     }
226
227    /**
228     * <p>This method add prefix to username.</p>
229     *
230     * @param userName
231     * @return prefix_userName
232     */

233    public final String JavaDoc getSystemUserName(final SecuritySession securitySession,
234            final String JavaDoc userName) {
235        //server.getJndiPrefix();
236
//ApplicationServer.get .getInstance().getJndiPrefix();
237
if (userNamePrefix.length() > 0) {
238            return userNamePrefix + "_" + userName;
239        } else {
240            return userName;
241        }
242    }
243
244     /**
245      * <p>Gets all of the email aliases for the user provided.</p>
246      *
247      * <p>This method calls the script <code>getUserAliases</code>.</p>
248      *
249      * @param userName the user for whom to get the email aliases.
250      * @return a <code>Collection</code> containing
251      * <code>String</code> values for each of the aliases.
252      * @throws GroupwareException containing the content of the standard
253      * error stream, if the script returns non-zero.
254      */

255     public List JavaDoc getUserAliases(final SecuritySession securitySession,
256             final String JavaDoc userName)
257         throws SystemException {
258         String JavaDoc[] argv = new String JavaDoc[1];
259
260         argv[0] = getSystemUserName(securitySession, userName);
261
262         return CollectionHandling.convertFromLines(scriptExecutor.exec("getUserAliases", argv));
263     }
264
265     /**
266      * <p>Get the email addresss this user's mail is forwarded to.</p>
267      *
268      * <p>This method calls the script <code>getUserForwarding</code>.</p>
269      *
270      * @param userName the user for whom to activate/deactive email
271      * forwarding.
272      * @return email address all email for this user is forwarded to, or
273      * <code>null</code> if there is no forwarding for this user.
274      * @throws GroupwareException containing the content of the standard
275      * error stream, if the script returns non-zero.
276      */

277     public final String JavaDoc getUserForwarding(final SecuritySession securitySession,
278             final String JavaDoc userName)
279         throws SystemException {
280         String JavaDoc[] argv = new String JavaDoc[1];
281
282         argv[0] = getSystemUserName(securitySession, userName);
283
284         return scriptExecutor.exec("getUserForwarding", argv);
285     }
286
287     /**
288      * <p>This method is converting SystemUserName to userName, it's oposite method to getSystemUserName.</p>
289      * @param systemUserName
290      * @return
291      */

292     public final String JavaDoc getUserNameFromSystemUserName(final SecuritySession securitySession,
293             final String JavaDoc systemUserName) {
294         if ((userNamePrefix.length() > 0) && systemUserName.startsWith(userNamePrefix)) {
295             return systemUserName.substring(userNamePrefix.length() + 1);
296         } else {
297             return systemUserName;
298         }
299     }
300
301     /**
302      * <p>Gets the current vacation method for the user provided. </p>
303      *
304      * <p>This method calls the script
305      * <code>getVacationMessage</code>.</p>
306      *
307      * @param userName the user for whom to set the vacation message.
308      * @return the current vacation message for this user, or
309      * <code>null</code> if
310      * the user does not have a vacation message.
311      * @throws GroupwareException containing the content of the standard
312      * error stream, if the script returns non-zero.
313      */

314     public final String JavaDoc getVacationMessage(final SecuritySession securitySession,
315             final String JavaDoc userName)
316         throws SystemException {
317         String JavaDoc[] argv = new String JavaDoc[1];
318
319         argv[0] = getSystemUserName(securitySession, userName);
320
321         String JavaDoc vacationMessage = scriptExecutor.exec("getVacationMessage", argv);
322
323         // TODO: we're assuming that an empty message = no message
324
return vacationMessage.equals("") ? null : vacationMessage;
325     }
326
327     /**
328      * <p>Removes an existing mailing list from the system.</p>
329      *
330      * <p>The <code>removeList</code> script is called with
331      * <code>name</code> as its only parameter.</p>
332      *
333      * @param name the name of the list to remove.
334      * @throws GroupwareException containing the content of the standard
335      * error stream, if the script returns non-zero.
336      */

337     public void removeList(final SecuritySession securitySession,
338             final String JavaDoc name) throws SystemException {
339         String JavaDoc[] argv = new String JavaDoc[1];
340
341         argv[0] = name;
342         scriptExecutor.exec("removeList", argv);
343     }
344
345     /**
346      * <p>Remove a user from the email system. <strong>Note:</strong> this user is
347      * not removed
348      * from the address book, only the email server. Comments by this user
349      * will still appear in the system, though the user will no longer
350      * have an email account.</p>
351      *
352      * <p>This is good for employees who have left the company. We still
353      * want to see their contributions, but they no longer have access to
354      * the system themselves.</p>
355      *
356      * <p>This method calls the <code>removeUser</code> script.</p>
357      *
358      * @param userName the user to remove from the email system.
359      * @throws GroupwareException containing the content of the standard
360      * error stream, if the script returns non-zero.
361      */

362     public void removeUser(final SecuritySession securitySession,
363             final String JavaDoc userName) throws SystemException {
364         String JavaDoc[] argv = new String JavaDoc[1];
365
366         argv[0] = getSystemUserName(securitySession, userName);
367         scriptExecutor.exec("removeUser", argv);
368     }
369
370     /**
371      * <p>Adds or amends a mailing list with the name and users
372      * provided.</p>
373      *
374      * <p>This method calls the <code>setList</code> script with the name
375      * as the first parameter and the user names of each of the users as
376      * following parameters.</p>
377      *
378      * @param name the name of the list to be added.
379      * @param users <code>Collection</code> containing
380      * <code>UserLocal</code> instances of each of the users in the list.
381      * Note that if you are changing an existing list, this array should
382      * contain all of the users, not just the new ones.
383      * @throws GroupwareException containing the content of the standard
384      * error stream, if the script returns non-zero.
385      */

386     public final void setList(final SecuritySession securitySession,
387             final String JavaDoc name,
388             final Collection JavaDoc users)
389         throws SystemException {
390         String JavaDoc[] argv = new String JavaDoc[users.size() + 1];
391         argv[0] = name;
392         int index = 1;
393
394         for (Iterator JavaDoc i = users.iterator(); i.hasNext(); ++index) {
395             argv[index] = (String JavaDoc) i.next();
396         }
397
398         scriptExecutor.exec("setList", argv);
399     }
400
401     /**
402      * <p>Set a user's password in the system. This method calls the
403      * <code>setPassword</code> script with <code>userName</code> and
404      * <code>password</code> as parameters.</p>
405      *
406      * @param userName the user for whom to set the password.
407      * @param password new value of the password.
408      * @throws GroupwareException containing the content of the standard
409      * error stream, if the script returns non-zero.
410      */

411     public final void setPassword(final SecuritySession securitySession,
412             final String JavaDoc userName,
413             final String JavaDoc password)
414             throws SystemException {
415         String JavaDoc[] argv = new String JavaDoc[3];
416
417         argv[0] = getSystemUserName(securitySession, userName);
418         argv[1] = password;
419         // this parameter should be no-zero to indicate conversion of error
420
// messages to localized keys
421
argv[2] = "1";
422         scriptExecutor.exec("setPassword", argv);
423     }
424
425     /**
426      * <p>Gets all of the email aliases for the user provided.</p>
427      *
428      * <p>This method calls the script <code>setUserAliases</code>.</p>
429      *
430      * @param userName the user for whom to get the email aliases.
431      * @param aliases a <code>Collection</code> containing
432      * <code>String</code> values for each of the aliases.
433      * @throws GroupwareException containing the content of the standard
434      * error stream, if the script returns non-zero.
435      */

436     public final void setUserAliases(final SecuritySession securitySession,
437             final String JavaDoc userName,
438             final Collection JavaDoc aliases)
439         throws SystemException {
440         String JavaDoc[] argv = new String JavaDoc[aliases.size() + 1];
441
442         argv[0] = getSystemUserName(securitySession, userName);
443
444         int index = 1;
445
446         for (Iterator JavaDoc i = aliases.iterator(); i.hasNext(); ++index) {
447             argv[index] = (String JavaDoc) i.next();
448         }
449
450         scriptExecutor.exec("setUserAliases", argv);
451     }
452
453     /**
454      * <p>Set an email addresss to forward this user's mail to. If
455      * <code>address</code> is set to <code>null</code> then any previous
456      * email
457      * forwarding is removed.
458      *
459      * @param userName the user for whom to activate/deactive email
460      * forwarding.
461      * @param address email address to forward all email for this user to.
462      * If this
463      * address is set to <code>null</code> then any previous forwarding is
464      * removed.
465      * @throws GroupwareException containing the content of the standard
466      * error
467      * stream, if the script returns non-zero.
468      *
469      *
470      */

471     public final void setUserForwarding(final SecuritySession securitySession,
472             final String JavaDoc userName,
473             final String JavaDoc address)
474         throws SystemException {
475         String JavaDoc[] argv = new String JavaDoc[2];
476
477         argv[0] = getSystemUserName(securitySession, userName);
478         argv[1] = address;
479         scriptExecutor.exec("setUserForwarding", argv);
480     }
481
482     /**
483      * <p>Sets the vacation method for the user provided. This message
484      * will be sent
485      * to all mails received at this address until it has been cleared, by
486      * calling
487      * this method again with a <code>null</code> value for the
488      * <code>message</code>
489      * parameter.</p>
490      *
491      * <p>This method calls the <code>setVacationMessage</code> script
492      * with <code>user name</code> and <code>message</code> as parameters.
493      * If the <code>message</code> parameter is <code>null</code>, then
494      * the
495      * script is called with just the
496      * <code>user name</code> parameter.</p>
497      *
498      * @param userName the user for whom to set the vacation message.
499      * @param messageParam the new vacation message for this user. Set to
500      * <code>null</code> to remove any existing vacation message.
501      * @throws GroupwareException containing the content of the standard
502      * error stream, if the script returns non-zero.
503      */

504     public final void setVacationMessage(final SecuritySession securitySession,
505             final String JavaDoc userName,
506             final String JavaDoc messageParam)
507             throws SystemException {
508         String JavaDoc[] argv = new String JavaDoc[2];
509         String JavaDoc message = messageParam;
510
511         // TODO: possibly handle vacation message removal differently here
512
// (null = remove vacation message - we just set it to the empty string)
513
if (message == null) {
514             message = "";
515         }
516
517         argv[0] = getSystemUserName(securitySession, userName);
518         argv[1] = message;
519         scriptExecutor.exec("setVacationMessage", argv);
520     }
521
522     /**
523      * Refer to {@link }.
524      *
525      * @param userNameParam
526      * @return
527      * @see com.ivata.groupware.admin.security.server.SecurityServer#isUser(java.lang.String)
528      */

529     public boolean isUser(final SecuritySession securitySession,
530             final String JavaDoc userNameParam) throws SystemException {
531         String JavaDoc[] argv = new String JavaDoc[1];
532
533         argv[0] = getSystemUserName(securitySession, userNameParam);
534
535         String JavaDoc isUser = scriptExecutor.exec("isUser", argv).trim();
536         return "true".equals(isUser);
537     }
538
539 }
540
Popular Tags