Package org.jeegen.jee6.util
Class AbstractLdapConnector
- java.lang.Object
-
- org.jeegen.jee6.util.AbstractLdapConnector
-
public abstract class AbstractLdapConnector extends Object
This class works as a LDAP connector.
-
-
Field Summary
Fields Modifier and Type Field Description protected DirContext
ctx
The directory context used internally.
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractLdapConnector(String url, String dn, String part, String secret)
This constructor initializes a connection to a LDAP server.protected
AbstractLdapConnector(DirContext context, String dn)
This constructor setups an existingDirContext
and the given dn.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
close()
This method closes the LDAP connection.void
create(String partDn, Attributes attrs)
This method creates a directory entry using the givenAttributes
.void
delete(String partDn)
This method deletes a directory entry from the LDAP server.Attributes
getAttributes(String partDn, String filter)
This method returns theAttributes
of a LDAP entry.Collection<String>
getGroup(String group)
This method returns aCollection
of all group members.protected abstract String
getGroupDn()
This method returns the groups sub DN of this LDAP connection.String
getSearchBase(String partDn)
Diese Methode baut eine komplette DN zusammen.Attributes
getUser(String user)
This method returns allAttributes
of a user.protected abstract String
getUserDn()
This method returns the users sub DN of this LDAP connection.static String
getValue(Attributes attributes, String key)
Diese Methode ermittelt aus Attributen ein mit einem Schlüsselwort definiertes Einfachattribut.static List<String>
getValues(Attributes attributes, String key)
Diese Methode ermittelt aus Attributen ein mit einem Schlüsselwort definiertes Mehrfachattribut.void
modify(String partDn, Attributes attributes)
This method modifies the givenAttributes
inside the given partial DN.NamingEnumeration<SearchResult>
search(String partDn, String filter)
This method searches for a sub DN inside a LDAP server.static void
setAttribute(Attributes attributes, String key, Object value)
This method sets anAttribute
with the specified key to the given value.static void
setAttribute(Attributes attributes, String key, String value)
-
-
-
Field Detail
-
ctx
protected DirContext ctx
The directory context used internally.
-
-
Constructor Detail
-
AbstractLdapConnector
protected AbstractLdapConnector(String url, String dn, String part, String secret) throws NamingException
This constructor initializes a connection to a LDAP server. If the user DN and the user secret is not null, the connection is bound with this user credential. The user DN is a sub DN of the base DN and are concatenated internally.- Parameters:
url
- URL to the LDAP serverdn
- The base DN.part
- The user part of the DN without the base DN.secret
- The user secret.- Throws:
NamingException
- on error.
-
AbstractLdapConnector
protected AbstractLdapConnector(DirContext context, String dn) throws NamingException
This constructor setups an existingDirContext
and the given dn.- Parameters:
context
- The initializedDirContext
dn
- The distinguished name to use.- Throws:
NamingException
- on error.
-
-
Method Detail
-
close
public void close()
This method closes the LDAP connection.
-
getSearchBase
public String getSearchBase(String partDn)
Diese Methode baut eine komplette DN zusammen. Die Basis-DN darf nicht angegeben werden, da sie ja schon mit einer Instanz dieser Klasse verknüpft ist. Es wird also nur eine Sub-DN mit angegeben.- Parameters:
partDn
- Die Sub-DN- Returns:
- Die komplette DN.
-
getValue
public static String getValue(Attributes attributes, String key) throws NamingException
Diese Methode ermittelt aus Attributen ein mit einem Schlüsselwort definiertes Einfachattribut.- Parameters:
attributes
- Die Attributekey
- Der Attributname- Returns:
- Der Attributwert
- Throws:
IllegalArgumentException
- Falls das Attribut doch ein Mehrfachattribut ist.NamingException
- on error.
-
getValues
public static List<String> getValues(Attributes attributes, String key) throws NamingException
Diese Methode ermittelt aus Attributen ein mit einem Schlüsselwort definiertes Mehrfachattribut.- Parameters:
attributes
- Die Attributekey
- Der Attributname- Returns:
- Eine Liste von Attributwerten.
- Throws:
NamingException
- on error.
-
search
public NamingEnumeration<SearchResult> search(String partDn, String filter) throws NamingException
This method searches for a sub DN inside a LDAP server. The complete DN is computed using thegetSearchBase(String)
method.- Parameters:
partDn
- The sub DNfilter
- The search filter- Returns:
- The search results as a
NamingEnumeration
. - Throws:
NamingException
- on error.- See Also:
getSearchBase(String)
-
getAttributes
public Attributes getAttributes(String partDn, String filter) throws NamingException
This method returns theAttributes
of a LDAP entry.- Parameters:
partDn
- Ths sub DN of theAttributes
filter
- An additional search filter.- Returns:
- The
Attributes
of the search result. - Throws:
NamingException
- on error.
-
getUser
public Attributes getUser(String user) throws NamingException
This method returns allAttributes
of a user.- Parameters:
user
- The User.- Returns:
- The
Attributes
of the user. - Throws:
NamingException
- on error.
-
getGroup
public Collection<String> getGroup(String group) throws NamingException
This method returns aCollection
of all group members.- Parameters:
group
- The group name.- Returns:
- The
Collection
of all group members. - Throws:
NamingException
- on error.
-
setAttribute
public static void setAttribute(Attributes attributes, String key, Object value)
This method sets anAttribute
with the specified key to the given value. If theAttribute
doesn't exist theAttribute
is created. If existing it will be modified. TheAttribute
is not transferred to the LDAP-server. This will be done using themodify(String, Attributes)
method.- Parameters:
attributes
- TheAttributes
.key
- The key of theAttribute
to change.value
- The value to use.
-
setAttribute
public static void setAttribute(Attributes attributes, String key, String value) throws UnsupportedEncodingException
This method sets anAttribute
with the specified key to the givenString
. TheString
ist converted into an UTF-8 byte sequence. If theAttribute
doesn't exist theAttribute
is created. If existing it will be modified. TheAttribute
is not transferred to the LDAP-server. This will be done using themodify(String, Attributes)
method.- Parameters:
attributes
- TheAttributes
.key
- The key of theAttribute
to change.value
- The value to use.- Throws:
UnsupportedEncodingException
- on error.
-
create
public void create(String partDn, Attributes attrs) throws NamingException
This method creates a directory entry using the givenAttributes
.- Parameters:
partDn
- The partial DN to create.attrs
- TheAttributes
to fill in.- Throws:
NamingException
- on error.- See Also:
getSearchBase(String)
-
modify
public void modify(String partDn, Attributes attributes) throws NamingException
This method modifies the givenAttributes
inside the given partial DN. Only the givenAttributes
are modified. The otherAttributes
inside the DN are left unmodified.- Parameters:
partDn
- The partial DN to modify.attributes
- The modifiedAttributes
.- Throws:
NamingException
- on error.- See Also:
getSearchBase(String)
-
delete
public void delete(String partDn) throws NamingException
This method deletes a directory entry from the LDAP server.- Parameters:
partDn
- The partial DN to delete.- Throws:
NamingException
- on error.- See Also:
getSearchBase(String)
-
getUserDn
protected abstract String getUserDn()
This method returns the users sub DN of this LDAP connection. The sub tree of this DN contains all users inside the LDAP server. This may be hardcoded into the implementation class.- Returns:
- The users sub DN.
-
getGroupDn
protected abstract String getGroupDn()
This method returns the groups sub DN of this LDAP connection. The sub tree of this DN contains all groups inside the LDAP server. This may be hardcoded into the implementation class.- Returns:
- The groups sub DN.
-
-