Search in sources :

Example 31 with Connection

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

the class DataLayer method rename.

public void rename(java.security.Principal principal, Guid guid, String newName, boolean deleteOldName) throws UMSException {
    String id = guid.getDn();
    ResultCode errorCode;
    try {
        ModifyDNRequest request = LDAPRequests.newModifyDNRequest(id, newName);
        int retry = 0;
        while (retry <= connNumRetry) {
            if (debug.messageEnabled()) {
                debug.message("DataLayer.rename retry: " + retry);
            }
            try (Connection conn = getConnection(principal)) {
                conn.applyChange(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.rename for DN: " + id, e);
        }
        errorCode = e.getResult().getResultCode();
        if (ResultCode.NO_SUCH_OBJECT.equals(errorCode)) {
            throw new EntryNotFoundException(id, e);
        } else if (ResultCode.INSUFFICIENT_ACCESS_RIGHTS.equals(errorCode)) {
            throw new AccessRightsException(id, e);
        } else {
            throw new UMSException(id, e);
        }
    }
}
Also used : ModifyDNRequest(org.forgerock.opendj.ldap.requests.ModifyDNRequest) 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 32 with Connection

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

the class DataLayer method modify.

/**
     * Modifies an ldap entry.
     * 
     * @param principal Authentication Principal.
     * @param guid globally unique identifier for the entry.
     * @param modifications Set of modifications for the entry.
     * @exception AccessRightsException if insufficient access
     * @exception EntryNotFoundException if the entry is not found.
     * @exception UMSException if failure
     *
     * @supported.api
     */
public void modify(Principal principal, Guid guid, Collection<Modification> modifications) throws UMSException {
    String id = guid.getDn();
    ResultCode errorCode;
    try {
        ModifyRequest request = LDAPRequests.newModifyRequest(id);
        for (Modification modification : modifications) {
            request.addModification(modification);
        }
        int retry = 0;
        while (retry <= connNumRetry) {
            if (debug.messageEnabled()) {
                debug.message("DataLayer.modify retry: " + retry);
            }
            try (Connection conn = getConnection(principal)) {
                conn.modify(request);
                return;
            } catch (LdapException e) {
                if (!retryErrorCodes.contains("" + e.getResult().getResultCode().toString()) || retry == connNumRetry) {
                    throw e;
                }
                retry++;
                try {
                    Thread.sleep(connRetryInterval);
                } catch (InterruptedException ex) {
                }
            }
        }
    } catch (LdapException e) {
        if (debug.warningEnabled()) {
            debug.warning("Exception in DataLayer.modify for DN: " + id, e);
        }
        errorCode = e.getResult().getResultCode();
        if (ResultCode.NO_SUCH_OBJECT.equals(errorCode)) {
            throw new EntryNotFoundException(id, e);
        } else if (ResultCode.INSUFFICIENT_ACCESS_RIGHTS.equals(errorCode)) {
            throw new AccessRightsException(id, e);
        } else {
            throw new UMSException(id, e);
        }
    }
}
Also used : Modification(org.forgerock.opendj.ldap.Modification) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) LdapException(org.forgerock.opendj.ldap.LdapException) ResultCode(org.forgerock.opendj.ldap.ResultCode)

Example 33 with Connection

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

the class IdRepoUtils method tagSwapAndImportSchema.

private static void tagSwapAndImportSchema(String schemaFile, Map attrValues, ServletContext servletCtx, String idRepoType) throws Exception {
    DataInputStream dis = null;
    try (ConnectionFactory factory = getLDAPConnection(attrValues);
        Connection ld = factory.getConnection();
        InputStreamReader fin = new InputStreamReader(servletCtx.getResourceAsStream(schemaFile))) {
        StringBuilder sbuf = new StringBuilder();
        char[] cbuf = new char[1024];
        int len;
        while ((len = fin.read(cbuf)) > 0) {
            sbuf.append(cbuf, 0, len);
        }
        String schemaStr = sbuf.toString();
        String suffix = CollectionHelper.getMapAttr(attrValues, "sun-idrepo-ldapv3-config-organization_name");
        if (suffix != null) {
            schemaStr = StringUtils.strReplaceAll(schemaStr, "@userStoreRootSuffix@", suffix);
            String dbName = LDAPUtils.getDBName(suffix, ld);
            schemaStr = StringUtils.strReplaceAll(schemaStr, "@DB_NAME@", dbName);
        }
        if (idRepoType.equals(LDAPv3ForADAM)) {
            String adamInstanceGUID = getADAMInstanceGUID(attrValues);
            if (adamInstanceGUID != null) {
                schemaStr = StringUtils.strReplaceAll(schemaStr, "@INSTANCE_GUID@", adamInstanceGUID);
            }
        }
        schemaStr = ServicesDefaultValues.tagSwap(schemaStr);
        dis = new DataInputStream(new ByteArrayInputStream(schemaStr.getBytes()));
        LdifUtils.createSchemaFromLDIF(dis, ld);
    } finally {
        if (dis != null) {
            try {
                dis.close();
            } catch (Exception ex) {
            //No handling requried
            }
        }
    }
}
Also used : ConnectionFactory(org.forgerock.opendj.ldap.ConnectionFactory) LDAPConnectionFactory(org.forgerock.opendj.ldap.LDAPConnectionFactory) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) Connection(org.forgerock.opendj.ldap.Connection) DataInputStream(java.io.DataInputStream) IdRepoException(com.sun.identity.idm.IdRepoException) ServerEntryNotFoundException(com.iplanet.services.naming.ServerEntryNotFoundException) SMSException(com.sun.identity.sm.SMSException) SSOException(com.iplanet.sso.SSOException)

