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_PREFIXES
Fields inherited from interface javax.naming.directory.DirContext
ADD_ATTRIBUTE, REMOVE_ATTRIBUTE, REPLACE_ATTRIBUTE
-
Constructor Summary
ModifierConstructorDescriptionDefault 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.protected
Constructor for cloning an existing adapter. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addAttributeValue
(String name, Object value) Add a value to the Attribute with the specified name.void
addAttributeValue
(String name, Object value, boolean addIfDuplicateExists) Add a value to the Attribute with the specified name.addToEnvironment
(String propName, Object propVal) boolean
attributeExists
(String name) Check if an Object attribute exists, regardless of whether it has a value or not.void
void
bind
(String name, Object obj, Attributes attrs) void
void
bind
(Name name, Object obj, Attributes attrs) void
close()
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) void
destroySubcontext
(String name) void
destroySubcontext
(Name name) boolean
protected final boolean
Checks if the attribute exists in this entry, either it was read or it has been added and update() has been called.protected final boolean
Checks 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.int
hashCode()
boolean
Checks whether this instance results from a referral.boolean
Gets the update mode.listBindings
(String name) listBindings
(Name name) lookupLink
(String name) lookupLink
(Name name) void
modifyAttributes
(String name, int modOp, Attributes attrs) void
modifyAttributes
(String name, ModificationItem[] mods) void
modifyAttributes
(Name name, int modOp, Attributes attrs) void
modifyAttributes
(Name name, ModificationItem[] mods) void
void
rebind
(String name, Object obj, Attributes attrs) void
void
rebind
(Name name, Object obj, Attributes attrs) void
removeAttributeValue
(String name, Object value) Remove a value from the Attribute with the specified name.removeFromEnvironment
(String propName) void
void
search
(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) void
setAttribute
(Attribute attribute) Set the supplied attribute.void
setAttributeValue
(String name, Object value) Set the with the namename
to thevalue
.void
setAttributeValues
(String name, Object[] values) Sets a multivalue attribute, disregarding the order of the values.void
setAttributeValues
(String name, Object[] values, boolean orderMatters) Sets a multivalue attribute.final void
Set the dn of this entry.void
setUpdateMode
(boolean mode) Sets the update mode.toString()
void
void
void
update()
Update the attributes.This will mean that the getters (getStringAttribute
methods) 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 befalse
for a new entry andtrue
for 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 betrue
for a new entry andtrue
for an existing entry that is being updated.- Specified by:
isUpdateMode
in 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:
getNamesOfModifiedAttributes
in 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:
getModificationItems
in 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,null
will be returned.- Specified by:
getStringAttribute
in interfaceLdapDataEntry
- Parameters:
name
- name of the attribute.- Returns:
- the value of the attribute if it exists, or
null
if 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,null
will be returned.- Specified by:
getObjectAttribute
in interfaceLdapDataEntry
- Parameters:
name
- name of the attribute.- Returns:
- the attribute value as an object if it exists, or
null
if 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:
attributeExists
in interfaceLdapDataEntry
- Parameters:
name
- name of the attribute- Returns:
true
if the attribute exists,false
otherwise
-
setAttributeValue
Set the with the namename
to thevalue
. If the value is aName
instance, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
setAttributeValue
in 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 aName
instance, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
addAttributeValue
in 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. TheaddIfDuplicateExists
parameter 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 aName
instance, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
addAttributeValue
in interfaceLdapDataEntry
- Parameters:
name
- the name of the Attribute to which the specified value should be added.value
- the Attribute value to add.addIfDuplicateExists
-true
will add the value regardless of whether there is an identical value already, allowing for duplicate attribute values;false
will 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 aName
instance, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
removeAttributeValue
in 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 areName
instances, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
setAttributeValues
in 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 areName
instances, equality for Distinguished Names will be used for calculating attribute modifications.- Specified by:
setAttributeValues
in 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 (getStringAttribute
methods) will return the updated values, and the modifications will be forgotten (i.e.AttributeModificationsAware.getModificationItems()
will return an empty array.- Specified by:
update
in interfaceDirContextOperations
-
getStringAttributes
Get all values of a String attribute.- Specified by:
getStringAttributes
in 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
null
otherwise.
-
getObjectAttributes
Get all values of an Object attribute.- Specified by:
getObjectAttributes
in 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
null
otherwise.
-
getAttributeSortedStringSet
Get all String values of the attribute as aSortedSet
.- Specified by:
getAttributeSortedStringSet
in interfaceLdapDataEntry
- Parameters:
name
- name of the attribute.- Returns:
- a
SortedSet
containing all values of the attribute, ornull
if the attribute does not exist.
-
setAttribute
Set the supplied attribute.- Parameters:
attribute
- the attribute to set.
-
getAttributes
Get all attributes.- Specified by:
getAttributes
in interfaceLdapDataEntry
- Returns:
- all attributes.
-
getAttributes
- Specified by:
getAttributes
in interfaceDirContext
- Throws:
NamingException
-
getAttributes
- Specified by:
getAttributes
in interfaceDirContext
- Throws:
NamingException
-
getAttributes
- Specified by:
getAttributes
in interfaceDirContext
- Throws:
NamingException
-
getAttributes
- Specified by:
getAttributes
in interfaceDirContext
- Throws:
NamingException
-
modifyAttributes
- Specified by:
modifyAttributes
in interfaceDirContext
- Throws:
NamingException
-
modifyAttributes
- Specified by:
modifyAttributes
in interfaceDirContext
- Throws:
NamingException
-
modifyAttributes
- Specified by:
modifyAttributes
in interfaceDirContext
- Throws:
NamingException
-
modifyAttributes
- Specified by:
modifyAttributes
in interfaceDirContext
- Throws:
NamingException
-
bind
- Specified by:
bind
in interfaceDirContext
- Throws:
NamingException
-
bind
- Specified by:
bind
in interfaceDirContext
- Throws:
NamingException
-
rebind
- Specified by:
rebind
in interfaceDirContext
- Throws:
NamingException
-
rebind
- Specified by:
rebind
in interfaceDirContext
- Throws:
NamingException
-
createSubcontext
- Specified by:
createSubcontext
in interfaceDirContext
- Throws:
NamingException
-
createSubcontext
- Specified by:
createSubcontext
in interfaceDirContext
- Throws:
NamingException
-
getSchema
- Specified by:
getSchema
in interfaceDirContext
- Throws:
NamingException
-
getSchema
- Specified by:
getSchema
in interfaceDirContext
- Throws:
NamingException
-
getSchemaClassDefinition
- Specified by:
getSchemaClassDefinition
in interfaceDirContext
- Throws:
NamingException
-
getSchemaClassDefinition
- Specified by:
getSchemaClassDefinition
in interfaceDirContext
- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException - Specified by:
search
in interfaceDirContext
- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes, String[] attributesToReturn) throws NamingException - Specified by:
search
in interfaceDirContext
- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(Name name, Attributes matchingAttributes) throws NamingException - Specified by:
search
in interfaceDirContext
- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(String name, Attributes matchingAttributes) throws NamingException - Specified by:
search
in interfaceDirContext
- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(Name name, String filter, SearchControls cons) throws NamingException - Specified by:
search
in interfaceDirContext
- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(String name, String filter, SearchControls cons) throws NamingException - Specified by:
search
in interfaceDirContext
- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(Name name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException - Specified by:
search
in interfaceDirContext
- Throws:
NamingException
-
search
public NamingEnumeration<SearchResult> search(String name, String filterExpr, Object[] filterArgs, SearchControls cons) throws NamingException - Specified by:
search
in interfaceDirContext
- Throws:
NamingException
-
lookup
- Specified by:
lookup
in interfaceContext
- Throws:
NamingException
-
lookup
- Specified by:
lookup
in interfaceContext
- Throws:
NamingException
-
bind
- Specified by:
bind
in interfaceContext
- Throws:
NamingException
-
bind
- Specified by:
bind
in interfaceContext
- Throws:
NamingException
-
rebind
- Specified by:
rebind
in interfaceContext
- Throws:
NamingException
-
rebind
- Specified by:
rebind
in interfaceContext
- Throws:
NamingException
-
unbind
- Specified by:
unbind
in interfaceContext
- Throws:
NamingException
-
unbind
- Specified by:
unbind
in interfaceContext
- Throws:
NamingException
-
rename
- Specified by:
rename
in interfaceContext
- Throws:
NamingException
-
rename
- Specified by:
rename
in interfaceContext
- Throws:
NamingException
-
list
- Specified by:
list
in interfaceContext
- Throws:
NamingException
-
list
- Specified by:
list
in interfaceContext
- Throws:
NamingException
-
listBindings
- Specified by:
listBindings
in interfaceContext
- Throws:
NamingException
-
listBindings
- Specified by:
listBindings
in interfaceContext
- Throws:
NamingException
-
destroySubcontext
- Specified by:
destroySubcontext
in interfaceContext
- Throws:
NamingException
-
destroySubcontext
- Specified by:
destroySubcontext
in interfaceContext
- Throws:
NamingException
-
createSubcontext
- Specified by:
createSubcontext
in interfaceContext
- Throws:
NamingException
-
createSubcontext
- Specified by:
createSubcontext
in interfaceContext
- Throws:
NamingException
-
lookupLink
- Specified by:
lookupLink
in interfaceContext
- Throws:
NamingException
-
lookupLink
- Specified by:
lookupLink
in interfaceContext
- Throws:
NamingException
-
getNameParser
- Specified by:
getNameParser
in interfaceContext
- Throws:
NamingException
-
getNameParser
- Specified by:
getNameParser
in interfaceContext
- Throws:
NamingException
-
composeName
- Specified by:
composeName
in interfaceContext
- Throws:
NamingException
-
composeName
- Specified by:
composeName
in interfaceContext
- Throws:
NamingException
-
addToEnvironment
- Specified by:
addToEnvironment
in interfaceContext
- Throws:
NamingException
-
removeFromEnvironment
- Specified by:
removeFromEnvironment
in interfaceContext
- Throws:
NamingException
-
getEnvironment
- Specified by:
getEnvironment
in interfaceContext
- Throws:
NamingException
-
close
- Specified by:
close
in interfaceContext
- Throws:
NamingException
-
getNameInNamespace
- Specified by:
getNameInNamespace
in interfaceContext
- Specified by:
getNameInNamespace
in 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:
getDn
in interfaceLdapDataEntry
- Returns:
- The distinguished name of the current context.
- See Also:
-
setDn
Set the dn of this entry.- Specified by:
setDn
in 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:
getReferralUrl
in 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:
isReferral
in interfaceDirContextOperations
- Returns:
true
if this instance results from a referral,false
otherwise.
-