Search in sources :

Example 36 with Connection

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

the class UserIdRepo method getADAMInstanceGUID.

private String getADAMInstanceGUID(Map userRepo) throws Exception {
    try (Connection ld = getLDAPConnection(userRepo)) {
        String attrName = "schemaNamingContext";
        ConnectionEntryReader res = ld.search(LDAPRequests.newSearchRequest("", SearchScope.BASE_OBJECT, "(objectclass=*)"));
        if (res.hasNext()) {
            SearchResultEntry entry = res.readEntry();
            Attribute ldapAttr = entry.getAttribute(attrName);
            if (ldapAttr != null) {
                String value = ldapAttr.firstValueAsString();
                int index = value.lastIndexOf("=");
                if (index != -1) {
                    return value.substring(index + 1).trim();
                }
            }
        }
    }
    return null;
}
Also used : ConnectionEntryReader(org.forgerock.opendj.ldif.ConnectionEntryReader) Attribute(org.forgerock.opendj.ldap.Attribute) Connection(org.forgerock.opendj.ldap.Connection) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 37 with Connection

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

the class EmbeddedOpenDS method getAdminPort.

/**
     * Get admin port of the OpenDJ server
     *
     * @param username The username of the directory admin
     * @param password The password of the directory admin
     * @param hostname The hostname of the directory server
     * @param port     The port of the directory server
     * @return The admin port
     */
public static String getAdminPort(String username, String password, String hostname, String port) {
    final String adminConnectorDN = "cn=Administration Connector,cn=config";
    final String[] attrs = { "ds-cfg-listen-port" };
    String adminPort = null;
    Connection ld = null;
    try (Connection conn = getLDAPConnection(hostname, port, username, password)) {
        if (conn != null) {
            SearchResultEntry le = conn.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(adminConnectorDN, attrs));
            if (le != null) {
                Attribute la = le.getAttribute(attrs[0]);
                if (la != null) {
                    adminPort = la.firstValueAsString();
                }
            }
        }
    } catch (Exception ex) {
        Debug.getInstance(SetupConstants.DEBUG_NAME).error("EmbeddedOpenDS.getAdminPort(). Error getting admin port:", ex);
    }
    return adminPort;
}
Also used : Attribute(org.forgerock.opendj.ldap.Attribute) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LdapException(org.forgerock.opendj.ldap.LdapException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 38 with Connection

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

the class EmbeddedOpenDS method getReplicationPort.

/**
     * Get replication port
     *
     * @param username
     * @param password
     * @param hostname
     * @param port
     * @return port number if replication is setup, null if not or on error.
     */
public static String getReplicationPort(String username, String password, String hostname, String port) {
    final String replDN = "cn=replication server,cn=Multimaster Synchronization,cn=Synchronization Providers,cn=config";
    final String[] attrs = { "ds-cfg-replication-port" };
    String replPort = null;
    Connection ld = null;
    username = "cn=Directory Manager";
    try (Connection conn = getLDAPConnection(hostname, port, username, password)) {
        // We'll use Directory Manager
        if (conn != null) {
            SearchResultEntry le = conn.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(replDN, attrs));
            if (le != null) {
                Attribute la = le.getAttribute(attrs[0]);
                if (la != null) {
                    replPort = la.firstValueAsString();
                }
            }
        }
    } catch (Exception ex) {
        Debug.getInstance(SetupConstants.DEBUG_NAME).error("EmbeddedOpenDS.getReplicationPort(). Error getting replication port:", ex);
    }
    return replPort;
}
Also used : Attribute(org.forgerock.opendj.ldap.Attribute) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) LdapException(org.forgerock.opendj.ldap.LdapException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IOException(java.io.IOException) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

Example 39 with Connection

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

the class SMSLdapObject method entryExists.

/**
     * Checks if the provided DN exists.
     */
private static boolean entryExists(String dn) throws SMSException {
    boolean entryExists = false;
    try (Connection conn = getConnection(adminPrincipal)) {
        // Use the Admin Principal to check if entry exists
        conn.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(dn, OU_ATTR));
        entryExists = true;
    } catch (EntryNotFoundException e) {
        debug.warning("SMSLdapObject:entryExists: {} does not exist", dn);
    } catch (LdapException e) {
        throw new SMSException("Unable to find entry with DN: " + dn, e, IUMSConstants.SMS_LDAP_OPERATION_FAILED);
    }
    return entryExists;
}
Also used : SMSException(com.sun.identity.sm.SMSException) Connection(org.forgerock.opendj.ldap.Connection) EntryNotFoundException(org.forgerock.opendj.ldap.EntryNotFoundException) LdapException(org.forgerock.opendj.ldap.LdapException)

