KickJava   Java API By Example, From Geeks To Geeks.

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


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: HMailServer.java,v $
31  * Revision 1.3.2.1 2005/10/08 17:32:58 colinmacleod
32  * Extended for hMailServer v4.x
33  *
34  * Revision 1.6 2005/10/03 10:21:15 colinmacleod
35  * Fixed some style and javadoc issues.
36  *
37  * Revision 1.5 2005/10/02 14:08:59 colinmacleod
38  * Added/improved log4j logging.
39  *
40  * Revision 1.4 2005/09/14 16:16:52 colinmacleod
41  * Removed unused local and class variables.
42  * Added serialVersionUID.
43  *
44  * Revision 1.3 2005/04/10 19:32:52 colinmacleod
45  * Removed extra blank lines.
46  *
47  * Revision 1.2 2005/04/09 17:20:01 colinmacleod
48  * Changed copyright text to GPL v2 explicitly.
49  *
50  * Revision 1.1.1.1 2005/03/10 17:51:22 colinmacleod
51  * Restructured ivata op around Hibernate/PicoContainer.
52  * Renamed ivata groupware.
53  *
54  * -----------------------------------------------------------------------------
55  */

56 package com.ivata.groupware.business.mail.server;
57
58 import java.util.Collection JavaDoc;
59 import java.util.HashMap JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.List JavaDoc;
62 import java.util.Map JavaDoc;
63 import java.util.Vector JavaDoc;
64
65 import jp.ne.so_net.ga2.no_ji.jcom.IDispatch;
66 import jp.ne.so_net.ga2.no_ji.jcom.JComException;
67 import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
68
69 import org.apache.log4j.Logger;
70
71 import com.ivata.groupware.admin.security.server.SecuritySession;
72 import com.ivata.groupware.admin.setting.Settings;
73 import com.ivata.groupware.business.addressbook.AddressBook;
74 import com.ivata.mask.util.SystemException;
75
76 /**
77  * <p>
78  * On <strong>Microsoft Windows</strong> platforms, we recommend you use the
79  * open source <a HREF='http://www.hmailserver.com/'>hMailServer</a>. This class
80  * provides an interface to that system.
81  * </p>
82  *
83  * <p>
84  * <strong>Note:</strong> this code indirectly uses
85  * <a HREF='http://www.microsoft.com/com/default.mspx'>Microsoft COM</a>
86  * and will only work on <strong>Microsoft Windows</strong>. For UNIX Systems,
87  * use the {@link com.ivata.groupware.business.mail.server.ScriptMailServer
88  * ScriptMailServer}.
89  * </p>
90  *
91  * <p>
92  * This version of the code has been tested to work with <strong>hMailServer
93  * versions 3.x and 4.x</strong>.
94  * </p>
95  *
96  * @since ivata groupware 0.10 (2005-02-21)
97  * @author Colin MacLeod
98  * <a HREF="mailto:colin.macleod@ivata.com">colin.macleod@ivata.com</a>
99  * @version $Revision: 1.3.2.1 $
100  */

101 public class HMailServer extends JavaMailServer implements MailServer {
102     /**
103      * Serialization version (for <code>Serializable</code> interface).
104      */

105     private static final long serialVersionUID = 1L;
106     /**
107      * Name of the COM object we'll be using for <strong>hMailServer
108      * v3.x</strong>.
109      */

110     private static final String JavaDoc V3_BASE_APP_NAME = "HCOM.BaseApp";
111     /**
112      * Name of the Domains COM object we'll be using for <strong>hMailServer
113      * v4.x</strong>.
114      */

115     private static final String JavaDoc V4_DOMAINS_NAME = "hMailServer.Domains";
116
117     /**
118      * Stores the only instance of the base app - through which we communicate
119      * to hMailServer. <strong>This is only used for hMailServer v3.x.</strong>.
120      */

121     private static IDispatch baseApp = null;
122     /**
123      * Logger for this class.
124      */

125     private static final Logger logger = Logger.getLogger(HMailServer.class);
126     /**
127      * JCom release object. See <a
128      * HREF='http://sourceforge.net/project/showfiles.php?group_id=12841'>jcom
129      * docs</a>.
130      */

131     private static ReleaseManager releaseManager;
132     static {
133         try {
134             releaseManager = new ReleaseManager();
135         } catch (Exception JavaDoc e) {
136             logger.error(e.getClass().getName()
137                     + " trying to create JCom ReleaseManager.");
138             throw new RuntimeException JavaDoc (e);
139         }
140         IDispatch domains;
141         try {
142             domains = new IDispatch(releaseManager, V4_DOMAINS_NAME);
143         } catch (Exception JavaDoc e) {
144             logger.error(e.getClass().getName()
145                     + " trying to retrieve hMailServer domains from COM "
146                     + "object '"
147                     + V4_DOMAINS_NAME
148                     + "'.", e);
149             // null case handled below
150
domains = null;
151         }
152         if (domains != null) {
153             if (logger.isDebugEnabled()) {
154                 logger.debug("Identified hMailServer v4.x domains.");
155             }
156         } else {
157             try {
158                 baseApp = new IDispatch(releaseManager, V3_BASE_APP_NAME);
159             } catch (Exception JavaDoc e) {
160                 if (logger.isDebugEnabled()) {
161                     logger.debug("Could not find hMailServer v3.x COM object '"
162                             + V3_BASE_APP_NAME
163                             + "'. Trying v4.x", e);
164                 }
165                 baseApp = null;
166             }
167             if (baseApp == null) {
168                 String JavaDoc message = "No hMailServer v3.x Base App found "
169                     + "with COM NAME '"
170                     + V3_BASE_APP_NAME
171                     + "', and no hMailServer 4.x Domains object found "
172                     + "with COM NAME '"
173                     + V4_DOMAINS_NAME
174                     + "'";
175                 logger.error(message);
176                 throw new NullPointerException JavaDoc(message);
177             }
178             if (logger.isDebugEnabled()) {
179                 logger.debug("Identified hMailServer v3.x base app.");
180             }
181             try {
182                 logger.info("Using hMailServer version "
183                         + baseApp.get("Version"));
184             } catch (Exception JavaDoc e) {
185                 logger.error(e.getClass().getName()
186                         + " trying to retrieve hMailServer version.", e);
187             }
188         }
189     }
190     /**
191      * Get all the domains of the mail server.
192      *
193      * @return Returns the mail server domains.
194      * @throws SystemException If the domains cannot be retrieved for any
195      * reason.
196      */

197     public static IDispatch getDomains() throws SystemException {
198         if (logger.isDebugEnabled()) {
199             logger.debug("getDomains() - start");
200         }
201         IDispatch domains;
202
203         // if we're using v3, just get it from the baseApp
204
if (baseApp != null) {
205             try {
206                 domains = (IDispatch) baseApp.get("Domains");
207             } catch (JComException e) {
208                 logger.error("Retrieving domains from hMailServer v3.x",
209                         e);
210                 throw new SystemException(e);
211             }
212         } else {
213             try {
214                 domains = new IDispatch(releaseManager, V4_DOMAINS_NAME);
215             } catch (JComException e) {
216                 logger.error("Retrieving domains from hMailServer v4.x, COM "
217                         + "name '"
218                         + V4_DOMAINS_NAME
219                         + "'.",
220                         e);
221                 throw new SystemException(e);
222             }
223         }
224
225         if (logger.isDebugEnabled()) {
226             logger.debug("getDomains() - end - return value = " + domains);
227         }
228         return domains;
229     }
230    /**
231     * Simple test routine, to check this class is working on
232     * <strong>Windows</strong>.
233     *
234     * @param args Program arguments - not used, in this program.
235     */

236    public static void main(final String JavaDoc[] args) {
237     if (logger.isDebugEnabled()) {
238         logger.debug("main(String[] args = " + args + ") - start");
239     }
240
241        HMailServer mailTest = null;
242        try {
243            mailTest = new HMailServer("win.ivata.com", null, null);
244        } catch (Exception JavaDoc e) {
245            logger.error("Cannot instantiate HMailServer", e);
246            System.exit(-1);
247        }
248
249        try {
250            mailTest.removeUser(null, "test");
251        } catch (Exception JavaDoc e) {
252            logger.warn("No previous test user", e);
253        }
254        try {
255            mailTest.addUser(null, "test", "test name");
256            mailTest.setPassword(null, "test", "mytest");
257            mailTest.checkPassword(null, "test", "mytest");
258            mailTest.setVacationMessage(null, "test",
259                    "This is a vacation message.");
260            logger.info("Vacation message is "
261                    + mailTest.getVacationMessage(null, "test"));
262        } catch (Exception JavaDoc e) {
263            logger.error("TEST ERROR", e);
264            System.exit(-1);
265        }
266        System.exit(0);
267
268     if (logger.isDebugEnabled()) {
269         logger.debug("main(String[]) - end");
270     }
271    }
272    /**
273     * <copyDoc>Refer to {@link HMailServer()}.</copyDoc>
274     */

275    private String JavaDoc domainName;
276    /**
277     * Constructor. Create a connection to <strong>hMailServer</strong>.
278     *
279     * @param domainNameParam hMailServer domain name.
280     * @param addressBook {@inheritDoc}
281     * @param settings {@inheritDoc}
282     */

283    public HMailServer(final String JavaDoc domainNameParam,
284                     final AddressBook addressBook,
285                     final Settings settings) {
286         super(addressBook, settings);
287         domainName = domainNameParam;
288     }
289     /**
290      * {@inheritDoc}
291      *
292      * @param securitySession {@inheritDoc}
293      * @param userNameParam {@inheritDoc}
294      * @param fullNameParam {@inheritDoc}
295      * @throws SystemException If the user already exists.
296      */

297     public void addUser(final SecuritySession securitySession,
298             final String JavaDoc userNameParam, final String JavaDoc fullNameParam)
299             throws SystemException {
300         if (logger.isDebugEnabled()) {
301             logger.debug("addUser(SecuritySession securitySession = "
302                     + securitySession + ", String userNameParam = "
303                     + userNameParam + ", String fullNameParam = "
304                     + fullNameParam + ") - start");
305         }
306
307         try {
308             IDispatch accounts = getDomainAccounts();
309             String JavaDoc address = getSystemUserName(securitySession, userNameParam);
310             // first check this is a unique address
311
if (isUser(securitySession, userNameParam)) {
312                 throw new SystemException("User address must be unique: "
313                         + "address '"
314                         + address
315                         + "' already exists.");
316             }
317
318             IDispatch newAccount = (IDispatch) accounts.method("Add",
319                     new Object JavaDoc[] {});
320             newAccount.put("Address", getSystemUserName(securitySession,
321                     userNameParam));
322             newAccount.put("Active", Boolean.TRUE);
323             newAccount.method("Save", new Object JavaDoc[] {});
324         } catch (SystemException e) {
325             logger.error("addUser(SecuritySession, String, String)", e);
326
327             throw e;
328         } catch (Exception JavaDoc e) {
329             logger.error("addUser(SecuritySession, String, String)", e);
330
331             throw new SystemException(e);
332         }
333
334         if (logger.isDebugEnabled()) {
335             logger.debug("addUser(SecuritySession, String, String) - end");
336         }
337     }
338
339     /**
340      * <copyDoc>Refer to {@link }.</copyDoc>
341      *
342      * @param securitySession {@inheritDoc}
343      * @param userNameParam {@inheritDoc}
344      * @param passwordParam {@inheritDoc}
345      * @throws SystemException If the password doesn't match, or there is
346      * a technical problem communicating with hMailServer.
347      */

348     public void checkPassword(final SecuritySession securitySession,
349             final String JavaDoc userNameParam,
350             final String JavaDoc passwordParam)
351             throws SystemException {
352         if (logger.isDebugEnabled()) {
353             logger.debug("checkPassword(SecuritySession securitySession = "
354                     + securitySession + ", String userNameParam = "
355                     + userNameParam + ", String passwordParam = "
356                     + passwordParam + ") - start");
357         }
358
359         try {
360             String JavaDoc address = getSystemUserName(securitySession,
361                     userNameParam);
362             IDispatch accounts = getDomainAccounts();
363             IDispatch account = (IDispatch)
364                 accounts.get("ItemByAddress", new Object JavaDoc[] {
365                     address
366             });
367             // if we're using v3, use the utilities from the baseApp
368
if (baseApp != null) {
369                 if (logger.isDebugEnabled()) {
370                     logger.debug("Checking the password for account '"
371                             + userNameParam
372                             + "' via hMailServer v3.x.");
373                 }
374                 IDispatch utilities;
375                 try {
376                     utilities = (IDispatch) baseApp.get("Utilities");
377                 } catch (JComException e) {
378                     logger.error("Getting the utilities object from "
379                             + "hMailServer v3.x",
380                             e);
381                     throw new SystemException(e);
382                 }
383                 String JavaDoc passwordCheck = (String JavaDoc) utilities.method("MD5",
384                         new Object JavaDoc[] {
385                     passwordParam
386                 });
387                 if (!passwordCheck.equals(account.get("Password"))) {
388                     throw new SystemException("Passwords did not match");
389                 }
390             } else {
391                 // v4.x uses the account object
392
if (logger.isDebugEnabled()) {
393                     logger.debug("Checking the password for account '"
394                             + userNameParam
395                             + "' via hMailServer v4.x.");
396                 }
397                 Boolean JavaDoc validatePassword = (Boolean JavaDoc)
398                     account.method("ValidatePassword", new Object JavaDoc[] {
399                             passwordParam});
400                 if (!Boolean.TRUE.equals(validatePassword)) {
401                     throw new SystemException("Passwords did not match");
402                 }
403             }
404         } catch (SystemException e) {
405             logger.error("checkPassword(SecuritySession, String, String)", e);
406
407             throw e;
408         } catch (Exception JavaDoc e) {
409             logger.error("checkPassword(SecuritySession, String, String)", e);
410
411             throw new SystemException(e);
412         }
413
414         if (logger.isDebugEnabled()) {
415             logger.debug("checkPassword - end");
416         }
417     }
418
419     /**
420      * Private helper. Get the with the given name (in the constructor). Throw
421      * an exception if the domain doesn't exist.
422      *
423      * @return the domain with the name given.
424      * @throws JComException Thrown if the domains collection cannot be
425      * retreived from hMailServer.
426      * @throws SystemException If there is no domain with the name
427      * provided.
428      */

429     private IDispatch getDomain()
430             throws JComException, SystemException {
431         if (logger.isDebugEnabled()) {
432             logger.debug("getDomain() - start");
433         }
434
435         IDispatch domain = null;
436         IDispatch domains = getDomains();
437         try {
438             domain = (IDispatch)
439                 domains.method("ItemByName", new Object JavaDoc[] {domainName});
440         } catch (JComException e) {
441             logger.error("getDomain()", e);
442
443             throw new SystemException("Could not find hMailServer domain "
444                     + "called '"
445                     + domainName
446                     + "'",
447                     e);
448         }
449
450         if (logger.isDebugEnabled()) {
451             logger.debug("getDomain() - end - return value = " + domain);
452         }
453         return domain;
454     }
455     /**
456      * Private helper. Get the accounts collection for the domain with the given
457      * name. Throw an exception if the domain doesn't exist.
458      *
459      * @return Domain accounts object.
460      * @throws JComException if the domain cannot be retrieved, or there is no
461      * accounts object.
462      * @throws SystemException <copyDoc>Refer to {@link #getDomain}.</copyDoc>
463      */

464     private IDispatch getDomainAccounts()
465             throws JComException, SystemException {
466         if (logger.isDebugEnabled()) {
467             logger.debug("getDomainAccounts() - start");
468         }
469
470         IDispatch domain = getDomain();
471         IDispatch accounts = (IDispatch) domain.get("Accounts");
472
473         if (logger.isDebugEnabled()) {
474             logger.debug("getDomainAccounts() - end - return value = "
475                     + accounts);
476         }
477         return accounts;
478     }
479     /**
480      * Private helper. Get the aliases collection for the domain with the given
481      * name. Throw an exception if the domain doesn't exist.
482      *
483      * @return Domain aliases object.
484      * @throws SystemException <copyDoc>Refer to {@link #getDomain}.</copyDoc>
485      * @throws JComException If the aliases cannot be retrieved from the
486      * domain.
487      */

488     private IDispatch getDomainAliases()
489             throws JComException, SystemException {
490         if (logger.isDebugEnabled()) {
491             logger.debug("getDomainAliases() - start");
492         }
493
494         IDispatch domain = getDomain();
495         IDispatch aliases = (IDispatch) domain.get("Aliases");
496
497         if (logger.isDebugEnabled()) {
498             logger
499                     .debug("getDomainAliases() - end - return value = "
500                             + aliases);
501         }
502         return aliases;
503     }
504
505     /**
506      * On <strong>hMailServer</strong>, the system user name is the user
507      * followed by the 'at sign' and the domain name.
508      *
509      * {@inheritDoc}
510      *
511      * @param securitySession {@inheritDoc}
512      * @param userNameParam {@inheritDoc}
513      * @return always returns <code>userNameParam</code> followed by the at
514      * sign and the domain name.
515      */

516     public String JavaDoc getSystemUserName(final SecuritySession securitySession,
517             final String JavaDoc userNameParam) {
518         if (logger.isDebugEnabled()) {
519             logger.debug("getSystemUserName(SecuritySession securitySession = "
520                     + securitySession + ", String userNameParam = "
521                     + userNameParam + ") - start");
522         }
523
524         String JavaDoc systemUserName = userNameParam + "@" + domainName;
525         if (logger.isDebugEnabled()) {
526             logger.debug("Returning '"
527                     + systemUserName
528                     + "' as system user name for plain user name '"
529                     + userNameParam
530                     + "'");
531         }
532         return systemUserName;
533     }
534     ////////////////////////////////////////////////////////////////////////////
535

536     /**
537      * {@inheritDoc}
538      *
539      * @param securitySession {@inheritDoc}
540      * @param userNameParam {@inheritDoc}
541      * @throws SystemException If there is a technical problem or the domain
542      * aliases cannot be retrieved.
543      */

544     public List JavaDoc getUserAliases(final SecuritySession securitySession,
545             final String JavaDoc userNameParam)
546         throws SystemException {
547         if (logger.isDebugEnabled()) {
548             logger.debug("getUserAliases(SecuritySession securitySession = "
549                     + securitySession + ", String userNameParam = "
550                     + userNameParam + ") - start");
551         }
552
553         List JavaDoc userAliases = new Vector JavaDoc();
554         try {
555             IDispatch aliases = getDomainAliases();
556             int count = ((Integer JavaDoc) aliases.get("Count")).intValue();
557             String JavaDoc address = getSystemUserName(securitySession,
558                     userNameParam);
559             String JavaDoc atDomain = "@" + domainName;
560
561             for (int i = 0; i < count; ++i) {
562                 IDispatch alias = (IDispatch) aliases.get("Item", new Object JavaDoc[] {
563                         new Integer JavaDoc(i)
564                 });
565                 String JavaDoc value = (String JavaDoc) alias.get("Value");
566                 // we're only interested in aliases for this one user
567
if (!address.equals(value)) {
568                     continue;
569                 }
570                 // we only want aliases to this domain
571
String JavaDoc name = (String JavaDoc) alias.get("Name");
572                 int pos = name.indexOf(atDomain);
573                 if (pos == -1) {
574                     continue;
575                 }
576                 userAliases.add(name.substring(0, pos - 1));
577             }
578         } catch (SystemException e) {
579             logger.error("getUserAliases(SecuritySession, String)", e);
580
581             throw e;
582         } catch (Exception JavaDoc e) {
583             logger.error("getUserAliases(SecuritySession, String)", e);
584
585             throw new SystemException(e);
586         }
587
588         if (logger.isDebugEnabled()) {
589             logger.debug("getUserAliases - end - return value = "
590                             + userAliases);
591         }
592         return userAliases;
593     }
594
595     /**
596      * {@inheritDoc}
597      *
598      * @param securitySession {@inheritDoc}
599      * @param userNameParam {@inheritDoc}
600      * @return Never returns. This method has not been implemeneted and only
601      * ever throws an exception.
602      * @throws SystemException Always thrown as this method is not implemented
603      * for hMailServer.
604      */

605     public String JavaDoc getUserForwarding(final SecuritySession securitySession,
606             final String JavaDoc userNameParam)
607             throws SystemException {
608         if (logger.isDebugEnabled()) {
609             logger.debug("getUserForwarding(SecuritySession securitySession = "
610                     + securitySession + ", String userNameParam = "
611                     + userNameParam + ") - start");
612         }
613
614         throw new SystemException(
615                 "getUserForwarding not implemented for "
616                 + "HMailServer");
617     }
618
619     /**
620      * Returns the username, the part before the 'at sign' in the system
621      * user name.
622      *
623      * {@inheritDoc}
624      *
625      * @param securitySession {@inheritDoc}
626      * @param systemUserNameParam {@inheritDoc}
627      * @return always returns <code>systemUserNameParam</code> before 'at sign'.
628      */

629     public String JavaDoc getUserNameFromSystemUserName(
630             final SecuritySession securitySession,
631             final String JavaDoc systemUserNameParam) {
632         if (logger.isDebugEnabled()) {
633             logger.debug("getUserNameFromSystemUserName = "
634                             + securitySession
635                             + ", String systemUserNameParam = "
636                             + systemUserNameParam + ") - start");
637         }
638
639         int atPos = systemUserNameParam.indexOf('@');
640         if (atPos != -1) {
641             String JavaDoc returnString = systemUserNameParam.substring(0, atPos);
642             if (logger.isDebugEnabled()) {
643                 logger.debug("getUserNameFromSystemUserName - end - "
644                         + "return value = "
645                         + returnString);
646             }
647             return returnString;
648         }
649
650         if (logger.isDebugEnabled()) {
651             logger.debug("getUserNameFromSystemUserName - end - return value = "
652                             + systemUserNameParam);
653         }
654         return systemUserNameParam;
655     }
656
657     /**
658      * {@inheritDoc}
659      *
660      * @param securitySession {@inheritDoc}
661      * @param userNameParam {@inheritDoc}
662      * @return The vacation string for this user.
663      * @throws SystemException If there is a technical problem or the domain
664      * aliases cannot be retrieved.
665      */

666     public String JavaDoc getVacationMessage(final SecuritySession securitySession,
667             final String JavaDoc userNameParam)
668             throws SystemException {
669         if (logger.isDebugEnabled()) {
670             logger.debug("getVacationMessage(SecuritySession securitySession = "
671                     + securitySession
672                     + ", String userNameParam = "
673                     + userNameParam + ") - start");
674         }
675
676         try {
677             String JavaDoc address = getSystemUserName(securitySession,
678                     userNameParam);
679             IDispatch accounts = getDomainAccounts();
680             IDispatch account = (IDispatch)
681                 accounts.get("ItemByAddress", new Object JavaDoc[] {
682                     address
683             });
684             String JavaDoc returnString = (String JavaDoc) account.get("VacationMessage");
685             if (logger.isDebugEnabled()) {
686                 logger.debug("getVacationMessage - end - return value = "
687                                 + returnString);
688             }
689             return returnString;
690         } catch (SystemException e) {
691             logger.error("getVacationMessage(SecuritySession, String)", e);
692
693             throw e;
694         } catch (Exception JavaDoc e) {
695             logger.error("getVacationMessage(SecuritySession, String)", e);
696
697             throw new SystemException(e);
698         }
699     }
700     /**
701      * {@inheritDoc}
702      *
703      * @param securitySession {@inheritDoc}
704      * @param userNameParam {@inheritDoc}
705      * @return <code>true</code> if the user exists.
706      * @throws SystemException If there is a technical problem or the domain
707      * aliases cannot be retrieved.
708      */

709     public boolean isUser(final SecuritySession securitySession,
710             final String JavaDoc userNameParam) throws SystemException {
711         if (logger.isDebugEnabled()) {
712             logger.debug("isUser(SecuritySession securitySession = "
713                     + securitySession + ", String userNameParam = "
714                     + userNameParam + ") - start");
715         }
716
717         try {
718             boolean exists = false;
719             String JavaDoc address = getSystemUserName(securitySession,
720                     userNameParam);
721             IDispatch accounts = getDomainAccounts();
722             int count = ((Integer JavaDoc) accounts.get("Count")).intValue();
723             for (int i = 0; i < count; ++i) {
724                 IDispatch account = (IDispatch)
725                     accounts.get("Item", new Object JavaDoc[] {new Integer JavaDoc(i)});
726                 if (address.equals(account.get("Address"))) {
727                     exists = true;
728                     break;
729                 }
730             }
731
732             if (logger.isDebugEnabled()) {
733                 logger.debug("isUser - end - return value = "
734                                 + exists);
735             }
736             return exists;
737         } catch (SystemException e) {
738             logger.error("isUser(SecuritySession, String)", e);
739
740             throw e;
741         } catch (Exception JavaDoc e) {
742             logger.error("isUser(SecuritySession, String)", e);
743
744             throw new SystemException(e);
745         }
746     }
747
748     /**
749      * {@inheritDoc}
750      *
751      * @param securitySession {@inheritDoc}
752      * @param nameParam {@inheritDoc}
753      * @throws SystemException Always thrown as this method is not implemented
754      * for hMailServer.
755      */

756     public void removeList(final SecuritySession securitySession,
757             final String JavaDoc nameParam) throws SystemException {
758         if (logger.isDebugEnabled()) {
759             logger.debug("removeList(SecuritySession securitySession = "
760                     + securitySession + ", String nameParam = " + nameParam
761                     + ") - start");
762         }
763
764
765         if (logger.isDebugEnabled()) {
766             logger.debug("removeList(SecuritySession, String) - end");
767         }
768         throw new SystemException(
769                 "removeList not implemented for "
770                 + "HMailServer");
771     }
772
773
774     /**
775      * {@inheritDoc}
776      *
777      * @param securitySession {@inheritDoc}
778      * @param userNameParam {@inheritDoc}
779      * @throws SystemException If there is a technical problem or the domain
780      * aliases cannot be retrieved.
781      */

782     public void removeUser(final SecuritySession securitySession,
783             final String JavaDoc userNameParam)
784             throws SystemException {
785         if (logger.isDebugEnabled()) {
786             logger.debug("removeUser(SecuritySession securitySession = "
787                     + securitySession + ", String userNameParam = "
788                     + userNameParam + ") - start");
789         }
790
791         try {
792             IDispatch accounts = getDomainAccounts();
793             IDispatch account = null;
794             String JavaDoc address = getSystemUserName(securitySession,
795                     userNameParam);
796             try {
797                 account = (IDispatch) accounts.get("ItemByAddress",
798                         new Object JavaDoc[] {address});
799             } catch (JComException e) {
800                 logger.error("removeUser(SecuritySession, String)", e);
801
802                 throw new SystemException("No user found with address '"
803                         + address
804                         + "'", e);
805             }
806             accounts.method("DeleteByDBID", new Object JavaDoc[] {account.get("ID")});
807         } catch (SystemException e) {
808             logger.error("removeUser(SecuritySession, String)", e);
809
810             throw e;
811         } catch (Exception JavaDoc e) {
812             logger.error("removeUser(SecuritySession, String)", e);
813
814             throw new SystemException(e);
815         }
816
817         if (logger.isDebugEnabled()) {
818             logger.debug("removeUser(SecuritySession, String) - end");
819         }
820     }
821
822     /**
823      * {@inheritDoc}
824      *
825      * @param securitySession {@inheritDoc}
826      * @param nameParam {@inheritDoc}
827      * @param usersParam {@inheritDoc}
828      * @throws SystemException Always thrown as this method is not implemented
829      * for hMailServer.
830      */

831     public void setList(final SecuritySession securitySession,
832             final String JavaDoc nameParam, final Collection JavaDoc usersParam)
833             throws SystemException {
834         if (logger.isDebugEnabled()) {
835             logger.debug("setList(SecuritySession securitySession = "
836                     + securitySession + ", String nameParam = " + nameParam
837                     + ", Collection usersParam = " + usersParam + ") - start");
838         }
839
840
841         if (logger.isDebugEnabled()) {
842             logger.debug("setList(SecuritySession, String, Collection) - end");
843         }
844         throw new SystemException(
845                 "setList not implemented for "
846                 + "HMailServer");
847     }
848
849     /**
850      * {@inheritDoc}
851      *
852      * @param securitySession {@inheritDoc}
853      * @param userNameParam {@inheritDoc}
854      * @param passwordParam {@inheritDoc}
855      * @throws SystemException If there is a technical problem or the domain
856      * aliases cannot be retrieved.
857      */

858     public void setPassword(final SecuritySession securitySession,
859             final String JavaDoc userNameParam,
860             final String JavaDoc passwordParam)
861             throws SystemException {
862         if (logger.isDebugEnabled()) {
863             logger.debug("setPassword(SecuritySession securitySession = "
864                     + securitySession + ", String userNameParam = "
865                     + userNameParam + ", String passwordParam = "
866                     + passwordParam + ") - start");
867         }
868
869         try {
870             String JavaDoc address = getSystemUserName(securitySession,
871                     userNameParam);
872             IDispatch accounts = getDomainAccounts();
873             IDispatch account = (IDispatch)
874                 accounts.get("ItemByAddress", new Object JavaDoc[] {
875                     address
876             });
877             account.put("Password", passwordParam);
878             account.method("Save", new Object JavaDoc[] {});
879         } catch (SystemException e) {
880             logger.error("setPassword(SecuritySession, String, String)", e);
881
882             throw e;
883         } catch (Exception JavaDoc e) {
884             logger.error("setPassword(SecuritySession, String, String)", e);
885
886             throw new SystemException(e);
887         }
888
889         if (logger.isDebugEnabled()) {
890             logger.debug("setPassword(SecuritySession, String, String) - end");
891         }
892     }
893
894     /**
895      * {@inheritDoc}
896      *
897      * @param securitySession {@inheritDoc}
898      * @param userNameParam {@inheritDoc}
899      * @param aliasesParam {@inheritDoc}
900      * @throws SystemException If there is a technical problem or the domain
901      * aliases cannot be retrieved.
902      */

903     public void setUserAliases(final SecuritySession securitySession,
904             final String JavaDoc userNameParam,
905             final Collection JavaDoc aliasesParam)
906             throws SystemException {
907         if (logger.isDebugEnabled()) {
908             logger.debug("setUserAliases(SecuritySession securitySession = "
909                     + securitySession + ", String userNameParam = "
910                     + userNameParam + ", Collection aliasesParam = "
911                     + aliasesParam + ") - start");
912         }
913
914         try {
915             IDispatch aliases = getDomainAliases();
916             int count = ((Integer JavaDoc) aliases.get("Count")).intValue();
917             String JavaDoc address = getSystemUserName(securitySession,
918                     userNameParam);
919             String JavaDoc atDomain = "@" + domainName;
920
921             // first get all existing aliases in one handy sized map
922
Map JavaDoc allAliases = new HashMap JavaDoc();
923             for (int i = 0; i < count; ++i) {
924                 IDispatch alias = (IDispatch) aliases.get("Item", new Object JavaDoc[] {
925                         new Integer JavaDoc(i)
926                 });
927                 allAliases.put(alias.get("Name"), alias.get("Value"));
928             }
929
930             // ok, this is a bit sloppy but for now, if the alias is already
931
// taken by someone else, it is just changed to this guy or gal
932
Iterator JavaDoc aliasIterator = aliasesParam.iterator();
933             while (aliasIterator.hasNext()) {
934                 String JavaDoc fullAlias = aliasIterator.next() + atDomain;
935                 String JavaDoc existing = (String JavaDoc) allAliases.get(fullAlias);
936                 IDispatch alias;
937                 if (existing != null) {
938                     // if it is already set to this user, safe to ignore
939
// otherwise, we'll just quietly take it over
940
if (address.equals(existing)) {
941                         continue;
942                     }
943                     alias = (IDispatch) aliases.get("ItemByName",
944                                 new Object JavaDoc[] {
945                             fullAlias
946                     });
947                 } else {
948                     // new alias here
949
alias = (IDispatch)
950                             aliases.method("Add", new Object JavaDoc[] {});
951                     alias.put("Name", fullAlias);
952                 }
953                 alias.put("Value", address);
954                 alias.put("Active", Boolean.TRUE);
955                 alias.method("Save", new Object JavaDoc[] {});
956             }
957         } catch (SystemException e) {
958             logger.error("setUserAliases(SecuritySession, String, Collection)",
959                     e);
960             throw e;
961         } catch (Exception JavaDoc e) {
962             logger.error("setUserAliases(SecuritySession, String, Collection)",
963                     e);
964             throw new SystemException(e);
965         }
966
967         if (logger.isDebugEnabled()) {
968             logger.debug("setUserAliases - end");
969         }
970     }
971
972     /**
973      * {@inheritDoc}
974      *
975      * @param securitySession {@inheritDoc}
976      * @param userNameParam {@inheritDoc}
977      * @param addressParam {@inheritDoc}
978      * @throws SystemException Always thrown as this method is not implemented
979      * for hMailServer.
980      */

981     public void setUserForwarding(final SecuritySession securitySession,
982             final String JavaDoc userNameParam, final String JavaDoc addressParam)
983             throws SystemException {
984         if (logger.isDebugEnabled()) {
985             logger.debug("setUserForwarding(SecuritySession securitySession = "
986                     + securitySession + ", String userNameParam = "
987                     + userNameParam + ", String addressParam = " + addressParam
988                     + ") - start");
989         }
990
991
992         if (logger.isDebugEnabled()) {
993             logger.debug("setUserForwarding - end");
994         }
995         throw new SystemException(
996                 "setUserForwarding not implemented for "
997                 + "HMailServer");
998     }
999
1000    /**
1001     * {@inheritDoc}
1002     *
1003     * @param securitySession {@inheritDoc}
1004     * @param userNameParam {@inheritDoc}
1005     * @param messageParam {@inheritDoc}
1006     * @throws SystemException If there is a technical problem or the domain
1007     * aliases cannot be retrieved.
1008     */

1009    public void setVacationMessage(final SecuritySession securitySession,
1010            final String JavaDoc userNameParam,
1011            final String JavaDoc messageParam)
1012            throws SystemException {
1013        if (logger.isDebugEnabled()) {
1014            logger.debug("setVacationMessage(SecuritySession securitySession = "
1015                    + securitySession
1016                    + ", String userNameParam = "
1017                    + userNameParam
1018                    + ", String messageParam = "
1019                    + messageParam + ") - start");
1020        }
1021
1022        try {
1023            String JavaDoc address = getSystemUserName(securitySession,
1024                    userNameParam);
1025            IDispatch accounts = getDomainAccounts();
1026            IDispatch account = (IDispatch)
1027                accounts.get("ItemByAddress", new Object JavaDoc[] {
1028                    address
1029            });
1030            account.put("VacationMessage", messageParam);
1031            account.method("Save", new Object JavaDoc[] {});
1032        } catch (SystemException e) {
1033            logger.error("setVacationMessage",
1034                    e);
1035
1036            throw e;
1037        } catch (Exception JavaDoc e) {
1038            logger.error("setVacationMessage",
1039                    e);
1040
1041            throw new SystemException(e);
1042        }
1043
1044        if (logger.isDebugEnabled()) {
1045            logger.debug("setVacationMessage - end");
1046        }
1047    }
1048}
1049
Popular Tags