directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r226963 - /directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/filters/SubstringFilter.java
Date Tue, 02 Aug 2005 06:30:11 GMT
Author: elecharny
Date: Mon Aug  1 23:30:08 2005
New Revision: 226963

URL: http://svn.apache.org/viewcvs?rev=226963&view=rev
Log:
Added the encode method

Modified:
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/filters/SubstringFilter.java

Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/filters/SubstringFilter.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/filters/SubstringFilter.java?rev=226963&r1=226962&r2=226963&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/filters/SubstringFilter.java
(original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/filters/SubstringFilter.java
Mon Aug  1 23:30:08 2005
@@ -16,10 +16,16 @@
  */
 package org.apache.asn1.ldap.pojo.filters;
 
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Iterator;
 
+import org.apache.asn1.EncoderException;
 import org.apache.asn1.ber.tlv.Length;
+import org.apache.asn1.ber.tlv.UniversalTag;
+import org.apache.asn1.ber.tlv.Value;
+import org.apache.asn1.ldap.codec.LdapConstants;
 import org.apache.asn1.ldap.codec.primitives.LdapString;
 
 
@@ -52,6 +58,10 @@
 
     /** The final filter */
     private LdapString finalSubstrings;
+    
+    private transient int substringsFilterLength;
+
+    private transient int substringsFilterSequenceLength;
 
     //~ Methods ------------------------------------------------------------------------------------
     
@@ -182,11 +192,12 @@
     public int computeLength()
     {
         // The type
-        int substringsFilterLength = 1 + Length.getNbBytes( type.getLength() ) + type.getLength();
+        substringsFilterLength = 1 + Length.getNbBytes( type.getLength() ) + type.getLength();
+        substringsFilterSequenceLength = 0;
         
         if ( initialSubstrings != null )
         {
-            substringsFilterLength += 1 + Length.getNbBytes( initialSubstrings.getLength()
) + initialSubstrings.getLength(); 
+            substringsFilterSequenceLength += 1 + Length.getNbBytes( initialSubstrings.getLength()
) + initialSubstrings.getLength(); 
         }
         
         if ( anySubstrings != null)
@@ -196,20 +207,106 @@
             while ( anyIterator.hasNext() )
             {
                 LdapString any = (LdapString)anyIterator.next();
-                substringsFilterLength += 1 + Length.getNbBytes( any.getLength() ) + any.getLength();
+                substringsFilterSequenceLength += 1 + Length.getNbBytes( any.getLength()
) + any.getLength();
             }
         }
         
         if ( finalSubstrings != null )
         {
-            substringsFilterLength += 1 + Length.getNbBytes( finalSubstrings.getLength()
) + finalSubstrings.getLength(); 
+            substringsFilterSequenceLength += 1 + Length.getNbBytes( finalSubstrings.getLength()
) + finalSubstrings.getLength(); 
         }
         
-        substringsFilterLength += 1 + Length.getNbBytes( substringsFilterLength );
+        substringsFilterLength += 1 + Length.getNbBytes( substringsFilterSequenceLength )
+ substringsFilterSequenceLength;
         
         return 1 + Length.getNbBytes( substringsFilterLength ) + substringsFilterLength;
     }
     
+    /**
+     * Encode the Substrings Filter to a PDU. 
+     * 
+     * Substrings Filter :
+     * 
+     * 0xA4 LL 
+     * 0x30 LL substringsFilter
+     *   0x04 LL type
+     *   0x30 LL substrings sequence
+     *    |  0x80 LL initial
+     *    | /  [0x81 LL any]* 
+     *    |/   [0x82 LL final]
+     *    +--[0x81 LL any]+
+     *     \   [0x82 LL final]
+     *      \
+     *       0x82 LL final
+     * 
+     * @param buffer The buffer where to put the PDU
+     * @return The PDU.
+     */
+    public ByteBuffer encode( ByteBuffer buffer ) throws EncoderException
+    {
+        if (buffer == null)
+        {
+            throw new EncoderException("Cannot put a PDU in a null buffer !");
+        }
+
+        try 
+        {
+            // The SubstringFilter Tag
+            buffer.put( (byte)LdapConstants.SUBSTRINGS_FILTER_TAG );
+            buffer.put( Length.getBytes( substringsFilterLength ) );
+            
+            // The type
+            Value.encode( buffer, type );
+
+            // The SubstringSequenceFilter Tag
+            buffer.put( UniversalTag.SEQUENCE_TAG );
+            buffer.put( Length.getBytes( substringsFilterSequenceLength ) );
+            
+
+            if ( ( initialSubstrings == null ) && 
+                 ( ( anySubstrings == null ) || ( anySubstrings.size() == 0 ) ) &&
+                 ( finalSubstrings == null ) ) 
+            {
+                throw new EncoderException("Cannot have a null initial, any and final substring");
+            }
+            
+            // The initial substring
+            if ( initialSubstrings != null ) 
+            {
+                buffer.put( (byte)LdapConstants.SEARCH_SUBSTRINGS_INITIAL_TAG );
+                buffer.put( Length.getBytes( initialSubstrings.getLength() ) );
+                buffer.put( initialSubstrings.getData() );                
+            }
+            
+            // The any substrings
+            if ( anySubstrings != null)
+            {
+                Iterator anyIterator = anySubstrings.iterator();
+                
+                while ( anyIterator.hasNext() )
+                {
+                    LdapString any = (LdapString)anyIterator.next();
+                    buffer.put( (byte)LdapConstants.SEARCH_SUBSTRINGS_ANY_TAG );
+                    buffer.put( Length.getBytes( any.getLength() ) );
+                    buffer.put( any.getData() );
+                }
+            }
+            
+            // The final substring
+            if ( finalSubstrings != null ) 
+            {
+                buffer.put( (byte)LdapConstants.SEARCH_SUBSTRINGS_FINAL_TAG );
+                buffer.put( Length.getBytes( finalSubstrings.getLength() ) );
+                buffer.put( finalSubstrings.getData() );                
+            }
+        }
+        catch ( BufferOverflowException boe )
+        {
+            throw new EncoderException("The PDU buffer size is too small !"); 
+        }
+        
+        return buffer;
+    }
+
     /**
      * Return a string compliant with RFC 2254 representing a Substring filter
      *



Mime
View raw message