Author: elecharny Date: Mon Apr 26 22:20:42 2010 New Revision: 938260 URL: http://svn.apache.org/viewvc?rev=938260&view=rev Log: Moved the hasObjectClass() and set( AttributeType...) methods Modified: directory/apacheds/trunk/core-entry/src/test/java/org/apache/directory/server/core/entry/DefaultServerEntryTest.java directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntryTest.java Modified: directory/apacheds/trunk/core-entry/src/test/java/org/apache/directory/server/core/entry/DefaultServerEntryTest.java URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/core-entry/src/test/java/org/apache/directory/server/core/entry/DefaultServerEntryTest.java?rev=938260&r1=938259&r2=938260&view=diff ============================================================================== --- directory/apacheds/trunk/core-entry/src/test/java/org/apache/directory/server/core/entry/DefaultServerEntryTest.java (original) +++ directory/apacheds/trunk/core-entry/src/test/java/org/apache/directory/server/core/entry/DefaultServerEntryTest.java Mon Apr 26 22:20:42 2010 @@ -1833,7 +1833,7 @@ public class DefaultServerEntryTest assertTrue( entry.hasObjectClass( "top" ) ); assertTrue( entry.hasObjectClass( "person" ) ); assertFalse( entry.hasObjectClass( "inetorgperson" ) ); - assertFalse( entry.hasObjectClass( null ) ); + assertFalse( entry.hasObjectClass( (String)null ) ); assertFalse( entry.hasObjectClass( "" ) ); } @@ -3610,10 +3610,18 @@ public class DefaultServerEntryTest { DN dn = new DN( "cn=test" ); DefaultServerEntry entry = new DefaultServerEntry( schemaManager, dn ); + List result = null; // First check that this method fails if we pass a null or empty ID - List result = entry.set( (String)null ); - assertNull( result ); + try + { + result = entry.set( (String)null ); + fail(); + } + catch ( IllegalArgumentException iae ) + { + // expected + } result = entry.set( " " ); assertNull( result ); @@ -3895,36 +3903,5 @@ public class DefaultServerEntryTest assertTrue( copyEntry.contains( "objectClass", "top", "person" ) ); assertTrue( copyEntry.contains( "cn", "test1", "test2" ) ); } - - - /** - * Test the conversion method - */ - @Test - @Ignore - public void testToClientEntry() throws LdapException - { - DN dn = new DN( "ou=system" ); - ServerEntry serverEntry = new DefaultServerEntry( schemaManager ); - serverEntry.setDn( dn ); - serverEntry.add( "cn", "test1", "test2" ); - serverEntry.add( "objectClass", "top", "person" ); - - Entry clientEntry = serverEntry.toClientEntry(); - - assertTrue( clientEntry instanceof Entry ); - assertFalse( clientEntry instanceof ServerEntry ); - - assertTrue( clientEntry.containsAttribute( "cn", "objectClass" ) ); - assertEquals( dn, clientEntry.getDn() ); - - serverEntry.removeAttributes( "cn" ); - assertTrue( clientEntry - .contains( "cn", "test1", "test2" ) ); - - serverEntry.remove( "objectClass", "person" ); - assertTrue( clientEntry - .contains( "objectClass", "top", "person" ) ); - } } Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java?rev=938260&r1=938259&r2=938260&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java (original) +++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/DefaultServerEntry.java Mon Apr 26 22:20:42 2010 @@ -22,9 +22,7 @@ package org.apache.directory.shared.ldap import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; -import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Map; import org.apache.directory.shared.i18n.I18n; @@ -400,171 +398,6 @@ public final class DefaultServerEntry ex //------------------------------------------------------------------------- // API //------------------------------------------------------------------------- - /** - * Tells if an entry has a specific ObjectClass Attribute - * - * @param objectClass The ObjectClass we want to check - * @return true if the ObjectClass value is present - * in the ObjectClass attribute - */ - public boolean hasObjectClass( EntryAttribute objectClass ) - { - if ( objectClass == null ) - { - return false; - } - - // We have to check that we are checking the ObjectClass attributeType - if ( !((EntryAttribute)objectClass).getAttributeType().equals( OBJECT_CLASS_AT ) ) - { - return false; - } - - EntryAttribute attribute = attributes.get( OBJECT_CLASS_AT.getOid() ); - - if ( attribute == null ) - { - // The entry does not have an ObjectClass attribute - return false; - } - - for ( Value value:objectClass ) - { - // Loop on all the values, and check if they are present - if ( !attribute.contains( value.getString() ) ) - { - return false; - } - } - - return true; - } - - - /** - *

- * Put some new attributes using the attributeTypes. - * No value is inserted. - *

- *

- * If an existing Attribute is found, it will be replaced by an - * empty attribute, and returned to the caller. - *

