Search in sources :

Example 56 with LdapException

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

the class DataLayer method readLDAPEntry.

public ConnectionEntryReader readLDAPEntry(Principal principal, SearchRequest request) throws UMSException {
    LdapException ldapEx = null;
    int retry = 0;
    int connRetry = 0;
    while (retry <= replicaRetryNum && connRetry <= connNumRetry) {
        if (debug.messageEnabled()) {
            debug.message("DataLayer.readLDAPEntry: connRetry: " + connRetry);
            debug.message("DataLayer.readLDAPEntry: retry: " + retry);
        }
        try (Connection conn = getConnection(principal)) {
            return conn.search(request);
        } catch (LdapException e) {
            ResultCode errorCode = e.getResult().getResultCode();
            if (ResultCode.NO_SUCH_OBJECT.equals(errorCode)) {
                if (debug.messageEnabled()) {
                    debug.message("Replica: entry not found: " + request.getName().toString() + " retry: " + retry);
                }
                if (retry == replicaRetryNum) {
                    ldapEx = e;
                } else {
                    try {
                        Thread.sleep(replicaRetryInterval);
                    } catch (Exception ex) {
                    }
                }
                retry++;
            } else if (retryErrorCodes.contains("" + errorCode)) {
                if (connRetry == connNumRetry) {
                    ldapEx = e;
                } else {
                    try {
                        Thread.sleep(connRetryInterval);
                    } catch (Exception ex) {
                    }
                }
                connRetry++;
            } else {
                throw new UMSException(e.getMessage(), e);
            }
        }
    }
    throw new UMSException(ldapEx.getMessage(), ldapEx);
}
Also used : Connection(org.forgerock.opendj.ldap.Connection) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode) LDAPServiceException(com.iplanet.services.ldap.LDAPServiceException) LdapException(org.forgerock.opendj.ldap.LdapException) IOException(java.io.IOException)

Example 57 with LdapException

use of org.forgerock.opendj.ldap.LdapException 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)

Example 58 with LdapException

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

the class DataLayer method addEntry.

/**
     * Adds entry to the server.
     * 
     * @param principal Authenticated Principal.
     * @param guid Distinguished name.
     * @param attrSet attribute set containing name/value pairs.
     * @exception AccessRightsException if insufficient access>
     * @exception EntryAlreadyExistsException if the entry already exists.
     * @exception UMSException if fail to add entry.
     *
     * @supported.api
     */
public void addEntry(java.security.Principal principal, Guid guid, AttrSet attrSet) throws UMSException {
    String id = guid.getDn();
    ResultCode errorCode;
    try {
        AddRequest request = LDAPRequests.newAddRequest(id);
        for (Attribute attribute : attrSet.toLDAPAttributeSet()) {
            request.addAttribute(attribute);
        }
        int retry = 0;
        while (retry <= connNumRetry) {
            if (debug.messageEnabled()) {
                debug.message("DataLayer.addEntry retry: " + retry);
            }
            try (Connection conn = getConnection(principal)) {
                conn.add(request);
                return;
            } catch (LdapException e) {
                errorCode = e.getResult().getResultCode();
                if (!retryErrorCodes.contains(errorCode) || retry == connNumRetry) {
                    throw e;
                }
                retry++;
                try {
                    Thread.sleep(connRetryInterval);
                } catch (InterruptedException ex) {
                }
            }
        }
    } catch (LdapException e) {
        if (debug.warningEnabled()) {
            debug.warning("Exception in DataLayer.addEntry for DN: " + id, e);
        }
        errorCode = e.getResult().getResultCode();
        String[] args = { id };
        if (ResultCode.ENTRY_ALREADY_EXISTS.equals(errorCode)) {
            throw new EntryAlreadyExistsException(i18n.getString(IUMSConstants.ENTRY_ALREADY_EXISTS, args), e);
        } else if (ResultCode.INSUFFICIENT_ACCESS_RIGHTS.equals(errorCode)) {
            throw new AccessRightsException(i18n.getString(IUMSConstants.INSUFFICIENT_ACCESS_ADD, args), e);
        } else {
            throw new UMSException(i18n.getString(IUMSConstants.UNABLE_TO_ADD_ENTRY, args), e);
        }
    }
}
Also used : AddRequest(org.forgerock.opendj.ldap.requests.AddRequest) Attribute(org.forgerock.opendj.ldap.Attribute) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Example 59 with LdapException

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

the class DataLayer method deleteEntry.

/**
     * Delete entry from the server
     * 
     * @param guid
     *            globally unique identifier for the entry
     * @exception AccessRightsException
     *                insufficient access
     * @exception EntryNotFoundException
     *                if the entry is not found
     * @exception UMSException
     *                Fail to delete the entry
     *
     * @supported.api
     */
