Return-Path: Delivered-To: apmail-incubator-directory-cvs-archive@www.apache.org Received: (qmail 67680 invoked from network); 28 Aug 2004 07:37:43 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 28 Aug 2004 07:37:43 -0000 Received: (qmail 16891 invoked by uid 500); 28 Aug 2004 07:37:42 -0000 Delivered-To: apmail-incubator-directory-cvs-archive@incubator.apache.org Received: (qmail 16851 invoked by uid 500); 28 Aug 2004 07:37:42 -0000 Mailing-List: contact directory-cvs-help@incubator.apache.org; run by ezmlm Precedence: bulk Reply-To: directory-dev@incubator.apache.org list-help: list-unsubscribe: list-post: Delivered-To: mailing list directory-cvs@incubator.apache.org Received: (qmail 16826 invoked by uid 99); 28 Aug 2004 07:37:41 -0000 X-ASF-Spam-Status: No, hits=-2.8 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [209.237.227.194] (HELO minotaur.apache.org) (209.237.227.194) by apache.org (qpsmtpd/0.27.1) with SMTP; Sat, 28 Aug 2004 00:37:41 -0700 Received: (qmail 67635 invoked by uid 65534); 28 Aug 2004 07:37:40 -0000 Date: 28 Aug 2004 07:37:40 -0000 Message-ID: <20040828073740.67631.qmail@minotaur.apache.org> From: akarasulu@apache.org To: directory-cvs@incubator.apache.org Subject: svn commit: rev 37159 - in incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src: java/org/apache/snickers/ldap/encoder java/org/apache/snickers/ldap/encoder/modify test/org/apache/snickers/ldap/encoder/modify X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N Author: akarasulu Date: Sat Aug 28 00:37:40 2004 New Revision: 37159 Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/ModificationItemEncoder.java incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/modify/ModifyRequestEncoder.java incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/modify/ModifyRequestEncoderTest.java Log: Commit changes ... o added a JNDI ModificationItem encoder o added the ModifyRequest encoder o added and passed test case for the ModifyRequest encoder Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/ModificationItemEncoder.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/ModificationItemEncoder.java Sat Aug 28 00:37:40 2004 @@ -0,0 +1,103 @@ +/* + * 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; + +import org.apache.snickers.ber.TupleNode; +import org.apache.snickers.ber.DefaultMutableTupleNode; +import org.apache.snickers.ber.Tuple; +import org.apache.snickers.ber.Length; +import org.apache.snickers.ber.primitives.UniversalTag; + +import javax.naming.directory.ModificationItem; +import javax.naming.directory.DirContext; + + +/** + * Encodes a Modification item in a ModifyRequest into a TupleNode tree. + * + * @author Apache Directory + * Project + * @version $Rev$ + */ +public class ModificationItemEncoder +{ + /** thread safe (flyweight) instance of this encoder */ + public static final ModificationItemEncoder INSTANCE = + new ModificationItemEncoder(); + + + /** + * Encodes a ModificationItem into a TupleNode tree using the following + * ASN.1. + * + * modification SEQUENCE OF SEQUENCE { + * operation ENUMERATED { + * add (0), + * delete (1), + * replace (2) }, + * modification AttributeTypeAndValues } } + * + * + * @param item the mod item being encoded + * @return the root TupleNode of the tlv tree for the mod item + */ + public TupleNode encode( ModificationItem item ) + { + DefaultMutableTupleNode child = null; + DefaultMutableTupleNode top = + new DefaultMutableTupleNode( new Tuple() ); + top.getTuple().setTag( UniversalTag.SEQUENCE_SEQUENCE_OF, false ); + top.getTuple().setLength( Length.INDEFINATE ); + + child = ( DefaultMutableTupleNode ) EncoderUtils.encode( + UniversalTag.ENUMERATED, + getLdapModOp( item.getModificationOp() ) ); + top.addLast( child ); + child.setParent( top ); + + child = ( DefaultMutableTupleNode ) AttributeEncoder. + INSTANCE.encode( item.getAttribute() ); + top.addLast( child ); + child.setParent( top ); + + return top; + } + + + /** + * Maps the JNDI ModificationItem operation to the LDAP modify operation + * ENUMERATED value. + * + * @param jndiModOp + * @return + */ + private int getLdapModOp( int jndiModOp ) + { + switch( jndiModOp ) + { + case( DirContext.ADD_ATTRIBUTE ): + return 0; + case( DirContext.REMOVE_ATTRIBUTE ): + return 1; + case( DirContext.REPLACE_ATTRIBUTE ): + return 2; + default: + throw new IllegalArgumentException( "Unrecognized JNDI " + + "ModificationItem operation" ); + } + } +} Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/modify/ModifyRequestEncoder.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/java/org/apache/snickers/ldap/encoder/modify/ModifyRequestEncoder.java Sat Aug 28 00:37:40 2004 @@ -0,0 +1,106 @@ +/* + * 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.modify; + + +import java.util.Iterator; +import javax.naming.directory.ModificationItem; + +import org.apache.ldap.common.message.ModifyRequest; + +import org.apache.snickers.ber.Tuple; +import org.apache.snickers.ber.Length; +import org.apache.snickers.ber.TupleNode; +import org.apache.snickers.ber.DefaultMutableTupleNode; +import org.apache.snickers.ber.primitives.UniversalTag; + +import org.apache.snickers.ldap.LdapTag; +import org.apache.snickers.ldap.encoder.EncoderUtils; +import org.apache.snickers.ldap.encoder.ModificationItemEncoder; + + +/** + * A ModifyRequest stub to TupleNode tree encoder. + * + * @author Apache Directory + * Project + * @version $Rev$ + */ +public class ModifyRequestEncoder +{ + /** thread safe (flyweight) instance of this encoder */ + public static final ModifyRequestEncoder INSTANCE = + new ModifyRequestEncoder(); + + + /** + * Encodes a ModifyRequest stub into a TupleNode tree. + * + * @param request the ModifyRequest stub to encode + * @return the root TupleNode of the tlv tree + */ + public TupleNode encode( ModifyRequest 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 request PDU + DefaultMutableTupleNode child = ( DefaultMutableTupleNode ) + EncoderUtils.encode( request.getMessageId() ); + top.addLast( child ); + child.setParent( top ); + + // Add modifyReq envelope sequence of node + DefaultMutableTupleNode modReq = + new DefaultMutableTupleNode( new Tuple() ); + modReq.getTuple().setTag( LdapTag.MODIFY_REQUEST, false ); + modReq.getTuple().setLength( Length.INDEFINATE ); + + // Add the LDAPDN name of the entry being modified + child = ( DefaultMutableTupleNode ) + EncoderUtils.encode( request.getName() ); + modReq.addLast( child ); + child.setParent( modReq ); + + // create and add the node containing the sequence of modifications + DefaultMutableTupleNode mods = + new DefaultMutableTupleNode( new Tuple() ); + mods.getTuple().setTag( UniversalTag.SEQUENCE_SEQUENCE_OF, false ); + mods.getTuple().setLength( Length.INDEFINATE ); + + // Add all the modification items using the ModificationItemEncoder + Iterator list = request.getModificationItems().iterator(); + while( list.hasNext() ) + { + ModificationItem item = ( ModificationItem ) list.next(); + DefaultMutableTupleNode itemNode = ( DefaultMutableTupleNode ) + ModificationItemEncoder.INSTANCE.encode( item ); + mods.addLast( itemNode ); + itemNode.setParent( mods ); + } + + modReq.addLast( mods ); + mods.setParent( modReq ); + + top.addLast( modReq ); + modReq.setParent( top ); + return top; + } +} Added: incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/modify/ModifyRequestEncoderTest.java ============================================================================== --- (empty file) +++ incubator/directory/snickers/branches/encoder-redesign/ldap-ber-provider/src/test/org/apache/snickers/ldap/encoder/modify/ModifyRequestEncoderTest.java Sat Aug 28 00:37:40 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.modify; + + +import org.apache.snickers.ber.TupleNode; +import org.apache.snickers.ber.DefaultMutableTupleNode; +import org.apache.snickers.ldap.encoder.AbstractEncoderTestCase; + +import org.apache.ldap.common.message.*; +import org.apache.commons.codec.DecoderException; + +import javax.naming.directory.ModificationItem; +import javax.naming.directory.DirContext; + + +/** + * Tests the ModifyRequest encoder. + * + * @author Apache Directory + * Project + */ +public class ModifyRequestEncoderTest extends AbstractEncoderTestCase +{ + /** + * Builds a ModifyRequest for testing purposes. + * + * @return the ModifyRequest to use for tests + */ + public ModifyRequest getRequest() + { + // Construct the Modify request to test + ModifyRequestImpl req = new ModifyRequestImpl( 45 ); + req.setName( "cn=admin,dc=apache,dc=org" ); + + LockableAttributeImpl attr = new LockableAttributeImpl( "attr0" ); + attr.add( "val0" ); + attr.add( "val1" ); + attr.add( "val2" ); + ModificationItem item = + new ModificationItem( DirContext.ADD_ATTRIBUTE, attr ); + req.addModification( item ); + + attr = new LockableAttributeImpl( "attr1" ); + attr.add( "val3" ); + item = new ModificationItem( DirContext.REMOVE_ATTRIBUTE, attr ); + req.addModification( item ); + + attr = new LockableAttributeImpl( "attr2" ); + attr.add( "val4" ); + attr.add( "val5" ); + item = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, attr ); + req.addModification( item ); + + return req; + } + + + /** + * Tests the encoder's encode() method. + */ + public void testEncode() throws DecoderException + { + ModifyRequest req = getRequest(); + + // Encode stub into tuple tree then into the accumulator + TupleNode node = ModifyRequestEncoder.INSTANCE.encode( req ); + encode( ( DefaultMutableTupleNode ) node ); + + // Test to see if original stub equals the round trip generated stub + assertTrue( req.equals( decode() ) ); + } +}