You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2011/09/03 13:56:18 UTC

svn commit: r1164848 - in /directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap: LdapProtocolUtils.java replication/provider/SyncReplRequestHandler.java replication/provider/SyncReplSearchListener.java

Author: kayyagari
Date: Sat Sep  3 11:56:18 2011
New Revision: 1164848

URL: http://svn.apache.org/viewvc?rev=1164848&view=rev
Log:
o moved the utility methods dealing with syncrepl cookie to LdapProtocolUtils class
o changed the cookie format to be compatible with OpenLDAP (DIRSERVER-1651)

Modified:
    directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolUtils.java
    directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplRequestHandler.java
    directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplSearchListener.java

Modified: directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolUtils.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolUtils.java?rev=1164848&r1=1164847&r2=1164848&view=diff
==============================================================================
--- directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolUtils.java (original)
+++ directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/LdapProtocolUtils.java Sat Sep  3 11:56:18 2011
@@ -21,8 +21,12 @@ package org.apache.directory.server.ldap
 
 
 import org.apache.directory.server.core.interceptor.context.OperationContext;
+import org.apache.directory.shared.ldap.model.csn.Csn;
 import org.apache.directory.shared.ldap.model.message.Request;
 import org.apache.directory.shared.ldap.model.message.Response;
+import org.apache.directory.shared.util.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * Utility methods used by the LDAP protocol service.
@@ -31,6 +35,21 @@ import org.apache.directory.shared.ldap.
  */
 public class LdapProtocolUtils
 {
+    /** A delimiter for the replicaId */
+    public static final String COOKIE_DELIM = ",";
+
+    /** the prefix for replicaId value */
+    public static final String REPLICA_ID_PREFIX = "rid=";
+
+    private static final int REPLICA_ID_PREFIX_LEN = REPLICA_ID_PREFIX.length();
+    
+    /** the prefix for Csn value */
+    public static final String CSN_PREFIX = "csn=";
+
+    private static final int CSN_PREFIX_LEN = CSN_PREFIX.length();
+    
+    private static final Logger LOG = LoggerFactory.getLogger( LdapProtocolUtils.class );
+    
     /**
      * Extracts request controls from a request to populate into an
      * OperationContext.
@@ -58,4 +77,81 @@ public class LdapProtocolUtils
     {
         opContext.addRequestControls( opContext.getResponseControls() );
     }
+    
+    
+    public static byte[] createCookie( int replicaId, String csn )
+    {
+        // the syncrepl cookie format (compatible with OpenLDAP)
+        // rid=nn,csn=xxxz
+        return Strings.getBytesUtf8( REPLICA_ID_PREFIX + replicaId + COOKIE_DELIM + CSN_PREFIX + csn );
+    }
+
+
+    /**
+     * Check the cookie syntax. A cookie must have the following syntax :
+     * { rid={replicaId},csn={CSN} }
+     */
+    public static boolean isValidCookie( String cookieString )
+    {
+        if ( ( cookieString == null ) || ( cookieString.trim().length() == 0 ) )
+        {
+            return false;
+        }
+    
+        int pos = cookieString.indexOf( COOKIE_DELIM );
+        
+        // position should start from REPLICA_ID_PREFIX_LEN or higher cause a cookie can be
+        // like "rid=0,csn={csn}" or "rid=11,csn={csn}"
+        if ( pos <= REPLICA_ID_PREFIX_LEN )  
+        {
+            return false;
+        }
+    
+        String replicaId = cookieString.substring( REPLICA_ID_PREFIX_LEN, pos );
+        
+        try
+        {
+            Integer.parseInt( replicaId );
+        }
+        catch ( NumberFormatException e )
+        {
+            LOG.debug( "Failed to parse the replica id {}", replicaId );
+            return false;
+        }
+    
+        if ( pos == cookieString.length() )
+        {
+            return false;
+        }
+    
+        String csnString = cookieString.substring( pos + 1 + CSN_PREFIX_LEN );
+    
+        return Csn.isValid( csnString );
+    }
+
+
+    /**
+     * returns the CSN present in cookie
+     * 
+     * @param cookieString the cookie
+     * @return
+     */
+    public static String getCsn( String cookieString )
+    {
+        int pos = cookieString.indexOf( COOKIE_DELIM );
+        return cookieString.substring( pos + 1 + CSN_PREFIX_LEN );
+    }
+
+
+    /**
+     * returns the replica id present in cookie
+     * 
+     * @param cookieString  the cookie
+     * @return
+     */
+    public static int getReplicaId( String cookieString )
+    {
+        String replicaId = cookieString.substring( REPLICA_ID_PREFIX_LEN, cookieString.indexOf( COOKIE_DELIM ) );
+        return Integer.parseInt( replicaId );
+    }
 }

