directory-api mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "George S." <geor...@mhsoftware.com>
Subject Re: LdapConnection.search() Help
Date Wed, 07 Mar 2018 15:35:57 GMT
Using 1.0.0 If I run this code:

EntryCursor cursor=lc.search(base,"(objectclass=*)",SearchScope.ONELEVEL);
for  (Entry entry : cursor){
   entries.add(entry);
} I get this exception. It may be related to referrals. The reason I 
tried this library is because the Novell one was having big problems 
with referrals.

Exception in thread "main" java.lang.RuntimeException: ERR_02002_FAILURE_ON_UNDERLYING_CURSOR
Failure on underlying Cursor.
         at org.apache.directory.api.ldap.model.cursor.CursorIterator.next(CursorIterator.java:89)
         at com.mhsoftware.ldap.BoundConnection.search(BoundConnection.java:360)
         at com.mhsoftware.ldap.LDAPConnectionFactory.main(LDAPConnectionFactory.java:265)
Caused by: org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException
         at org.apache.directory.ldap.client.api.EntryCursorImpl.get(EntryCursorImpl.java:174)
\       lda        at org.apache.directory.ldap.client.api.EntryCursorImpl.get(EntryCursorImpl.java:52)
         at org.apache.directory.api.ldap.model.cursor.CursorIterator.next(CursorIterator.java:82)
         ... 2 more

If I run this code, it works.

  

SearchRequest sr = new SearchRequestImpl();
sr.setBase(new Dn(base));
sr.setTimeLimit(Integer.MAX_VALUE);
sr.setFilter("(objectclass=*)");
sr.addAttributes(attributeNames);
sr.setScope(SearchScope.ONELEVEL);
sr.setDerefAliases(AliasDerefMode.DEREF_ALWAYS);

SearchCursor cursor = lc.search(sr);


for (Response response: cursor) {
if (response instanceof SearchResultEntry) {
Entry resultEntry = ((SearchResultEntry)response).getEntry();
entries.add(resultEntry);
}
}


On 3/7/2018 8:18 AM, Emmanuel Lécharny wrote:
> So I have it working fine with this code :
>
>          EntryCursor cursor = connection.search( "ou=system",
> "(objectclass=*)",
>              SearchScope.ONELEVEL,
>              "*", "+" );
>          int count = 0;
>
>          for ( Entry entry : cursor )
>          {
>              assertNotNull( entry );
>              count++;
>          }
>
>          SearchResultDone done = cursor.getSearchResultDone();
>
> As you can see, I also use an iterator, I don't user a SearchRequest,
> and I get 5 entries back (count == 5 at the end).
>
> EntryCursorImpl is Iterable, and uses a CursorIterator to handle
> iteration, which move forward immediately when created, so that the
> first get() works (available() will succeed).
>
> AFAICT, it works properly.
>
> The only annoying problem is that a cursor is never going to give you a
> result if you don't move forward beforehand : its position is always
> *before* the first element. It may sound weird, but it's because we need
> to be able to move forward and backward properly.
>
>
> Le 07/03/2018 à 01:53, George S. a écrit :
>> It's definitely a problem.
>>
>>
>> On 3/6/2018 5:03 PM, George S. wrote:
>>> I looked at
>>> LdapNetworkConnection.search(String,String,SearchScope,String...) and
>>> it's a wrapper around a call to search using a SearchRequest, and
>>> constructing a EntryCursorImpl from the SearchCursor.
>>>
>>> There seems to be something wrong in EntryCursorImpl. If I change my
>>> code to use a SearchRequest, then I get results in a SearchCursor and
>>> I can iterate over them.
>> Looking at the code, EntryCursorImpl implicitly calls
>> searchCursor.available(), which is returning false because it appears
>> response is not set until a call to next(). See SearchResultImpl line
>> 117 or so.
>>
>>
>>> public  Collection<Response>search(String  base,String...
>>> attributeNames)throws  LdapException{
>>>
>>>      if  (attributeNames.length ==0){
>>>          attributeNames =new  String[]{
>>> "distinguishedName","objectClass","name",
>>> prop.getProperty("emailAddress","mail")};
>>>      }
>>>      Collection<Response> entries =new  ArrayList<>();
>>>      SearchRequest sr=new  SearchRequestImpl();
>>>      sr.setBase(new  Dn(base));
>>>      sr.setTimeLimit(Integer.MAX_VALUE);
>>>      sr.setFilter("(objectclass=*)");
>>>      sr.addAttributes(attributeNames);
>>>      sr.setScope(SearchScope.ONELEVEL);
>>>      sr.setDerefAliases(AliasDerefMode.DEREF_ALWAYS);
>>>
>>>      SearchCursor cursor = lc.search(sr);
>>>      // EntryCursor cursor=new EntryCursorImpl(scursor);
>>>
>>>      if  (isDebugMode()){
>>>          System.err.print("search(\""+base+"\"");
>>>          for  (String  s : attributeNames){
>>>              System.err.print(",\""+s+"\"");
>>>          }
>>>          System.err.println(");");
>>>      }
>>>          if  (true  || cursor.available()){
>>>          for  (Response entry: cursor){
>>>              entries.add(entry);
>>>          }
>>>      }  else  {
>>>          if  (isDebugMode()){
>>>              System.err.println("SearchResults came back null!");
>>>          }
>>>      }
>>>      try  {
>>>          cursor.close();
>>>      }  catch  (IOException ioeClose){
>>>          ioeClose.printStackTrace(System.err);
>>>      }
>>>      return  entries;
>>> }
>>>
>>>
>>> On 3/6/2018 4:27 PM, Emmanuel Lécharny wrote:
>>>> Le 07/03/2018 à 00:08, George S. a écrit :
>>>>> and, just to throw in another point, I've got some other code using the
>>>>> Novell LDAP API library and it is able to do the query, and I can use
>>>>> JXplorer to browse the tree.
>>>>>
>>>>> It's really looking like there's something wrong in the library.
>>>> Sorry, I missed the point. Removed the 'if ( cursor.available() )', it
>>>> should work.
>>>>

-- 
George S.
*MH Software, Inc.*
Voice: 303 438 9585
http://www.mhsoftware.com

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message