Search in sources :

Example 1 with AMSearchResults

use of com.iplanet.am.sdk.AMSearchResults in project OpenAM by OpenRock.

the class RemoteServicesImpl method search.

/**
     * Search the Directory
     * 
     * @param token
     *            SSOToken
     * @param entryDN
     *            DN of the entry to start the search with
     * @param searchFilter
     *            search filter
     * @param searchControl
     *            search control defining the VLV indexes and search scope
     * @param attrNames
     *            attributes name
     * @return Set of matching DNs
     */
public AMSearchResults search(SSOToken token, String entryDN, String searchFilter, SearchControl searchControl, String[] attrNames) throws AMException {
    try {
        SortKey[] keys = searchControl.getSortKeys();
        LinkedList sortKeys = new LinkedList();
        for (int i = 0; (keys != null) && (i < keys.length); i++) {
            if (keys[i].reverse) {
                sortKeys.add("true:" + keys[i].attributeName);
            } else {
                // Using "fals" instead of "false" so that it
                // has 4 characters as "true", hence easy to
                // reconstruct SortKey
                sortKeys.add("fals:" + keys[i].attributeName);
            }
        }
        int[] vlvRange = searchControl.getVLVRange();
        if (vlvRange == null) {
            vlvRange = new int[3];
        }
        Set attrNamesSet = MiscUtils.stringArrayToSet(attrNames);
        String tokenID = token.getTokenID().toString();
        Object[] objs = { tokenID, entryDN, searchFilter, sortKeys, new Integer(vlvRange[0]), new Integer(vlvRange[1]), new Integer(vlvRange[2]), searchControl.getVLVJumpTo(), new Integer(searchControl.getTimeOut()), new Integer(searchControl.getMaxResults()), new Integer(searchControl.getSearchScope()), Boolean.valueOf(searchControl.isGetAllReturnAttributesEnabled()), attrNamesSet };
        Map results = (Map) client.send(client.encodeMessage("search3", objs), sessionCookies.getLBCookie(tokenID), null);
        String cString = (String) results.remove(AMSR_COUNT);
        Set dns = (Set) results.remove(AMSR_RESULTS);
        String eString = (String) results.remove(AMSR_CODE);
        int count = 0, errorCode = 0;
        try {
            count = Integer.parseInt(cString);
            errorCode = Integer.parseInt(eString);
        } catch (NumberFormatException nfe) {
            getDebug().error("RemoteServicesImpl.search: caught number " + "format error", nfe);
        }
        return (new AMSearchResults(count, dns, errorCode, results));
    } catch (AMRemoteException amrex) {
        if (getDebug().messageEnabled()) {
            getDebug().message("RemoteServicesImpl.search2 : entryDN" + entryDN + ";  AMRemoteException caught exception=", amrex);
        }
        throw convertException(amrex);
    } catch (RemoteException rex) {
        getDebug().error("RemoteServicesImpl.search: caught exception=", rex);
        throw new AMException(AMSDKBundle.getString("1000"), "1000");
    } catch (Exception ex) {
        if (getDebug().messageEnabled()) {
            getDebug().message("RemoteServicesImpl.search2 : entryDN=" + entryDN + ";  caught exception=", ex);
        }
        throw new AMException(AMSDKBundle.getString("1000"), "1000");
    }
}
Also used : Set(java.util.Set) AMException(com.iplanet.am.sdk.AMException) SortKey(com.iplanet.ums.SortKey) AMSearchResults(com.iplanet.am.sdk.AMSearchResults) LinkedList(java.util.LinkedList) AMEntryExistsException(com.iplanet.am.sdk.AMEntryExistsException) AMEventManagerException(com.iplanet.am.sdk.AMEventManagerException) RemoteException(java.rmi.RemoteException) AMException(com.iplanet.am.sdk.AMException) SSOException(com.iplanet.sso.SSOException) RemoteException(java.rmi.RemoteException) AMHashMap(com.iplanet.am.sdk.AMHashMap) Map(java.util.Map)

Example 2 with AMSearchResults

use of com.iplanet.am.sdk.AMSearchResults in project OpenAM by OpenRock.

the class DirectoryServicesImpl method search.

// RENAME from searchUsingSearchControl => search()
/**
     * Search the Directory
     * 
     * @param token
     *            SSOToken
     * @param entryDN
     *            DN of the entry to start the search with
     * @param searchFilter
     *            search filter
     * @param searchControl
     *            search control defining the VLV indexes and search scope
     * @param attrNames
     *            name of attributes
     * @return Set set of matching DNs
     */