Modified: directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplRequestHandler.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplRequestHandler.java?rev=1164848&r1=1164847&r2=1164848&view=diff
==============================================================================
--- directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplRequestHandler.java (original)
+++ directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplRequestHandler.java Sat Sep  3 11:56:18 2011
@@ -38,6 +38,7 @@ import org.apache.directory.server.core.
 import org.apache.directory.server.core.event.NotificationCriteria;
 import org.apache.directory.server.core.filtering.EntryFilteringCursor;
 import org.apache.directory.server.i18n.I18n;
+import org.apache.directory.server.ldap.LdapProtocolUtils;
 import org.apache.directory.server.ldap.LdapServer;
 import org.apache.directory.server.ldap.LdapSession;
 import org.apache.directory.server.ldap.handlers.SearchAbandonListener;
@@ -54,7 +55,6 @@ import org.apache.directory.shared.ldap.
 import org.apache.directory.shared.ldap.extras.controls.syncrepl_impl.SyncInfoValueDecorator;
 import org.apache.directory.shared.ldap.extras.controls.syncrepl_impl.SyncStateValueDecorator;
 import org.apache.directory.shared.ldap.model.constants.SchemaConstants;
-import org.apache.directory.shared.ldap.model.csn.Csn;
 import org.apache.directory.shared.ldap.model.entry.Attribute;
 import org.apache.directory.shared.ldap.model.entry.Entry;
 import org.apache.directory.shared.ldap.model.entry.StringValue;
@@ -104,9 +104,6 @@ public class SyncReplRequestHandler impl
     /** A logger for the replication provider */
     private static final Logger PROVIDER_LOG = LoggerFactory.getLogger( "PROVIDER_LOG" );
 
-    /** A delimiter for the replicaId */
-    public static final String REPLICA_ID_DELIM = ";";
-
     /** Tells if the replication handler is already started */
     private boolean initialized = false;
 
@@ -245,7 +242,7 @@ public class SyncReplRequestHandler impl
                 PROVIDER_LOG.debug( "Received a replication request {} with a cookie '{}'", request, cookieString );
                 LOG.debug( "search request received with the cookie {}", cookieString );
                 
-                if ( !isValidCookie( cookieString ) )
+                if ( !LdapProtocolUtils.isValidCookie( cookieString ) )
                 {
                     LOG.error( "received a invalid cookie {} from the consumer with session {}", cookieString, session );
                     PROVIDER_LOG.debug( "received a invalid cookie {} from the consumer with session {}", cookieString, session );
@@ -265,7 +262,7 @@ public class SyncReplRequestHandler impl
                     }
                     else
                     {
-                        String consumerCsn = getCsn( cookieString );
+                        String consumerCsn = LdapProtocolUtils.getCsn( cookieString );
                         doContentUpdate( session, request, clientMsgLog, consumerCsn );
                     }
                 }
@@ -362,7 +359,7 @@ public class SyncReplRequestHandler impl
         String lastSentCsn = sendContentFromLog( session, req, replicaLog, consumerCsn );
 
         PROVIDER_LOG.debug( "The latest entry sent to the consumer {} has this CSN : {}", replicaLog.getId(), lastSentCsn );