Example 34 with Connection

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

the class UpgradeUtils method loadLdif.

/**
     * Loads the ldif changes to the directory server.
     *
     * @param ldifFileName the name of the ldif file.
     */
public static void loadLdif(String ldifFileName) {
    String classMethod = "UpgradeUtils:loadLdif : ";
    try (Connection conn = getLDAPConnection()) {
        System.out.println(bundle.getString("upg-load-ldif-file") + " :" + ldifFileName);
        LDIFChangeRecordReader ldifChangeRecordReader = new LDIFChangeRecordReader(ldifFileName);
        LdifUtils.createSchemaFromLDIF(ldifChangeRecordReader, conn);
    } catch (IOException ioe) {
        debug.error("{} Cannot find file . Error loading ldif {}", classMethod, ldifFileName, ioe);
    }
}
Also used : Connection(org.forgerock.opendj.ldap.Connection) LDIFChangeRecordReader(org.forgerock.opendj.ldif.LDIFChangeRecordReader) ByteString(org.forgerock.opendj.ldap.ByteString) IOException(java.io.IOException)

Example 35 with Connection

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

the class UpgradeUtils method getExistingValues.

/**
     * Returns a set of valid attributes values for an attribute.
     *
     * @param subConfig the <code>ServiceConfig</code> object.
     * @param attrName the attribute name.
     * @param defaultVal set of attribute values to validate with the
     *    the existing attribute values.
     */
static Set getExistingValues(ServiceConfig subConfig, String attrName, Set defaultVal) {
    Set<String> valSet = new HashSet<>();
    String classMethod = "UpgradeUtils:getExistingValues : ";
    try (Connection conn = getLDAPConnection()) {
        if (conn != null) {
            String dn = subConfig.getDN();
            SearchResultEntry result = conn.searchSingleEntry(LDAPRequests.newSingleEntrySearchRequest(dn));
            if (result != null) {
                for (Attribute attribute : result.getAllAttributes()) {
                    String attributeName = attribute.getAttributeDescriptionAsString();
                    if (attributeName != null && ATTR_SUN_KEY_VALUE.equalsIgnoreCase(attributeName)) {
                        for (ByteString value : attribute) {
                            String valueString = value.toString();
                            int index = valueString.indexOf("=");
                            if (index != -1) {
                                String key = valueString.substring(0, index);
                                if (attributeName.equalsIgnoreCase(key)) {
                                    String v = valueString.substring(index + 1, valueString.length());
                                    if (defaultVal.contains(v)) {
                                        valSet.add(v);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        debug.error(classMethod + "Error retreving attribute values ", e);
    }
    if (debug.messageEnabled()) {
        debug.message(classMethod + "Default Values are :" + valSet);
    }
    return valSet;
}
Also used : Attribute(org.forgerock.opendj.ldap.Attribute) ByteString(org.forgerock.opendj.ldap.ByteString) Connection(org.forgerock.opendj.ldap.Connection) ByteString(org.forgerock.opendj.ldap.ByteString) LoginException(javax.security.auth.login.LoginException) InvalidAuthContextException(com.sun.identity.authentication.internal.InvalidAuthContextException) UnknownPropertyNameException(com.sun.identity.common.configuration.UnknownPropertyNameException) PolicyException(com.sun.identity.policy.PolicyException) FileNotFoundException(java.io.FileNotFoundException) SSOException(com.iplanet.sso.SSOException) LdapException(org.forgerock.opendj.ldap.LdapException) SMSException(com.sun.identity.sm.SMSException) IOException(java.io.IOException) AMException(com.iplanet.am.sdk.AMException) ConfigurationException(com.sun.identity.common.configuration.ConfigurationException) HashSet(java.util.HashSet) SearchResultEntry(org.forgerock.opendj.ldap.responses.SearchResultEntry)

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