public AMSearchResults search(SSOToken token, String entryDN, String searchFilter, SearchControl searchControl, String[] attrNames) throws AMException {
    AMSearchResults amResults = null;
    try {
        SortKey[] skeys = searchControl.getSortKeys();
        SortKey skey = null;
        if (skeys != null && skeys.length > 0 && skeys[0].attributeName != null) {
            skey = skeys[0];
        }
        String userLocale = CommonUtils.getUserLocale(token);
        if (debug.messageEnabled()) {
            debug.message("DirectoryServicesImpl.search() search with " + "searchcontrol locale = " + userLocale);
        }
        Collator collator = Collator.getInstance(Locale.getLocale(userLocale));
        SearchControl sc;
        if (skey != null) {
            sc = new SearchControl();
            sc.setMaxResults(searchControl.getMaxResults());
            sc.setSearchScope(searchControl.getSearchScope());
            sc.setTimeOut(searchControl.getTimeOut());
        } else {
            sc = searchControl;
        }
        PersistentObject po = UMSObject.getObjectHandle(token, new Guid(entryDN));
        SearchResults results;
        if (attrNames == null) {
            if (skey == null) {
                results = po.search(searchFilter, sc);
            } else {
                String[] tmpAttrNames = { skey.attributeName };
                results = po.search(searchFilter, tmpAttrNames, sc);
            }
        } else {
            if (skey == null) {
                results = po.search(searchFilter, attrNames, sc);
            } else {
                String[] tmpAttrNames = new String[attrNames.length + 1];
                System.arraycopy(attrNames, 0, tmpAttrNames, 0, attrNames.length);
                tmpAttrNames[attrNames.length] = skey.attributeName;
                results = po.search(searchFilter, tmpAttrNames, sc);
            }
        }
        amResults = getSearchResults(results, skey, attrNames, collator, sc.isGetAllReturnAttributesEnabled());
    } catch (UMSException ue) {
        if (debug.warningEnabled()) {
            debug.warning("DirectoryServicesImpl.search() with search " + "control entryDN: " + entryDN + " Search Filter: " + searchFilter + " Error occurred: ", ue);
        }
        processInternalException(token, ue, "341");
    }
    return amResults;
}
Also used : UMSException(com.iplanet.ums.UMSException) PersistentObject(com.iplanet.ums.PersistentObject) SortKey(com.iplanet.ums.SortKey) SearchControl(com.iplanet.ums.SearchControl) Guid(com.iplanet.ums.Guid) AMSearchResults(com.iplanet.am.sdk.AMSearchResults) AMSearchResults(com.iplanet.am.sdk.AMSearchResults) SearchResults(com.iplanet.ums.SearchResults) Collator(java.text.Collator)

Example 3 with AMSearchResults

use of com.iplanet.am.sdk.AMSearchResults in project OpenAM by OpenRock.

the class DirectoryServicesImpl method getSearchResults.

/**
     * convert search results to a AMSearchResults object TODO: Refactor code
     */
