Search in sources :

Example 31 with SearchRequest

use of org.forgerock.opendj.ldap.requests.SearchRequest in project OpenAM by OpenRock.

the class DataLayer method getAttributes.

/**
     * Returns attributes for the given attribute names.
     * 
     * @param principal Authentication Principal.
     * @param guid Distinguished name.
     * @param attrNames Attribute names.
     * @return collection of Attr.
     *
     * @supported.api
     */
public Collection<Attr> getAttributes(Principal principal, Guid guid, Collection<String> attrNames) {
    String id = guid.getDn();
    SearchRequest request = LDAPRequests.newSearchRequest(id, SearchScope.BASE_OBJECT, "(objectclass=*)", attrNames.toArray(EMPTY_STRING_ARRAY));
    ConnectionEntryReader ldapEntry;
    try {
        ldapEntry = readLDAPEntry(principal, request);
        if (ldapEntry == null) {
            debug.warning("No attributes returned may not have permission to read");
            return Collections.emptySet();
        }
        Collection<Attr> attributes = new ArrayList<>();
        while (ldapEntry.hasNext()) {
            if (ldapEntry.isEntry()) {
                SearchResultEntry entry = ldapEntry.readEntry();
                for (Attribute attr : entry.getAllAttributes()) {
                    attributes.add(new Attr(attr));
                }
            }
        }
        return attributes;
    } catch (Exception e) {
        debug.warning("Exception in DataLayer.getAttributes for DN: {}", id, e);
        return null;
    }
}
Also used : SearchRequest(org.forgerock.opendj.ldap.requests.SearchRequest) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) Attribute(org.forgerock.opendj.ldap.Attribute) ArrayList(java.util.ArrayList) ByteString(org.forgerock.opendj.ldap.ByteString) Attr(com.iplanet.services.ldap.Attr) LDAPServiceException(com.iplanet.services.ldap.LDAPServiceException) LdapException(org.forgerock.opendj.ldap.LdapException) IOException(java.io.IOException) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 32 with SearchRequest

use of org.forgerock.opendj.ldap.requests.SearchRequest in project OpenAM by OpenRock.

the class DataLayer method search.

/**
     * Performs synchronous search based on specified ldap filter. This is low
     * level API which assumes caller knows how to construct a data store filer.
     * 
     * @param principal Authenticated Principal.
     * @param guid Unique identifier for the entry.
     * @param scope Scope can be either <code>SCOPE_ONE</code>,
     *        <code>SCOPE_SUB</code> or <code>SCOPE_BASE</code>.
     * @param searchFilter Search filter for this search.
     * @param attrNames Attribute name for retrieving.
     * @param attrOnly if true, returns the names but not the values of the
     *        attributes found.
     * @param searchControl Search Control.
     * @exception UMSException if failure.
     * @exception InvalidSearchFilterException if failure
     *
     * @supported.api
     */
