use of org.forgerock.opendj.ldap.requests.DeleteRequest in project OpenAM by OpenRock.
the class RemoveReferralsStep method deleteExistingReferrals.
private void deleteExistingReferrals() throws UpgradeException {
try (Connection connection = getConnection()) {
for (DN referral : referralsToBeRemoved) {
UpgradeProgress.reportStart(AUDIT_REMOVING_REFERRAL_START, referral);
DeleteRequest request = LDAPRequests.newDeleteRequest(referral);
connection.delete(request);
UpgradeProgress.reportEnd(AUDIT_UPGRADE_SUCCESS);
}
} catch (DataLayerException | LdapException e) {
UpgradeProgress.reportEnd(AUDIT_UPGRADE_FAIL);
throw new UpgradeException("Failed to delete referrals", e);
}
}
use of org.forgerock.opendj.ldap.requests.DeleteRequest 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);
}
}
}
use of org.forgerock.opendj.ldap.requests.DeleteRequest in project OpenAM by OpenRock.
the class LdapAdapterTest method shouldUseConnectionForDelete.
@Test
public void shouldUseConnectionForDelete() throws Exception {
// Given
String tokenId = "badger";
DN testDN = DN.rootDN();
Result successResult = mockSuccessfulResult();
given(mockConnection.delete(any(DeleteRequest.class))).willReturn(successResult);
given(mockConversion.generateTokenDN(anyString())).willReturn(testDN);
// When
adapter.delete(mockConnection, tokenId);
// Then
ArgumentCaptor<DeleteRequest> captor = ArgumentCaptor.forClass(DeleteRequest.class);
verify(mockConnection).delete(captor.capture());
assertEquals(testDN, captor.getValue().getName());
}
use of org.forgerock.opendj.ldap.requests.DeleteRequest in project OpenAM by OpenRock.
the class LdifUtils method createSchemaFromLDIF.
/**
* Creates LDAP schema from LDIF file.
*
* @param ldif LDIF object.
* @param ld LDAP Connection.
* @throws IOException If an error occurs when reading the LDIF file.
*/
public static void createSchemaFromLDIF(LDIFChangeRecordReader ldif, final Connection ld) throws IOException {
while (ldif.hasNext()) {
final ChangeRecord changeRecord = ldif.readChangeRecord();
changeRecord.accept(new ChangeRecordVisitor<Void, Void>() {
@Override
public Void visitChangeRecord(Void aVoid, AddRequest change) {
try {
change.addControl(TransactionIdControl.newControl(AuditRequestContext.createSubTransactionIdValue()));
ld.add(change);
} catch (LdapException e) {
if (ResultCode.ENTRY_ALREADY_EXISTS.equals(e.getResult().getResultCode())) {
for (Attribute attr : change.getAllAttributes()) {
ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(change.getName());
modifyRequest.addModification(new Modification(ModificationType.ADD, attr));
try {
ld.modify(modifyRequest);
} catch (LdapException ex) {
DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not modify schema: {}", modifyRequest, ex);
}
}
} else {
DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not add to schema: {}", change, e);
}
}
return null;
}
@Override
public Void visitChangeRecord(Void aVoid, ModifyRequest change) {
try {
change.addControl(TransactionIdControl.newControl(AuditRequestContext.createSubTransactionIdValue()));
ld.modify(change);
} catch (LdapException e) {
DEBUG.warning("LDAPUtils.createSchemaFromLDIF - Could not modify schema: {}", change, e);
}
return null;
}
@Override
public Void visitChangeRecord(Void aVoid, ModifyDNRequest change) {
return null;
}
@Override
public Void visitChangeRecord(Void aVoid, DeleteRequest change) {
DEBUG.message("Delete request ignored: {}", changeRecord);
return null;
}
}, null);
}
}
use of org.forgerock.opendj.ldap.requests.DeleteRequest in project OpenAM by OpenRock.
the class RemoveReferralsStepTest method simpleSuccessfulPassThrough.
@Test
public void simpleSuccessfulPassThrough() throws Exception {
// Given
given(connectionFactory.create()).willReturn(connection);
given(connection.search(isA(SearchRequest.class))).willReturn(entryReader);
given(entryReader.hasNext()).willReturn(true).willReturn(false);
given(entryReader.readEntry()).willReturn(resultEntry);
given(resultEntry.getName()).willReturn(DN.valueOf("ou=test,ou=forgerock,ou=org"));
JsonValue jsonValue = json(object(field("name", "ref"), field("mapApplNameToResources", object(field("app1", array("*://*:*/*")))), field("realms", array("/a"))));
Set<String> values = singleton("serializable=" + jsonValue.toString());
Attribute attribute = new LinkedAttribute("ou", values);
AttributeParser attributeParser = AttributeParser.parseAttribute(attribute);
given(resultEntry.parseAttribute("sunKeyValue")).willReturn(attributeParser);
Application app1 = new Application();
app1.setName("app1");
app1.addAllResourceTypeUuids(singleton("123"));
given(applicationService.getApplication(isA(Subject.class), eq("/"), eq("app1"))).willReturn(app1);
given(policyServiceFactory.get(eq("/a"), isA(Subject.class))).willReturn(policyService);
Privilege policy1 = new OpenSSOPrivilege();
policy1.setName("pol1");
given(policyService.findAllPoliciesByApplication("app1")).willReturn(singletonList(policy1));
ResourceType resourceType1 = ResourceType.builder().setName("resourceType1").setUUID("123").build();
given(resourceTypeService.getResourceType(isA(Subject.class), eq("/"), eq("123"))).willReturn(resourceType1);
// When
testStep.initialize();
boolean isApplicable = testStep.isApplicable();
testStep.perform();
String shortReport = testStep.getShortReport("");
String longReport = testStep.getDetailedReport("");
// Then
assertThat(isApplicable).isTrue();
assertThat(shortReport).containsSequence("applications to be cloned", "Referrals found");
assertThat(longReport).containsSequence("app1", "ou=test,ou=forgerock,ou=org");
verify(resourceTypeService).saveResourceType(isA(Subject.class), eq("/a"), resourceTypeCaptor.capture());
verify(applicationService).saveApplication(isA(Subject.class), eq("/a"), applicationCaptor.capture());
verify(policyService).modify(policyCaptor.capture());
ResourceType clonedResourceType = resourceTypeCaptor.getValue();
assertThat(clonedResourceType).isNotEqualTo(resourceType1);
assertThat(clonedResourceType.getName()).isEqualTo("resourceType1");
Application clonedApplication = applicationCaptor.getValue();
assertThat(clonedApplication).isNotEqualTo(app1);
assertThat(clonedApplication.getName()).isEqualTo("app1");
assertThat(clonedApplication.getResourceTypeUuids()).containsExactly(clonedResourceType.getUUID());
Privilege modifiedPolicy = policyCaptor.getValue();
assertThat(modifiedPolicy).isEqualTo(modifiedPolicy);
assertThat(modifiedPolicy.getResourceTypeUuid()).isEqualTo(clonedResourceType.getUUID());
verify(connection).delete(deleteRequestCaptor.capture());
DeleteRequest request = deleteRequestCaptor.getValue();
assertThat(request.getName().toString()).isEqualTo("ou=test,ou=forgerock,ou=org");
}
Aggregations