directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r1424916 - in /directory: apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/search/ shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/ shared/trunk/ldap/model/src/m...
Date Fri, 21 Dec 2012 13:50:08 GMT
Author: elecharny
Date: Fri Dec 21 13:50:08 2012
New Revision: 1424916

URL: http://svn.apache.org/viewvc?rev=1424916&view=rev
Log:
o Added a Cursor exception to handle the LdapReferrals
o Added a Client test to check that we are correctly receiving referrals
o Added some missing Javadoc
o Modified the EntryCursor to throw a CursorLdapReferralException when it receives some referral.
o We don't anymore loop for ever in this case (DIRAPI-89)

Added:
    directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/search/SearchWithReferralsTest.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/cursor/CursorLdapReferralException.java
Modified:
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/EntryCursorImpl.java
    directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapReferralException.java

Added: directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/search/SearchWithReferralsTest.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/search/SearchWithReferralsTest.java?rev=1424916&view=auto
==============================================================================
--- directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/search/SearchWithReferralsTest.java
(added)
+++ directory/apacheds/trunk/ldap-client-test/src/test/java/org/apache/directory/shared/client/api/operations/search/SearchWithReferralsTest.java
Fri Dec 21 13:50:08 2012
@@ -0,0 +1,212 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+
+package org.apache.directory.shared.client.api.operations.search;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.StringReader;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException;
+import org.apache.directory.api.ldap.model.cursor.EntryCursor;
+import org.apache.directory.api.ldap.model.entry.DefaultEntry;
+import org.apache.directory.api.ldap.model.entry.Entry;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.ldif.LdifEntry;
+import org.apache.directory.api.ldap.model.ldif.LdifReader;
+import org.apache.directory.api.ldap.model.message.SearchScope;
+import org.apache.directory.ldap.client.api.LdapNetworkConnection;
+import org.apache.directory.server.annotations.CreateLdapServer;
+import org.apache.directory.server.annotations.CreateTransport;
+import org.apache.directory.server.core.annotations.ApplyLdifs;
+import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
+import org.apache.directory.server.core.integ.FrameworkRunner;
+import org.apache.directory.shared.client.api.LdapApiIntegrationUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+
+/**
+ * A class to test the search operation with referrals
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+@RunWith(FrameworkRunner.class)
+@CreateLdapServer(transports =
+    { @CreateTransport(protocol = "LDAP"), @CreateTransport(protocol = "LDAPS") })
+@ApplyLdifs(
+    {
+        // Add new ref for ou=RemoteUsers
+        "dn: ou=RemoteUsers,ou=system",
+        "objectClass: top",
+        "objectClass: referral",
+        "objectClass: extensibleObject",
+        "ou: RemoteUsers",
+        "ref: ldap://fermi:10389/ou=users,ou=system",
+        "ref: ldap://hertz:10389/ou=users,dc=example,dc=com",
+        "ref: ldap://maxwell:10389/ou=users,ou=system",
+
+        "dn: c=France,ou=system",
+        "objectClass: top",
+        "objectClass: country",
+        "c: France",
+
+        "dn: c=USA,ou=system",
+        "objectClass: top",
+        "objectClass: country",
+        "c: USA",
+
+        "dn: l=Paris,c=france,ou=system",
+        "objectClass: top",
+        "objectClass: locality",
+        "l: Paris",
+
+        "dn: l=Jacksonville,c=usa,ou=system",
+        "objectClass: top",
+        "objectClass: locality",
+        "l: Jacksonville",
+
+        "dn: cn=emmanuel lecharny,l=paris,c=france,ou=system",
+        "objectClass: top",
+        "objectClass: person",
+        "objectClass: residentialPerson",
+        "cn: emmanuel lecharny",
+        "sn: elecharny",
+        "l: Paris",
+
+        "dn: cn=alex karasulu,l=jacksonville,c=usa,ou=system",
+        "objectClass: top",
+        "objectClass: person",
+        "objectClass: residentialPerson",
+        "cn: alex karasulu",
+        "sn: karasulu",
+        "l: Jacksonville",
+
+        "dn: ou=Countries,ou=system",
+        "objectClass: top",
+        "objectClass: organizationalUnit",
+        "ou: Countries"
+})
+public class SearchWithReferralsTest extends AbstractLdapTestUnit
+{
+private LdapNetworkConnection connection;
+
+
+@Before
+public void setupReferrals() throws Exception
+{
+    String ldif =
+        "dn: c=europ,ou=Countries,ou=system\n" +
+            "objectClass: top\n" +
+            "objectClass: referral\n" +
+            "objectClass: extensibleObject\n" +
+            "c: europ\n" +
+            "ref: ldap://localhost:52489/c=france,ou=system\n\n" +
+
+            "dn: c=america,ou=Countries,ou=system\n" +
+            "objectClass: top\n" +
+            "objectClass: referral\n" +
+            "objectClass: extensibleObject\n" +
+            "c: america\n" +
+            "ref: ldap://localhost:52489/c=usa,ou=system\n\n";
+
+    LdifReader reader = new LdifReader( new StringReader( ldif ) );
+
+    while ( reader.hasNext() )
+    {
+        LdifEntry entry = reader.next();
+        getLdapServer().getDirectoryService().getAdminSession().add(
+            new DefaultEntry( getLdapServer().getDirectoryService().getSchemaManager(), entry.getEntry()
) );
+    }
+
+    reader.close();
+
+    connection = ( LdapNetworkConnection ) LdapApiIntegrationUtils.getPooledAdminConnection(
getLdapServer() );
+}
+
+
+@After
+public void shutdown() throws Exception
+{
+    LdapApiIntegrationUtils.releasePooledAdminConnection( connection, getLdapServer() );
+}
+
+
+/**
+ * Test of an search operation with a referral
+ *
+ * search for "cn=alex karasulu" on "c=america, ou=system"
+ * we should get a referral URL thrown, which point to
+ * "c=usa, ou=system", and ask for a subtree search
+ */
+@Test
+public void testSearchWithReferralThrow() throws Exception
+{
+    try
+    {
+        EntryCursor cursor = connection.search( "ou=Countries,ou=system", "(objectClass=*)",
+            SearchScope.SUBTREE, "*", "+" );
+        int count = 0;
+        Entry entry = null;
+        List<String> refs = new ArrayList<String>();
+
+        while ( cursor.next() )
+        {
+            try
+            {
+                entry = cursor.get();
+
+                assertNotNull( entry );
+                count++;
+                System.out.println( entry );
+            }
+            catch ( CursorLdapReferralException clre )
+            {
+                count++;
+
+                do
+                {
+                    String ref = clre.getReferralInfo();
+                    refs.add( ref );
+                }
+                while ( clre.skipReferral() );
+            }
+        }
+
+        assertEquals( 3, count );
+        assertEquals( 2, refs.size() );
+        assertTrue( refs.contains( "ldap://localhost:52489/c=usa,ou=system??sub" ) );
+        assertTrue( refs.contains( "ldap://localhost:52489/c=france,ou=system??sub" ) );
+        cursor.close();
+    }
+    catch ( LdapException le )
+    {
+        String referral = le.getMessage();
+        assertEquals( "ldap://localhost:" + getLdapServer().getPort() + "/c=usa,ou=system??sub",
referral );
+    }
+}
+}

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/EntryCursorImpl.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/EntryCursorImpl.java?rev=1424916&r1=1424915&r2=1424916&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/EntryCursorImpl.java
(original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/EntryCursorImpl.java
Fri Dec 21 13:50:08 2012
@@ -26,11 +26,13 @@ import java.io.IOException;
 import org.apache.directory.api.i18n.I18n;
 import org.apache.directory.api.ldap.model.cursor.AbstractCursor;
 import org.apache.directory.api.ldap.model.cursor.CursorException;
+import org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException;
 import org.apache.directory.api.ldap.model.cursor.EntryCursor;
 import org.apache.directory.api.ldap.model.cursor.InvalidCursorPositionException;
 import org.apache.directory.api.ldap.model.cursor.SearchCursor;
 import org.apache.directory.api.ldap.model.entry.Entry;
 import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.exception.LdapReferralException;
 import org.apache.directory.api.ldap.model.message.Response;
 import org.apache.directory.api.ldap.model.message.SearchResultDone;
 import org.apache.directory.api.ldap.model.message.SearchResultEntry;
@@ -75,7 +77,7 @@ public class EntryCursorImpl extends Abs
         {
             LOG_CURSOR.debug( "Creating EntryCursorImpl {}", this );
         }
-        
+
         this.searchCursor = searchCursor;
         messageId = -1;
     }