-        byte[] cookie = Strings.getBytesUtf8(replicaLog.getId() + REPLICA_ID_DELIM + lastSentCsn);
+        byte[] cookie = LdapProtocolUtils.createCookie( replicaLog.getId(), lastSentCsn );
 
         if ( refreshNPersist )
         {
@@ -462,12 +459,12 @@ public class SyncReplRequestHandler impl
         if ( searchDoneResp.getLdapResult().getResultCode() == ResultCodeEnum.SUCCESS )
         {
             replicaLog.setLastSentCsn( contextCsn );
-            byte[] cookie = Strings.getBytesUtf8( replicaLog.getId() + REPLICA_ID_DELIM + contextCsn );
+            byte[] cookie = LdapProtocolUtils.createCookie( replicaLog.getId(), contextCsn );
 
             if ( refreshNPersist ) // refreshAndPersist mode
             {
                 contextCsn = sendContentFromLog( session, request, replicaLog, contextCsn );
-                cookie = Strings.getBytesUtf8(replicaLog.getId() + REPLICA_ID_DELIM + contextCsn);
+                cookie = LdapProtocolUtils.createCookie(replicaLog.getId(), contextCsn);
 
                 IntermediateResponse intermResp = new IntermediateResponseImpl( request.getMessageId() );
                 intermResp.setResponseName( SyncInfoValue.OID );
@@ -976,83 +973,15 @@ public class SyncReplRequestHandler impl
 
 
     /**
-     * Check the cookie syntax. A cookie must have the following syntax :
-     * <replicaId> [ ';' [ <CSN> ] ]
-     */
-    private boolean isValidCookie( String cookieString )
-    {
-        if ( ( cookieString == null ) || ( cookieString.trim().length() == 0 ) )
-        {
-            return false;
-        }
-
-        int pos = cookieString.indexOf( REPLICA_ID_DELIM );
-        
-        // position should start from 1 or higher cause a cookie can be
-        // like "0;<csn>" or "11;<csn>"
-        if ( pos <= 0 )  
-        {
-            return false;
-        }
-
-        String replicaId = cookieString.substring( 0, pos );
-        
-        try
-        {
-            Integer.parseInt( replicaId );
-        }
-        catch ( NumberFormatException e )
-        {
-            LOG.debug( "Failed to parse the replica id {}", replicaId );
-            return false;
-        }
-
-        if ( pos == cookieString.length() )
-        {
-            return false;
-        }
-
-        String csnString = cookieString.substring( pos + 1 );
-
-        return Csn.isValid( csnString );
-    }
-
-    /**
-     * returns the CSN present in cookie
-     * 
-     * @param cookieString the cookie
-     * @return
-     */
-    private String getCsn( String cookieString )
-    {
-        int pos = cookieString.indexOf( REPLICA_ID_DELIM );
-        return cookieString.substring( pos + 1 );
-    }
-
-    
-    /**
-     * returns the replica id present in cookie
-     * 
-     * @param cookieString  the cookie
-     * @return
-     */
-    private int getReplicaId( String cookieString )
-    {
-        String replicaId = cookieString.substring( 0, cookieString.indexOf( REPLICA_ID_DELIM ) );
-        return Integer.parseInt( replicaId );
-    }
-
-
-    /**
      * Get the Replica event log from the replica ID found in the cookie 
      */
     private ReplicaEventLog getReplicaEventLog( String cookieString ) throws Exception
     {
         ReplicaEventLog replicaLog = null;
 
-        if ( isValidCookie( cookieString ) )
+        if ( LdapProtocolUtils.isValidCookie( cookieString ) )
         {
-            int clientId = getReplicaId( cookieString );
+            int clientId = LdapProtocolUtils.getReplicaId( cookieString );
             replicaLog = replicaLogMap.get( clientId );
         }
 

Modified: directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplSearchListener.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplSearchListener.java?rev=1164848&r1=1164847&r2=1164848&view=diff
==============================================================================
--- directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplSearchListener.java (original)
+++ directory/apacheds/trunk/protocol-ldap/src/main/java/org/apache/directory/server/ldap/replication/provider/SyncReplSearchListener.java Sat Sep  3 11:56:18 2011
@@ -31,6 +31,7 @@ import org.apache.directory.server.core.
 import org.apache.directory.server.core.interceptor.context.MoveOperationContext;
 import org.apache.directory.server.core.interceptor.context.RenameOperationContext;
 import org.apache.directory.server.i18n.I18n;
+import org.apache.directory.server.ldap.LdapProtocolUtils;
 import org.apache.directory.server.ldap.LdapSession;
 import org.apache.directory.server.ldap.replication.ReplicaEventMessage;
 import org.apache.directory.shared.ldap.extras.controls.SyncStateTypeEnum;
@@ -453,7 +454,7 @@ public class SyncReplSearchListener impl
     {
         String csn = entry.get( SchemaConstants.ENTRY_CSN_AT ).getString();
 
-        return Strings.getBytesUtf8( consumerMsgLog.getId() + SyncReplRequestHandler.REPLICA_ID_DELIM + csn );
+        return LdapProtocolUtils.createCookie( consumerMsgLog.getId(), csn );
     }