Example 40 with Connection

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

the class SMSLdapObject method create.

/**
     * Create an entry in the directory using the principal name
     */
private static void create(Principal p, String dn, Map attrs) throws SMSException, SSOException {
    int retry = 0;
    Entry entry = copyMapToEntry(attrs).setName(dn);
    while (retry <= connNumRetry) {
        debug.message("SMSLdapObject.create() retry: {}", retry);
        try (Connection conn = getConnection(p)) {
            conn.add(LDAPRequests.newAddRequest(entry));
            debug.message("SMSLdapObject.create Successfully created entry: {}", dn);
            break;
        } catch (LdapException e) {
            ResultCode errorCode = e.getResult().getResultCode();
            if (errorCode.equals(ResultCode.ENTRY_ALREADY_EXISTS) && retry > 0) {
                // During install time and other times,
                // this error gets throws due to unknown issue. Issue:
                // Hence mask it.
                debug.warning("SMSLdapObject.create() Entry Already Exists Error for DN {}", dn);
                break;
            }
            if (!retryErrorCodes.contains(errorCode) || retry >= connNumRetry) {
                debug.error("SMSLdapObject.create() Error in creating: {} By Principal: {}", dn, p.getName(), e);
                throw new SMSException(e, "sms-entry-cannot-create");
            }
            retry++;
            try {
                Thread.sleep(connRetryInterval);
            } catch (InterruptedException ex) {
            //ignored
            }
        }
    }
}
Also used : SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry) SMSDataEntry(com.sun.identity.sm.SMSDataEntry) SMSEntry(com.sun.identity.sm.SMSEntry) LinkedHashMapEntry(org.forgerock.opendj.ldap.LinkedHashMapEntry) Entry(org.forgerock.opendj.ldap.Entry) SMSException(com.sun.identity.sm.SMSException) Connection(org.forgerock.opendj.ldap.Connection) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Aggregations

Connection (org.forgerock.opendj.ldap.Connection)94 LdapException (org.forgerock.opendj.ldap.LdapException)72 ByteString (org.forgerock.opendj.ldap.ByteString)47 SearchResultEntry (org.forgerock.opendj.ldap.responses.SearchResultEntry)46 ConnectionEntryReader (org.forgerock.opendj.ldif.ConnectionEntryReader)39 ResultCode (org.forgerock.opendj.ldap.ResultCode)29 Attribute (org.forgerock.opendj.ldap.Attribute)27 HashSet (java.util.HashSet)26 SearchRequest (org.forgerock.opendj.ldap.requests.SearchRequest)20 SearchResultReferenceIOException (org.forgerock.opendj.ldap.SearchResultReferenceIOException)19 IOException (java.io.IOException)18 SSOException (com.iplanet.sso.SSOException)15 PolicyException (com.sun.identity.policy.PolicyException)14 SMSException (com.sun.identity.sm.SMSException)13 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)13 ModifyRequest (org.forgerock.opendj.ldap.requests.ModifyRequest)12 BindResult (org.forgerock.opendj.ldap.responses.BindResult)12 DN (org.forgerock.opendj.ldap.DN)11 CaseInsensitiveHashSet (com.sun.identity.common.CaseInsensitiveHashSet)10 InvalidNameException (com.sun.identity.policy.InvalidNameException)10