public SearchResults search(java.security.Principal principal, Guid guid, int scope, String searchFilter, String[] attrNames, boolean attrOnly, SearchControl searchControl) throws UMSException {
    String id = guid.getDn();
    // always add "objectclass" to attributes to get, to find the right java
    // class
    String[] attrNames1 = null;
    if (attrNames != null) {
        attrNames1 = new String[attrNames.length + 1];
        System.arraycopy(attrNames, 0, attrNames1, 0, attrNames.length);
        attrNames1[attrNames1.length - 1] = "objectclass";
    } else {
        attrNames1 = new String[] { "objectclass" };
    }
    ConnectionEntryReader ldapResults = null;
    // if searchFilter is null, search for everything under the base
    if (searchFilter == null) {
        searchFilter = "(objectclass=*)";
    }
    ResultCode errorCode;
    try {
        Connection conn = getConnection(principal);
        List<Control> controls = getSearchControls(searchControl);
        // assume replica case when replicaRetryNum is not 0
        if (replicaRetryNum != 0) {
            readLDAPEntry(conn, id, null);
        }
        SearchRequest request = null;
        int retry = 0;
        while (retry <= connNumRetry) {
            if (debug.messageEnabled()) {
                debug.message("DataLayer.search retry: " + retry);
            }
            if (searchControl != null && searchControl.isGetAllReturnAttributesEnabled()) {
                /*
                     * The array {"*"} is used, because LDAPv3 defines
                     * "*" as a special string indicating all
                     * attributes. This gets all the attributes.
                     */
                attrNames1 = new String[] { "*" };
            }
            request = LDAPRequests.newSearchRequest(id, SearchScope.valueOf(scope), searchFilter, attrNames1);
            break;
        }
        for (Control control : controls) {
            request.addControl(control);
        }
        ldapResults = conn.search(request);
        // TODO: need review and see if conn is recorded properly for
        // subsequent use
        //
        SearchResults result = new SearchResults(conn, ldapResults, conn, this);
        result.set(SearchResults.BASE_ID, id);
        result.set(SearchResults.SEARCH_FILTER, searchFilter);
        result.set(SearchResults.SEARCH_SCOPE, scope);
        if ((searchControl != null) && (searchControl.contains(SearchControl.KeyVlvRange) || searchControl.contains(SearchControl.KeyVlvJumpTo))) {
            result.set(SearchResults.EXPECT_VLV_RESPONSE, Boolean.TRUE);
        }
        if (searchControl != null && searchControl.contains(SearchControl.KeySortKeys)) {
            SortKey[] sortKeys = searchControl.getSortKeys();
            if (sortKeys != null && sortKeys.length > 0) {
                result.set(SearchResults.SORT_KEYS, sortKeys);
            }
        }
        return result;
    } catch (LdapException e) {
        errorCode = e.getResult().getResultCode();
        if (debug.warningEnabled()) {
            debug.warning("Exception in DataLayer.search: ", e);
        }
        String msg = i18n.getString(IUMSConstants.SEARCH_FAILED);
        if (ResultCode.TIME_LIMIT_EXCEEDED.equals(errorCode)) {
            int timeLimit = searchControl != null ? searchControl.getTimeOut() : 0;
            throw new TimeLimitExceededException(String.valueOf(timeLimit), e);
        } else if (ResultCode.SIZE_LIMIT_EXCEEDED.equals(errorCode)) {
            int sizeLimit = searchControl != null ? searchControl.getMaxResults() : 0;
            throw new SizeLimitExceededException(String.valueOf(sizeLimit), e);
        } else if (ResultCode.CLIENT_SIDE_PARAM_ERROR.equals(errorCode) || ResultCode.PROTOCOL_ERROR.equals(errorCode)) {
            throw new InvalidSearchFilterException(searchFilter, e);
        } else {
            throw new UMSException(msg, e);
        }
    }
}
Also used : SearchRequest(org.forgerock.opendj.ldap.requests.SearchRequest) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) VirtualListViewRequestControl(org.forgerock.opendj.ldap.controls.VirtualListViewRequestControl) ServerSideSortRequestControl(org.forgerock.opendj.ldap.controls.ServerSideSortRequestControl) ProxiedAuthV1RequestControl(org.forgerock.opendj.ldap.controls.ProxiedAuthV1RequestControl) Control(org.forgerock.opendj.ldap.controls.Control) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Aggregations

SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)32 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)26 LdapException (org.forgerock.opendj.ldap.LdapException)25 Connection (org.forgerock.opendj.ldap.Connection)20 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)19 ByteString (org.forgerock.opendj.ldap.ByteString)18 ResultCode (org.forgerock.opendj.ldap.ResultCode)15 HashSet (java.util.HashSet)13 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)10 Attribute (org.forgerock.opendj.ldap.Attribute)9 DN (org.forgerock.opendj.ldap.DN)9 SSOException (com.iplanet.sso.SSOException)8 PolicyException (com.sun.identity.policy.PolicyException)8 InvalidNameException (com.sun.identity.policy.InvalidNameException)7 NameNotFoundException (com.sun.identity.policy.NameNotFoundException)7 LinkedHashSet (java.util.LinkedHashSet)7 SMSException (com.sun.identity.sm.SMSException)6 Filter (org.forgerock.opendj.ldap.Filter)6 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)5 ArrayList (java.util.ArrayList)4