Java Code Examples for org.apache.directory.api.ldap.model.entry.Attribute#remove()

The following examples show how to use org.apache.directory.api.ldap.model.entry.Attribute#remove() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method contains( String... )
 */
@Test
public void testContainsStringArray() throws LdapException
{
    Attribute attr1 = new DefaultAttribute( atEMail );

    assertEquals( 0, attr1.size() );
    assertFalse( attr1.contains( "a" ) );
    assertFalse( attr1.contains( ( String ) null ) );

    attr1.add( ( String ) null );
    assertEquals( 1, attr1.size() );
    assertTrue( attr1.contains( ( String ) null ) );

    attr1.remove( ( String ) null );
    assertFalse( attr1.contains( ( String ) null ) );
    assertEquals( 0, attr1.size() );

    attr1.add( "a", "b", "c" );
    assertEquals( 3, attr1.size() );
    assertTrue( attr1.contains( "a" ) );
    assertTrue( attr1.contains( "b" ) );
    assertTrue( attr1.contains( "c" ) );
    assertFalse( attr1.contains( "e" ) );
    assertFalse( attr1.contains( ( String ) null ) );
}
 
Example 2
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method contains( byte[]... )
 */
@Test
public void testContainsByteArray() throws LdapException
{
    Attribute attr1 = new DefaultAttribute( atPwd );

    assertEquals( 0, attr1.size() );
    assertFalse( attr1.contains( BYTES1 ) );
    assertFalse( attr1.contains( ( byte[] ) null ) );

    attr1.add( ( byte[] ) null );
    assertEquals( 1, attr1.size() );
    assertTrue( attr1.contains( ( byte[] ) null ) );

    attr1.remove( ( byte[] ) null );
    assertFalse( attr1.contains( ( byte[] ) null ) );
    assertEquals( 0, attr1.size() );

    attr1.add( BYTES1, BYTES2, BYTES3 );
    assertEquals( 3, attr1.size() );
    assertTrue( attr1.contains( BYTES1 ) );
    assertTrue( attr1.contains( BYTES2 ) );
    assertTrue( attr1.contains( BYTES3 ) );
    assertFalse( attr1.contains( BYTES4 ) );
    assertFalse( attr1.contains( ( byte[] ) null ) );
}
 
Example 3
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test method testClone()
 */
@Test
public void testClone() throws LdapException
{
    Attribute attr = new DefaultAttribute( atDC );

    Attribute clone = attr.clone();

    assertEquals( attr, clone );
    attr.setUpId( "DomainComponent" );
    assertEquals( "0.9.2342.19200300.100.1.25", clone.getId() );

    attr.add( "a", ( String ) null, "b" );
    clone = attr.clone();
    assertEquals( attr, clone );

    attr.remove( "a" );
    assertNotSame( attr, clone );

    clone = attr.clone();
    assertEquals( attr, clone );
}
 
Example 4
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * Test the conversion method
 */
@Test
public void testToClientAttribute() throws LdapException
{
    Attribute attribute = new DefaultAttribute( atCN, "test", "test2" );

    Attribute clientAttribute = attribute.clone();

    assertTrue( clientAttribute instanceof Attribute );

    assertTrue( clientAttribute.contains( "test", "test2" ) );
    assertEquals( "2.5.4.3", clientAttribute.getId() );

    attribute.remove( "test", "test2" );
    assertTrue( clientAttribute.contains( "test", "test2" ) );
}
 
Example 5
Source File: SchemaAwareModificationSerializationTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServerModification() throws LdapException
{
    Attribute attribute = new DefaultAttribute( "cn", cnAT );
    attribute.add( "test1", "test2" );

    Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
    Modification clone = mod.clone();

    attribute.remove( "test2" );

    Attribute clonedAttribute = clone.getAttribute();

    assertEquals( 1, mod.getAttribute().size() );
    assertTrue( mod.getAttribute().contains( "TEST1" ) );

    assertEquals( 2, clonedAttribute.size() );
    assertTrue( clone.getAttribute().contains( "test1" ) );
    assertTrue( clone.getAttribute().contains( "test2" ) );
}
 
Example 6
Source File: ModificationTest.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateServerModification() throws LdapException
{
    Attribute attribute = new DefaultAttribute( "cn" );
    attribute.add( "test1", "test2" );

    Modification mod = new DefaultModification( ModificationOperation.ADD_ATTRIBUTE, attribute );
    Modification clone = mod.clone();

    attribute.remove( "test2" );

    Attribute clonedAttribute = clone.getAttribute();

    assertEquals( 1, mod.getAttribute().size() );
    assertTrue( mod.getAttribute().contains( "test1" ) );

    assertEquals( 2, clonedAttribute.size() );
    assertTrue( clone.getAttribute().contains( "test1" ) );
    assertTrue( clone.getAttribute().contains( "test2" ) );
}
 
