Class DirContextAdapter
- All Implemented Interfaces:
Context,DirContext,AttributeModificationsAware,DirContextOperations,LdapDataEntry
DefaultDirObjectFactory in your
ContextSource (which is the default) you will receive instances of this
class from searches and lookups. This can be particularly useful when updating data,
since this class implements AttributeModificationsAware, providing a
getModificationItems() method. When in update mode, an object of this class
keeps track of the changes made to its attributes, making them available as an array of
ModificationItem objects, suitable as input to
LdapTemplate.modifyAttributes(DirContextOperations).
This class is aware of the specifics of Name instances with regards to equality
when working with attribute values. This comes in very handy when working with e.g.
security groups and modifications of them. If Name instances are supplied to
one of the Attribute manipulation methods (e.g.
addAttributeValue(String, Object),
removeAttributeValue(String, Object),
setAttributeValue(String, Object), or
setAttributeValues(String, Object[])), the produced modifications will be
calculated using Name equality. This means that if an the member
has a value of "cn=John Doe,ou=People", and we call
addAttributeValue("member", LdapUtils.newLdapName("CN=John Doe,OU=People"),
this will not be considered a modification since the two DN strings
represent the same distinguished name (case and spacing between attributes is
disregarded).
Note that this is not a complete implementation of DirContext. Several methods are not relevant for the intended usage of this class, so they throw UnsupportOperationException.
-
Field Summary
Fields inherited from interface javax.naming.Context
APPLET, AUTHORITATIVE, BATCHSIZE, DNS_URL, INITIAL_CONTEXT_FACTORY, LANGUAGE, OBJECT_FACTORIES, PROVIDER_URL, REFERRAL, SECURITY_AUTHENTICATION, SECURITY_CREDENTIALS, SECURITY_PRINCIPAL, SECURITY_PROTOCOL, STATE_FACTORIES, URL_PKG_PREFIXESFields inherited from interface javax.naming.directory.DirContext
ADD_ATTRIBUTE, REMOVE_ATTRIBUTE, REPLACE_ATTRIBUTE -
Constructor Summary
ConstructorsModifierConstructorDescriptionDefault constructor.DirContextAdapter(String dnString) Create a new DirContextAdapter from the supplied DN String.DirContextAdapter(Attributes attrs, Name dn) Create a new adapter from the supplied attributes and dn.DirContextAdapter(Attributes attrs, Name dn, Name base) Create a new adapter from the supplied attributes, dn, and base.DirContextAdapter(Attributes attrs, Name dn, Name base, String referralUrl) Create a new adapter from the supplied attributes, dn, base, and referral url.Create a new adapter from the supplied dn.protectedConstructor for cloning an existing adapter. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAttributeValue(String name, Object value) Add a value to the Attribute with the specified name.voidaddAttributeValue(String name, Object value, boolean addIfDuplicateExists) Add a value to the Attribute with the specified name.addToEnvironment(String propName, Object propVal) booleanattributeExists(String name) Check if an Object attribute exists, regardless of whether it has a value or not.voidvoidbind(String name, Object obj, Attributes attrs) voidvoidbind(Name name, Object obj, Attributes attrs) voidclose()composeName(String name, String prefix) composeName(Name name, Name prefix) createSubcontext(String name) createSubcontext(String name, Attributes attrs) createSubcontext(Name name) createSubcontext(Name name, Attributes attrs) voiddestroySubcontext(String name) voiddestroySubcontext(Name name) booleanprotected final booleanChecks if the attribute exists in this entry, either it was read or it has been added and update() has been called.protected final booleanChecks if an entry has a specific attribute.Get all attributes.getAttributes(String name) getAttributes(String name, String[] attrIds) getAttributes(Name name) getAttributes(Name name, String[] attrIds) Get all String values of the attribute as aSortedSet.getDn()Returns the DN relative to the base path.Hashtable<?,?> Creates an array of which attributes have been changed, added or removed since the initialization of this object.getNameParser(String name) getNameParser(Name name) String[]Creates a String array of the names of the attributes which have been changed.getObjectAttribute(String name) Get the value of an Object attribute.Object[]getObjectAttributes(String name) Get all values of an Object attribute.If this instance results from a referral, this method returns the url of the referred server.getSchemaClassDefinition(Name name) getStringAttribute(String name) Get the value of a String attribute.String[]getStringAttributes(String name) Get all values of a String attribute.inthashCode()booleanChecks whether this instance results from a referral.booleanGets the update mode.listBindings(String name) listBindings(Name name) lookupLink(String name) lookupLink(Name name) voidmodifyAttributes(String name, int modOp, Attributes attrs) voidmodifyAttributes(String name, ModificationItem[] mods) voidmodifyAttributes(Name name, int modOp, Attributes attrs) voidmodifyAttributes(Name name, ModificationItem[] mods) voidvoidrebind(String name, Object obj, Attributes attrs) voidvoidrebind(Name name, Object obj, Attributes attrs) voidremoveAttributeValue(String name, Object value) Remove a value from the Attribute with the specified name.removeFromEnvironment(String propName) voidvoidsearch(String name, String filterExpr, Object[] filterArgs, SearchControls cons) search(String name, String filter, SearchControls cons) search(String name, Attributes matchingAttributes) search(String name, Attributes matchingAttributes, String[] attributesToReturn) search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) search(Name name, String filter, SearchControls cons) search(Name name, Attributes matchingAttributes) search(Name name, Attributes matchingAttributes, String[] attributesToReturn) voidsetAttribute(Attribute attribute) Set the supplied attribute.voidsetAttributeValue(String name, Object value) Set the with the namenameto thevalue.voidsetAttributeValues(String name, Object[] values) Sets a multivalue attribute, disregarding the order of the values.voidsetAttributeValues(String name, Object[] values, boolean orderMatters) Sets a multivalue attribute.final voidSet the dn of this entry.voidsetUpdateMode(boolean mode) Sets the update mode.toString()voidvoidvoidupdate()Update the attributes.This will mean that the getters (getStringAttributemethods) will return the updated values, and the modifications will be forgotten (i.e.
-
Constructor Details
-
DirContextAdapter
public DirContextAdapter()Default constructor. -
DirContextAdapter
Create a new DirContextAdapter from the supplied DN String.- Parameters:
dnString- the DN string. Must be syntactically correct, or an exception will be thrown.
-
DirContextAdapter
Create a new adapter from the supplied dn.- Parameters:
dn- the dn.
-
DirContextAdapter
Create a new adapter from the supplied attributes and dn.- Parameters:
attrs- the attributes.dn- the dn.
-
DirContextAdapter
Create a new adapter from the supplied attributes, dn, and base.- Parameters:
attrs- the attributes.dn- the dn.base- the base name.
-
DirContextAdapter
Create a new adapter from the supplied attributes, dn, base, and referral url.- Parameters:
attrs- the attributes.dn- the dn.base- the base.referralUrl- the referral url (if this instance results from a referral).
-
DirContextAdapter
Constructor for cloning an existing adapter.- Parameters:
main- The adapter to be copied.
-
-
Method Details
-
setUpdateMode
public void setUpdateMode(boolean mode) Sets the update mode. The update mode should befalsefor a new entry andtruefor an existing entry that is being updated.- Parameters:
mode- Update mode.
-
isUpdateMode
public boolean isUpdateMode()Gets the update mode. An entry in update mode will keep track of its modifications so that they can be retrieved usingAttributeModificationsAware.getModificationItems(). The update mode should betruefor a new entry andtruefor an existing entry that is being updated.- Specified by:
isUpdateModein interfaceDirContextOperations- Returns:
- update mode.
-
getNamesOfModifiedAttributes
Creates a String array of the names of the attributes which have been changed. If this is a new entry, all set entries will be in the list. If this is an updated entry, only changed and removed entries will be in the array.- Specified by:
getNamesOfModifiedAttributesin interfaceDirContextOperations- Returns:
- Array of String
-
getModificationItems
Creates an array of which attributes have been changed, added or removed since the initialization of this object.- Specified by:
getModificationItemsin interfaceAttributeModificationsAware- Returns:
- an array of modification items.
-
exists
Checks if an entry has a specific attribute. This method simply calls exists(String) with the attribute name.- Parameters:
attr- the attribute to check.- Returns:
- true if attribute exists in entry.
-
exists
Checks if the attribute exists in this entry, either it was read or it has been added and update() has been called.- Parameters:
attrId- id of the attribute to check.- Returns:
- true if the attribute exists in the entry.
-
getStringAttribute
Get the value of a String attribute. If more than one attribute value exists for the specified attribute, only the first one will be returned. If an attribute has no value,nullwill be returned.- Specified by:
getStringAttributein interfaceLdapDataEntry- Parameters:
name- name of the attribute.- Returns:
- the value of the attribute if it exists, or
nullif the attribute doesn't exist or if it exists but with no value.
-
getObjectAttribute
Get the value of an Object attribute. If more than one attribute value exists for the specified attribute, only the first one will be returned. If an attribute has no value,nullwill be returned.- Specified by:
getObjectAttributein interfaceLdapDataEntry- Parameters:
name- name of the attribute.- Returns:
- the attribute value as an object if it exists, or
nullif the attribute doesn't exist or if it exists but with no value.
-
attributeExists
Check if an Object attribute exists, regardless of whether it has a value or not.- Specified by:
attributeExistsin interfaceLdapDataEntry- Parameters:
name- name of the attribute- Returns:
trueif the attribute exists,falseotherwise
-
setAttributeValue
Set the with the namenameto thevalue. If the value is aNameinstance, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
setAttributeValuein interfaceLdapDataEntry- Parameters:
name- name of the attribute.value- value to set the attribute to.
-
addAttributeValue
Add a value to the Attribute with the specified name. If the Attribute doesn't exist it will be created. This method makes sure that the there will be no duplicates of an added value - it the value exists it will not be added again. If the value is aNameinstance, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
addAttributeValuein interfaceLdapDataEntry- Parameters:
name- the name of the Attribute to which the specified value should be added.value- the Attribute value to add.
-
addAttributeValue
Add a value to the Attribute with the specified name. If the Attribute doesn't exist it will be created. TheaddIfDuplicateExistsparameter controls the handling of duplicates. Itfalse, this method makes sure that the there will be no duplicates of an added value - it the value exists it will not be added again. If the value is aNameinstance, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
addAttributeValuein interfaceLdapDataEntry- Parameters:
name- the name of the Attribute to which the specified value should be added.value- the Attribute value to add.addIfDuplicateExists-truewill add the value regardless of whether there is an identical value already, allowing for duplicate attribute values;falsewill not add the value if it already exists.
-
removeAttributeValue
Remove a value from the Attribute with the specified name. If the Attribute doesn't exist, do nothing. If the value is aNameinstance, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
removeAttributeValuein interfaceLdapDataEntry- Parameters:
name- the name of the Attribute from which the specified value should be removed.value- the value to remove.
-
setAttributeValues
Sets a multivalue attribute, disregarding the order of the values. If value is null or value.length == 0 then the attribute will be removed. If update mode, changes will be made only if the array has more or less objects or if one or more object has changed. Reordering the objects will not cause an update. If the values areNameinstances, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
setAttributeValuesin interfaceLdapDataEntry- Parameters:
name- The id of the attribute.values- Attribute values.
-
setAttributeValues
Sets a multivalue attribute. If value is null or value.length == 0 then the attribute will be removed. If update mode, changes will be made if the array has more or less objects or if one or more string has changed. Reordering the objects will only cause an update if orderMatters is set to true. If the values areNameinstances, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
setAttributeValuesin interfaceLdapDataEntry- Parameters:
name- The id of the attribute.values- Attribute values.orderMatters- Iftrue, it will be changed even if data was just reordered.
-
update
public void update()Update the attributes.This will mean that the getters (getStringAttributemethods) will return the updated values, and the modifications will be forgotten (i.e.AttributeModificationsAware.getModificationItems()will return an empty array.- Specified by:
updatein interfaceDirContextOperations
-
getStringAttributes
Get all values of a String attribute.- Specified by:
getStringAttributesin interfaceLdapDataEntry- Parameters:
name- name of the attribute.- Returns:
- a (possibly empty) array containing all registered values of the attribute
as Strings if the attribute is defined or
nullotherwise.
-
getObjectAttributes
Get all values of an Object attribute.- Specified by:
getObjectAttributesin interfaceLdapDataEntry- Parameters:
name- name of the attribute.- Returns:
- a (possibly empty) array containing all registered values of the attribute
if the attribute is defined or
nullotherwise.
-
getAttributeSortedStringSet
Get all String values of the attribute as aSortedSet.- Specified by:
getAttributeSortedStringSetin interfaceLdapDataEntry- Parameters:
name- name of the attribute.- Returns:
- a
SortedSetcontaining all values of the attribute, ornullif the attribute does not exist.
-
setAttribute
Set the supplied attribute.- Parameters:
attribute- the attribute to set.
-
getAttributes
Get all attributes.- Specified by:
getAttributesin interfaceLdapDataEntry- Returns:
- all attributes.
-
getAttributes
- Specified by:
getAttributesin interfaceDirContext- Throws:
NamingException
-
getAttributes
- Specified by:
getAttributesin interfaceDirContext- Throws:
NamingException
-
getAttributes
- Specified by:
getAttributesin interfaceDirContext- Throws:
NamingException
-
getAttributes
- Specified by:
getAttributesin interfaceDirContext- Throws:
NamingException
-
modifyAttributes
- Specified by:
modifyAttributesin interfaceDirContext- Throws:
NamingException
-
modifyAttributes
- Specified by:
modifyAttributesin interfaceDirContext- Throws:
NamingException
-
modifyAttributes
- Specified by:
modifyAttributesin interfaceDirContext- Throws:
NamingException
-
modifyAttributes
- Specified by:
modifyAttributesin interfaceDirContext- Throws:
NamingException
-
bind
- Specified by:
bindin interfaceDirContext- Throws:
NamingException
-
bind
- Specified by:
bindin interfaceDirContext- Throws:
NamingException
-
rebind
- Specified by:
rebindin interfaceDirContext- Throws:
NamingException
-
rebind
- Specified by:
rebindin interfaceDirContext- Throws:
NamingException
-
createSubcontext
- Specified by:
createSubcontextin interfaceDirContext- Throws:
NamingException
-
createSubcontext
- Specified by:
createSubcontextin interfaceDirContext- Throws:
NamingException
-
getSchema
- Specified by:
getSchemain interfaceDirContext- Throws:
NamingException
-
getSchema
- Specified by:
getSchemain interfaceDirContext- Throws:
NamingException
-
getSchemaClassDefinition
- Specified by:
getSchemaClassDefinitionin interfaceDirContext- Throws:
NamingException
-
getSchemaClassDefinition
- Specified by:
getSchemaClassDefinitionin interfaceDirContext- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException - Specified by:
searchin interfaceDirContext- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException - Specified by:
searchin interfaceDirContext- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes) throws NamingException - Specified by:
searchin interfaceDirContext- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes) throws NamingException - Specified by:
searchin interfaceDirContext- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(Name name, String filter, SearchControls cons) throws NamingException - Specified by:
searchin interfaceDirContext- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(String name, String filter, SearchControls cons) throws NamingException - Specified by:
searchin interfaceDirContext- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException - Specified by:
searchin interfaceDirContext- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(String name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException - Specified by:
searchin interfaceDirContext- Throws:
NamingException
-
lookup
- Specified by:
lookupin interfaceContext- Throws:
NamingException
-
lookup
- Specified by:
lookupin interfaceContext- Throws:
NamingException
-
bind
- Specified by:
bindin interfaceContext- Throws:
NamingException
-
bind
- Specified by:
bindin interfaceContext- Throws:
NamingException
-
rebind
- Specified by:
rebindin interfaceContext- Throws:
NamingException
-
rebind
- Specified by:
rebindin interfaceContext- Throws:
NamingException
-
unbind
- Specified by:
unbindin interfaceContext- Throws:
NamingException
-
unbind
- Specified by:
unbindin interfaceContext- Throws:
NamingException
-
rename
- Specified by:
renamein interfaceContext- Throws:
NamingException
-
rename
- Specified by:
renamein interfaceContext- Throws:
NamingException
-
list
- Specified by:
listin interfaceContext- Throws:
NamingException
-
list
- Specified by:
listin interfaceContext- Throws:
NamingException
-
listBindings
- Specified by:
listBindingsin interfaceContext- Throws:
NamingException
-
listBindings
- Specified by:
listBindingsin interfaceContext- Throws:
NamingException
-
destroySubcontext
- Specified by:
destroySubcontextin interfaceContext- Throws:
NamingException
-
destroySubcontext
- Specified by:
destroySubcontextin interfaceContext- Throws:
NamingException
-
createSubcontext
- Specified by:
createSubcontextin interfaceContext- Throws:
NamingException
-
createSubcontext
- Specified by:
createSubcontextin interfaceContext- Throws:
NamingException
-
lookupLink
- Specified by:
lookupLinkin interfaceContext- Throws:
NamingException
-
lookupLink
- Specified by:
lookupLinkin interfaceContext- Throws:
NamingException
-
getNameParser
- Specified by:
getNameParserin interfaceContext- Throws:
NamingException
-
getNameParser
- Specified by:
getNameParserin interfaceContext- Throws:
NamingException
-
composeName
- Specified by:
composeNamein interfaceContext- Throws:
NamingException
-
composeName
- Specified by:
composeNamein interfaceContext- Throws:
NamingException
-
addToEnvironment
- Specified by:
addToEnvironmentin interfaceContext- Throws:
NamingException
-
removeFromEnvironment
- Specified by:
removeFromEnvironmentin interfaceContext- Throws:
NamingException
-
getEnvironment
- Specified by:
getEnvironmentin interfaceContext- Throws:
NamingException
-
close
- Specified by:
closein interfaceContext- Throws:
NamingException
-
getNameInNamespace
- Specified by:
getNameInNamespacein interfaceContext- Specified by:
getNameInNamespacein interfaceDirContextOperations
-
getDn
Returns the DN relative to the base path. NB: as of version 2.0 the returned name will be an LdapName instance.- Specified by:
getDnin interfaceLdapDataEntry- Returns:
- The distinguished name of the current context.
- See Also:
-
setDn
Set the dn of this entry.- Specified by:
setDnin interfaceDirContextOperations- Parameters:
dn- the dn.
-
equals
-
hashCode
public int hashCode() -
toString
-
getReferralUrl
If this instance results from a referral, this method returns the url of the referred server.- Specified by:
getReferralUrlin interfaceDirContextOperations- Returns:
- The url of the referred server, e.g.
ldap://localhost:389, or the empty string if this is not a referral.
-
isReferral
public boolean isReferral()Checks whether this instance results from a referral.- Specified by:
isReferralin interfaceDirContextOperations- Returns:
trueif this instance results from a referral,falseotherwise.
-