Skip to content

Instantly share code, notes, and snippets.

@danshan
Last active December 31, 2015 07:11
Show Gist options
  • Save danshan/7ea18543018160d991a8 to your computer and use it in GitHub Desktop.
Save danshan/7ea18543018160d991a8 to your computer and use it in GitHub Desktop.
Java Code Examples for org.apache.directory.shared.ldap.model.message.ModifyDnRequestImpl
/**
* {@inheritDoc}
*/
public void action(LdapMessageContainer<ModifyDnRequestDecorator> container){
ModifyDnRequest internalModifyDnRequest=new ModifyDnRequestImpl();
internalModifyDnRequest.setMessageId(container.getMessageId());
ModifyDnRequestDecorator modifyDnRequest=new ModifyDnRequestDecorator(container.getLdapCodecService(),internalModifyDnRequest);
container.setMessage(modifyDnRequest);
LOG.debug("ModifyDn request");
}
/**
* Tests referral handling when an ancestor is a referral.
*/
@Test public void testAncestorReferral() throws Exception {
LOG.debug("");
LdapConnection conn=getWiredConnection(getLdapServer());
ModifyDnRequest modifyDnRequest=new ModifyDnRequestImpl();
modifyDnRequest.setName(new Dn("ou=Computers,uid=akarasuluref,ou=users,ou=system"));
modifyDnRequest.setNewRdn(new Rdn("ou=Machines"));
modifyDnRequest.setDeleteOldRdn(true);
ModifyDnResponse modifyDnResponse=conn.modifyDn(modifyDnRequest);
assertEquals(ResultCodeEnum.REFERRAL,modifyDnResponse.getLdapResult().getResultCode());
assertTrue(modifyDnResponse.getLdapResult().getReferral().getLdapUrls().contains("ldap://localhost:10389/ou=Computers,uid=akarasulu,ou=users,ou=system"));
assertTrue(modifyDnResponse.getLdapResult().getReferral().getLdapUrls().contains("ldap://foo:10389/ou=Computers,uid=akarasulu,ou=users,ou=system"));
assertTrue(modifyDnResponse.getLdapResult().getReferral().getLdapUrls().contains("ldap://bar:10389/ou=Computers,uid=akarasulu,ou=users,ou=system"));
conn.close();
}
@Test public void testModifyDnAsync() throws Exception {
Dn oldDn=new Dn(DN);
Dn newDn=new Dn("cn=modifyDnWithString,ou=system");
ModifyDnRequest modDnReq=new ModifyDnRequestImpl();
modDnReq.setName(oldDn);
modDnReq.setNewRdn(new Rdn("cn=modifyDnWithString"));
modDnReq.setDeleteOldRdn(true);
ModifyDnFuture modifyDnFuture=connection.modifyDnAsync(modDnReq);
ModifyDnResponse response=modifyDnFuture.get(1000,TimeUnit.MILLISECONDS);
assertNotNull(response);
assertTrue(connection.isAuthenticated());
assertFalse(session.exists(oldDn));
assertTrue(session.exists(newDn));
assertTrue(session.exists(new Dn("cn=modifyDnWithString,ou=system")));
}
/**
* {@inheritDoc}
*/
public void move(Dn entryDn,Dn newSuperiorDn) throws LdapException {
if (entryDn == null) {
String msg="Cannot process a move of a null Dn";
LOG.debug(msg);
throw new IllegalArgumentException(msg);
}
if (newSuperiorDn == null) {
String msg="Cannot process a move to a null Dn";
LOG.debug(msg);
throw new IllegalArgumentException(msg);
}
ModifyDnRequest iModDnReq=new ModifyDnRequestImpl();
iModDnReq.setName(entryDn);
iModDnReq.setNewSuperior(newSuperiorDn);
ModifyDnResponse modifyDnResponse=modifyDn(iModDnReq);
processResponse(modifyDnResponse);
}
/**
* Moves and renames the given entryDn.The old Rdn will be deleted if requested
* @param entryDn The original entry Dn
* @param newDn The new Entry Dn
* @param deleteOldRdn Tells if the old Rdn must be removed
*/
public void moveAndRename(Dn entryDn,Dn newDn,boolean deleteOldRdn) throws LdapException {
if (entryDn == null) {
throw new IllegalArgumentException("The entry Dn must not be null");
}
if (entryDn.isRootDse()) {
throw new IllegalArgumentException("The RootDSE cannot be moved");
}
if (newDn == null) {
throw new IllegalArgumentException("The new Dn must not be null");
}
if (newDn.isRootDse()) {
throw new IllegalArgumentException("The RootDSE cannot be the target");
}
ModifyDnResponse resp=new ModifyDnResponseImpl();
resp.getLdapResult().setResultCode(ResultCodeEnum.SUCCESS);
ModifyDnRequest modifyDnRequest=new ModifyDnRequestImpl();
modifyDnRequest.setName(entryDn);
modifyDnRequest.setNewRdn(newDn.getRdn());
modifyDnRequest.setNewSuperior(newDn.getParent());
modifyDnRequest.setDeleteOldRdn(deleteOldRdn);
ModifyDnResponse modifyDnResponse=modifyDn(modifyDnRequest);
processResponse(modifyDnResponse);
}
/**
* Tests ModifyDN operation with newSuperior on referral entry with the ManageDsaIT control.
*/
@Test public void testNewSuperiorOnReferralWithManageDsaITControl() throws Exception {
LdapConnection conn=getWiredConnection(getLdapServer());
ManageDsaIT manageDSAIT=new ManageDsaITImpl();
manageDSAIT.setCritical(true);
ModifyDnRequest modifyDnRequest=new ModifyDnRequestImpl();
modifyDnRequest.setName(new Dn("uid=elecharny,ou=users,ou=system"));
modifyDnRequest.setNewRdn(new Rdn("uid=newuser"));
modifyDnRequest.setNewSuperior(new Dn("uid=akarasuluref,ou=users,ou=system"));
modifyDnRequest.setDeleteOldRdn(true);
modifyDnRequest.addControl(manageDSAIT);
try {
conn.modifyDn(modifyDnRequest);
}
catch ( LdapOperationException le) {
assertEquals(ResultCodeEnum.AFFECTS_MULTIPLE_DSAS,le.getResultCode());
}
conn.close();
}
/**
* Tests ModifyDN operation on normal and referral entries without the ManageDsaIT control. Referrals are sent back to the client with a non-success result code.
*/
@Test public void testOnReferral() throws Exception {
LdapConnection conn=getWiredConnection(getLdapServer());
ModifyDnRequest modifyDnRequest=new ModifyDnRequestImpl();
modifyDnRequest.setName(new Dn("uid=akarasuluref,ou=users,ou=system"));
modifyDnRequest.setNewRdn(new Rdn("uid=ref"));
modifyDnRequest.setDeleteOldRdn(true);
ModifyDnResponse modifyDnResponse=conn.modifyDn(modifyDnRequest);
assertEquals(ResultCodeEnum.REFERRAL,modifyDnResponse.getLdapResult().getResultCode());
assertTrue(modifyDnResponse.getLdapResult().getReferral().getLdapUrls().contains("ldap://localhost:10389/uid=akarasulu,ou=users,ou=system"));
assertTrue(modifyDnResponse.getLdapResult().getReferral().getLdapUrls().contains("ldap://foo:10389/uid=akarasulu,ou=users,ou=system"));
assertTrue(modifyDnResponse.getLdapResult().getReferral().getLdapUrls().contains("ldap://bar:10389/uid=akarasulu,ou=users,ou=system"));
conn.close();
}
/**
* {@inheritDoc}
*/
public void rename(Dn entryDn,Rdn newRdn,boolean deleteOldRdn) throws LdapException {
if (entryDn == null) {
String msg="Cannot process a rename of a null Dn";
LOG.debug(msg);
throw new IllegalArgumentException(msg);
}
if (newRdn == null) {
String msg="Cannot process a rename with a null Rdn";
LOG.debug(msg);
throw new IllegalArgumentException(msg);
}
ModifyDnRequest modifyDnRequest=new ModifyDnRequestImpl();
modifyDnRequest.setName(entryDn);
modifyDnRequest.setNewRdn(newRdn);
modifyDnRequest.setDeleteOldRdn(deleteOldRdn);
ModifyDnResponse modifyDnResponse=modifyDn(modifyDnRequest);
processResponse(modifyDnResponse);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment