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);
}
}
}
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);
}
}
}
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
}
}
}
}
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);
}
}
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;
}
Aggregations