Example 7
Source File: RangedAttributeInterceptor.java    From keycloak with Apache License 2.0 6 votes vote down vote up
private Entry prepareEntry(Entry e) {
    Attribute attr = e.get(name);
    if (attr != null) {
        int start = (min != null)? min : 0;
        start = (start < attr.size())? start : attr.size() - 1;
        int end = (max != null && max < attr.size() - 1)? max : attr.size() - 1;
        if (start != 0 || end != attr.size() - 1) {
            // some values should be stripped out
            Iterator<Value> it = attr.iterator();
            Set<Value> valuesToRemove = new HashSet<>(end - start + 1);
            for (int i = 0; i < attr.size(); i++) {
                Value v = it.next();
                if (i < start || i > end) {
                    valuesToRemove.add(v);
                }
            }
            attr.setUpId(attr.getUpId() + ";range=" + start + "-" + ((end == attr.size() - 1)? "*" : end));
            attr.remove(valuesToRemove.toArray(new Value[0]));
        } else if (min != null) {
            // range explicitly requested although no value stripped
            attr.setUpId(attr.getUpId() + ";range=0-*");
        }
    }
    return e;
}
 
Example 8
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Test method get()
 */
@Test
public void testGet() throws LdapException
{
    Attribute attr1 = new DefaultAttribute( "dc", atDC );

    attr1.add( ( String ) null );
    assertEquals( nullStringValue, attr1.get() );

    Attribute attr2 = new DefaultAttribute( "email", atEMail );

    attr2.add( "a", "b", "c" );
    assertEquals( "a", attr2.get().getString() );

    attr2.remove( "a" );
    assertEquals( "b", attr2.get().getString() );

    attr2.remove( "b" );
    assertEquals( "c", attr2.get().getString() );

    attr2.remove( "c" );
    assertNull( attr2.get() );

    Attribute attr3 = new DefaultAttribute( "userPassword", atPwd );

    attr3.add( BYTES1, BYTES2, BYTES3 );
    assertTrue( Arrays.equals( BYTES1, attr3.get().getBytes() ) );

    attr3.remove( BYTES1 );
    assertTrue( Arrays.equals( BYTES2, attr3.get().getBytes() ) );

    attr3.remove( BYTES2 );
    assertTrue( Arrays.equals( BYTES3, attr3.get().getBytes() ) );

    attr3.remove( BYTES3 );
    assertNull( attr2.get() );
}
 
Example 9
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Check a String attribute to see if there is some byte[] value in it.
 *
 * If this is the case, try to change it to a String value.
 */
private boolean checkHumanReadable( Attribute attribute ) throws LdapException
{
    boolean isModified = false;

    // Loop on each values
    for ( Value<?> value : attribute )
    {
        if ( value instanceof StringValue )
        {
            continue;
        }
        else if ( value instanceof BinaryValue )
        {
            // we have a byte[] value. It should be a String UTF-8 encoded
            // Let's transform it
            try
            {
                String valStr = new String( value.getBytes(), "UTF-8" );
                attribute.remove( value );
                attribute.add( valStr );
                isModified = true;
            }
            catch ( UnsupportedEncodingException uee )
            {
                throw new LdapException( I18n.err( I18n.ERR_281 ) );
            }
        }
        else
        {
            throw new LdapException( I18n.err( I18n.ERR_282 ) );
        }
    }

    return isModified;
}
 
Example 10
Source File: SchemaInterceptor.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Check a String attribute to see if there is some byte[] value in it.
 *
 * If this is the case, try to change it to a String value.
 */
private boolean checkHumanReadable( Attribute attribute ) throws LdapException
{
    boolean isModified = false;

    // Loop on each values
    for ( Value<?> value : attribute )
    {
        if ( value instanceof StringValue )
        {
            continue;
        }
        else if ( value instanceof BinaryValue )
        {
            // we have a byte[] value. It should be a String UTF-8 encoded
            // Let's transform it
            try
            {
                String valStr = new String( value.getBytes(), "UTF-8" );
                attribute.remove( value );
                attribute.add( valStr );
                isModified = true;
            }
            catch ( UnsupportedEncodingException uee )
            {
                throw new LdapException( I18n.err( I18n.ERR_281 ) );
            }
        }
        else
        {
            throw new LdapException( I18n.err( I18n.ERR_282 ) );
        }
    }

    return isModified;
}
 
