LDAP-139: Added mirrored method in SimpleLdapTemplate that take Name parameters.

LDAP-140: Added bindmethod in LdapTemplate and SimpleLdapTemplate that take a DirContextOperations instance and
uses the information from that instance (DN and Attributes) to perform the bind.
This commit is contained in:
Mattias Arthursson
2008-10-23 09:14:06 +00:00
parent e22fa053a3
commit c416a4576f
8 changed files with 583 additions and 106 deletions

View File

@@ -17,6 +17,7 @@ package org.springframework.ldap.core.simple;
import java.util.List;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
@@ -55,6 +56,21 @@ public interface SimpleLdapOperations {
*/
<T> List<T> search(String base, String filter, ParameterizedContextMapper<T> mapper);
/**
* Search for a List of type T using the supplied filter and link
* ParametrizedContextMapper.
*
* @param base Base DN relative to the base of the ContextSource - where to
* start the search.
* @param filter Search filter.
* @param mapper the Mapper to supply all results to.
* @return a List of type T containing objects for all entries found, as
* mapped by the ParametrizedContextMapper.
* @throws NamingException if any error occurs.
* @since 1.3
*/
<T> List<T> search(Name base, String filter, ParameterizedContextMapper<T> mapper);
/**
* Search for a List of type T using the supplied filter, SearchControls,
* DirContextProcessor and ParametrizedContextMapper.
@@ -74,6 +90,26 @@ public interface SimpleLdapOperations {
<T> List<T> search(String base, String filter, SearchControls controls, ParameterizedContextMapper<T> mapper,
DirContextProcessor processor);
/**
* Search for a List of type T using the supplied filter, SearchControls,
* DirContextProcessor and ParametrizedContextMapper.
*
* @param base Base DN relative to the base of the ContextSource - where to
* start the search.
* @param filter Search filter.
* @param controls the SearchControls. Make sure that the returningObjFlag
* is set to <code>true</code>.
* @param mapper the Mapper to supply all results to.
* @param processor the DirContextProcessor to be used for applying pre/post
* processing on the DirContext instance.
* @return a List of type T containing objects for all entries found, as
* mapped by the ParametrizedContextMapper.
* @throws NamingException if any error occurs.
* @since 1.3
*/
<T> List<T> search(Name base, String filter, SearchControls controls, ParameterizedContextMapper<T> mapper,
DirContextProcessor processor);
/**
* Perform a lookup of the specified DN and map the result using the mapper.
*
@@ -84,6 +120,17 @@ public interface SimpleLdapOperations {
*/
<T> T lookup(String dn, ParameterizedContextMapper<T> mapper);
/**
* Perform a lookup of the specified DN and map the result using the mapper.
*
* @param dn the Distinguished Name to look up.
* @param mapper the mapper to use.
* @return the mapped object, as received by the ParametrizedContextMapper.
* @throws NamingException if any error occurs.
* @since 1.3
*/
<T> T lookup(Name dn, ParameterizedContextMapper<T> mapper);
/**
* Look up the specified DN, and automatically cast it to a
* {@link DirContextOperations} instance.
@@ -95,11 +142,23 @@ public interface SimpleLdapOperations {
*/
DirContextOperations lookupContext(String dn);
/**
* Look up the specified DN, and automatically cast it to a
* {@link DirContextOperations} instance.
*
* @param dn The Distinguished Name of the entry to look up.
* @return A {@link DirContextOperations} instance constructed from the
* found entry.
* @throws NamingException if any error occurs.
* @since 1.3
*/
DirContextOperations lookupContext(Name dn);
/**
* Create an entry in the LDAP tree. The attributes used to create the entry
* are either retrieved from the <code>obj</code> parameter or the
* <code>attributes</code> parameter (or both). One of these parameters
* may be null but not both.
* <code>attributes</code> parameter (or both). One of these parameters may
* be null but not both.
*
* @param dn The distinguished name to bind the object and attributes to.
* @param obj The object to bind, may be null. Typically a DirContext
@@ -109,6 +168,31 @@ public interface SimpleLdapOperations {
*/
void bind(String dn, Object obj, Attributes attributes);
/**
* Create an entry in the LDAP tree. The attributes used to create the entry
* are either retrieved from the <code>obj</code> parameter or the
* <code>attributes</code> parameter (or both). One of these parameters may
* be null but not both.
*
* @param dn The distinguished name to bind the object and attributes to.
* @param obj The object to bind, may be null. Typically a DirContext
* implementation.
* @param attributes The attributes to bind, may be null.
* @throws NamingException if any error occurs.
* @since 1.3
*/
void bind(Name dn, Object obj, Attributes attributes);
/**
* Bind the data in the supplied context in the tree. All specified
* Attributes in will be bound to the DN set on the instance.
*
* @param ctx the context to bind
* @throws IllegalStateException if no DN is set or if the instance is in
* update mode.
*/
void bind(DirContextOperations ctx);
/**
* Remove an entry from the LDAP tree. The entry must not have any children.
*
@@ -117,11 +201,20 @@ public interface SimpleLdapOperations {
*/
void unbind(String dn);
/**
* Remove an entry from the LDAP tree. The entry must not have any children.
*
* @param dn The distinguished name to unbind.
* @throws NamingException if any error occurs.
* @since 1.3
*/
void unbind(Name dn);
/**
* Modify the Attributes of the entry corresponding to the supplied
* {@link DirContextOperations} instance. The instance should have been
* received from the {@link #lookupContext(String)} operation, and then modified to
* match the current state of the matching domain object, e.g.:
* received from the {@link #lookupContext(String)} operation, and then
* modified to match the current state of the matching domain object, e.g.:
*
* <pre>
* public void update(Person person) {

View File

@@ -17,6 +17,7 @@ package org.springframework.ldap.core.simple;
import java.util.List;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import javax.naming.directory.SearchControls;
@@ -61,7 +62,10 @@ public class SimpleLdapTemplate implements SimpleLdapOperations {
/*
* (non-Javadoc)
* @see org.springframework.ldap.core.simple.SimpleLdapOperations#getLdapOperations()
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#getLdapOperations
* ()
*/
public LdapOperations getLdapOperations() {
return ldapOperations;
@@ -69,7 +73,10 @@ public class SimpleLdapTemplate implements SimpleLdapOperations {
/*
* (non-Javadoc)
* @see org.springframework.ldap.core.simple.SimpleLdapOperations#lookup(java.lang.String,
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#lookup(java
* .lang.String,
* org.springframework.ldap.core.simple.ParametrizedContextMapper)
*/
@SuppressWarnings("unchecked")
@@ -79,8 +86,10 @@ public class SimpleLdapTemplate implements SimpleLdapOperations {
/*
* (non-Javadoc)
* @see org.springframework.ldap.core.simple.SimpleLdapOperations#search(java.lang.String,
* java.lang.String,
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#search(java
* .lang.String, java.lang.String,
* org.springframework.ldap.core.simple.ParametrizedContextMapper)
*/
@SuppressWarnings("unchecked")
@@ -90,20 +99,25 @@ public class SimpleLdapTemplate implements SimpleLdapOperations {
/*
* (non-Javadoc)
* @see org.springframework.ldap.core.simple.SimpleLdapOperations#search(java.lang.String,
* java.lang.String, javax.naming.directory.SearchControls,
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#search(java
* .lang.String, java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.simple.ParametrizedContextMapper,
* org.springframework.ldap.core.DirContextProcessor)
*/
@SuppressWarnings("unchecked")
public <T> List<T> search(String base, String filter, SearchControls controls, ParameterizedContextMapper<T> mapper,
DirContextProcessor processor) {
public <T> List<T> search(String base, String filter, SearchControls controls,
ParameterizedContextMapper<T> mapper, DirContextProcessor processor) {
return ldapOperations.search(base, filter, controls, mapper, processor);
}
/*
* (non-Javadoc)
* @see org.springframework.ldap.core.simple.SimpleLdapOperations#lookup(java.lang.String)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#lookup(java
* .lang.String)
*/
public DirContextOperations lookupContext(String dn) {
return ldapOperations.lookupContext(dn);
@@ -111,7 +125,10 @@ public class SimpleLdapTemplate implements SimpleLdapOperations {
/*
* (non-Javadoc)
* @see org.springframework.ldap.core.simple.SimpleLdapOperations#modifyAttributes(org.springframework.ldap.core.DirContextOperations)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#modifyAttributes
* (org.springframework.ldap.core.DirContextOperations)
*/
public void modifyAttributes(DirContextOperations ctx) {
ldapOperations.modifyAttributes(ctx);
@@ -119,8 +136,10 @@ public class SimpleLdapTemplate implements SimpleLdapOperations {
/*
* (non-Javadoc)
* @see org.springframework.ldap.core.simple.SimpleLdapOperations#bind(java.lang.String,
* java.lang.Object, javax.naming.directory.Attributes)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#bind(java.lang
* .String, java.lang.Object, javax.naming.directory.Attributes)
*/
public void bind(String dn, Object obj, Attributes attributes) {
ldapOperations.bind(dn, obj, attributes);
@@ -128,10 +147,96 @@ public class SimpleLdapTemplate implements SimpleLdapOperations {
/*
* (non-Javadoc)
* @see org.springframework.ldap.core.simple.SimpleLdapOperations#unbind(java.lang.String)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#unbind(java
* .lang.String)
*/
public void unbind(String dn) {
ldapOperations.unbind(dn);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#bind(javax.
* naming.Name, java.lang.Object, javax.naming.directory.Attributes)
*/
public void bind(Name dn, Object obj, Attributes attributes) {
ldapOperations.bind(dn, obj, attributes);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#lookup(javax
* .naming.Name,
* org.springframework.ldap.core.simple.ParameterizedContextMapper)
*/
@SuppressWarnings("unchecked")
public <T> T lookup(Name dn, ParameterizedContextMapper<T> mapper) {
return (T) ldapOperations.lookup(dn, mapper);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#lookupContext
* (javax.naming.Name)
*/
public DirContextOperations lookupContext(Name dn) {
return ldapOperations.lookupContext(dn);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#search(javax
* .naming.Name, java.lang.String,
* org.springframework.ldap.core.simple.ParameterizedContextMapper)
*/
@SuppressWarnings("unchecked")
public <T> List<T> search(Name base, String filter, ParameterizedContextMapper<T> mapper) {
return ldapOperations.search(base, filter, mapper);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#search(javax
* .naming.Name, java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.simple.ParameterizedContextMapper,
* org.springframework.ldap.core.DirContextProcessor)
*/
@SuppressWarnings("unchecked")
public <T> List<T> search(Name base, String filter, SearchControls controls, ParameterizedContextMapper<T> mapper,
DirContextProcessor processor) {
return ldapOperations.search(base, filter, controls, mapper, processor);
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.ldap.core.simple.SimpleLdapOperations#unbind(javax
* .naming.Name)
*/
public void unbind(Name dn) {
ldapOperations.unbind(dn);
}
/*
* (non-Javadoc)
*
* @seeorg.springframework.ldap.core.simple.SimpleLdapOperations#bind(org.
* springframework.ldap.core.DirContextOperations)
*/
public void bind(DirContextOperations ctx) {
ldapOperations.bind(ctx);
}
}

View File

@@ -19,6 +19,7 @@ package org.springframework.ldap.core;
import java.util.SortedSet;
import javax.naming.Name;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
/**
@@ -213,4 +214,12 @@ public interface DirContextOperations extends DirContext, AttributeModifications
* @since 1.3
*/
boolean isReferral();
/**
* Get all the Attributes.
*
* @return all the Attributes.
* @since 1.3
*/
Attributes getAttributes();
}

View File

@@ -1572,4 +1572,23 @@ public interface LdapOperations {
*/
void modifyAttributes(DirContextOperations ctx)
throws IllegalStateException, NamingException;
/**
* Bind the data in the supplied context in the tree. All specified
* Attributes in will be bound to the DN set on the instance.
* <p>Example:<br>
* <pre>
* DirContextOperations ctx = new DirContextAdapter(dn);
* ctx.setAttributeValue("cn", "john doe");
* ctx.setAttributeValue("description", "some description");
* //More initialization here.
*
* ldapTemplate.bind(ctx);
* </pre>
* @param ctx the context to bind
* @throws IllegalStateException if no DN is set or if the instance is in
* update mode.
* @since 1.3
*/
void bind(DirContextOperations ctx);
}

View File

@@ -40,17 +40,16 @@ import org.springframework.ldap.support.LdapUtils;
* the user of the burden of looking up contexts, looping through
* NamingEnumerations and closing contexts.
* <p>
* <b>Note for Active Directory (AD) users:</b> AD servers are apparently
* unable to handle referrals automatically, which causes a
* <b>Note for Active Directory (AD) users:</b> AD servers are apparently unable
* to handle referrals automatically, which causes a
* <code>PartialResultException</code> to be thrown whenever a referral is
* encountered in a search. To avoid this, set the
* <code>ignorePartialResultException</code> property to <code>true</code>.
* There is currently no way of manually handling these referrals in the form of
* <code>ReferralException</code>, i.e. either you get the exception (and
* your results are lost) or all referrals are ignored (if the server is unable
* to handle them properly. Neither is there any simple way to get notified that
* a <code>PartialResultException</code> has been ignored (other than in the
* log).
* <code>ReferralException</code>, i.e. either you get the exception (and your
* results are lost) or all referrals are ignored (if the server is unable to
* handle them properly. Neither is there any simple way to get notified that a
* <code>PartialResultException</code> has been ignored (other than in the log).
*
* @see org.springframework.ldap.core.ContextSource
*
@@ -129,10 +128,10 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/**
* Specify whether <code>PartialResultException</code> should be ignored
* in searches. AD servers typically have a problem with referrals. Normally
* a referral should be followed automatically, but this does not seem to
* work with AD servers. The problem manifests itself with a a
* Specify whether <code>PartialResultException</code> should be ignored in
* searches. AD servers typically have a problem with referrals. Normally a
* referral should be followed automatically, but this does not seem to work
* with AD servers. The problem manifests itself with a a
* <code>PartialResultException</code> being thrown when a referral is
* encountered by the server. Setting this property to <code>true</code>
* presents a workaround to this problem by causing
@@ -149,7 +148,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, int, boolean,
* org.springframework.ldap.core.NameClassPairCallbackHandler)
*/
@@ -160,7 +160,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, int, boolean,
* org.springframework.ldap.core.NameClassPairCallbackHandler)
*/
@@ -171,7 +172,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.NameClassPairCallbackHandler)
*/
@@ -189,7 +191,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.NameClassPairCallbackHandler)
*/
@@ -207,7 +210,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.NameClassPairCallbackHandler,
* org.springframework.ldap.core.DirContextProcessor)
@@ -226,7 +230,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.NameClassPairCallbackHandler,
* org.springframework.ldap.core.DirContextProcessor)
@@ -261,8 +266,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
* @param handler the NameClassPairCallbackHandler to which each found entry
* will be passed.
* @param processor DirContextProcessor for custom pre- and post-processing.
* Must not be <code>null</code>. If no custom processing should take
* place, please use e.g.
* Must not be <code>null</code>. If no custom processing should take place,
* please use e.g.
* {@link #search(SearchExecutor, NameClassPairCallbackHandler)}.
* @throws NamingException if any error occurs. Note that a
* NameNotFoundException will be ignored. Instead this is interpreted that
@@ -347,7 +352,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String,
* org.springframework.ldap.core.NameClassPairCallbackHandler)
*/
@@ -361,7 +367,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String,
* org.springframework.ldap.core.NameClassPairCallbackHandler)
*/
@@ -375,7 +382,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, int, java.lang.String[],
* org.springframework.ldap.core.AttributesMapper)
*/
@@ -384,7 +392,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, int, java.lang.String[],
* org.springframework.ldap.core.AttributesMapper)
*/
@@ -393,7 +402,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, int, org.springframework.ldap.core.AttributesMapper)
*/
public List search(Name base, String filter, int searchScope, AttributesMapper mapper) {
@@ -402,7 +412,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, int, org.springframework.ldap.core.AttributesMapper)
*/
public List search(String base, String filter, int searchScope, AttributesMapper mapper) {
@@ -411,7 +422,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, org.springframework.ldap.core.AttributesMapper)
*/
public List search(Name base, String filter, AttributesMapper mapper) {
@@ -420,7 +432,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, org.springframework.ldap.core.AttributesMapper)
*/
public List search(String base, String filter, AttributesMapper mapper) {
@@ -429,7 +442,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, int, java.lang.String[],
* org.springframework.ldap.core.ContextMapper)
*/
@@ -439,7 +453,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, int, java.lang.String[],
* org.springframework.ldap.core.ContextMapper)
*/
@@ -449,7 +464,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, int, org.springframework.ldap.core.ContextMapper)
*/
public List search(Name base, String filter, int searchScope, ContextMapper mapper) {
@@ -458,7 +474,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, int, org.springframework.ldap.core.ContextMapper)
*/
public List search(String base, String filter, int searchScope, ContextMapper mapper) {
@@ -467,7 +484,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, org.springframework.ldap.core.ContextMapper)
*/
public List search(Name base, String filter, ContextMapper mapper) {
@@ -476,7 +494,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, org.springframework.ldap.core.ContextMapper)
*/
public List search(String base, String filter, ContextMapper mapper) {
@@ -485,7 +504,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.ContextMapper)
*/
@@ -495,7 +515,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.ContextMapper)
*/
@@ -505,7 +526,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.AttributesMapper)
*/
@@ -515,7 +537,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.AttributesMapper)
*/
@@ -526,7 +549,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.AttributesMapper,
* org.springframework.ldap.core.DirContextProcessor)
@@ -542,7 +566,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.AttributesMapper,
* org.springframework.ldap.core.DirContextProcessor)
@@ -558,7 +583,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#search(java.lang.String,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.ContextMapper,
* org.springframework.ldap.core.DirContextProcessor)
@@ -575,7 +601,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#search(javax.naming.Name,
* java.lang.String, javax.naming.directory.SearchControls,
* org.springframework.ldap.core.ContextMapper,
* org.springframework.ldap.core.DirContextProcessor)
@@ -652,8 +679,9 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#listBindings(java.lang.String,
* org.springframework.ldap.core.NameClassPairCallbackHandler)
* @see
* org.springframework.ldap.core.LdapOperations#listBindings(java.lang.String
* , org.springframework.ldap.core.NameClassPairCallbackHandler)
*/
public void listBindings(final String base, NameClassPairCallbackHandler handler) {
SearchExecutor searchExecutor = new SearchExecutor() {
@@ -666,8 +694,9 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#listBindings(javax.naming.Name,
* org.springframework.ldap.core.NameClassPairCallbackHandler)
* @see
* org.springframework.ldap.core.LdapOperations#listBindings(javax.naming
* .Name, org.springframework.ldap.core.NameClassPairCallbackHandler)
*/
public void listBindings(final Name base, NameClassPairCallbackHandler handler) {
SearchExecutor searchExecutor = new SearchExecutor() {
@@ -680,8 +709,9 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#listBindings(java.lang.String,
* org.springframework.ldap.core.NameClassPairMapper)
* @see
* org.springframework.ldap.core.LdapOperations#listBindings(java.lang.String
* , org.springframework.ldap.core.NameClassPairMapper)
*/
public List listBindings(String base, NameClassPairMapper mapper) {
CollectingNameClassPairCallbackHandler handler = new MappingCollectingNameClassPairCallbackHandler(mapper);
@@ -690,8 +720,9 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#listBindings(javax.naming.Name,
* org.springframework.ldap.core.NameClassPairMapper)
* @see
* org.springframework.ldap.core.LdapOperations#listBindings(javax.naming
* .Name, org.springframework.ldap.core.NameClassPairMapper)
*/
public List listBindings(Name base, NameClassPairMapper mapper) {
CollectingNameClassPairCallbackHandler handler = new MappingCollectingNameClassPairCallbackHandler(mapper);
@@ -700,22 +731,27 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#listBindings(java.lang.String)
* @see
* org.springframework.ldap.core.LdapOperations#listBindings(java.lang.String
* )
*/
public List listBindings(final String base) {
return listBindings(base, new DefaultNameClassPairMapper());
}
/*
* @see org.springframework.ldap.core.LdapOperations#listBindings(javax.naming.Name)
* @see
* org.springframework.ldap.core.LdapOperations#listBindings(javax.naming
* .Name)
*/
public List listBindings(final Name base) {
return listBindings(base, new DefaultNameClassPairMapper());
}
/*
* @see org.springframework.ldap.core.LdapOperations#listBindings(java.lang.String,
* org.springframework.ldap.core.ContextMapper)
* @see
* org.springframework.ldap.core.LdapOperations#listBindings(java.lang.String
* , org.springframework.ldap.core.ContextMapper)
*/
public List listBindings(String base, ContextMapper mapper) {
@@ -726,8 +762,9 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#listBindings(javax.naming.Name,
* org.springframework.ldap.core.ContextMapper)
* @see
* org.springframework.ldap.core.LdapOperations#listBindings(javax.naming
* .Name, org.springframework.ldap.core.ContextMapper)
*/
public List listBindings(Name base, ContextMapper mapper) {
@@ -738,7 +775,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#executeReadOnly(org.springframework.ldap.core.DirContextProcessor)
* @seeorg.springframework.ldap.core.LdapOperations#executeReadOnly(org.
* springframework.ldap.core.DirContextProcessor)
*/
public Object executeReadOnly(ContextExecutor ce) {
DirContext ctx = contextSource.getReadOnlyContext();
@@ -746,7 +784,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#executeReadWrite(org.springframework.ldap.core.DirContextProcessor)
* @seeorg.springframework.ldap.core.LdapOperations#executeReadWrite(org.
* springframework.ldap.core.DirContextProcessor)
*/
public Object executeReadWrite(ContextExecutor ce) {
DirContext ctx = contextSource.getReadWriteContext();
@@ -766,7 +805,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name)
* @see
* org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name)
*/
public Object lookup(final Name dn) {
return executeReadOnly(new ContextExecutor() {
@@ -777,7 +817,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(java.lang.String)
* @see
* org.springframework.ldap.core.LdapOperations#lookup(java.lang.String)
*/
public Object lookup(final String dn) {
return executeReadOnly(new ContextExecutor() {
@@ -788,7 +829,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name,
* org.springframework.ldap.core.AttributesMapper)
*/
public Object lookup(final Name dn, final AttributesMapper mapper) {
@@ -801,7 +843,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#lookup(java.lang.String,
* org.springframework.ldap.core.AttributesMapper)
*/
public Object lookup(final String dn, final AttributesMapper mapper) {
@@ -815,7 +858,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name,
* org.springframework.ldap.core.ContextMapper)
*/
public Object lookup(final Name dn, final ContextMapper mapper) {
@@ -828,7 +872,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#lookup(java.lang.String,
* org.springframework.ldap.core.ContextMapper)
*/
public Object lookup(final String dn, final ContextMapper mapper) {
@@ -842,7 +887,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name,
* java.lang.String[], org.springframework.ldap.core.AttributesMapper)
*/
public Object lookup(final Name dn, final String[] attributes, final AttributesMapper mapper) {
@@ -856,7 +902,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#lookup(java.lang.String,
* java.lang.String[], org.springframework.ldap.core.AttributesMapper)
*/
public Object lookup(final String dn, final String[] attributes, final AttributesMapper mapper) {
@@ -869,7 +916,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#lookup(javax.naming.Name,
* java.lang.String[], org.springframework.ldap.core.ContextMapper)
*/
public Object lookup(final Name dn, final String[] attributes, final ContextMapper mapper) {
@@ -884,7 +932,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#lookup(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#lookup(java.lang.String,
* java.lang.String[], org.springframework.ldap.core.ContextMapper)
*/
public Object lookup(final String dn, final String[] attributes, final ContextMapper mapper) {
@@ -900,8 +949,9 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#modifyAttributes(javax.naming.Name,
* javax.naming.directory.ModificationItem[])
* @see
* org.springframework.ldap.core.LdapOperations#modifyAttributes(javax.naming
* .Name, javax.naming.directory.ModificationItem[])
*/
public void modifyAttributes(final Name dn, final ModificationItem[] mods) {
executeReadWrite(new ContextExecutor() {
@@ -913,8 +963,9 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#modifyAttributes(java.lang.String,
* javax.naming.directory.ModificationItem[])
* @see
* org.springframework.ldap.core.LdapOperations#modifyAttributes(java.lang
* .String, javax.naming.directory.ModificationItem[])
*/
public void modifyAttributes(final String dn, final ModificationItem[] mods) {
@@ -955,21 +1006,24 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#unbind(javax.naming.Name)
* @see
* org.springframework.ldap.core.LdapOperations#unbind(javax.naming.Name)
*/
public void unbind(final Name dn) {
doUnbind(dn);
}
/*
* @see org.springframework.ldap.core.LdapOperations#unbind(java.lang.String)
* @see
* org.springframework.ldap.core.LdapOperations#unbind(java.lang.String)
*/
public void unbind(final String dn) {
doUnbind(dn);
}
/*
* @see org.springframework.ldap.core.LdapOperations#unbind(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#unbind(javax.naming.Name,
* boolean)
*/
public void unbind(final Name dn, boolean recursive) {
@@ -982,7 +1036,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#unbind(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#unbind(java.lang.String,
* boolean)
*/
public void unbind(final String dn, boolean recursive) {
@@ -1067,7 +1122,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#rebind(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#rebind(javax.naming.Name,
* java.lang.Object, javax.naming.directory.Attributes)
*/
public void rebind(final Name dn, final Object obj, final Attributes attributes) {
@@ -1081,7 +1137,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#rebind(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#rebind(java.lang.String,
* java.lang.Object, javax.naming.directory.Attributes)
*/
public void rebind(final String dn, final Object obj, final Attributes attributes) {
@@ -1095,7 +1152,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#rename(javax.naming.Name,
* @see
* org.springframework.ldap.core.LdapOperations#rename(javax.naming.Name,
* javax.naming.Name)
*/
public void rename(final Name oldDn, final Name newDn) {
@@ -1109,7 +1167,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.core.LdapOperations#rename(java.lang.String,
* @see
* org.springframework.ldap.core.LdapOperations#rename(java.lang.String,
* java.lang.String)
*/
public void rename(final String oldDn, final String newDn) {
@@ -1123,7 +1182,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
* @see
* org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
public void afterPropertiesSet() throws Exception {
if (contextSource == null) {
@@ -1220,7 +1280,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
}
/*
* @see org.springframework.ldap.CollectingNameClassPairCallbackHandler#getObjectFromNameClassPair(javax.naming.NameClassPair)
* @seeorg.springframework.ldap.CollectingNameClassPairCallbackHandler#
* getObjectFromNameClassPair(javax.naming.NameClassPair)
*/
public Object getObjectFromNameClassPair(NameClassPair nameClassPair) {
try {
@@ -1235,7 +1296,9 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.core.LdapOperations#lookupContext(javax.naming.Name)
* @see
* org.springframework.ldap.core.LdapOperations#lookupContext(javax.naming
* .Name)
*/
public DirContextOperations lookupContext(Name dn) {
return (DirContextOperations) lookup(dn);
@@ -1244,7 +1307,9 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.core.LdapOperations#lookupContext(java.lang.String)
* @see
* org.springframework.ldap.core.LdapOperations#lookupContext(java.lang.
* String)
*/
public DirContextOperations lookupContext(String dn) {
return (DirContextOperations) lookup(dn);
@@ -1253,7 +1318,8 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
/*
* (non-Javadoc)
*
* @see org.springframework.ldap.core.LdapOperations#modifyAttributes(org.springframework.ldap.core.DirContextOperations)
* @seeorg.springframework.ldap.core.LdapOperations#modifyAttributes(org.
* springframework.ldap.core.DirContextOperations)
*/
public void modifyAttributes(DirContextOperations ctx) {
Name dn = ctx.getDn();
@@ -1263,6 +1329,21 @@ public class LdapTemplate implements LdapOperations, InitializingBean {
else {
throw new IllegalStateException("The DirContextOperations instance needs to be properly initialized.");
}
}
/*
* (non-Javadoc)
*
* @seeorg.springframework.ldap.core.simple.SimpleLdapOperations#bind(org.
* springframework.ldap.core.DirContextOperations)
*/
public void bind(DirContextOperations ctx) {
Name dn = ctx.getDn();
if (dn != null && !ctx.isUpdateMode()) {
bind(dn, ctx, null);
}
else {
throw new IllegalStateException("The DirContextOperations instance needs to be properly initialized.");
}
}
}

View File

@@ -136,7 +136,7 @@ public class PersonDaoImpl implements PersonDao {
context.setAttributeValue("sn", p.getLastname());
context.setAttributeValue("description", p.getDescription());</emphasis>
ldapTemplate.bind(dn, context, null);
ldapTemplate.bind(context);
}
}</programlisting>
</example>
@@ -210,7 +210,7 @@ public class PersonDaoImpl implements PersonDao {
Name dn = buildDn(p);
DirContextAdapter context = new DirContextAdapter(dn);
mapToContext(p, context);
ldapTemplate.bind(dn, context, null);
ldapTemplate.bind(context);
}
public void update(Person p) {
@@ -265,9 +265,9 @@ public class PersonDaoImpl implements PersonDao {
}
public void create(Person person) {
DirContextAdapter context = new DirContextAdapter();
DirContextAdapter context = new DirContextAdapter(buildDn(person));
mapToContext(person, context);
ldapTemplate.bind(buildDn(person), context, null);
ldapTemplate.bind(context);
}
public void update(Person person) {

View File

@@ -93,6 +93,19 @@ public class LdapTemplateBindUnbindITest extends AbstractLdapTemplateIntegration
verifyCleanup();
}
@Test
public void testBindAndUnbindPlainDirContextAdapter() {
DirContextAdapter adapter = new DirContextAdapter(new DistinguishedName(DN));
adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
adapter.setAttributeValue("cn", "Some Person4");
adapter.setAttributeValue("sn", "Person4");
tested.bind(adapter);
verifyBoundCorrectData();
tested.unbind(DN);
verifyCleanup();
}
private Attributes setupAttributes() {
Attributes attributes = new BasicAttributes();
BasicAttribute ocattr = new BasicAttribute("objectclass");

View File

@@ -16,18 +16,30 @@
package org.springframework.ldap.core.simple;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.AbstractLdapTemplateIntegrationTest;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.ldap.core.DirContextAdapter;
import org.springframework.ldap.core.DirContextOperations;
import org.springframework.ldap.core.DirContextProcessor;
import org.springframework.ldap.core.DistinguishedName;
import org.springframework.test.context.ContextConfiguration;
@ContextConfiguration(locations = { "/conf/simpleLdapTemplateTestContext.xml" })
public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest {
private static String DN_STRING = "cn=Some Person4,ou=company1,c=Sweden";
private static DistinguishedName DN = new DistinguishedName("cn=Some Person4,ou=company1,c=Sweden");
@Autowired
private SimpleLdapTemplate ldapTemplate;
@@ -38,6 +50,13 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
assertEquals("Some Person", result);
}
@Test
public void testLookupName() {
String result = ldapTemplate.lookup(new DistinguishedName("cn=Some Person,ou=company1,c=Sweden"),
new CnContextMapper());
assertEquals("Some Person", result);
}
@Test
public void testSearch() {
List<String> cns = ldapTemplate.search("", "(&(objectclass=person)(sn=Person3))", new CnContextMapper());
@@ -46,6 +65,45 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
assertEquals("Some Person3", cns.get(0));
}
@Test
public void testSearchProcessor() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
DummyDirContextProcessor processor = new DummyDirContextProcessor();
List<String> cns = ldapTemplate.search("", "(&(objectclass=person)(sn=Person3))", searchControls,
new CnContextMapper(), processor);
assertEquals(1, cns.size());
assertEquals("Some Person3", cns.get(0));
assertTrue(processor.isPreProcessCalled());
assertTrue(processor.isPostProcessCalled());
}
@Test
public void testSearchProcessorName() {
SearchControls searchControls = new SearchControls();
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
DummyDirContextProcessor processor = new DummyDirContextProcessor();
List<String> cns = ldapTemplate.search(DistinguishedName.EMPTY_PATH, "(&(objectclass=person)(sn=Person3))",
searchControls, new CnContextMapper(), processor);
assertEquals(1, cns.size());
assertEquals("Some Person3", cns.get(0));
assertTrue(processor.isPreProcessCalled());
assertTrue(processor.isPostProcessCalled());
}
@Test
public void testSearchName() {
List<String> cns = ldapTemplate.search(DistinguishedName.EMPTY_PATH, "(&(objectclass=person)(sn=Person3))",
new CnContextMapper());
assertEquals(1, cns.size());
assertEquals("Some Person3", cns.get(0));
}
@Test
public void testModifyAttributes() {
DirContextOperations ctx = ldapTemplate.lookupContext("cn=Some Person,ou=company1,c=Sweden");
@@ -66,7 +124,83 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
});
}
private static class CnContextMapper implements ParameterizedContextMapper<String> {
@Test
public void testModifyAttributesName() {
DirContextOperations ctx = ldapTemplate.lookupContext(new DistinguishedName(
"cn=Some Person,ou=company1,c=Sweden"));
ctx.setAttributeValue("description", "updated description");
ctx.setAttributeValue("telephoneNumber", "0000001");
ldapTemplate.modifyAttributes(ctx);
// verify that the data was properly updated.
ldapTemplate.lookup("cn=Some Person,ou=company1,c=Sweden", new ParameterizedContextMapper<Object>() {
public Object mapFromContext(Object ctx) {
DirContextAdapter adapter = (DirContextAdapter) ctx;
assertEquals("updated description", adapter.getStringAttribute("description"));
assertEquals("0000001", adapter.getStringAttribute("telephoneNumber"));
return null;
}
});
}
@Test
public void testBindAndUnbind() {
DirContextAdapter adapter = new DirContextAdapter();
adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
adapter.setAttributeValue("cn", "Some Person4");
adapter.setAttributeValue("sn", "Person4");
ldapTemplate.bind(DN_STRING, adapter, null);
verifyBoundCorrectData();
ldapTemplate.unbind(DN_STRING);
verifyCleanup();
}
@Test
public void testBindAndUnbindName() {
DirContextAdapter adapter = new DirContextAdapter();
adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
adapter.setAttributeValue("cn", "Some Person4");
adapter.setAttributeValue("sn", "Person4");
ldapTemplate.bind(DN, adapter, null);
verifyBoundCorrectData();
ldapTemplate.unbind(DN);
verifyCleanup();
}
@Test
public void testBindAndUnbindWithDirContextAdapter() {
DirContextAdapter adapter = new DirContextAdapter(DN);
adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
adapter.setAttributeValue("cn", "Some Person4");
adapter.setAttributeValue("sn", "Person4");
ldapTemplate.bind(adapter);
verifyBoundCorrectData();
ldapTemplate.unbind(DN);
verifyCleanup();
}
private void verifyBoundCorrectData() {
DirContextOperations result = ldapTemplate.lookupContext(DN_STRING);
assertEquals("Some Person4", result.getStringAttribute("cn"));
assertEquals("Person4", result.getStringAttribute("sn"));
}
private void verifyCleanup() {
try {
ldapTemplate.lookupContext(DN_STRING);
fail("NameNotFoundException expected");
}
catch (NameNotFoundException expected) {
assertTrue(true);
}
}
private static final class CnContextMapper implements ParameterizedContextMapper<String> {
public String mapFromContext(Object ctx) {
DirContextAdapter adapter = (DirContextAdapter) ctx;
@@ -75,4 +209,27 @@ public class SimpleLdapTemplateITest extends AbstractLdapTemplateIntegrationTest
}
}
private static final class DummyDirContextProcessor implements DirContextProcessor {
private boolean preProcessCalled;
private boolean postProcessCalled;
public boolean isPreProcessCalled() {
return preProcessCalled;
}
public boolean isPostProcessCalled() {
return postProcessCalled;
}
public void postProcess(DirContext ctx) throws NamingException {
preProcessCalled = true;
}
public void preProcess(DirContext ctx) throws NamingException {
postProcessCalled = true;
}
}
}