1 56 package com.ivata.groupware.business.mail.server; 57 58 import java.util.Collection ; 59 import java.util.HashMap ; 60 import java.util.Iterator ; 61 import java.util.List ; 62 import java.util.Map ; 63 import java.util.Vector ; 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 101 public class HMailServer extends JavaMailServer implements MailServer { 102 105 private static final long serialVersionUID = 1L; 106 110 private static final String V3_BASE_APP_NAME = "HCOM.BaseApp"; 111 115 private static final String V4_DOMAINS_NAME = "hMailServer.Domains"; 116 117 121 private static IDispatch baseApp = null; 122 125 private static final Logger logger = Logger.getLogger(HMailServer.class); 126 131 private static ReleaseManager releaseManager; 132 static { 133 try { 134 releaseManager = new ReleaseManager(); 135 } catch (Exception e) { 136 logger.error(e.getClass().getName() 137 + " trying to create JCom ReleaseManager."); 138 throw new RuntimeException (e); 139 } 140 IDispatch domains; 141 try { 142 domains = new IDispatch(releaseManager, V4_DOMAINS_NAME); 143 } catch (Exception e) { 144 logger.error(e.getClass().getName() 145 + " trying to retrieve hMailServer domains from COM " 146 + "object '" 147 + V4_DOMAINS_NAME 148 + "'.", e); 149 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 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 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 (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 e) { 185 logger.error(e.getClass().getName() 186 + " trying to retrieve hMailServer version.", e); 187 } 188 } 189 } 190 197 public static IDispatch getDomains() throws SystemException { 198 if (logger.isDebugEnabled()) { 199 logger.debug("getDomains() - start"); 200 } 201 IDispatch domains; 202 203 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 236 public static void main(final String [] 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 e) { 245 logger.error("Cannot instantiate HMailServer", e); 246 System.exit(-1); 247 } 248 249 try { 250 mailTest.removeUser(null, "test"); 251 } catch (Exception 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 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 275 private String domainName; 276 283 public HMailServer(final String domainNameParam, 284 final AddressBook addressBook, 285 final Settings settings) { 286 super(addressBook, settings); 287 domainName = domainNameParam; 288 } 289 297 public void addUser(final SecuritySession securitySession, 298 final String userNameParam, final String 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 address = getSystemUserName(securitySession, userNameParam); 310 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 [] {}); 320 newAccount.put("Address", getSystemUserName(securitySession, 321 userNameParam)); 322 newAccount.put("Active", Boolean.TRUE); 323 newAccount.method("Save", new Object [] {}); 324 } catch (SystemException e) { 325 logger.error("addUser(SecuritySession, String, String)", e); 326 327 throw e; 328 } catch (Exception 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 348 public void checkPassword(final SecuritySession securitySession, 349 final String userNameParam, 350 final String 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 address = getSystemUserName(securitySession, 361 userNameParam); 362 IDispatch accounts = getDomainAccounts(); 363 IDispatch account = (IDispatch) 364 accounts.get("ItemByAddress", new Object [] { 365 address 366 }); 367 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 passwordCheck = (String ) utilities.method("MD5", 384 new Object [] { 385 passwordParam 386 }); 387 if (!passwordCheck.equals(account.get("Password"))) { 388 throw new SystemException("Passwords did not match"); 389 } 390 } else { 391 if (logger.isDebugEnabled()) { 393 logger.debug("Checking the password for account '" 394 + userNameParam 395 + "' via hMailServer v4.x."); 396 } 397 Boolean validatePassword = (Boolean ) 398 account.method("ValidatePassword", new Object [] { 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 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 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 [] {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 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 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 516 public String getSystemUserName(final SecuritySession securitySession, 517 final String userNameParam) { 518 if (logger.isDebugEnabled()) { 519 logger.debug("getSystemUserName(SecuritySession securitySession = " 520 + securitySession + ", String userNameParam = " 521 + userNameParam + ") - start"); 522 } 523 524 String 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 536 544 public List getUserAliases(final SecuritySession securitySession, 545 final String 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 userAliases = new Vector (); 554 try { 555 IDispatch aliases = getDomainAliases(); 556 int count = ((Integer ) aliases.get("Count")).intValue(); 557 String address = getSystemUserName(securitySession, 558 userNameParam); 559 String atDomain = "@" + domainName; 560 561 for (int i = 0; i < count; ++i) { 562 IDispatch alias = (IDispatch) aliases.get("Item", new Object [] { 563 new Integer (i) 564 }); 565 String value = (String ) alias.get("Value"); 566 if (!address.equals(value)) { 568 continue; 569 } 570 String name = (String ) 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 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 605 public String getUserForwarding(final SecuritySession securitySession, 606 final String 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 629 public String getUserNameFromSystemUserName( 630 final SecuritySession securitySession, 631 final String 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 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 666 public String getVacationMessage(final SecuritySession securitySession, 667 final String 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 address = getSystemUserName(securitySession, 678 userNameParam); 679 IDispatch accounts = getDomainAccounts(); 680 IDispatch account = (IDispatch) 681 accounts.get("ItemByAddress", new Object [] { 682 address 683 }); 684 String returnString = (String ) 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 e) { 695 logger.error("getVacationMessage(SecuritySession, String)", e); 696 697 throw new SystemException(e); 698 } 699 } 700 709 public boolean isUser(final SecuritySession securitySession, 710 final String 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 address = getSystemUserName(securitySession, 720 userNameParam); 721 IDispatch accounts = getDomainAccounts(); 722 int count = ((Integer ) accounts.get("Count")).intValue(); 723 for (int i = 0; i < count; ++i) { 724 IDispatch account = (IDispatch) 725 accounts.get("Item", new Object [] {new Integer (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 e) { 742 logger.error("isUser(SecuritySession, String)", e); 743 744 throw new SystemException(e); 745 } 746 } 747 748 756 public void removeList(final SecuritySession securitySession, 757 final String 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 782 public void removeUser(final SecuritySession securitySession, 783 final String 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 address = getSystemUserName(securitySession, 795 userNameParam); 796 try { 797 account = (IDispatch) accounts.get("ItemByAddress", 798 new Object [] {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 [] {account.get("ID")}); 807 } catch (SystemException e) { 808 logger.error("removeUser(SecuritySession, String)", e); 809 810 throw e; 811 } catch (Exception 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 831 public void setList(final SecuritySession securitySession, 832 final String nameParam, final Collection 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 858 public void setPassword(final SecuritySession securitySession, 859 final String userNameParam, 860 final String 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 address = getSystemUserName(securitySession, 871 userNameParam); 872 IDispatch accounts = getDomainAccounts(); 873 IDispatch account = (IDispatch) 874 accounts.get("ItemByAddress", new Object [] { 875 address 876 }); 877 account.put("Password", passwordParam); 878 account.method("Save", new Object [] {}); 879 } catch (SystemException e) { 880 logger.error("setPassword(SecuritySession, String, String)", e); 881 882 throw e; 883 } catch (Exception 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 903 public void setUserAliases(final SecuritySession securitySession, 904 final String userNameParam, 905 final Collection 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 ) aliases.get("Count")).intValue(); 917 String address = getSystemUserName(securitySession, 918 userNameParam); 919 String atDomain = "@" + domainName; 920 921 Map allAliases = new HashMap (); 923 for (int i = 0; i < count; ++i) { 924 IDispatch alias = (IDispatch) aliases.get("Item", new Object [] { 925 new Integer (i) 926 }); 927 allAliases.put(alias.get("Name"), alias.get("Value")); 928 } 929 930 Iterator aliasIterator = aliasesParam.iterator(); 933 while (aliasIterator.hasNext()) { 934 String fullAlias = aliasIterator.next() + atDomain; 935 String existing = (String ) allAliases.get(fullAlias); 936 IDispatch alias; 937 if (existing != null) { 938 if (address.equals(existing)) { 941 continue; 942 } 943 alias = (IDispatch) aliases.get("ItemByName", 944 new Object [] { 945 fullAlias 946 }); 947 } else { 948 alias = (IDispatch) 950 aliases.method("Add", new Object [] {}); 951 alias.put("Name", fullAlias); 952 } 953 alias.put("Value", address); 954 alias.put("Active", Boolean.TRUE); 955 alias.method("Save", new Object [] {}); 956 } 957 } catch (SystemException e) { 958 logger.error("setUserAliases(SecuritySession, String, Collection)", 959 e); 960 throw e; 961 } catch (Exception 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 981 public void setUserForwarding(final SecuritySession securitySession, 982 final String userNameParam, final String 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 1009 public void setVacationMessage(final SecuritySession securitySession, 1010 final String userNameParam, 1011 final String 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 address = getSystemUserName(securitySession, 1024 userNameParam); 1025 IDispatch accounts = getDomainAccounts(); 1026 IDispatch account = (IDispatch) 1027 accounts.get("ItemByAddress", new Object [] { 1028 address 1029 }); 1030 account.put("VacationMessage", messageParam); 1031 account.method("Save", new Object [] {}); 1032 } catch (SystemException e) { 1033 logger.error("setVacationMessage", 1034 e); 1035 1036 throw e; 1037 } catch (Exception 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 |