You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2005/08/02 01:49:41 UTC
svn commit: r226911 -
/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchRequest.java
Author: elecharny
Date: Mon Aug 1 16:49:38 2005
New Revision: 226911
URL: http://svn.apache.org/viewcvs?rev=226911&view=rev
Log:
Added the encode method
Modified:
directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchRequest.java
Modified: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchRequest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchRequest.java?rev=226911&r1=226910&r2=226911&view=diff
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchRequest.java (original)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/ldap/pojo/SearchRequest.java Mon Aug 1 16:49:38 2005
@@ -17,13 +17,17 @@
package org.apache.asn1.ldap.pojo;
import org.apache.asn1.Asn1Object;
+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.LdapDN;
import org.apache.asn1.ldap.codec.primitives.LdapString;
import org.apache.asn1.ldap.pojo.filters.Filter;
+import java.nio.BufferOverflowException;
+import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Iterator;
@@ -69,6 +73,12 @@
/** The current filter. This is used while decoding a PDU */
private transient Filter currentFilter;
+
+ /** The searchRequest length */
+ private transient int searchRequestLength;
+
+ /** The attributeDescriptionList length */
+ private transient int attributeDescriptionListLength;
//~ Constructors -------------------------------------------------------------------------------
@@ -298,7 +308,7 @@
*/
public int computeLength()
{
- int searchRequestLength = 0;
+ searchRequestLength = 0;
// The baseObject
searchRequestLength += 1 + Length.getNbBytes( baseObject.getLength() ) + baseObject.getLength();
@@ -322,7 +332,7 @@
searchRequestLength += filter.computeLength();
// The attributes description list
- int attributesLength = 0;
+ attributeDescriptionListLength = 0;
if ( ( attributes != null ) && ( attributes.size() != 0 ) )
{
@@ -334,16 +344,96 @@
LdapString attribute = (LdapString)attributeIterator.next();
// add the attribute length to the attributes length
- attributesLength += 1 + Length.getNbBytes( attribute.getLength() ) + attribute.getLength();
+ attributeDescriptionListLength += 1 + Length.getNbBytes( attribute.getLength() ) + attribute.getLength();
}
}
- searchRequestLength += 1 + Length.getNbBytes( attributesLength ) + attributesLength;
+ searchRequestLength += 1 + Length.getNbBytes( attributeDescriptionListLength ) + attributeDescriptionListLength;
// Return the result.
return 1 + Length.getNbBytes( searchRequestLength ) + searchRequestLength;
}
+ /**
+ * Encode the SearchRequest message to a PDU.
+ *
+ * SearchRequest :
+ *
+ * 0x63 LL
+ * 0x04 LL baseObject
+ * 0x0A 01 scope
+ * 0x0A 01 derefAliases
+ * 0x02 0N sizeLimit
+ * 0x02 0N timeLimit
+ * 0x01 0x01 typesOnly
+ * filter.encode()
+ * 0x30 LL attributeDescriptionList
+ * 0x04 LL attributeDescription
+ * ...
+ * 0x04 LL attributeDescription
+ *
+ * @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 SearchRequest Tag
+ buffer.put( LdapConstants.SEARCH_REQUEST_TAG );
+ buffer.put( Length.getBytes( searchRequestLength ) ) ;
+
+ // The baseObject
+ Value.encode( buffer, baseObject );
+
+ // The scope
+ Value.encodeEnumerated( buffer, scope );
+
+ // The derefAliases
+ Value.encodeEnumerated( buffer, derefAliases );
+
+ // The sizeLimit
+ Value.encode( buffer, sizeLimit );
+
+ // The timeLimit
+ Value.encode( buffer, timeLimit );
+
+ // The typesOnly
+ Value.encode( buffer, typesOnly );
+
+ // The filter
+ filter.encode( buffer );
+
+ // The attributeDescriptionList
+ buffer.put( UniversalTag.SEQUENCE_TAG );
+ buffer.put( Length.getBytes( attributeDescriptionListLength ) );
+
+ if ( ( attributes != null ) && ( attributes.size() != 0 ) )
+ {
+ Iterator attributeIterator = attributes.iterator();
+
+ // encode each attribute
+ while ( attributeIterator.hasNext() )
+ {
+ LdapString attribute = (LdapString)attributeIterator.next();
+
+ Value.encode( buffer, attribute );
+ }
+ }
+ }
+ catch ( BufferOverflowException boe )
+ {
+ throw new EncoderException("The PDU buffer size is too small !");
+ }
+
+ return buffer;
+ }
+
/**
*
* @return A string that represent the Filter