private AMSearchResults getSearchResults(SearchResults results, SortKey skey, String[] attrNames, Collator collator, boolean getAllAttrs) throws UMSException {
    TreeMap tm = null;
    TreeSet tmpTreeSet = null;
    if (skey != null) {
        tm = new TreeMap(collator);
        tmpTreeSet = new TreeSet();
    }
    Set set = new OrderedSet();
    Map map = new HashMap();
    int errorCode = AMSearchResults.SUCCESS;
    try {
        if (results != null) {
            while (results.hasMoreElements()) {
                PersistentObject po = results.next();
                String dn = po.getGuid().toString();
                if (tm != null) {
                    Attr attr = po.getAttribute(skey.attributeName);
                    if (attr != null) {
                        String attrValue = attr.getStringValues()[0];
                        Object obj = tm.get(attrValue);
                        if (obj == null) {
                            tm.put(attrValue, dn);
                        } else if (obj instanceof java.lang.String) {
                            TreeSet tmpSet = new TreeSet();
                            tmpSet.add(obj);
                            tmpSet.add(dn);
                            tm.put(attrValue, tmpSet);
                        } else {
                            ((TreeSet) obj).add(dn);
                        }
                    } else {
                        tmpTreeSet.add(dn);
                    }
                } else {
                    set.add(dn);
                }
                AttrSet attrSet = new AttrSet();
                if (attrNames != null) {
                    // Support for multiple return values
                    attrSet = po.getAttributes(attrNames, true);
                } else {
                    /*
                         * Support for multiple return values when attribute
                         * names are not passed as part of the return
                         * attributes. This boolean check is to make sure user
                         * has set the setAllReturnAttributes flag in
                         * AMSearchControl in order to get all attributes or
                         * not.
                         */
                    if (getAllAttrs) {
                        attrSet = po.getAttributes(po.getAttributeNames(), true);
                    }
                }
                map.put(dn, CommonUtils.attrSetToMap(attrSet));
            }
        }
    } catch (SizeLimitExceededException slee) {
        errorCode = AMSearchResults.SIZE_LIMIT_EXCEEDED;
    } catch (TimeLimitExceededException tlee) {
        errorCode = AMSearchResults.TIME_LIMIT_EXCEEDED;
    }
    Integer count = (Integer) results.get(SearchResults.VLVRESPONSE_CONTENT_COUNT);
    int countValue;
    if (count == null) {
        countValue = AMSearchResults.UNDEFINED_RESULT_COUNT;
    } else {
        countValue = count.intValue();
    }
    if (tm != null) {
        Object[] values = tm.values().toArray();
        int len = values.length;
        if (skey.reverse) {
            for (int i = len - 1; i >= 0; i--) {
                Object obj = values[i];
                if (obj instanceof java.lang.String) {
                    set.add(obj);
                } else {
                    set.addAll((Collection) obj);
                }
            }
        } else {
            for (int i = 0; i < len; i++) {
                Object obj = values[i];
                if (obj instanceof java.lang.String) {
                    set.add(obj);
                } else {
                    set.addAll((Collection) obj);
                }
            }
        }
        Iterator iter = tmpTreeSet.iterator();
        while (iter.hasNext()) {
            set.add(iter.next());
        }
    }
    AMSearchResults searchResults = new AMSearchResults(countValue, set, errorCode, map);
    return searchResults;
}
Also used : OrderedSet(com.sun.identity.shared.datastruct.OrderedSet) Set(java.util.Set) OrderedSet(com.sun.identity.shared.datastruct.OrderedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) AttrSet(com.iplanet.services.ldap.AttrSet) AMHashMap(com.iplanet.am.sdk.AMHashMap) HashMap(java.util.HashMap) PersistentObject(com.iplanet.ums.PersistentObject) TreeMap(java.util.TreeMap) AMSearchResults(com.iplanet.am.sdk.AMSearchResults) Attr(com.iplanet.services.ldap.Attr) AttrSet(com.iplanet.services.ldap.AttrSet) SizeLimitExceededException(com.iplanet.ums.SizeLimitExceededException) TreeSet(java.util.TreeSet) Iterator(java.util.Iterator) AMObject(com.iplanet.am.sdk.AMObject) UMSObject(com.iplanet.ums.UMSObject) PersistentObject(com.iplanet.ums.PersistentObject) TimeLimitExceededException(com.iplanet.ums.TimeLimitExceededException) Map(java.util.Map) AMHashMap(com.iplanet.am.sdk.AMHashMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 4 with AMSearchResults

use of com.iplanet.am.sdk.AMSearchResults in project OpenAM by OpenRock.

the class DSAMERole method getValidValues.

/**
     * Returns a list of possible values for the <code>Subject
     * </code> that matches the pattern. 
     *
     * @param token the <code>SSOToken</code> that will be used
     * to determine the possible values
     *
     * @return <code>ValidValues</code> object
     *
     * @exception SSOException if SSO token is not valid
     * @exception PolicyException if unable to get the list of valid
     * names.
     */
public ValidValues getValidValues(SSOToken token, String pattern) throws SSOException, PolicyException {
    if (!initialized) {
        throw (new PolicyException(ResBundleUtils.rbName, "role_subject_not_yet_initialized", null, null));
    }
    try {
        AMStoreConnection amConnection = new AMStoreConnection(token);
        AMOrganization orgObject = amConnection.getOrganization(organizationDN);
        AMSearchControl sc = new AMSearchControl();
        sc.setMaxResults(maxResults);
        sc.setTimeOut(timeLimit);
        sc.setSearchScope(roleSearchScope);
        AMSearchResults results = orgObject.searchAllRoles(pattern, sc);
        int status;
        switch(results.getErrorCode()) {
            case AMSearchResults.SUCCESS:
                status = ValidValues.SUCCESS;
                break;
            case AMSearchResults.SIZE_LIMIT_EXCEEDED:
                status = ValidValues.SIZE_LIMIT_EXCEEDED;
                break;
            case AMSearchResults.TIME_LIMIT_EXCEEDED:
                status = ValidValues.TIME_LIMIT_EXCEEDED;
                break;
            default:
                status = ValidValues.SUCCESS;
        }
        return new ValidValues(status, results.getSearchResults());
    } catch (AMException e) {
        LdapException lde = e.getLDAPException();
        if (lde != null) {
            ResultCode ldapErrorCode = lde.getResult().getResultCode();
            if (ResultCode.INVALID_CREDENTIALS.equals(ldapErrorCode)) {
                throw new PolicyException(ResBundleUtils.rbName, "ldap_invalid_password", null, null);
            } else if (ResultCode.NO_SUCH_OBJECT.equals(ldapErrorCode)) {
                String[] objs = { organizationDN };
                throw new PolicyException(ResBundleUtils.rbName, "no_such_am_roles_base_dn", objs, null);
            }
            String errorMsg = lde.getResult().getDiagnosticMessage();
            String additionalMsg = lde.getResult().getResultCode().getName().toString(Locale.ROOT);
            if (additionalMsg != null) {
                throw new PolicyException(errorMsg + ": " + additionalMsg);
            } else {
                throw new PolicyException(errorMsg);
            }
        }
        throw new PolicyException(e);
    }
}
Also used : AMStoreConnection(com.iplanet.am.sdk.AMStoreConnection) AMSearchControl(com.iplanet.am.sdk.AMSearchControl) PolicyException(com.sun.identity.policy.PolicyException) ValidValues(com.sun.identity.policy.ValidValues) AMOrganization(com.iplanet.am.sdk.AMOrganization) AMException(com.iplanet.am.sdk.AMException) AMSearchResults(com.iplanet.am.sdk.AMSearchResults) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Example 5 with AMSearchResults

use of com.iplanet.am.sdk.AMSearchResults in project OpenAM by OpenRock.

the class DirectoryManagerImpl method search2.

public Map search2(String token, String entryDN, String searchFilter, List sortKeys, int startIndex, int beforeCount, int afterCount, String jumpTo, int timeOut, int maxResults, int scope, boolean allAttributes, String[] attrNames) throws AMRemoteException, SSOException, RemoteException {
    // Construct the SortKeys
    initialize();
    SortKey[] keys = null;
    int keysLength = 0;
    if (sortKeys != null && (keysLength = sortKeys.size()) != 0) {
        keys = new SortKey[keysLength];
        for (int i = 0; i < keysLength; i++) {
            String data = (String) sortKeys.get(i);
            keys[i] = new SortKey();
            keys[i].reverse = data.startsWith("true:");
            keys[i].attributeName = data.substring(5);
        }
    }
    // Construct SearchControl
    SearchControl sc = new SearchControl();
    if (keys != null) {
        sc.setSortKeys(keys);
    }
    if (jumpTo == null) {
        sc.setVLVRange(startIndex, beforeCount, afterCount);
    } else {
        sc.setVLVRange(jumpTo, beforeCount, afterCount);
    }
    sc.setTimeOut(timeOut);
    sc.setMaxResults(maxResults);
    sc.setSearchScope(scope);
    sc.setAllReturnAttributes(allAttributes);
    // Perform the search
    try {
        AMSearchResults results = dsServices.search(tm.createSSOToken(token), entryDN, searchFilter, sc, attrNames);
        // Convert results to Map
        Map answer = results.getResultAttributes();
        if (answer == null) {
            answer = new HashMap();
        }
        answer.put(com.iplanet.am.sdk.remote.RemoteServicesImpl.AMSR_COUNT, Integer.toString(results.getTotalResultCount()));
        answer.put(com.iplanet.am.sdk.remote.RemoteServicesImpl.AMSR_RESULTS, results.getSearchResults());
        answer.put(com.iplanet.am.sdk.remote.RemoteServicesImpl.AMSR_CODE, Integer.toString(results.getErrorCode()));
        return (answer);
    } catch (AMException amex) {
        if (debug.messageEnabled()) {
            debug.message("DMI::search(with SearchControl):  entryDN=" + entryDN + "the exception is: " + amex);
        }
        throw convertException(amex);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AMException(com.iplanet.am.sdk.AMException) SortKey(com.iplanet.ums.SortKey) SearchControl(com.iplanet.ums.SearchControl) AMSearchResults(com.iplanet.am.sdk.AMSearchResults) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Aggregations

AMSearchResults (com.iplanet.am.sdk.AMSearchResults)7 Map (java.util.Map)5 AMException (com.iplanet.am.sdk.AMException)4 SortKey (com.iplanet.ums.SortKey)4 HashMap (java.util.HashMap)4 SearchControl (com.iplanet.ums.SearchControl)3 Set (java.util.Set)3 AMHashMap (com.iplanet.am.sdk.AMHashMap)2 AMSearchControl (com.iplanet.am.sdk.AMSearchControl)2 SSOException (com.iplanet.sso.SSOException)2 PersistentObject (com.iplanet.ums.PersistentObject)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)2 AMEntryExistsException (com.iplanet.am.sdk.AMEntryExistsException)1 AMEventManagerException (com.iplanet.am.sdk.AMEventManagerException)1 AMObject (com.iplanet.am.sdk.AMObject)1 AMOrganization (com.iplanet.am.sdk.AMOrganization)1 AMStoreConnection (com.iplanet.am.sdk.AMStoreConnection)1