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 2009/10/06 18:16:08 UTC
svn commit: r822331 - in /directory/shared/branches/shared-schema/ldap/src:
main/java/org/apache/directory/shared/ldap/name/
main/java/org/apache/directory/shared/ldap/util/
test/java/org/apache/directory/shared/ldap/name/
test/java/org/apache/director...
Author: elecharny
Date: Tue Oct 6 16:16:08 2009
New Revision: 822331
URL: http://svn.apache.org/viewvc?rev=822331&view=rev
Log:
Added utility methods for LdapDN and Attributes creation, using varags
Modified:
directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java
Modified: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java?rev=822331&r1=822330&r2=822331&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java (original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/name/LdapDN.java Tue Oct 6 16:16:08 2009
@@ -216,6 +216,73 @@
/**
+ * Creates a new instance of LdapDN, using varargs to declare the RDNs. Each
+ * String is either a full RDN, or a couple of AttributeType DI and a value.
+ * If the String contains a '=' symbol, the the constructor will assume that
+ * the String arg contains afull RDN, otherwise, it will consider that the
+ * following arg is the value.
+ * An example of usage would be :
+ * <pre>
+ * String exampleName = "example";
+ * String baseDn = "dc=apache,dc=org";
+ *
+ * LdapDN dn = new LdapDN(
+ * "cn=Test",
+ * "ou", exampleName,
+ * baseDn);
+ * </pre>
+ *
+ * @param upNames
+ * @throws InvalidNameException
+ */
+ public LdapDN( String... upNames ) throws InvalidNameException
+ {
+ StringBuilder sb = new StringBuilder();
+ boolean valueExpected = false;
+ boolean isFirst = true;
+
+ for ( String upName : upNames )
+ {
+ if ( isFirst )
+ {
+ isFirst = false;
+ }
+ else if ( !valueExpected )
+ {
+ sb.append( ',' );
+ }
+
+ if ( !valueExpected )
+ {
+ sb.append( upName );
+
+ if ( upName.indexOf( '=' ) == -1 )
+ {
+ valueExpected = true;
+ }
+ }
+ else
+ {
+ sb.append( "=" ).append( upName );
+
+ valueExpected = false;
+ }
+ }
+
+ if ( valueExpected )
+ {
+ throw new InvalidNameException( "A value is missing on some RDN" );
+ }
+
+ // Stores the representations of a DN : internal (as a string and as a
+ // byte[]) and external.
+ upName = sb.toString();
+ LdapDnParser.parseInternal( upName, rdns );
+ normalizeInternal();
+ normalized = false;
+ }
+
+ /**
* Create a DN when deserializing it.
*
* Note : this constructor is used only by the deserialization method.
Modified: directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java?rev=822331&r1=822330&r2=822331&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java (original)
+++ directory/shared/branches/shared-schema/ldap/src/main/java/org/apache/directory/shared/ldap/util/AttributeUtils.java Tue Oct 6 16:16:08 2009
@@ -32,6 +32,7 @@
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.InvalidAttributeIdentifierException;
+import javax.naming.directory.InvalidAttributeValueException;
import org.apache.directory.shared.ldap.entry.Entry;
import org.apache.directory.shared.ldap.entry.EntryAttribute;
@@ -1324,19 +1325,73 @@
/**
- * Build a new ATtributes instance from a LDIF list of lines
- *
- * @param avas The AttributeType and Values, using a ldif format
+ * Build a new Attributes instance from a LDIF list of lines. The values can be
+ * either a complete AVA, or a couple of AttributeType ID and a value (a String or
+ * a byte[]). The following sample shows the three cases :
+ *
+ * <pre>
+ * Attribute attr = AttributeUtils.createAttributes(
+ * "objectclass: top",
+ * "cn", "My name",
+ * "jpegPhoto", new byte[]{0x01, 0x02} );
+ * </pre>
+ *
+ * @param avas The AttributeType and Values, using a ldif format, or a couple of
+ * Attribute ID/Value
* @return An Attributes instance
* @throws NamingException If the data are invalid
*/
- public static Attributes createAttributes( String... avas ) throws NamingException
+ public static Attributes createAttributes( Object... avas ) throws NamingException
{
StringBuilder sb = new StringBuilder();
+ int pos = 0;
+ boolean valueExpected = false;
+
+ for ( Object ava : avas)
+ {
+ if ( !valueExpected )
+ {
+ if ( !(ava instanceof String) )
+ {
+ throw new InvalidAttributeValueException( "The Attribute ID #" + (pos+1) + " must be a String" );
+ }
+
+ String attribute = (String)ava;
+ sb.append( attribute );
+
+ if ( attribute.indexOf( ':' ) != -1 )
+ {
+ sb.append( '\n' );
+ }
+ else
+ {
+ valueExpected = true;
+ }
+ }
+ else
+ {
+ if ( ava instanceof String )
+ {
+ sb.append( ": " ).append( (String)ava ).append( '\n' );
+ }
+ else if ( ava instanceof byte[] )
+ {
+ sb.append( ":: " );
+ sb.append( new String( Base64.encode( (byte[] )ava ) ) );
+ sb.append( '\n' );
+ }
+ else
+ {
+ throw new InvalidAttributeValueException( "The Attribute value #" + (pos+1) + " must be a String or a byte[]" );
+ }
+
+ valueExpected = false;
+ }
+ }
- for ( String ava : avas)
+ if ( valueExpected )
{
- sb.append( ava ).append( '\n' );
+ throw new InvalidAttributeValueException( "A value is missing at the end" );
}
LdifAttributesReader reader = new LdifAttributesReader();
Modified: directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java?rev=822331&r1=822330&r2=822331&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java (original)
+++ directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/name/LdapDNTest.java Tue Oct 6 16:16:08 2009
@@ -20,6 +20,12 @@
package org.apache.directory.shared.ldap.name;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
@@ -42,21 +48,12 @@
import javax.naming.ldap.LdapName;
import org.apache.directory.shared.ldap.constants.SchemaConstants;
-import org.apache.directory.shared.ldap.name.LdapDN;
-import org.apache.directory.shared.ldap.name.LdapDnParser;
-import org.apache.directory.shared.ldap.name.Rdn;
import org.apache.directory.shared.ldap.schema.normalizers.DeepTrimToLowerNormalizer;
import org.apache.directory.shared.ldap.schema.normalizers.OidNormalizer;
import org.apache.directory.shared.ldap.util.StringTools;
import org.junit.Before;
import org.junit.Test;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
-import static org.junit.Assert.assertSame;
-
/**
* Test the class LdapDN
@@ -697,6 +694,22 @@
assertEquals( 3, dn.size() );
}
+
+ @Test
+ public void testLdapDNAddVarargs() throws InvalidNameException
+ {
+ String c = "C";
+ String dd = "D = D";
+
+ LdapDN dn = new LdapDN(
+ "a = A",
+ "b", "B",
+ "c", c,
+ dd );
+
+ assertEquals( "a=A,b=B,c=C,d=D", dn.toString() );
+ assertEquals( "a = A,b=B,c=C,D = D", dn.getUpName() );
+ }
/**
* test Add a composite RDN to an existing LdapDN
Modified: directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java?rev=822331&r1=822330&r2=822331&view=diff
==============================================================================
--- directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java (original)
+++ directory/shared/branches/shared-schema/ldap/src/test/java/org/apache/directory/shared/ldap/util/AttributeUtilsTest.java Tue Oct 6 16:16:08 2009
@@ -19,10 +19,18 @@
*/
package org.apache.directory.shared.ldap.util;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
import java.util.HashSet;
import java.util.Set;
import javax.naming.NamingException;
+import javax.naming.directory.Attributes;
+import javax.naming.directory.InvalidAttributeValueException;
import org.apache.directory.shared.ldap.entry.Entry;
import org.apache.directory.shared.ldap.entry.EntryAttribute;
@@ -32,12 +40,7 @@
import org.apache.directory.shared.ldap.entry.client.ClientModification;
import org.apache.directory.shared.ldap.entry.client.DefaultClientAttribute;
import org.apache.directory.shared.ldap.entry.client.DefaultClientEntry;
-
import org.junit.Test;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertNull;
/**
* A test case for the AttributeUtils methods
@@ -394,5 +397,37 @@
assertNotNull( entry.get( "cn" ) );
assertNull( entry.get( "ou" ) );
}
+
+ @Test
+ public void testCreateAttributesVarargs() throws NamingException
+ {
+ String mOid = "m-oid: 1.2.3.4";
+ String description = "description";
+
+ Attributes attrs = AttributeUtils.createAttributes(
+ "objectClass: top",
+ "objectClass: metaTop",
+ "objectClass: metaSyntax",
+ mOid,
+ "m-description", description );
+
+ assertEquals( "top", attrs.get( "objectClass" ).get( 0 ) );
+ assertEquals( "metaTop", attrs.get( "objectClass" ).get( 1 ) );
+ assertEquals( "metaSyntax", attrs.get( "objectClass" ).get( 2 ) );
+ assertEquals( "1.2.3.4", attrs.get( "m-oid" ).get() );
+ assertEquals( "description", attrs.get( "m-description" ).get() );
+
+ try
+ {
+ AttributeUtils.createAttributes(
+ "objectClass", "top",
+ "objectClass" );
+ fail();
+ }
+ catch ( InvalidAttributeValueException iave )
+ {
+ assertTrue( true );
+ }
+ }
}