0001: /**
0002: * $Id: LdapAddressBook.java,v 1.11 2005/09/21 10:47:48 dg154973 Exp $
0003: * Copyright 2002 Sun Microsystems, Inc. All
0004: * rights reserved. Use of this product is subject
0005: * to license terms. Federal Acquisitions:
0006: * Commercial Software -- Government Users
0007: * Subject to Standard License Terms and
0008: * Conditions.
0009: *
0010: * Sun, Sun Microsystems, the Sun logo, and iPlanet
0011: * are trademarks or registered trademarks of Sun Microsystems,
0012: * Inc. in the United States and other countries.
0013: */package com.sun.addressbook.ldap;
0014:
0015: import java.util.ArrayList;
0016: import java.util.Properties;
0017: import java.util.Enumeration;
0018: import java.util.logging.Logger;
0019: import java.util.logging.Level;
0020: import java.util.logging.LogRecord;
0021:
0022: import com.sun.addressbook.ABFilter;
0023: import com.sun.addressbook.ABSearchTerm;
0024: import com.sun.addressbook.ABStoreException;
0025: import com.sun.addressbook.Element;
0026: import com.sun.addressbook.Entry;
0027: import com.sun.addressbook.ABSession;
0028: import com.sun.addressbook.Group;
0029: import com.sun.addressbook.ABStore;
0030: import com.sun.addressbook.AddressBook;
0031: import com.sun.addressbook.MissingPropertiesException;
0032: import com.sun.addressbook.OperationNotSupportedException;
0033: import com.sun.addressbook.ABLogger;
0034:
0035: import netscape.ldap.LDAPConnection;
0036: import netscape.ldap.LDAPSearchResults;
0037: import netscape.ldap.LDAPException;
0038: import netscape.ldap.LDAPEntry;
0039: import netscape.ldap.LDAPModification;
0040: import netscape.ldap.LDAPModificationSet;
0041: import netscape.ldap.LDAPAttribute;
0042: import netscape.ldap.LDAPAttributeSet;
0043: import netscape.ldap.LDAPv2;
0044: import netscape.ldap.LDAPCompareAttrNames;
0045: import java.lang.reflect.Field;
0046: import java.io.*;
0047:
0048: /**
0049: * Ldap Adapter Address Book implementation: encapsulates the
0050: * commands to be carried out on the address book.
0051: *
0052: */
0053: public class LdapAddressBook extends AddressBook {
0054:
0055: private static long lastPabTimeStamp = 0L;
0056: private LdapABStore ldapStore = null;
0057: private String fullPabSearchBase = null;
0058: private String pabLang = null;
0059:
0060: // Create a Logger for this class
0061: private static Logger debugLogger = ABLogger
0062: .getLogger("com.sun.portal.addressbook.ldap");
0063:
0064: public LdapAddressBook(ABStore store, String abID) {
0065: super (store, abID);
0066: ldapStore = (LdapABStore) store;
0067: fullPabSearchBase = ldapStore.getFullPabSearchBase();
0068: pabLang = ldapStore.getPabLang();
0069: }
0070:
0071: /**
0072: * Run a search on the address book given the contraints and
0073: * filters specified in the ABFilter argument.
0074: *
0075: * @param abFilter Definition of the contraints and the filters for the
0076: * fetch.
0077: * @return Array of Address Book Element objects.
0078: * @exception ABStoreException if unable to connect to back end service.
0079: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
0080: */
0081: public Element[] fetch(ABFilter abFilter) throws ABStoreException,
0082: OperationNotSupportedException {
0083: if (abFilter.getElementType() == Element.ENTRY) {
0084: // Fetch only Address Book Entries
0085: debugLogger.finer("PSJA_CSAL0026");
0086: return fetchEntries(abFilter);
0087: } else if (abFilter.getElementType() == Element.GROUP) {
0088: // Fetch only Address Book Groups
0089: debugLogger.finer("PSJA_CSAL0031");
0090: return fetchGroups(abFilter);
0091: } else if (abFilter.getElementType() == Element.ADDRESSBOOK) {
0092: // Fetch only Address Book Groups
0093: // sub folders not supported yet ..
0094: String msg = "LdapAddressBook.fetch: Multiple/Sub address books not supported in SunONE Messaging Server";
0095: debugLogger.finer("PSJA_CSAL0032");
0096: throw new OperationNotSupportedException(msg);
0097: } else {
0098: // Default - return groups and entries.
0099: // In case the type is ALL, UNSPECIFIED or any other value,
0100: // fetch the complete Address Book - entries and groups
0101: debugLogger.finer("PSJA_CSAL0033");
0102: return fetchAll(abFilter);
0103: }
0104: }
0105:
0106: protected Entry[] fetchEntries(ABFilter abFilter)
0107: throws ABStoreException, OperationNotSupportedException {
0108: // Get connection to the Database
0109: LDAPConnection ld = ldapStore.LDAPConnect();
0110:
0111: ArrayList listEntries = null;
0112:
0113: if (ld == null) {
0114: String msg = "LdapAddressBook.fetch:failed to get ldap conn from pool";
0115: debugLogger.finer("PSJA_CSAL0009");
0116: throw new ABStoreException(msg);
0117: }
0118:
0119: String searchBase = fullPabSearchBase;
0120: String searchFilterHeader = "(&("
0121: + LdapABConstants.ATTR_memberofpab + "=" + abID + ")("
0122: + LdapABConstants.ATTR_objectclass + "="
0123: + LdapABConstants.OBJECTCLASS_ENTRY + ")";
0124:
0125: if (abFilter.getGroup() != null) {
0126: searchFilterHeader += "("
0127: + LdapABConstants.ATTR_memberofpabgroup + "="
0128: + abFilter.getGroup().getUn() + ")";
0129: }
0130: String searchFilter = (String) abFilter.getSearchFilter();
0131:
0132: searchFilter = searchFilterHeader + searchFilter + ")";
0133: debugLogger.log(Level.FINER, "PSJA_CSAL0001", searchFilter);
0134: LDAPSearchResults ldapsearchresults = null;
0135:
0136: // Entry object gets generated with the fields in the same sequence as
0137: // searchAttr array. Make sure this sequence is same as Entry constructor.
0138: String searchAttr[] = { "un", LdapABConstants.ATTR_fn,
0139: LdapABConstants.ATTR_ln, LdapABConstants.ATTR_cn,
0140: LdapABConstants.ATTR_em,
0141: LdapABConstants.ATTR_description,
0142: LdapABConstants.ATTR_bp, LdapABConstants.ATTR_hp,
0143: LdapABConstants.ATTR_mp,
0144: LdapABConstants.ATTR_homeStreet,
0145: LdapABConstants.ATTR_homeCity,
0146: LdapABConstants.ATTR_homeState,
0147: LdapABConstants.ATTR_homeZip,
0148: LdapABConstants.ATTR_homeCountry,
0149: LdapABConstants.ATTR_fp, LdapABConstants.ATTR_pp,
0150: LdapABConstants.ATTR_dob, LdapABConstants.ATTR_uri,
0151: LdapABConstants.ATTR_memberofpab,
0152: LdapABConstants.ATTR_memberofpabgroup };
0153:
0154: // Search the address book
0155: try {
0156: ldapsearchresults = ld.search(searchBase, ld.SCOPE_SUB,
0157: searchFilter, searchAttr, false);
0158: if (ldapsearchresults == null) {
0159: debugLogger.warning("PSJA_CSAL0034");
0160: ldapStore.LDAPClose(ld);
0161: throw new ABStoreException(
0162: "LdapAddressBook.fetch: ldapsearchresults should never null");
0163: }
0164: } catch (LDAPException ldapE) {
0165: debugLogger.log(Level.INFO, "PSJA_CSAL0035", ldapE);
0166: ldapStore.LDAPClose(ld);
0167: throw new ABStoreException(
0168: "LdapAddressBook.fetch: search for pab entries failed: "
0169: + ldapE);
0170: }
0171: if (ldapsearchresults != null) {
0172: debugLogger.log(Level.FINER, "PSJA_CSAL0036", Integer
0173: .toString(ldapsearchresults.getCount()));
0174:
0175: String sortBy = abFilter.getSortBy();
0176: // Sort the results depending upon user's input
0177: if ((sortBy != null) && (!sortBy.trim().equals(""))) {
0178: // Set the sort direction
0179: boolean ascend = true;
0180: if (abFilter.getSortOrder() == ABFilter.ASCENDING) {
0181: ascend = true;
0182: } else if ((abFilter.getSortOrder() == ABFilter.DESCENDING)) {
0183: ascend = false;
0184: } else {
0185: ascend = true;
0186: }
0187:
0188: String sortAttr = null;
0189: // Set the attribute to sort on
0190: LdapABConstants constants = new LdapABConstants();
0191: try {
0192: Field attr = constants.getClass().getField(
0193: "ATTR_" + sortBy);
0194: String attrName = (String) attr.get(null);
0195: debugLogger.log(Level.FINER, "PSJA_CSAL0037",
0196: attrName);
0197: sortAttr = attrName;
0198: } catch (IllegalAccessException iae) {
0199: debugLogger
0200: .log(Level.INFO, "PSJA_CSAL0003", sortBy);
0201: throw new ABStoreException("Cannot access ATTR_"
0202: + sortBy + " from LdapABConstants");
0203: } catch (NoSuchFieldException nfe) {
0204: debugLogger
0205: .log(Level.INFO, "PSJA_CSAL0004", sortBy);
0206: throw new ABStoreException("Field ATTR_" + sortBy
0207: + " not found");
0208: }
0209:
0210: if (sortAttr != null) {
0211: ldapsearchresults.sort(new LDAPCompareAttrNames(
0212: sortAttr, ascend));
0213: }
0214: }
0215:
0216: listEntries = new ArrayList(ldapsearchresults.getCount());
0217: int index = 0;
0218: try {
0219: while (ldapsearchresults.hasMoreElements()) {
0220: LDAPEntry ldapentry = ldapsearchresults.next();
0221: //printResults(ldapentry, searchAttr);
0222: listEntries.add(toEntry(ldapentry, searchAttr,
0223: index));
0224: index++;
0225: }
0226: } catch (LDAPException ldapE) {
0227: String msg = "LdapAddressBook.fetch: problem processing pab search results: "
0228: + ldapE;
0229: debugLogger.info("PSJA_CSAL0038");
0230: ldapStore.LDAPClose(ld);
0231: throw new ABStoreException(msg);
0232: }
0233: }
0234:
0235: // Clean up and Close connection
0236: ldapsearchresults = null;
0237: ldapStore.LDAPClose(ld);
0238: debugLogger.log(Level.FINER, "PSJA_CSAL0039", Integer
0239: .toString(listEntries.size()));
0240: return (Entry[]) listEntries.toArray(new Entry[listEntries
0241: .size()]);
0242: }
0243:
0244: /**
0245: * Fetch the list of groups in the address book.
0246: *
0247: * @return String array of groups in the address book.
0248: * @exception ABStoreException if unable to connect to back end service.
0249: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
0250: */
0251: protected Group[] fetchGroups(ABFilter abFilter)
0252: throws ABStoreException, OperationNotSupportedException {
0253: Group[] groups = null;
0254:
0255: // Get connection to the Database
0256: LDAPConnection ld = ldapStore.LDAPConnect();
0257: ArrayList listGroups = null;
0258:
0259: if (ld == null) {
0260: String msg = "LdapAddressBook.connect:failed to get ldap connection from pool";
0261: debugLogger.info("PSJA_CSAL0009");
0262: throw new ABStoreException(msg);
0263: }
0264:
0265: String searchBase = fullPabSearchBase;
0266:
0267: String searchFilterHeader = "(&("
0268: + LdapABConstants.ATTR_memberofpab + "=" + abID + ")("
0269: + LdapABConstants.ATTR_objectclass + "="
0270: + LdapABConstants.OBJECTCLASS_GROUP + ")";
0271:
0272: if (abFilter.getGroup() != null) {
0273: searchFilterHeader += "("
0274: + LdapABConstants.ATTR_memberofpabgroup + "="
0275: + abFilter.getGroup().getUn() + ")";
0276: }
0277: String searchFilter = (String) abFilter.getSearchFilter();
0278:
0279: searchFilter = searchFilterHeader + searchFilter + ")";
0280: debugLogger.log(Level.FINER, "PSJA_CSAL0001", searchFilter);
0281:
0282: LDAPSearchResults ldapsearchresults = null;
0283:
0284: // Group object gets generated with the fields in the same sequence as
0285: // searchAttr array. Make sure this sequence is same as Group
0286: // constructor.
0287: String searchAttr[] = { "un", LdapABConstants.ATTR_cn,
0288: LdapABConstants.ATTR_description };
0289:
0290: // Search the address book
0291: try {
0292: ldapsearchresults = ld.search(searchBase, ld.SCOPE_SUB,
0293: searchFilter, searchAttr, false);
0294: if (ldapsearchresults == null) {
0295: debugLogger.warning("PSJA_CSAL0034");
0296: ldapStore.LDAPClose(ld);
0297: throw new ABStoreException(
0298: "LdapAddressBook.fetchGroups: ldapsearchresults should never null");
0299: }
0300: } catch (LDAPException ldapE) {
0301: debugLogger.log(Level.INFO, "PSJA_CSAL0035", ldapE);
0302:
0303: ldapStore.LDAPClose(ld);
0304: throw new ABStoreException(
0305: "LdapAddressBook.fetchGroups: search for pab entries failed: "
0306: + ldapE);
0307: }
0308: if (ldapsearchresults != null) {
0309: debugLogger.log(Level.FINER, "PSJA_CSAL0036", Integer
0310: .toString(ldapsearchresults.getCount()));
0311:
0312: String sortBy = abFilter.getSortBy();
0313: // Sort the results depending upon user's input
0314: // Sorting in groups has to be done based on cn
0315: if ((sortBy != null)
0316: && (sortBy.trim().equals(LdapABConstants.ATTR_cn))) {
0317: // Set the sort direction
0318: boolean ascend = true;
0319: if (abFilter.getSortOrder() == ABFilter.ASCENDING) {
0320: ascend = true;
0321: } else if ((abFilter.getSortOrder() == ABFilter.DESCENDING)) {
0322: ascend = false;
0323: } else {
0324: ascend = true;
0325: }
0326:
0327: String sortAttr = null;
0328: ldapsearchresults.sort(new LDAPCompareAttrNames(sortBy,
0329: ascend));
0330: }
0331:
0332: listGroups = new ArrayList(ldapsearchresults.getCount());
0333: int index = 0;
0334: try {
0335: while (ldapsearchresults.hasMoreElements()) {
0336: LDAPEntry ldapentry = ldapsearchresults.next();
0337: //printResults(ldapentry, searchAttr);
0338: listGroups
0339: .add(toGroup(ldapentry, searchAttr, index));
0340: index++;
0341: }
0342: } catch (LDAPException ldapE) {
0343: //errorDebug("LdapABStore.fetchGroups: problem processing pab search results: " + ldapE);
0344: ldapStore.LDAPClose(ld);
0345: throw new ABStoreException(
0346: "LdapAddressBook.fetchGroups: problem processing groups fetch results: "
0347: + ldapE);
0348: }
0349: }
0350:
0351: // Clean up and Close connection
0352: ldapsearchresults = null;
0353: ldapStore.LDAPClose(ld);
0354: debugLogger.log(Level.FINER, "PSJA_CSAL0040", Integer
0355: .toString(listGroups.size()));
0356: return (Group[]) listGroups
0357: .toArray(new Group[listGroups.size()]);
0358: }
0359:
0360: /**
0361: * Fetch all the elements in the address book - namely address book
0362: * entries and groups
0363: */
0364:
0365: protected Element[] fetchAll(ABFilter abFilter)
0366: throws ABStoreException, OperationNotSupportedException {
0367: // Get connection to the Database
0368: LDAPConnection ld = ldapStore.LDAPConnect();
0369:
0370: ArrayList listElements = null;
0371:
0372: if (ld == null) {
0373: String msg = "LdapAddressBook.fetch:failed to get ldap conn from pool";
0374: debugLogger.info("PSJA_CSAL0009");
0375: throw new ABStoreException(msg);
0376: }
0377:
0378: String searchBase = fullPabSearchBase;
0379:
0380: // The spaces in the search filter portion
0381: // (| ( objectclass=pabgroup ) ( objectclass=pabperson ) ) are
0382: // important. If not, then the ldap server searches for name-value
0383: // pair "|(objectclass" = "pabperson)(objectclass=pabgroup)" for
0384: // whatever reason! Bug in Directory?
0385:
0386: String searchFilterHeader = "(&("
0387: + LdapABConstants.ATTR_memberofpab + "=" + abID
0388: + ")(| ( " + LdapABConstants.ATTR_objectclass + "="
0389: + LdapABConstants.OBJECTCLASS_ENTRY + " ) ( "
0390: + LdapABConstants.ATTR_objectclass + "="
0391: + LdapABConstants.OBJECTCLASS_GROUP + " ) )";
0392:
0393: if (abFilter.getGroup() != null) {
0394: searchFilterHeader += "("
0395: + LdapABConstants.ATTR_memberofpabgroup + "="
0396: + abFilter.getGroup().getUn() + ")";
0397: }
0398: String searchFilter = (String) abFilter.getSearchFilter();
0399:
0400: searchFilter = searchFilterHeader + searchFilter + ")";
0401: debugLogger.log(Level.FINER, "PSJA_CSAL0001", searchFilter);
0402: debugLogger.log(Level.FINER, "PSJA_CSAL0041",
0403: searchFilterHeader);
0404: LDAPSearchResults ldapsearchresults = null;
0405:
0406: // Entry object gets generated with the fields in the same
0407: // sequence as searchAttr array. Make sure this sequence is same as
0408: // Entry constructor.
0409:
0410: String searchAttr[] = { "un", LdapABConstants.ATTR_fn,
0411: LdapABConstants.ATTR_ln, LdapABConstants.ATTR_cn,
0412: LdapABConstants.ATTR_em,
0413: LdapABConstants.ATTR_description,
0414: LdapABConstants.ATTR_bp, LdapABConstants.ATTR_hp,
0415: LdapABConstants.ATTR_mp,
0416: LdapABConstants.ATTR_homeStreet,
0417: LdapABConstants.ATTR_homeCity,
0418: LdapABConstants.ATTR_homeState,
0419: LdapABConstants.ATTR_homeZip,
0420: LdapABConstants.ATTR_homeCountry,
0421: LdapABConstants.ATTR_fp, LdapABConstants.ATTR_pp,
0422: LdapABConstants.ATTR_dob, LdapABConstants.ATTR_uri,
0423: LdapABConstants.ATTR_memberofpab,
0424: LdapABConstants.ATTR_memberofpabgroup,
0425: LdapABConstants.ATTR_objectclass };
0426: // Search the address book
0427: try {
0428: ldapsearchresults = ld.search(searchBase, ld.SCOPE_SUB,
0429: searchFilter, searchAttr, false);
0430: if (ldapsearchresults == null) {
0431: debugLogger.warning("PSJA_CSAL0034");
0432: ldapStore.LDAPClose(ld);
0433: throw new ABStoreException(
0434: "LdapAddressBook.fetch: ldapsearchresults should never null");
0435: }
0436: } catch (LDAPException ldapE) {
0437: debugLogger.log(Level.INFO, "PSJA_CSAL0035", ldapE);
0438:
0439: ldapStore.LDAPClose(ld);
0440: throw new ABStoreException(
0441: "LdapAddressBook.fetch: search for pab entries failed: "
0442: + ldapE);
0443: }
0444: if (ldapsearchresults != null) {
0445: debugLogger.log(Level.FINER, "PSJA_CSAL0036", Integer
0446: .toString(ldapsearchresults.getCount()));
0447: String sortBy = abFilter.getSortBy();
0448: // Sort the results depending upon user's input
0449: // The only common sort criteria can be cn
0450: debugLogger.log(Level.FINER, "PSJA_CSAL0037", sortBy);
0451: if ((sortBy != null) && (sortBy.trim().equals("cn"))) {
0452: // Set the sort direction
0453: boolean ascend = true;
0454: if (abFilter.getSortOrder() == ABFilter.ASCENDING) {
0455: ascend = true;
0456: } else if ((abFilter.getSortOrder() == ABFilter.DESCENDING)) {
0457: ascend = false;
0458: } else {
0459: ascend = true;
0460: }
0461: debugLogger.log(Level.FINER, "PSJA_CSAL0042", Boolean
0462: .toString(ascend));
0463: ldapsearchresults.sort(new LDAPCompareAttrNames(sortBy,
0464: ascend));
0465: } else {
0466: debugLogger.finer("PSJA_CSAL0043");
0467: }
0468:
0469: listElements = new ArrayList(ldapsearchresults.getCount());
0470: int index = 0;
0471: try {
0472: while (ldapsearchresults.hasMoreElements()) {
0473: LDAPEntry ldapentry = ldapsearchresults.next();
0474: printResults(ldapentry, searchAttr);
0475: // Find out whether the element is an entry or group
0476: LDAPAttribute tmp = ldapentry
0477: .getAttribute(LdapABConstants.ATTR_objectclass);
0478: String obj_class = tmp.getStringValueArray()[tmp
0479: .size() - 1];
0480:
0481: if (obj_class
0482: .equals(LdapABConstants.OBJECTCLASS_ENTRY)) {
0483: // The element is an entry
0484: listElements.add(toEntry(ldapentry, searchAttr,
0485: index));
0486: } else if (obj_class
0487: .equals(LdapABConstants.OBJECTCLASS_GROUP)) {
0488: // The element is a group
0489: // groups attrs are:
0490: String groupAttr[] = { "un",
0491: LdapABConstants.ATTR_cn,
0492: LdapABConstants.ATTR_description };
0493:
0494: listElements.add(toGroup(ldapentry, groupAttr,
0495: index));
0496: } else {
0497: // Unkwon element
0498: // ignore
0499: }
0500:
0501: index++;
0502: }
0503: } catch (LDAPException ldapE) {
0504: String msg = "LdapAddressBook.fetch: problem processing pab search results: "
0505: + ldapE;
0506: debugLogger.log(Level.INFO, "PSJA_CSAL0038", ldapE);
0507: ldapStore.LDAPClose(ld);
0508: throw new ABStoreException(msg);
0509: }
0510: }
0511:
0512: // Clean up and Close connection
0513: ldapsearchresults = null;
0514: ldapStore.LDAPClose(ld);
0515: debugLogger.log(Level.FINER, "PSJA_CSAL0044", Integer
0516: .toString(listElements.size()));
0517: return (Element[]) listElements
0518: .toArray(new Element[listElements.size()]);
0519: }
0520:
0521: /**
0522: * Add a new Element to the address book.
0523: *
0524: * @param element The new Element to be added including the unique
0525: * identifier.
0526: * @exception ABStoreException if unable to connect to back end service.
0527: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
0528: */
0529: public void add(Element element) throws ABStoreException,
0530: OperationNotSupportedException {
0531:
0532: if (element.getElementType() == Element.ENTRY) {
0533: // Add Address Book Entry
0534: addEntry((Entry) element);
0535: return;
0536: } else if (element.getElementType() == Element.GROUP) {
0537: // Add Address Book Group
0538: addGroup((Group) element);
0539: return;
0540: } else if (element.getElementType() == Element.ADDRESSBOOK) {
0541: // Add Address book
0542: // sub folders not supported yet ..
0543: String msg = "LdapAddressBook.add: Creating address books not supported";
0544: throw new OperationNotSupportedException(msg);
0545: } else {
0546: String msg = "LdapAddressBook.add: Invalid elementType";
0547: throw new ABStoreException(msg);
0548: }
0549: }
0550:
0551: /**
0552: * private add Entry method
0553: */
0554: protected void addEntry(Entry entry) throws ABStoreException,
0555: OperationNotSupportedException {
0556: String objClass[] = { "top", "person", "organizationalPerson",
0557: "inetOrgPerson", LdapABConstants.OBJECTCLASS_ENTRY };
0558:
0559: // Get connection to the Database
0560: LDAPConnection ld = ldapStore.LDAPConnect();
0561:
0562: if (ld == null) {
0563: String msg = "LdapAddressBook.connect:failed to get ldap connection from pool";
0564: debugLogger.info("PSJA_CSAL0009");
0565: throw new ABStoreException(msg);
0566: }
0567:
0568: LDAPAttributeSet entryAttr = new LDAPAttributeSet();
0569:
0570: entryAttr.add(new LDAPAttribute(
0571: LdapABConstants.ATTR_objectclass, objClass));
0572: entryAttr.add(new LDAPAttribute(
0573: LdapABConstants.ATTR_memberofpab, abID));
0574:
0575: if (entry.getFn() != null)
0576: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_fn,
0577: entry.getFn()));
0578:
0579: if (entry.getLn() != null)
0580: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_ln,
0581: entry.getLn()));
0582:
0583: if (entry.getCn() == null) {
0584: entry.setCn(entry.getFn() + " " + entry.getLn());
0585: }
0586: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_cn, entry
0587: .getCn()));
0588:
0589: if (entry.getEm() != null)
0590: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_em,
0591: entry.getEm()));
0592:
0593: if (entry.getDescription() != null)
0594: entryAttr.add(new LDAPAttribute(
0595: LdapABConstants.ATTR_description, entry
0596: .getDescription()));
0597:
0598: if (entry.getBp() != null)
0599: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_bp,
0600: entry.getBp()));
0601:
0602: if (entry.getHp() != null)
0603: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_hp,
0604: entry.getHp()));
0605:
0606: if (entry.getMp() != null)
0607: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_mp,
0608: entry.getMp()));
0609:
0610: if (entry.getHomeStreet() != null)
0611: entryAttr.add(new LDAPAttribute(
0612: LdapABConstants.ATTR_homeStreet, entry
0613: .getHomeStreet()));
0614:
0615: if (entry.getHomeCity() != null)
0616: entryAttr
0617: .add(new LDAPAttribute(
0618: LdapABConstants.ATTR_homeCity, entry
0619: .getHomeCity()));
0620: if (entry.getHomeState() != null)
0621: entryAttr.add(new LDAPAttribute(
0622: LdapABConstants.ATTR_homeState, entry
0623: .getHomeState()));
0624:
0625: if (entry.getHomeZip() != null)
0626: entryAttr.add(new LDAPAttribute(
0627: LdapABConstants.ATTR_homeZip, entry.getHomeZip()));
0628:
0629: if (entry.getHomeCountry() != null)
0630: entryAttr.add(new LDAPAttribute(
0631: LdapABConstants.ATTR_homeCountry, entry
0632: .getHomeCountry()));
0633:
0634: if (entry.getFp() != null)
0635: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_fp,
0636: entry.getFp()));
0637:
0638: if (entry.getPp() != null)
0639: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_pp,
0640: entry.getPp()));
0641:
0642: if (entry.getDob() != null)
0643: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_dob,
0644: entry.getDob()));
0645:
0646: if (entry.getUri() != null)
0647: entryAttr.add(new LDAPAttribute(LdapABConstants.ATTR_uri,
0648: entry.getUri()));
0649:
0650: if (entry.getMemberofpabgroup() != null)
0651: entryAttr.add(new LDAPAttribute(
0652: LdapABConstants.ATTR_memberofpabgroup, entry
0653: .getMemberofpabgroup()));
0654:
0655: String dn = "un=" + trimForIMS51(entry.getFn())
0656: + trimForIMS51(entry.getLn()) + pabTimeStamp() + ","
0657: + fullPabSearchBase;
0658:
0659: LDAPEntry ldapentry = new LDAPEntry(dn, entryAttr);
0660:
0661: // Add Entry
0662: try {
0663: ld.add(ldapentry);
0664: } catch (LDAPException ldapE) {
0665: String msg = "LdapAddressBook.add: failed to add pab entry - "
0666: + entry.getUn() + " : " + ldapE;
0667: if (debugLogger.isLoggable(Level.INFO)) {
0668: LogRecord logRecord = new LogRecord(Level.INFO,
0669: "PSJA_CSAL0045");
0670: logRecord.setParameters(new String[] { entry.getUn() });
0671: logRecord.setThrown(ldapE);
0672: logRecord.setLoggerName(debugLogger.getName());
0673: debugLogger.log(logRecord);
0674: }
0675: ldapStore.LDAPClose(ld);
0676: throw new ABStoreException(msg);
0677: }
0678:
0679: // Close connection
0680: ldapStore.LDAPClose(ld);
0681: }
0682:
0683: /**
0684: * Create a new group in the address book.
0685: *
0686: * @param group The new Group to be added.
0687: * @exception ABStoreException if unable to connect to back end service.
0688: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
0689: */
0690: protected void addGroup(Group group) throws ABStoreException,
0691: OperationNotSupportedException {
0692:
0693: String objClass[] = { "top", LdapABConstants.OBJECTCLASS_GROUP };
0694:
0695: // Get connection to the Database
0696: LDAPConnection ld = ldapStore.LDAPConnect();
0697:
0698: if (ld == null) {
0699: String msg = "LdapAddressBook.connect:failed to get ldap connection from pool";
0700: debugLogger.info("PSJA_CSAL0009");
0701: throw new ABStoreException(msg);
0702: }
0703:
0704: LDAPAttributeSet groupAttr = new LDAPAttributeSet();
0705:
0706: groupAttr.add(new LDAPAttribute(
0707: LdapABConstants.ATTR_objectclass, objClass));
0708: groupAttr.add(new LDAPAttribute(
0709: LdapABConstants.ATTR_memberofpab, abID));
0710:
0711: if (group.getCn() != null)
0712: groupAttr.add(new LDAPAttribute(LdapABConstants.ATTR_cn,
0713: group.getCn()));
0714:
0715: if (group.getDescription() != null)
0716: groupAttr.add(new LDAPAttribute(
0717: LdapABConstants.ATTR_description, group
0718: .getDescription()));
0719:
0720: String dn = "un=" + trimForIMS51(group.getCn())
0721: + pabTimeStamp() + "," + fullPabSearchBase;
0722:
0723: LDAPEntry ldapentry = new LDAPEntry(dn, groupAttr);
0724:
0725: // Add Group
0726: try {
0727: ld.add(ldapentry);
0728: } catch (LDAPException ldapE) {
0729: String msg = "LdapAddressBook.addGroup: failed to add pab group - "
0730: + group.getUn() + " :" + ldapE;
0731: if (debugLogger.isLoggable(Level.INFO)) {
0732: LogRecord logRecord = new LogRecord(Level.INFO,
0733: "PSJA_CSAL0046");
0734: logRecord.setParameters(new String[] { group.getUn() });
0735: logRecord.setThrown(ldapE);
0736: logRecord.setLoggerName(debugLogger.getName());
0737: debugLogger.log(logRecord);
0738: }
0739: ldapStore.LDAPClose(ld);
0740: throw new ABStoreException(msg);
0741: }
0742:
0743: // Close connection
0744: ldapStore.LDAPClose(ld);
0745: }
0746:
0747: /**
0748: * Modify an Element from the address book.
0749: *
0750: * @param oldElement The Element in the address book to be replaced.
0751: * @param newElement The new Element to replace the exisiting entry.
0752: * @exception ABStoreException if unable to connect to back end service.
0753: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
0754: */
0755: public void modify(Element oldElement, Element newElement)
0756: throws ABStoreException, OperationNotSupportedException {
0757:
0758: if (oldElement.getElementType() == Element.ENTRY) {
0759: // Modify Address Book Entry
0760: modifyEntry((Entry) oldElement, (Entry) newElement);
0761: return;
0762: } else if (oldElement.getElementType() == Element.GROUP) {
0763: // Modify Address Book Group
0764: modifyGroup((Group) oldElement, (Group) newElement);
0765: return;
0766: } else if (oldElement.getElementType() == Element.ADDRESSBOOK) {
0767: // Mpdify Address book
0768: // sub folders not supported yet ..
0769: String msg = "LdapAddressBook.add: Modifying address books not supported";
0770: throw new OperationNotSupportedException(msg);
0771: } else {
0772: String msg = "LdapAddressBook.add: Invalid elementType";
0773: throw new ABStoreException(msg);
0774: }
0775: }
0776:
0777: /**
0778: * Private Modify an Entry
0779: */
0780: protected void modifyEntry(Entry oldEntry, Entry newEntry)
0781: throws ABStoreException, OperationNotSupportedException {
0782: // Get connection to the Database
0783: LDAPConnection ld = ldapStore.LDAPConnect();
0784: if (ld == null) {
0785: String msg = "LdapAddressBook.connect:failed to get ldap connection from pool";
0786: debugLogger.info("PSJA_CSAL0009");
0787: throw new ABStoreException(msg);
0788: }
0789:
0790: LDAPModificationSet mods = new LDAPModificationSet();
0791:
0792: if (newEntry.getFn() != null)
0793: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0794: LdapABConstants.ATTR_fn, newEntry.getFn()));
0795:
0796: if (newEntry.getLn() != null)
0797: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0798: LdapABConstants.ATTR_ln, newEntry.getLn()));
0799:
0800: if (newEntry.getCn() == null) {
0801: String fn = newEntry.getFn();
0802: String ln = newEntry.getLn();
0803: if (ln == null)
0804: ln = "";
0805: if (fn == null)
0806: fn = "";
0807: newEntry.setCn(fn + " " + ln);
0808: }
0809: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0810: LdapABConstants.ATTR_cn, newEntry.getCn()));
0811:
0812: if (newEntry.getEm() != null)
0813: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0814: LdapABConstants.ATTR_em, newEntry.getEm()));
0815:
0816: if (newEntry.getDescription() != null)
0817: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0818: LdapABConstants.ATTR_description, newEntry
0819: .getDescription()));
0820:
0821: if (newEntry.getBp() != null)
0822: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0823: LdapABConstants.ATTR_bp, newEntry.getBp()));
0824:
0825: if (newEntry.getHp() != null)
0826: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0827: LdapABConstants.ATTR_hp, newEntry.getHp()));
0828:
0829: if (newEntry.getMp() != null)
0830: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0831: LdapABConstants.ATTR_mp, newEntry.getMp()));
0832:
0833: if (newEntry.getHomeStreet() != null)
0834: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0835: LdapABConstants.ATTR_homeStreet, newEntry
0836: .getHomeStreet()));
0837:
0838: if (newEntry.getHomeCity() != null)
0839: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0840: LdapABConstants.ATTR_homeCity, newEntry
0841: .getHomeCity()));
0842:
0843: if (newEntry.getHomeState() != null)
0844: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0845: LdapABConstants.ATTR_homeState, newEntry
0846: .getHomeState()));
0847:
0848: if (newEntry.getHomeZip() != null)
0849: mods.add(LDAPModification.REPLACE,
0850: new LDAPAttribute(LdapABConstants.ATTR_homeZip,
0851: newEntry.getHomeZip()));
0852:
0853: if (newEntry.getHomeCountry() != null)
0854: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0855: LdapABConstants.ATTR_homeCountry, newEntry
0856: .getHomeCountry()));
0857:
0858: if (newEntry.getFp() != null)
0859: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0860: LdapABConstants.ATTR_fp, newEntry.getFp()));
0861:
0862: if (newEntry.getPp() != null)
0863: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0864: LdapABConstants.ATTR_pp, newEntry.getPp()));
0865:
0866: if (newEntry.getDob() != null)
0867: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0868: LdapABConstants.ATTR_dob, newEntry.getDob()));
0869:
0870: if (newEntry.getUri() != null)
0871: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0872: LdapABConstants.ATTR_uri, newEntry.getUri()));
0873:
0874: if (newEntry.getMemberofpabgroup() != null)
0875: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0876: LdapABConstants.ATTR_memberofpabgroup, newEntry
0877: .getMemberofpabgroup()));
0878:
0879: String dn = "un=" + oldEntry.getUn() + "," + fullPabSearchBase;
0880:
0881: // Modify entry
0882: try {
0883: ld.modify(dn, mods);
0884: } catch (LDAPException ldapE) {
0885: String msg = "LdapAddressBook.modify: failed to modify pab entry - "
0886: + oldEntry.getUn() + " :" + ldapE;
0887: if (debugLogger.isLoggable(Level.INFO)) {
0888: LogRecord logRecord = new LogRecord(Level.INFO,
0889: "PSJA_CSAL0047");
0890: logRecord
0891: .setParameters(new String[] { oldEntry.getUn() });
0892: logRecord.setThrown(ldapE);
0893: logRecord.setLoggerName(debugLogger.getName());
0894: debugLogger.log(logRecord);
0895: }
0896: ldapStore.LDAPClose(ld);
0897: throw new ABStoreException(msg);
0898: }
0899:
0900: // Close connection
0901: ldapStore.LDAPClose(ld);
0902: }
0903:
0904: /**
0905: * Modify a group in the address book.
0906: *
0907: * @param oldGroup The Group to be changed.
0908: * @param newGroup The new Group name.
0909: * @exception ABStoreException if unable to connect to back end service.
0910: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
0911: */
0912: public void modifyGroup(Group oldGroup, Group newGroup)
0913: throws ABStoreException, OperationNotSupportedException {
0914: // Get connection to the Database
0915: LDAPConnection ld = ldapStore.LDAPConnect();
0916: if (ld == null) {
0917: String msg = "LdapAddressBook.connect:failed to get ldap connection from pool";
0918: debugLogger.info("PSJA_CSAL0009");
0919: throw new ABStoreException(msg);
0920: }
0921:
0922: LDAPModificationSet mods = new LDAPModificationSet();
0923:
0924: if (newGroup.getCn() != null)
0925: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0926: LdapABConstants.ATTR_cn, newGroup.getCn()));
0927:
0928: if (newGroup.getDescription() != null)
0929: mods.add(LDAPModification.REPLACE, new LDAPAttribute(
0930: LdapABConstants.ATTR_description, newGroup
0931: .getDescription()));
0932:
0933: String dn = "un=" + oldGroup.getUn() + "," + fullPabSearchBase;
0934:
0935: // Modify Group
0936: try {
0937: ld.modify(dn, mods);
0938: } catch (LDAPException ldapE) {
0939: String msg = "LdapAddressBook.renameGroup: failed to modify pab group - "
0940: + oldGroup.getUn() + " :" + ldapE;
0941: if (debugLogger.isLoggable(Level.INFO)) {
0942: LogRecord logRecord = new LogRecord(Level.INFO,
0943: "PSJA_CSAL0048");
0944: logRecord
0945: .setParameters(new String[] { oldGroup.getUn() });
0946: logRecord.setThrown(ldapE);
0947: logRecord.setLoggerName(debugLogger.getName());
0948: debugLogger.log(logRecord);
0949: }
0950: ldapStore.LDAPClose(ld);
0951: throw new ABStoreException(msg);
0952: }
0953:
0954: // Close connection
0955: ldapStore.LDAPClose(ld);
0956: }
0957:
0958: /**
0959: * Delete an Element from the address book.
0960: *
0961: * @param element The Element to be deleted.
0962: * @exception ABStoreException if unable to connect to back end service.
0963: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
0964: */
0965: public void delete(Element element) throws ABStoreException,
0966: OperationNotSupportedException {
0967:
0968: if (element.getElementType() == Element.ENTRY) {
0969: // Delete Address Book Entry
0970: deleteEntry((Entry) element);
0971: return;
0972: } else if (element.getElementType() == Element.GROUP) {
0973: // Delete Address Book Group
0974: deleteGroup((Group) element);
0975: return;
0976: } else if (element.getElementType() == Element.ADDRESSBOOK) {
0977: // Delete Address book
0978: // sub folders not supported yet ..
0979: String msg = "LdapAddressBook.add: Deleting address books not supported";
0980: throw new OperationNotSupportedException(msg);
0981: } else {
0982: String msg = "LdapAddressBook.add: Invalid elementType";
0983: throw new ABStoreException(msg);
0984: }
0985: }
0986:
0987: /**
0988: * Private Delete Entry
0989: */
0990: protected void deleteEntry(Entry entry) throws ABStoreException,
0991: OperationNotSupportedException {
0992: // Get connection to the Database
0993: LDAPConnection ld = ldapStore.LDAPConnect();
0994:
0995: if (ld == null) {
0996: String msg = "LdapAddressBook.connect:failed to get ldap connection from pool";
0997: debugLogger.info("PSJA_CSAL0009");
0998: throw new ABStoreException(msg);
0999: }
1000: String entryUNtoDel = entry.getUn();
1001: // Delete Entry
1002: try {
1003: String dn = "un=" + entryUNtoDel + "," + fullPabSearchBase;
1004:
1005: ld.delete(dn);
1006: } catch (LDAPException ldapE) {
1007: String msg = "LdapAddressBook.delete: failed to delete Entry - "
1008: + entry.getUn() + " : " + ldapE;
1009: if (debugLogger.isLoggable(Level.INFO)) {
1010: LogRecord logRecord = new LogRecord(Level.INFO,
1011: "PSJA_CSAL0049");
1012: logRecord.setParameters(new String[] { entry.getUn() });
1013: logRecord.setThrown(ldapE);
1014: logRecord.setLoggerName(debugLogger.getName());
1015: debugLogger.log(logRecord);
1016: }
1017: ldapStore.LDAPClose(ld);
1018: throw new ABStoreException(msg);
1019: }
1020:
1021: // Close Connection
1022: ldapStore.LDAPClose(ld);
1023:
1024: }
1025:
1026: /**
1027: * Delete an existing group in the address book.
1028: *
1029: * @param group The Group to be deleted.
1030: * @exception ABStoreException if unable to connect to back end service.
1031: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
1032: */
1033: protected void deleteGroup(Group group) throws ABStoreException,
1034: OperationNotSupportedException {
1035:
1036: // Get connection to the Database
1037: LDAPConnection ld = ldapStore.LDAPConnect();
1038:
1039: if (ld == null) {
1040: String msg = "LdapAddressBook.connect:failed to get ldap connection from pool";
1041: debugLogger.info("PSJA_CSAL0009");
1042: throw new ABStoreException(msg);
1043: }
1044: String groupUNtoDel = group.getUn();
1045: // Delete Group
1046: try {
1047: String dn = "un=" + groupUNtoDel + "," + fullPabSearchBase;
1048: ld.delete(dn);
1049: } catch (LDAPException ldapE) {
1050: String msg = "LdapAddressBook.deleteGroup: failed to delete Group - "
1051: + group.getUn() + " :" + ldapE;
1052: if (debugLogger.isLoggable(Level.INFO)) {
1053: LogRecord logRecord = new LogRecord(Level.INFO,
1054: "PSJA_CSAL0050");
1055: logRecord.setParameters(new String[] { group.getUn() });
1056: logRecord.setThrown(ldapE);
1057: logRecord.setLoggerName(debugLogger.getName());
1058: debugLogger.log(logRecord);
1059: }
1060: ldapStore.LDAPClose(ld);
1061: throw new ABStoreException(msg);
1062: }
1063:
1064: // Close Connection
1065: ldapStore.LDAPClose(ld);
1066:
1067: }
1068:
1069: /**
1070: * Return the ABSearchTerm object corresponding to the service type.
1071: *
1072: * @return ABSearchTerm object corresponding to the service type.
1073: */
1074: public ABSearchTerm newABSearchTerm(String name, String value,
1075: boolean exact) {
1076: return new LdapABSearchTerm(name, value, exact);
1077: }
1078:
1079: /**
1080: * Return the ABSearchTerm object corresponding to the service type.
1081: *
1082: * @return ABSearchTerm object corresponding to the service type.
1083: */
1084: public ABSearchTerm newABSearchTerm(ABSearchTerm term, int op)
1085: throws ABStoreException {
1086: return new LdapABSearchTerm(term, op);
1087: }
1088:
1089: /**
1090: * Return the ABSearchTerm object corresponding to the service type.
1091: *
1092: * @return ABSearchTerm object corresponding to the service type.
1093: */
1094: public ABSearchTerm newABSearchTerm(ABSearchTerm[] terms, int op)
1095: throws ABStoreException {
1096: return new LdapABSearchTerm(terms, op);
1097: }
1098:
1099: /**
1100: * Fetch the address book Elements in the specified group
1101: *
1102: * @return String array of Elements in the address book.
1103: * @exception ABStoreException if unable to connect to back end service.
1104: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
1105: */
1106: public Element[] fetchGroupMembers(ABFilter filter, Group group)
1107: throws ABStoreException, OperationNotSupportedException {
1108: filter.setGroup(group);
1109: Element[] elements = fetch(filter);
1110: filter.setGroup(null);
1111: return elements;
1112: }
1113:
1114: /**
1115: * Add an entry to an existing group in the address book.
1116: *
1117: * @param Element The Element to be added.
1118: * @param Group The group to which the entry is to be added.
1119: * @exception ABStoreException if unable to connect to back end service.
1120: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
1121: */
1122: public void addGroupMember(Element element, Group group)
1123: throws ABStoreException, OperationNotSupportedException {
1124: if (group.getUn() == null) {
1125: throw new ABStoreException(
1126: "LdapAddressBook.addGroupMember: Address Book Group not defined");
1127: }
1128: // Get connection to the Database
1129: LDAPConnection ld = ldapStore.LDAPConnect();
1130: if (ld == null) {
1131: String msg = "LdapAddressBook.connect:failed to get ldap connection from pool";
1132: debugLogger.info("PSJA_CSAL0009");
1133: throw new ABStoreException(msg);
1134: }
1135:
1136: if (element.getElementType() == Element.ENTRY) {
1137: LDAPModificationSet mods = new LDAPModificationSet();
1138: mods.add(LDAPModification.ADD, new LDAPAttribute(
1139: LdapABConstants.ATTR_memberofpabgroup, group
1140: .getUn()));
1141:
1142: String dn = "un=" + element.getUn() + ","
1143: + fullPabSearchBase;
1144:
1145: // Modify Group
1146: try {
1147: ld.modify(dn, mods);
1148: } catch (LDAPException ldapE) {
1149: String msg = "LdapAddressBook.addGroupMember: failed to add address book element to group - "
1150: + group.getUn() + " :" + ldapE;
1151: if (debugLogger.isLoggable(Level.INFO)) {
1152: LogRecord logRecord = new LogRecord(Level.INFO,
1153: "PSJA_CSAL0051");
1154: logRecord.setParameters(new String[] { group
1155: .getUn() });
1156: logRecord.setThrown(ldapE);
1157: logRecord.setLoggerName(debugLogger.getName());
1158: debugLogger.log(logRecord);
1159: }
1160: ldapStore.LDAPClose(ld);
1161: throw new ABStoreException(msg);
1162: }
1163: } else if (element.getElementType() == Element.GROUP) {
1164: // Ldap schema for iMS doesnt support gruops inside groups
1165: // So expand the group to be added and add all the entries to
1166: // the group
1167: Group subGroup = (Group) element;
1168: ABFilter filter = new ABFilter();
1169: ABSearchTerm term = newABSearchTerm("any", "*", true);
1170: filter.setSearchTerm(term);
1171: filter.setElementType(Element.ALL);
1172: Element[] elms = fetchGroupMembers(filter, subGroup);
1173: for (int i = 0; i < elms.length; i++) {
1174: LDAPModificationSet mods = new LDAPModificationSet();
1175: mods.add(LDAPModification.ADD, new LDAPAttribute(
1176: LdapABConstants.ATTR_memberofpabgroup, group
1177: .getUn()));
1178:
1179: String dn = "un=" + elms[i].getUn() + ","
1180: + fullPabSearchBase;
1181:
1182: // Modify Group
1183: try {
1184: ld.modify(dn, mods);
1185: } catch (LDAPException ldapE) {
1186: String msg = "LdapAddressBook.addGroupMember: failed to add address book element to group - "
1187: + group.getUn() + " :" + ldapE;
1188: if (debugLogger.isLoggable(Level.INFO)) {
1189: LogRecord logRecord = new LogRecord(Level.INFO,
1190: "PSJA_CSAL0051");
1191: logRecord.setParameters(new String[] { group
1192: .getUn() });
1193: logRecord.setThrown(ldapE);
1194: logRecord.setLoggerName(debugLogger.getName());
1195: debugLogger.log(logRecord);
1196: }
1197: ldapStore.LDAPClose(ld);
1198: throw new ABStoreException(msg);
1199: }
1200: }
1201: }
1202:
1203: // Close connection
1204: ldapStore.LDAPClose(ld);
1205: }
1206:
1207: /**
1208: * Delete an Element from a group in the address book.
1209: *
1210: * @param Element The Element to be deleted.
1211: * @param Group The group to which the entry is to be deleted.
1212: * @exception ABStoreException if unable to connect to back end service.
1213: * @exception OperationNotSupportedException if the adapter doesnt support the functionality
1214: */
1215: public void deleteGroupMember(Element element, Group group)
1216: throws ABStoreException, OperationNotSupportedException {
1217:
1218: if (group.getUn() == null) {
1219: throw new ABStoreException(
1220: "LdapAddressBook.deleteGroupMember: Address Book Group not defined");
1221: }
1222: // Get connection to the Database
1223: LDAPConnection ld = ldapStore.LDAPConnect();
1224: if (ld == null) {
1225: String msg = "LdapAddressBook.connect:failed to get ldap connection from pool";
1226: debugLogger.info("PSJA_CSAL0009");
1227: throw new ABStoreException(msg);
1228: }
1229:
1230: LDAPModificationSet mods = new LDAPModificationSet();
1231:
1232: mods.add(LDAPModification.DELETE, new LDAPAttribute(
1233: LdapABConstants.ATTR_memberofpabgroup, group.getUn()));
1234:
1235: String dn = "un=" + element.getUn() + "," + fullPabSearchBase;
1236:
1237: // Modify group
1238: try {
1239: ld.modify(dn, mods);
1240: } catch (LDAPException ldapE) {
1241: String msg = "LdapAddressBook.deleteGroupMember: failed to delete address book element from group - "
1242: + group.getUn() + " :" + ldapE;
1243: if (debugLogger.isLoggable(Level.INFO)) {
1244: LogRecord logRecord = new LogRecord(Level.INFO,
1245: "PSJA_CSAL0051");
1246: logRecord.setParameters(new String[] { group.getUn() });
1247: logRecord.setThrown(ldapE);
1248: logRecord.setLoggerName(debugLogger.getName());
1249: debugLogger.log(logRecord);
1250: }
1251: ldapStore.LDAPClose(ld);
1252: throw new ABStoreException(msg);
1253: }
1254:
1255: // Close connection
1256: ldapStore.LDAPClose(ld);
1257: }
1258:
1259: /*
1260: * pabTimeStamp emulates the behaviour of WebMail in generating the unique id.
1261: *
1262: * @return timeStamp String - returns the current time to
1263: * achieve uniqueness while adding entries.
1264: */
1265:
1266: private synchronized String pabTimeStamp() {
1267: long current = System.currentTimeMillis() / 1000 - 915170400;
1268: if (current > lastPabTimeStamp) {
1269: lastPabTimeStamp = current;
1270: } else {
1271: lastPabTimeStamp++;
1272: }
1273: return Long.toString(lastPabTimeStamp);
1274: }
1275:
1276: /**
1277: * Trim off the whitespaces and special characters from the string.
1278: * Used to generate the UN from PAB in iMS5.1, so Messenger Express
1279: * would still work.
1280: *
1281: * @param String String to be trimmed.
1282: * @return String Trimmed String .
1283: */
1284:
1285: private String trimForIMS51(String str) {
1286: StringBuffer trimmedStr = new StringBuffer();
1287: for (int i = 0; i < str.length(); i++) {
1288: char c = str.charAt(i);
1289: // Only include English digits and letters in UN
1290: if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
1291: || (c >= '0' && c <= '9')) {
1292: trimmedStr.append(c);
1293: }
1294: }
1295: return trimmedStr.toString();
1296: }
1297:
1298: /**
1299: * Converts the LDAPEntry object to Entry object.
1300: *
1301: * @param ldapentry LDAPEntry to convert to Entry bean.
1302: * @param attrs searchAttributes used to obtain the result.
1303: * @param index index of this entry in the resultset.
1304: *
1305: * @return entry corrsponding Entry object.
1306: */
1307: private Entry toEntry(LDAPEntry ldapentry, String attrs[], int index) {
1308: Entry entry = null;
1309: String[] strEntry = new String[attrs.length];
1310:
1311: for (int i = 0; i < attrs.length; i++) {
1312: // Get the attribute which may have lang tag to maintain
1313: // backward compatibility with iMS5.0
1314: LDAPAttribute ldapattribute = ldapentry.getAttribute(
1315: attrs[i], "lang-" + pabLang);
1316: if (ldapattribute != null) {
1317: Enumeration enumeration = ldapattribute
1318: .getStringValues();
1319: boolean flag;
1320: for (flag = false; enumeration != null
1321: && enumeration.hasMoreElements(); flag = true) {
1322: strEntry[i] = (String) enumeration.nextElement();
1323: }
1324:
1325: if (!flag) {
1326: strEntry[i] = "";
1327: }
1328: } else {
1329: strEntry[i] = "";
1330: }
1331: }
1332: entry = new Entry(strEntry[0], strEntry[1], strEntry[2],
1333: strEntry[3], strEntry[4], strEntry[5], strEntry[6],
1334: strEntry[7], strEntry[8], strEntry[9], strEntry[10],
1335: strEntry[11], strEntry[12], strEntry[13], null, null,
1336: null, null,
1337: null, // business address
1338: strEntry[14], strEntry[15], strEntry[16], strEntry[17],
1339: strEntry[18], strEntry[19], "", // this field is for smsid
1340: Integer.toString(index));
1341: return entry;
1342: }
1343:
1344: /**
1345: * Converts the LDAPEntry object to Group object.
1346: *
1347: * @param ldapentry LDAPEntry to convert to Group bean.
1348: * @param attrs searchAttributes used to obtain the result.
1349: * @param index index of this entry in the resultset.
1350: *
1351: * @return group corrsponding Group object.
1352: */
1353: private Group toGroup(LDAPEntry ldapentry, String attrs[], int index) {
1354: Group group = null;
1355: String[] strGroup = new String[attrs.length];
1356:
1357: for (int i = 0; i < attrs.length; i++) {
1358: // Get the attribute which may have lang tag to maintain
1359: // backward compatibility with iMS5.0
1360: LDAPAttribute ldapattribute = ldapentry.getAttribute(
1361: attrs[i], "lang-" + pabLang);
1362: if (ldapattribute != null) {
1363: Enumeration enumeration = ldapattribute
1364: .getStringValues();
1365: boolean flag;
1366: for (flag = false; enumeration != null
1367: && enumeration.hasMoreElements(); flag = true) {
1368: strGroup[i] = (String) enumeration.nextElement();
1369: }
1370:
1371: if (!flag) {
1372: strGroup[i] = "";
1373: }
1374: } else {
1375: strGroup[i] = "";
1376: }
1377: }
1378: group = new Group(strGroup[0], strGroup[1], strGroup[2],
1379: Integer.toString(index));
1380: return group;
1381: }
1382:
1383: /**
1384: * Debug method .. for printing out the resultset.
1385: *
1386: */
1387: private void printResults(LDAPEntry ldapentry, String as[]) {
1388: StringBuffer content = new StringBuffer();
1389: content
1390: .append("==================================================================\n");
1391: content.append("Length of attributes is " + as.length + "\n");
1392: for (int i = 0; i < as.length; i++) {
1393: LDAPAttribute ldapattribute = ldapentry.getAttribute(as[i],
1394: "lang-" + pabLang);
1395: if (ldapattribute != null) {
1396: Enumeration enumeration = ldapattribute
1397: .getStringValues();
1398: boolean flag;
1399: for (flag = false; enumeration != null
1400: && enumeration.hasMoreElements(); flag = true) {
1401: String s = (String) enumeration.nextElement();
1402: content.append(as[i] + " : " + s + "\n");
1403: }
1404:
1405: if (!flag) {
1406: content.append(as[i] + " has no value\n");
1407: }
1408: content.append("----------------\n");
1409: } else {
1410: content.append(as[i] + " not present!!! \n");
1411: }
1412: }
1413:
1414: content.append("DN: " + ldapentry.getDN() + "\n");
1415: content
1416: .append("==================================================================\n");
1417: debugLogger.log(Level.FINER, "PSJA_CSAL0052", content
1418: .toString());
1419: }
1420:
1421: }
|