- * - * @param attributeTypes The AttributeTypes to add. - * @return A list of replaced Attributes, of null if no attribute are removed. - */ - public List set( AttributeType... attributeTypes ) - { - List removed = new ArrayList(); - - // Now, loop on all the attributeType to add - for ( AttributeType attributeType:attributeTypes ) - { - if ( attributeType == null ) - { - String message = I18n.err( I18n.ERR_04467 ); - LOG.error( message ); - continue; - } - - EntryAttribute attribute = attributes.put( attributeType.getOid(), new DefaultEntryAttribute( attributeType ) ); - - if ( attribute != null ) - { - removed.add( attribute ); - } - } - - if ( removed.size() == 0 ) - { - return null; - } - else - { - return removed; - } - } - - - /** - *

- * Put some new EntryAttribute using the User Provided ID. - * No value is inserted. - *

- *

- * If an existing Attribute is found, it will be replaced by an - * empty attribute, and returned to the caller. - *

- * - * @param upIds The user provided IDs of the AttributeTypes to add. - * @return A list of replaced Attributes. - */ - public List set( String... upIds ) - { - List removed = new ArrayList(); - - for ( String upId:upIds ) - { - // Search for the corresponding AttributeType, based on the upID - AttributeType attributeType = null; - - try - { - attributeType = getAttributeType( upId ); - } - catch ( LdapException ne ) - { - LOG.warn( "Trying to add a bad attribute type '{}', error : ", upId, ne.getLocalizedMessage() ); - continue; - } - catch ( IllegalArgumentException iae ) - { - LOG.warn( "Trying to add a bad attribute type '{}', error : ", upId, iae.getLocalizedMessage() ); - continue; - } - - EntryAttribute attribute = attributes.put( attributeType.getOid(), - new DefaultEntryAttribute( upId, attributeType )); - - if ( attribute != null ) - { - removed.add( attribute ); - } - } - - if ( removed.size() == 0 ) - { - return null; - } - else - { - return removed; - } - } - - - /** - * Convert the ServerEntry to a ClientEntry - * - * @return An instance of ClientEntry - */ - public Entry toClientEntry() throws LdapException - { - // Copy the DN - Entry clientEntry = new DefaultClientEntry( dn ); - - // Convert each attribute - for ( EntryAttribute serverAttribute:this ) - { - EntryAttribute clientAttribute = serverAttribute.clone(); - clientEntry.add( clientAttribute ); - } - - return clientEntry; - } - - //------------------------------------------------------------------------- // Object methods //------------------------------------------------------------------------- Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java?rev=938260&r1=938259&r2=938260&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java (original) +++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Entry.java Mon Apr 26 22:20:42 2010 @@ -78,6 +78,16 @@ public interface Entry extends Cloneable /** + * Tells if an entry has a specific ObjectClass Attribute + * + * @param objectClass The ObjectClass we want to check + * @return true if the ObjectClass value is present + * in the ObjectClass attribute + */ + boolean hasObjectClass( EntryAttribute objectClass ); + + + /** *

* Returns the attribute with the specified alias. The return value * is null if no match is found. @@ -136,6 +146,22 @@ public interface Entry extends Cloneable /** + *

+ * Put some new attributes using the attributeTypes. + * No value is inserted. + *

+ *

+ * If an existing Attribute is found, it will be replaced by an + * empty attribute, and returned to the caller. + *

+ * + * @param attributeTypes The AttributeTypes to add. + * @return A list of replaced Attributes, of null if no attribute are removed. + */ + List set( AttributeType... attributeTypes ); + + + /** * Set this entry's DN. * * @param dn The DN associated with this entry Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java?rev=938260&r1=938259&r2=938260&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java (original) +++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/ServerEntry.java Mon Apr 26 22:20:42 2010 @@ -35,41 +35,7 @@ import org.apache.directory.shared.ldap. public interface ServerEntry extends Entry { /** - * Tells if an entry has a specific ObjectClass Attribute - * - * @param objectClass The ObjectClass we want to check - * @return true if the ObjectClass value is present - * in the ObjectClass attribute - */ - boolean hasObjectClass( EntryAttribute objectClass ); - - - /** - *

- * Put some new attributes using the attributeTypes. - * No value is inserted. - *

- *

- * If an existing Attribute is found, it will be replaced by an - * empty attribute, and returned to the caller. - *

- * - * @param attributeTypes The AttributeTypes to add. - * @return A list of replaced Attributes, of null if no attribute are removed. - */ - List set( AttributeType... attributeTypes ); - - - /** * A clone method to produce a clone of the current object */ Entry clone(); - - - /** - * Convert the ServerEntry to a ClientEntry - * - * @return An instance of ClientEntry - */ - Entry toClientEntry() throws LdapException; } Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java?rev=938260&r1=938259&r2=938260&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java (original) +++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntry.java Mon Apr 26 22:20:42 2010 @@ -1177,38 +1177,124 @@ public class DefaultClientEntry implemen */ public List set( String... upIds ) { - if ( upIds == null ) + if ( ( upIds == null ) || ( upIds.length == 0 ) ) { String message = I18n.err( I18n.ERR_04135 ); LOG.error( message ); throw new IllegalArgumentException( message ); } - List returnedClientAttributes = new ArrayList(); + List removed = new ArrayList(); + boolean added = false; + + if ( schemaManager == null ) + { + // Now, loop on all the attributeType to add + for ( String upId:upIds ) + { + if ( upId == null ) + { + String message = I18n.err( I18n.ERR_04135 ); + LOG.info( message ); + throw new IllegalArgumentException( message ); + } + + String id = getId( upId ); + + if ( attributes.containsKey( id ) ) + { + // Add the removed serverAttribute to the list + removed.add( attributes.remove( id ) ); + } + + EntryAttribute newAttribute = new DefaultEntryAttribute( upId ); + attributes.put( id, newAttribute ); + added = true; + } + } + else + { + for ( String upId:upIds ) + { + if ( upId == null ) + { + String message = I18n.err( I18n.ERR_04135 ); + LOG.info( message ); + throw new IllegalArgumentException( message ); + } + + // Search for the corresponding AttributeType, based on the upID + AttributeType attributeType = null; + + try + { + attributeType = getAttributeType( upId ); + } + catch ( LdapException ne ) + { + LOG.warn( "Trying to add a bad attribute type '{}', error : ", upId, ne.getLocalizedMessage() ); + continue; + } + catch ( IllegalArgumentException iae ) + { + LOG.warn( "Trying to add a bad attribute type '{}', error : ", upId, iae.getLocalizedMessage() ); + continue; + } + + String oid = attributeType.getOid(); + + if ( attributes.containsKey( oid ) ) + { + removed.add( attributes.get( oid ) ); + } + + attributes.put( oid, new DefaultEntryAttribute( upId, attributeType ) ); + added = true; + } + } + + if ( ( !added ) || ( removed.size() == 0 ) ) + { + return null; + } + + return removed; + } + + + /** + * {@inheritDoc} + **/ + public List set( AttributeType... attributeTypes ) + { + List removed = new ArrayList(); // Now, loop on all the attributeType to add - for ( String upId:upIds ) + for ( AttributeType attributeType:attributeTypes ) { - String id = StringTools.trim( StringTools.toLowerCase( upId ) ); - - if ( id == null ) + if ( attributeType == null ) { - String message = I18n.err( I18n.ERR_04136 ); + String message = I18n.err( I18n.ERR_04467 ); LOG.error( message ); - throw new IllegalArgumentException( message ); + continue; } - if ( attributes.containsKey( id ) ) + EntryAttribute attribute = attributes.put( attributeType.getOid(), new DefaultEntryAttribute( attributeType ) ); + + if ( attribute != null ) { - // Add the removed serverAttribute to the list - returnedClientAttributes.add( attributes.remove( id ) ); + removed.add( attribute ); } - - EntryAttribute newAttribute = new DefaultEntryAttribute( upId ); - attributes.put( id, newAttribute ); } - return returnedClientAttributes; + if ( removed.size() == 0 ) + { + return null; + } + else + { + return removed; + } } @@ -2224,6 +2310,43 @@ public class DefaultClientEntry implemen /** + * {@inheritDoc} + */ + public boolean hasObjectClass( EntryAttribute objectClass ) + { + if ( objectClass == null ) + { + return false; + } + + // We have to check that we are checking the ObjectClass attributeType + if ( !objectClass.getAttributeType().equals( OBJECT_CLASS_AT ) ) + { + return false; + } + + EntryAttribute attribute = attributes.get( OBJECT_CLASS_AT.getOid() ); + + if ( attribute == null ) + { + // The entry does not have an ObjectClass attribute + return false; + } + + for ( Value value:objectClass ) + { + // Loop on all the values, and check if they are present + if ( !attribute.contains( value.getString() ) ) + { + return false; + } + } + + return true; + } + + + /** * @see Object#equals(Object) */ public boolean equals( Object o ) Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntryTest.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntryTest.java?rev=938260&r1=938259&r2=938260&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntryTest.java (original) +++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/client/DefaultClientEntryTest.java Mon Apr 26 22:20:42 2010 @@ -776,7 +776,7 @@ public class DefaultClientEntryTest assertTrue( entry.hasObjectClass( "top" ) ); assertTrue( entry.hasObjectClass( "person" ) ); assertFalse( entry.hasObjectClass( "inetorgperson" ) ); - assertFalse( entry.hasObjectClass( null ) ); + assertFalse( entry.hasObjectClass( (String)null ) ); assertFalse( entry.hasObjectClass( "" ) ); }