public void deleteEntry(java.security.Principal principal, Guid guid) throws UMSException {
    if (guid == null) {
        String msg = i18n.getString(IUMSConstants.BAD_ID);
        throw new IllegalArgumentException(msg);
    }
    String id = guid.getDn();
    ResultCode errorCode;
    try {
        DeleteRequest request = LDAPRequests.newDeleteRequest(id);
        int retry = 0;
        while (retry <= connNumRetry) {
            if (debug.messageEnabled()) {
                debug.message("DataLayer.deleteEntry retry: " + retry);
            }
            try (Connection conn = getConnection(principal)) {
                conn.delete(request);
                return;
            } catch (LdapException e) {
                if (!retryErrorCodes.contains(e.getResult().getResultCode()) || retry == connNumRetry) {
                    throw e;
                }
                retry++;
                try {
                    Thread.sleep(connRetryInterval);
                } catch (InterruptedException ex) {
                }
            }
        }
    } catch (LdapException e) {
        debug.error("Exception in DataLayer.deleteEntry for DN: " + id, e);
        errorCode = e.getResult().getResultCode();
        String[] args = { id };
        if (ResultCode.NO_SUCH_OBJECT.equals(errorCode)) {
            throw new EntryNotFoundException(i18n.getString(IUMSConstants.ENTRY_NOT_FOUND, args), e);
        } else if (ResultCode.INSUFFICIENT_ACCESS_RIGHTS.equals(errorCode)) {
            throw new AccessRightsException(i18n.getString(IUMSConstants.INSUFFICIENT_ACCESS_DELETE, args), e);
        } else {
            throw new UMSException(i18n.getString(IUMSConstants.UNABLE_TO_DELETE_ENTRY, args), e);
        }
    }
}
Also used : Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) DeleteRequest(org.forgerock.opendj.ldap.requests.DeleteRequest) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Example 60 with LdapException

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

the class DataLayer method readLDAPEntry.

public Entry readLDAPEntry(Connection ld, String dn, String[] attrnames) throws LdapException {
    LdapException ldapEx = null;
    int retry = 0;
    int connRetry = 0;
    while (retry <= replicaRetryNum && connRetry <= connNumRetry) {
        if (debug.messageEnabled()) {
            debug.message("DataLayer.readLDAPEntry: connRetry: " + connRetry);
            debug.message("DataLayer.readLDAPEntry: retry: " + retry);
        }
        try {
            if (attrnames == null) {
                return ld.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(dn));
            } else {
                return ld.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(dn, attrnames));
            }
        } catch (LdapException e) {
            ResultCode errorCode = e.getResult().getResultCode();
            if (ResultCode.NO_SUCH_OBJECT.equals(errorCode)) {
                if (debug.messageEnabled()) {
                    debug.message("Replica: entry not found: " + dn + " retry: " + retry);
                }
                if (retry == replicaRetryNum) {
                    ldapEx = e;
                } else {
                    try {
                        Thread.sleep(replicaRetryInterval);
                    } catch (Exception ignored) {
                    }
                }
                retry++;
            } else if (retryErrorCodes.contains("" + errorCode)) {
                if (connRetry == connNumRetry) {
                    ldapEx = e;
                } else {
                    try {
                        Thread.sleep(connRetryInterval);
                    } catch (Exception ignored) {
                    }
                }
                connRetry++;
            } else {
                throw e;
            }
        }
    }
    throw ldapEx;
}
Also used : LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode) LDAPServiceException(com.iplanet.services.ldap.LDAPServiceException) LdapException(org.forgerock.opendj.ldap.LdapException) IOException(java.io.IOException)

Aggregations

LdapException (org.forgerock.opendj.ldap.LdapException)88 Connection (org.forgerock.opendj.ldap.Connection)62 ByteString (org.forgerock.opendj.ldap.ByteString)41 ResultCode (org.forgerock.opendj.ldap.ResultCode)37 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)35 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)34 SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)23 HashSet (java.util.HashSet)22 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)22 Attribute (org.forgerock.opendj.ldap.Attribute)17 PolicyException (com.sun.identity.policy.PolicyException)13 SMSException (com.sun.identity.sm.SMSException)12 ModifyRequest (org.forgerock.opendj.ldap.requests.ModifyRequest)12 SSOException (com.iplanet.sso.SSOException)11 LinkedHashSet (java.util.LinkedHashSet)11 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)10 IOException (java.io.IOException)10 DN (org.forgerock.opendj.ldap.DN)10 InvalidNameException (com.sun.identity.policy.InvalidNameException)9 NameNotFoundException (com.sun.identity.policy.NameNotFoundException)9