You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@directory.apache.org by Stefan Zoerner <st...@labeo.de> on 2009/05/21 20:54:14 UTC

Using a CoreSession in embeded mode to perform a search

Hi all,

currently I implement an Apache Tomcat Realm which embeds ApacheDS and 
uses it for authentication and authorization. The thing already works 
and looks quite promising, I will post a first version at weekend on 
this list in order to get some feedback.

During implementation, I tried to avoid JNDI calls and used the 
CoreSession interface which I can get from DirectoryService.

Code (which searches a user entry by uid) goes like this:

...
CoreSession adminSession = directoryService.getAdminSession();

String base = "ou=system";
LdapDN basedn = new LdapDN(base);
SearchScope scope = SearchScope.SUBTREE;
MessageFormat f = new MessageFormat(
		"(&(objectClass=person)(uid={0}))");
String sFilter = f.format(new Object[] { username });

ExprNode filter = FilterParser.parse(sFilter);
EntryFilteringCursor cursor = adminSession.search(basedn, scope,
		filter, AliasDerefMode.NEVER_DEREF_ALIASES, null, 0, 0);
CursorIterator iter = new CursorIterator(cursor);
if (iter.hasNext()) {
     ServerEntry entry = (ServerEntry) iter.next();
     dn = entry.getDn();
}
adminSession.unbind();
...

I am not happy with the use of CursorIterator (especially the cast). 
Anybody a better idea how to perform a search and process the results 
from an embedded server?

If needed, complete code can be found here
https://svn.apache.org/repos/asf/directory/sandbox/szoerner/apacheds-tomcatrealm

Thanks in advance,
     StefanZ



Re: Using a CoreSession in embeded mode to perform a search

Posted by Stefan Zoerner <st...@labeo.de>.
Hi Emmanuel!

Emmanuel Lecharny wrote:
>> I am not happy with the use of CursorIterator (especially the cast). 
>> Anybody a better idea how to perform a search and process the results 
>> from an embedded server?
> 
> You don't have to use a CursorIterator. Once you get a cursor, you can 
> do something like :
> 
> cursor.beforeFirst(); // To position the cursor at the beginning
> 
> while (cursor.next() )
> {
>    ServerEntry entry = cursor.get();
> }
> 
> No need to cast, and the entry you get is a clone.

I was blind. Thanks E, works great!
Greetings from Hamburg,
    Stefan


Re: Using a CoreSession in embeded mode to perform a search

Posted by Emmanuel Lecharny <el...@apache.org>.
Stefan Zoerner wrote:
> Hi all,
Hi Stefan,
>
> currently I implement an Apache Tomcat Realm which embeds ApacheDS and 
> uses it for authentication and authorization. The thing already works 
> and looks quite promising, I will post a first version at weekend on 
> this list in order to get some feedback.
>
> During implementation, I tried to avoid JNDI calls and used the 
> CoreSession interface which I can get from DirectoryService.
>
> Code (which searches a user entry by uid) goes like this:

>
> ...
> CoreSession adminSession = directoryService.getAdminSession();
>
> String base = "ou=system";
> LdapDN basedn = new LdapDN(base);
> SearchScope scope = SearchScope.SUBTREE;
> MessageFormat f = new MessageFormat(
>         "(&(objectClass=person)(uid={0}))");
> String sFilter = f.format(new Object[] { username });
>
> ExprNode filter = FilterParser.parse(sFilter);
> EntryFilteringCursor cursor = adminSession.search(basedn, scope,
>         filter, AliasDerefMode.NEVER_DEREF_ALIASES, null, 0, 0);
> CursorIterator iter = new CursorIterator(cursor);
> if (iter.hasNext()) {
>     ServerEntry entry = (ServerEntry) iter.next();
>     dn = entry.getDn();
> }
> adminSession.unbind();
> ...
>
> I am not happy with the use of CursorIterator (especially the cast). 
> Anybody a better idea how to perform a search and process the results 
> from an embedded server?

You don't have to use a CursorIterator. Once you get a cursor, you can 
do something like :

cursor.beforeFirst(); // To position the cursor at the beginning

while (cursor.next() )
{
    ServerEntry entry = cursor.get();
}

No need to cast, and the entry you get is a clone.


-- 
--
cordialement, regards,
Emmanuel Lécharny
www.iktek.com
directory.apache.org