Example 11
Source File: LdapPosixTestEnvironment.java    From camunda-bpm-platform with Apache License 2.0 5 votes vote down vote up
@Override
public void init() throws Exception {
  initializeDirectory();

  // Enable POSIX groups in ApacheDS
  Dn nis = new Dn("cn=nis,ou=schema");
  if (service.getAdminSession().exists(nis)) {
    Entry entry = service.getAdminSession().lookup(nis);
    Attribute nisDisabled = entry.get("m-disabled");
    if (null != nisDisabled && "TRUE".equalsIgnoreCase(nisDisabled.getString())) {
      nisDisabled.remove("TRUE");
      nisDisabled.add("FALSE");
      List<Modification> modifications = new ArrayList<Modification>();
      modifications.add(new DefaultModification(ModificationOperation.REPLACE_ATTRIBUTE, nisDisabled));
      service.getAdminSession().modify(nis, modifications);
      service.shutdown();
      initializeDirectory(); // Note: This instantiates service again for schema modifications to take effect.
    }
  }

  startServer();

  createGroup("office-berlin");
  createUserUid("daniel", "office-berlin", "Daniel", "Meyer", "[email protected]");

  createGroup("people");
  createUserUid("ruecker", "people", "Bernd", "Ruecker", "[email protected]");
  createUserUid("monster", "people", "Cookie", "Monster", "[email protected]");
  createUserUid("fozzie", "people", "Bear", "Fozzie", "[email protected]");

  createGroup("groups");
  createPosixGroup("1", "posix-group-without-members");
  createPosixGroup("2", "posix-group-with-members", "fozzie", "monster", "ruecker");
}
 
Example 12
Source File: SchemaAwareAttributeTest.java    From directory-ldap-api with Apache License 2.0 4 votes vote down vote up
/**
 * Test method contains( Value... ) throws LdapException
 */
@Test
public void testContainsValueArray() throws LdapException
{
    Attribute attr1 = new DefaultAttribute( atEMail );

    assertEquals( 0, attr1.size() );
    assertFalse( attr1.contains( stringValue1 ) );
    assertFalse( attr1.contains( nullStringValue ) );

    attr1.add( ( String ) null );
    assertEquals( 1, attr1.size() );
    assertTrue( attr1.contains( nullStringValue ) );

    attr1.remove( ( String ) null );
    assertFalse( attr1.contains( nullStringValue ) );
    assertEquals( 0, attr1.size() );

    attr1.add( "a", "b", "c" );
    assertEquals( 3, attr1.size() );
    assertTrue( attr1.contains( stringValue1 ) );
    assertTrue( attr1.contains( stringValue2 ) );
    assertTrue( attr1.contains( stringValue3 ) );
    assertTrue( attr1.contains( stringValue1, stringValue3 ) );
    assertFalse( attr1.contains( stringValue4 ) );
    assertFalse( attr1.contains( nullStringValue ) );

    Attribute attr2 = new DefaultAttribute( atPwd );
    assertEquals( 0, attr2.size() );
    assertFalse( attr2.contains( BYTES1 ) );
    assertFalse( attr2.contains( nullBinaryValue ) );

    attr2.add( ( byte[] ) null );
    assertEquals( 1, attr2.size() );
    assertTrue( attr2.contains( nullBinaryValue ) );

    attr2.remove( ( byte[] ) null );
    assertFalse( attr2.contains( nullBinaryValue ) );
    assertEquals( 0, attr2.size() );

    attr2.add( BYTES1, BYTES2, BYTES3 );
    assertEquals( 3, attr2.size() );
    assertTrue( attr2.contains( binaryValue1 ) );
    assertTrue( attr2.contains( binaryValue2 ) );
    assertTrue( attr2.contains( binaryValue3 ) );
    assertFalse( attr2.contains( nullBinaryValue ) );
}
 
Example 13
Source File: DefaultPartitionNexus.java    From MyVirtualDirectory with Apache License 2.0 3 votes vote down vote up
/**
 * Unregisters an ContextPartition with this BackendManager.  Called for each
 * registered Backend right befor it is to be stopped.  This prevents
 * protocol server requests from reaching the Backend and effectively puts
 * the ContextPartition's naming context offline.
 *
 * Operations against the naming context should result in an LDAP BUSY
 * result code in the returnValue if the naming context is not online.
 *
 * @param partition ContextPartition component to unregister with this
 * BackendNexus.
 * @throws Exception if there are problems unregistering the partition
 */
private void unregister( Partition partition ) throws Exception
{
    Attribute namingContexts = rootDse.get( SchemaConstants.NAMING_CONTEXTS_AT );

    if ( namingContexts != null )
    {
        namingContexts.remove( partition.getSuffixDn().getName() );
    }

    partitions.remove( partition.getSuffixDn().getName() );
}
 
Example 14
Source File: DefaultPartitionNexus.java    From MyVirtualDirectory with Apache License 2.0 3 votes vote down vote up
/**
 * Unregisters an ContextPartition with this BackendManager.  Called for each
 * registered Backend right befor it is to be stopped.  This prevents
 * protocol server requests from reaching the Backend and effectively puts
 * the ContextPartition's naming context offline.
 *
 * Operations against the naming context should result in an LDAP BUSY
 * result code in the returnValue if the naming context is not online.
 *
 * @param partition ContextPartition component to unregister with this
 * BackendNexus.
 * @throws Exception if there are problems unregistering the partition
 */
private void unregister( Partition partition ) throws Exception
{
    Attribute namingContexts = rootDse.get( SchemaConstants.NAMING_CONTEXTS_AT );

    if ( namingContexts != null )
    {
        namingContexts.remove( partition.getSuffixDn().getName() );
    }

    partitions.remove( partition.getSuffixDn().getName() );
}