@@ -150,15 +152,23 @@ public class EntryCursorImpl extends Abs
                 {
                     return ( ( SearchResultEntry ) response ).getEntry();
                 }
+
+                if ( response instanceof SearchResultReference )
+                {
+                    throw new LdapReferralException( ( ( SearchResultReference ) response
).getReferral().getLdapUrls() );
+                }
             }
             while ( next() && !( response instanceof SearchResultDone ) );
         }
+        catch ( LdapReferralException lre )
+        {
+            throw new CursorLdapReferralException( lre );
+        }
         catch ( Exception e )
         {
-            throw new CursorException( e.getMessage(), e );
+            throw new CursorException( e );
         }
 
-
         return null;
     }
 
@@ -191,7 +201,7 @@ public class EntryCursorImpl extends Abs
         {
             LOG_CURSOR.debug( "Closing EntryCursorImpl {}", this );
         }
-        
+
         searchCursor.close();
     }
 
@@ -206,7 +216,7 @@ public class EntryCursorImpl extends Abs
         {
             LOG_CURSOR.debug( "Closing EntryCursorImpl {}", this );
         }
-        
+
         searchCursor.close( cause );
     }
 

Added: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/cursor/CursorLdapReferralException.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/cursor/CursorLdapReferralException.java?rev=1424916&view=auto
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/cursor/CursorLdapReferralException.java
(added)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/cursor/CursorLdapReferralException.java
Fri Dec 21 13:50:08 2012
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.directory.api.ldap.model.cursor;
+
+
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.apache.directory.api.ldap.model.exception.LdapReferralException;
+import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
+import org.apache.directory.api.ldap.model.name.Dn;
+
+
+/**
+ * A specific form of CursorException used when a referral is met
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class CursorLdapReferralException extends CursorException
+{
+    /** The serialVersion UID */
+    private static final long serialVersionUID = -5723233489761854394L;
+
+    /** A static exception to be used by the monitor */
+    public static final CursorLdapReferralException INSTANCE = new CursorLdapReferralException(
null );
+
+    /** The contained referralException */
+    private LdapReferralException ldapReferralException;
+
+
+    /**
+     * Creates a new instance of CursorClosedException.
+     */
+    public CursorLdapReferralException( LdapReferralException ldapReferralException )
+    {
+        this.ldapReferralException = ldapReferralException;
+    }
+
+
+    /**
+     * Creates a new instance of CursorClosedException.
+     *
+     * @param message The associated message
+     */
+    public CursorLdapReferralException( LdapReferralException ldapReferralException, String
message )
+    {
+        super( message );
+
+        this.ldapReferralException = ldapReferralException;
+    }
+
+
+    /**
+     * Creates a new instance of CursorClosedException.
+     *
+     * @param message The associated message
+     * @param cause The original cause
+     */
+    public CursorLdapReferralException( LdapReferralException ldapReferralException, String
message, Throwable cause )
+    {
+        super( message, cause );
+
+        this.ldapReferralException = ldapReferralException;
+    }
+
+
+    /**
+     * Always returns {@link ResultCodeEnum#REFERRAL}
+     * 
+     * @see LdapException#getResultCode()
+     */
+    public ResultCodeEnum getResultCode()
+    {
+        if ( ldapReferralException != null )
+        {
+            return ldapReferralException.getResultCode();
+        }
+        else
+        {
+            return ResultCodeEnum.UNKNOWN;
+        }
+    }
+
+
+    /**
+     * @return The current Referral
+     */
+    public String getReferralInfo()
+    {
+        if ( ldapReferralException != null )
+        {
+            return ldapReferralException.getReferralInfo();
+        }
+        else
+        {
+            return "";
+        }
+    }
+
+
+    /**
+     * Move to the next referral
+     * @return true if there is some next referral
+     */
+    public boolean skipReferral()
+    {
+        if ( ldapReferralException != null )
+        {
+            return ldapReferralException.skipReferral();
+        }
+        else
+        {
+            return false;
+        }
+    }
+
+
+    /**
+     * @return the remainingDn
+     */
+    public Dn getRemainingDn()
+    {
+        if ( ldapReferralException != null )
+        {
+            return ldapReferralException.getRemainingDn();
+        }
+        else
+        {
+            return Dn.EMPTY_DN;
+        }
+    }
+
+
+    /**
+     * @return the resolvedObject
+     */
+    public Object getResolvedObject()
+    {
+        if ( ldapReferralException != null )
+        {
+            return ldapReferralException.getResolvedObject();
+        }
+        else
+        {
+            return null;
+        }
+    }
+}

Modified: directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapReferralException.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapReferralException.java?rev=1424916&r1=1424915&r2=1424916&view=diff
==============================================================================
--- directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapReferralException.java
(original)
+++ directory/shared/trunk/ldap/model/src/main/java/org/apache/directory/api/ldap/model/exception/LdapReferralException.java
Fri Dec 21 13:50:08 2012
@@ -95,6 +95,9 @@ public class LdapReferralException exten
     }
 
 
+    /**
+     * @return The current Referral
+     */
     public String getReferralInfo()
     {
         return refs.get( index );
@@ -113,6 +116,10 @@ public class LdapReferralException exten
     }
 
 
+    /**
+     * Move to the next referral
+     * @return true if there is some next referral
+     */
     public boolean skipReferral()
     {
         index++;



Mime
View raw message