You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ak...@apache.org on 2004/08/25 03:21:19 UTC
svn commit: rev 36831 - in incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src: java/org/apache/snickers/ldap/encoder/search test/org/apache/snickers/ldap/encoder/search
Author: akarasulu
Date: Tue Aug 24 18:21:19 2004
New Revision: 36831
Added:
incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/search/SearchRequestEncoder.java
incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/search/SearchRequestEncoderTest.java
Log:
Commit changes ...
o completed search request stub encoder
o added and passed test case for encoder
Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/search/SearchRequestEncoder.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/search/SearchRequestEncoder.java Tue Aug 24 18:21:19 2004
@@ -0,0 +1,378 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.snickers.ldap.encoder.search;
+
+
+import org.apache.ldap.common.filter.*;
+import org.apache.ldap.common.message.SearchRequest;
+
+import org.apache.snickers.ber.*;
+import org.apache.snickers.ber.primitives.UniversalTag;
+
+import org.apache.snickers.ldap.LdapTag;
+import org.apache.snickers.ldap.encoder.EncoderUtils;
+
+import java.util.Iterator;
+import java.util.ArrayList;
+import java.util.Collection;
+
+
+/**
+ * A SearchRequest to TupleNode tlv tree encoder.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ * Project</a> $Rev$
+ */
+public class SearchRequestEncoder
+{
+ /** a thread safe instance of this encoder */
+ public static final SearchRequestEncoder INSTANCE =
+ new SearchRequestEncoder();
+
+ public TupleNode encode( SearchRequest request )
+ {
+ /// Create the top level TupleNode of the PDU
+ DefaultMutableTupleNode top =
+ new DefaultMutableTupleNode( new Tuple() );
+ top.getTuple().setTag( UniversalTag.SEQUENCE_SEQUENCE_OF, false );
+ top.getTuple().setLength( Length.INDEFINATE );
+
+ // Create and add the message id to PDU
+ DefaultMutableTupleNode child = ( DefaultMutableTupleNode )
+ EncoderUtils.encode( request.getMessageId() );
+ top.addLast( child );
+ child.setParent( top );
+
+ // Create the search request sequence of TLV tuple
+ DefaultMutableTupleNode searchReq =
+ new DefaultMutableTupleNode( new Tuple() );
+ searchReq.getTuple().setTag( LdapTag.SEARCH_REQUEST, false );
+ searchReq.getTuple().setLength( Length.INDEFINATE );
+
+ // Add the OCTET_STRING baseObject or base DN
+ child = ( DefaultMutableTupleNode )
+ EncoderUtils.encode( request.getBase() );
+ searchReq.addLast( child );
+ child.setParent( searchReq );
+
+ // Add the ENUMERATED scope parameter
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ UniversalTag.ENUMERATED, request.getScope().getLdapValue() );
+ searchReq.addLast( child );
+ child.setParent( searchReq );
+
+ // Add the ENUMERATED derefAliases parameter
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ UniversalTag.ENUMERATED, request.getDerefAliases().getValue() );
+ searchReq.addLast( child );
+ child.setParent( searchReq );
+
+ // Add the INTEGER sizeLimit parameter
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ request.getSizeLimit() );
+ searchReq.addLast( child );
+ child.setParent( searchReq );
+
+ // Add the INTEGER timeLimit parameter
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ request.getTimeLimit() );
+ searchReq.addLast( child );
+ child.setParent( searchReq );
+
+ // Add the BOOLEAN typesOnly parameter
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ request.getTypesOnly() );
+ searchReq.addLast( child );
+ child.setParent( searchReq );
+
+ // Add the Filter filter parameter
+ encode( searchReq, request.getFilter() );
+
+ // Add the AttributeDescriptionList attributes parameter
+ child = encode( request.getAttributes() );
+ searchReq.addLast( child );
+ child.setParent( searchReq );
+
+ top.addLast( searchReq );
+ searchReq.setParent( top );
+ return top;
+ }
+
+
+ /**
+ * Encodes a SimpleNode as a TupleNode Tree into another top TupleNode tree.
+ *
+ * @param top the TupleNode that is having nodes added as we encode
+ * @param tag the tag enumeration to use for the TupleNode to add
+ * @param node a filter expression tree's simple node
+ */
+ public void encode( DefaultMutableTupleNode top, TagEnum tag,
+ SimpleNode node )
+ {
+ DefaultMutableTupleNode child = null;
+ DefaultMutableTupleNode parent =
+ new DefaultMutableTupleNode( new Tuple() );
+ parent.getTuple().setTag( tag, false );
+ parent.getTuple().setLength( Length.INDEFINATE );
+
+ child = ( DefaultMutableTupleNode )
+ EncoderUtils.encode( node.getAttribute() );
+ parent.addLast( child );
+ child.setParent( parent );
+
+ child = ( DefaultMutableTupleNode )
+ EncoderUtils.encode( node.getValue() );
+ parent.addLast( child );
+ child.setParent( parent );
+
+ top.addLast( parent );
+ parent.setParent( top );
+ }
+
+
+ public void encode( DefaultMutableTupleNode top, TagEnum tag,
+ SubstringNode node )
+ {
+ DefaultMutableTupleNode child = null;
+ DefaultMutableTupleNode parent =
+ new DefaultMutableTupleNode( new Tuple() );
+ parent.getTuple().setTag( tag, false );
+ parent.getTuple().setLength( Length.INDEFINATE );
+
+ child = ( DefaultMutableTupleNode )
+ EncoderUtils.encode( node.getAttribute() );
+ parent.addLast( child );
+ child.setParent( parent );
+
+
+ if ( node.getInitial() != null || node.getFinal() != null ||
+ node.getAny().size() > 0 )
+ {
+ DefaultMutableTupleNode seq =
+ new DefaultMutableTupleNode( new Tuple() );
+ seq.getTuple().setTag( UniversalTag.SEQUENCE_SEQUENCE_OF, false );
+ seq.getTuple().setLength( Length.INDEFINATE );
+
+ if ( node.getInitial() != null )
+ {
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ LdapTag.CONTEXT_SPECIFIC_TAG_0, node.getInitial() );
+ seq.addLast( child );
+ child.setParent( seq );
+ }
+
+ if ( node.getAny().size() > 0 )
+ {
+ for( int ii = 0; ii < node.getAny().size(); ii++ )
+ {
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ LdapTag.CONTEXT_SPECIFIC_TAG_1,
+ ( String ) node.getAny().get( ii ) );
+ seq.addLast( child );
+ child.setParent( seq );
+ }
+ }
+
+ if ( node.getFinal() != null )
+ {
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ LdapTag.CONTEXT_SPECIFIC_TAG_2, node.getFinal() );
+ seq.addLast( child );
+ child.setParent( seq );
+ }
+
+ parent.addLast( seq );
+ seq.setParent( parent );
+ }
+
+ top.addLast( parent );
+ parent.setParent( top );
+ }
+
+
+ public void encode( DefaultMutableTupleNode parent, TagEnum tag,
+ PresenceNode node )
+ {
+ DefaultMutableTupleNode child = ( DefaultMutableTupleNode )
+ EncoderUtils.encode( LdapTag.CONTEXT_SPECIFIC_TAG_7,
+ node.getAttribute() );
+ parent.addLast( child );
+ child.setParent( parent );
+ }
+
+
+ public void encode( DefaultMutableTupleNode top, TagEnum tag,
+ ExtensibleNode node )
+ {
+ DefaultMutableTupleNode child = null;
+ DefaultMutableTupleNode parent =
+ new DefaultMutableTupleNode( new Tuple() );
+ parent.getTuple().setTag( tag, false );
+ parent.getTuple().setLength( Length.INDEFINATE );
+
+
+ if ( node.getMatchingRuleId() != null )
+ {
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ LdapTag.CONTEXT_SPECIFIC_TAG_1, node.getMatchingRuleId() );
+ parent.addLast( child );
+ child.setParent( parent );
+ }
+
+ if ( node.getAttribute() != null )
+ {
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ LdapTag.CONTEXT_SPECIFIC_TAG_2, node.getAttribute() );
+ parent.addLast( child );
+ child.setParent( parent );
+ }
+
+
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ LdapTag.CONTEXT_SPECIFIC_TAG_3, node.getValue() );
+ parent.addLast( child );
+ child.setParent( parent );
+
+ child = ( DefaultMutableTupleNode ) EncoderUtils.encode(
+ LdapTag.CONTEXT_SPECIFIC_TAG_4, node.dnAttributes() );
+ parent.addLast( child );
+ child.setParent( parent );
+
+ top.addLast( parent );
+ parent.setParent( top );
+ }
+
+
+ public void encodeBranchNode( DefaultMutableTupleNode parent,
+ BranchNode node )
+ {
+ DefaultMutableTupleNode child =
+ new DefaultMutableTupleNode( new Tuple() );
+
+ if ( node.isNegation() ) // NOT (!)
+ {
+ child.getTuple().setTag( LdapTag.CONTEXT_SPECIFIC_TAG_2, false );
+ child.getTuple().setLength( Length.INDEFINATE );
+ encode( child, node.getChild() );
+ parent.addLast( child );
+ child.setParent( parent );
+ return;
+ }
+
+
+ if ( node.isConjunction() ) // AND (&)
+ {
+ child.getTuple().setTag( LdapTag.CONTEXT_SPECIFIC_TAG_0, false );
+ }
+ else if ( node.isDisjunction() ) // OR (|)
+ {
+ child.getTuple().setTag( LdapTag.CONTEXT_SPECIFIC_TAG_1, false );
+ }
+
+ child.getTuple().setLength( Length.INDEFINATE );
+
+ ArrayList children = node.getChildren();
+ for( int ii = 0; ii < children.size(); ii++ )
+ {
+ encode( child, ( ExprNode ) children.get( ii ) );
+ }
+
+ parent.addLast( child );
+ child.setParent( parent );
+ }
+
+
+ /**
+ * Encodes a Filter expression tree composed of ExprNodes into a parent
+ * tlv tree node.
+ *
+ * @param parent the parent TupleNode to add TupleNodes to while encoding
+ * @param filter the root of the filter expression tree
+ */
+ private void encode( DefaultMutableTupleNode parent, ExprNode filter )
+ {
+ if ( filter.isLeaf() )
+ {
+ LeafNode node = ( LeafNode ) filter;
+
+ switch( node.getAssertionType() )
+ {
+ case( LeafNode.EQUALITY ):
+ encode( parent, LdapTag.CONTEXT_SPECIFIC_TAG_3,
+ ( SimpleNode ) node );
+ break;
+ case( LeafNode.SUBSTRING ):
+ encode( parent, ( SubstringNode ) node );
+ break;
+ case( LeafNode.GREATEREQ ):
+ encode( parent, LdapTag.CONTEXT_SPECIFIC_TAG_5,
+ ( SimpleNode ) node );
+ break;
+ case( LeafNode.LESSEQ ):
+ encode( parent, LdapTag.CONTEXT_SPECIFIC_TAG_6,
+ ( SimpleNode ) node );
+ break;
+ case( LeafNode.PRESENCE ):
+ PresenceNode presence = ( PresenceNode ) node;
+ encode( parent, LdapTag.CONTEXT_SPECIFIC_TAG_7, presence );
+ case( LeafNode.APPROXIMATE ):
+ encode( parent, LdapTag.CONTEXT_SPECIFIC_TAG_8,
+ ( SimpleNode ) node );
+ break;
+ case( LeafNode.EXTENSIBLE ):
+ encode( parent, ( ExtensibleNode ) node );
+ break;
+ default:
+ throw new IllegalArgumentException(
+ "Unrecognized assertion type value: "
+ + node.getAssertionType() );
+ }
+
+ return;
+ }
+
+ encodeBranchNode( parent, ( BranchNode ) filter );
+ }
+
+
+ /**
+ * Encodes a SEQUENCE OF AttributeDescriptions (OCTET_STRINGs) representing
+ * the attributes of interest to return in a search request.
+ *
+ * @param attributes the sequence of AttributeDescriptions
+ * @return the encoded root TupleNode of the tlv tree
+ */
+ private DefaultMutableTupleNode encode( Collection attributes )
+ {
+ DefaultMutableTupleNode child = null;
+ DefaultMutableTupleNode parent =
+ new DefaultMutableTupleNode( new Tuple() );
+ parent.getTuple().setTag( UniversalTag.SEQUENCE_SEQUENCE_OF, false );
+ parent.getTuple().setLength( Length.INDEFINATE );
+
+ Iterator list = attributes.iterator();
+ while ( list.hasNext() )
+ {
+ child = ( DefaultMutableTupleNode )
+ EncoderUtils.encode( ( String ) list.next() );
+ parent.addLast( child );
+ child.setParent( parent );
+ }
+
+ return parent;
+ }
+}
Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/search/SearchRequestEncoderTest.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/search/SearchRequestEncoderTest.java Tue Aug 24 18:21:19 2004
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2004 The Apache Software Foundation
+ *
+ * Licensed 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.snickers.ldap.encoder.search;
+
+
+import org.apache.snickers.ldap.encoder.AbstractEncoderTestCase;
+import org.apache.snickers.ber.TupleNode;
+import org.apache.snickers.ber.DefaultMutableTupleNode;
+import org.apache.ldap.common.message.SearchRequest;
+import org.apache.ldap.common.message.SearchRequestImpl;
+import org.apache.ldap.common.message.DerefAliasesEnum;
+import org.apache.ldap.common.message.ScopeEnum;
+import org.apache.ldap.common.filter.FilterParserImpl;
+
+import java.io.IOException;
+import java.text.ParseException;
+
+
+/**
+ * TestCase for the SearchRequestEncoder class.
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org"> Apache Directory
+ * Project</a> $Rev$
+ */
+public class SearchRequestEncoderTest extends AbstractEncoderTestCase
+{
+ /**
+ * Tests the encode method.
+ */
+ public void testEncode()
+ {
+ FilterParserImpl parser = null;
+ try
+ {
+ parser = new FilterParserImpl();
+ }
+ catch ( IOException e )
+ {
+ e.printStackTrace();
+ }
+
+ SearchRequestImpl request = new SearchRequestImpl( 33 );
+ request.setBase( "dc=apache,dc=org" );
+ request.setDerefAliases( DerefAliasesEnum.DEREFINSEARCHING );
+
+ try
+ {
+ request.setFilter( parser.parse(
+ "(& (ou=Engineering) (l=Sunnyvale) )" ) );
+ }
+ catch ( IOException e )
+ {
+ e.printStackTrace();
+ }
+ catch ( ParseException e )
+ {
+ e.printStackTrace();
+ }
+
+ request.setScope( ScopeEnum.SINGLELEVEL );
+ request.setSizeLimit( 12 );
+ request.setTimeLimit( 300 );
+ request.setTypesOnly( true );
+
+
+ // Encode stub into tuple tree then into the accumulator
+ TupleNode node = SearchRequestEncoder.INSTANCE.encode( request );
+ encode( ( DefaultMutableTupleNode ) node );
+
+ // Test to see if original stub equals the round trip generated stub
+ assertTrue( request.equals( decode() ) );
+ }
+}