Java Code Examples for javax.naming.directory.BasicAttribute#add()

The following examples show how to use javax.naming.directory.BasicAttribute#add() . 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: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 7 votes vote down vote up
@Test
public void testRemoveOneOfSeveralDnAttributeSyntacticallyEqual() throws NamingException {
    BasicAttributes attributes = new BasicAttributes();
    BasicAttribute attribute = new BasicAttribute("uniqueMember", "cn=john doe,OU=company");
    attribute.add("cn=jane doe, ou=company");
    attributes.put(attribute);

    DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
    tested.setUpdateMode(true);

    tested.removeAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
    ModificationItem[] modificationItems = tested.getModificationItems();
    assertThat(modificationItems.length).isEqualTo(1);

    ModificationItem modificationItem = modificationItems[0];
    assertThat(modificationItem.getModificationOp()).isEqualTo(DirContext.REMOVE_ATTRIBUTE);
    assertThat(modificationItem.getAttribute().getID()).isEqualTo("uniqueMember");
    assertThat(modificationItem.getAttribute().get()).isEqualTo("cn=john doe,OU=company");
}
 
Example 2
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveAttributeValueAttributeWithOtherAndSameValueExists()
		throws NamingException {
	BasicAttribute basicAttribute = new BasicAttribute("abc");
	basicAttribute.add("123");
	basicAttribute.add("321");
	tested.setAttribute(basicAttribute);

	// Perform test
	tested.removeAttributeValue("abc", "123");

	Attributes attributes = tested.getAttributes();
	Attribute attr = attributes.get("abc");
	assertThat(attr).isNotNull();
	assertThat(attr.size()).isEqualTo(1);
	assertThat(attr.get()).isEqualTo("321");
}
 
Example 3
Source File: NamingManager.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static Context getURLContext(
        String scheme, Hashtable<?,?> environment)
        throws NamingException {
    return new DnsContext("", null, new Hashtable<String,String>()) {
        public Attributes getAttributes(String name, String[] attrIds)
                throws NamingException {
            return new BasicAttributes() {
                public Attribute get(String attrID) {
                    BasicAttribute ba  = new BasicAttribute(attrID);
                    ba.add("1 1 99 b.com.");
                    ba.add("0 0 88 a.com.");    // 2nd has higher priority
                    return ba;
                }
            };
        }
    };
}
 
Example 4
Source File: NamingManager.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static Context getURLContext(
        String scheme, Hashtable<?,?> environment)
        throws NamingException {
    return new DnsContext("", null, new Hashtable<String,String>()) {
        public Attributes getAttributes(String name, String[] attrIds)
                throws NamingException {
            return new BasicAttributes() {
                public Attribute get(String attrID) {
                    BasicAttribute ba  = new BasicAttribute(attrID);
                    ba.add("1 1 99 b.com.");
                    ba.add("0 0 88 a.com.");    // 2nd has higher priority
                    return ba;
                }
            };
        }
    };
}
 
Example 5
Source File: NamingManager.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
public static Context getURLContext(
        String scheme, Hashtable<?,?> environment)
        throws NamingException {
    return new DnsContext("", null, new Hashtable<String,String>()) {
        public Attributes getAttributes(String name, String[] attrIds)
                throws NamingException {
            return new BasicAttributes() {
                public Attribute get(String attrID) {
                    BasicAttribute ba  = new BasicAttribute(attrID);
                    ba.add("1 1 99 b.com.");
                    ba.add("0 0 88 a.com.");    // 2nd has higher priority
                    return ba;
                }
            };
        }
    };
}
 
Example 6
Source File: LdapUtilsTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testCollectAttributeValues() {
    String expectedAttributeName = "someAttribute";
    BasicAttribute expectedAttribute = new BasicAttribute(expectedAttributeName);
    expectedAttribute.add("value1");
    expectedAttribute.add("value2");

    BasicAttributes attributes = new BasicAttributes();
    attributes.put(expectedAttribute);

    LinkedList list = new LinkedList();
    LdapUtils.collectAttributeValues(attributes, expectedAttributeName, list);

    assertThat(list).hasSize(2);
    assertThat(list.get(0)).isEqualTo("value1");
    assertThat(list.get(1)).isEqualTo("value2");
}
 
Example 7
Source File: NamingManager.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public static Context getURLContext(
        String scheme, Hashtable<?,?> environment)
        throws NamingException {
    return new DnsContext("", null, new Hashtable<String,String>()) {
        public Attributes getAttributes(String name, String[] attrIds)
                throws NamingException {
            return new BasicAttributes() {
                public Attribute get(String attrID) {
                    BasicAttribute ba  = new BasicAttribute(attrID);
                    ba.add("1 1 99 b.com.");
                    ba.add("0 0 88 a.com.");    // 2nd has higher priority
                    return ba;
                }
            };
        }
    };
}
 
Example 8
Source File: NamingManager.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public static Context getURLContext(
        String scheme, Hashtable<?,?> environment)
        throws NamingException {
    return new DnsContext("", null, new Hashtable<String,String>()) {
        public Attributes getAttributes(String name, String[] attrIds)
                throws NamingException {
            return new BasicAttributes() {
                public Attribute get(String attrID) {
                    BasicAttribute ba  = new BasicAttribute(attrID);
                    ba.add("1 1 99 b.com.");
                    ba.add("0 0 88 a.com.");    // 2nd has higher priority
                    return ba;
                }
            };
        }
    };
}
 
Example 9
Source File: NamingManager.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static Context getURLContext(
        String scheme, Hashtable<?,?> environment)
        throws NamingException {
    return new InitialDirContext() {
        public Attributes getAttributes(String name, String[] attrIds)
                throws NamingException {
            return new BasicAttributes() {
                public Attribute get(String attrID) {
                    BasicAttribute ba  = new BasicAttribute(attrID);
                    ba.add("1 1 99 b.com.");
                    ba.add("0 0 88 a.com.");    // 2nd has higher priority
                    return ba;
                }
            };
        }
    };
}
 
Example 10
Source File: NamingManager.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static Context getURLContext(
        String scheme, Hashtable<?,?> environment)
        throws NamingException {
    return new DnsContext("", null, new Hashtable<String,String>()) {
        public Attributes getAttributes(String name, String[] attrIds)
                throws NamingException {
            return new BasicAttributes() {
                public Attribute get(String attrID) {
                    BasicAttribute ba  = new BasicAttribute(attrID);
                    ba.add("1 1 99 b.com.");
                    ba.add("0 0 88 a.com.");    // 2nd has higher priority
                    return ba;
                }
            };
        }
    };
}
 
Example 11
Source File: NamingManager.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public static Context getURLContext(
        String scheme, Hashtable<?,?> environment)
        throws NamingException {
    return new DnsContext("", null, new Hashtable<String,String>()) {
        public Attributes getAttributes(String name, String[] attrIds)
                throws NamingException {
            return new BasicAttributes() {
                public Attribute get(String attrID) {
                    BasicAttribute ba  = new BasicAttribute(attrID);
                    ba.add("1 1 99 b.com.");
                    ba.add("0 0 88 a.com.");    // 2nd has higher priority
                    return ba;
                }
            };
        }
    };
}
 
Example 12
Source File: NamingManager.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public static Context getURLContext(
        String scheme, Hashtable<?,?> environment)
        throws NamingException {
    return new DnsContext("", null, new Hashtable<String,String>()) {
        public Attributes getAttributes(String name, String[] attrIds)
                throws NamingException {
            return new BasicAttributes() {
                public Attribute get(String attrID) {
                    BasicAttribute ba  = new BasicAttribute(attrID);
                    ba.add("1 1 99 b.com.");
                    ba.add("0 0 88 a.com.");    // 2nd has higher priority
                    return ba;
                }
            };
        }
    };
}
 
Example 13
Source File: DefaultIncrementalAttributesMapper.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Override
public final Attributes getCollectedAttributes() {
    BasicAttributes attributes = new BasicAttributes();

    Set<String> attributeNames = stateMap.keySet();
    for (String attributeName : attributeNames) {
        BasicAttribute oneAttribute = new BasicAttribute(attributeName);
        List<Object> values = getValues(attributeName);
        if (values != null) {
            for (Object oneValue : values) {
                oneAttribute.add(oneValue);
            }
        }

        attributes.put(oneAttribute);
    }

    return attributes;
}
 
Example 14
Source File: LdapTemplateModifyITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private Attributes setupAttributes() {
	Attributes attributes = new BasicAttributes();
	BasicAttribute ocattr = new BasicAttribute("objectclass");
	ocattr.add("top");
	ocattr.add("person");
	attributes.put(ocattr);
	attributes.put("cn", "Some Person4");
	attributes.put("sn", "Person4");
	attributes.put("description", "Some other description");
	return attributes;
}
 
Example 15
Source File: ModifyAttributesOperationRecorderTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCompensatingModificationItem_ReplaceExistingAttribute()
        throws NamingException {
    BasicAttribute attribute = new BasicAttribute("someattr");
    attribute.add("value1");
    attribute.add("value2");
    Attributes attributes = new BasicAttributes();
    attributes.put(attribute);

    BasicAttribute modificationAttribute = new BasicAttribute("someattr");
    modificationAttribute.add("newvalue1");
    modificationAttribute.add("newvalue2");
    ModificationItem originalItem = new ModificationItem(
            DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("someattr"));

    // Perform test
    ModificationItem result = tested.getCompensatingModificationItem(
            attributes, originalItem);

    // Verify result
    assertThat(result.getModificationOp()).isEqualTo(DirContext.REPLACE_ATTRIBUTE);
    Attribute resultAttribute = result.getAttribute();
    assertThat(resultAttribute.getID()).isEqualTo("someattr");
    Object object = resultAttribute.get(0);
    assertThat(object).isEqualTo("value1");
    assertThat(resultAttribute.get(1)).isEqualTo("value2");
}
 
Example 16
Source File: JNDIProviderImpl.java    From ldapchai with GNU Lesser General Public License v2.1 5 votes vote down vote up
@LdapOperation
@ModifyOperation
public final void createEntry( final String entryDN, final Set<String> baseObjectClasses, final Map<String, String> stringAttributes )
        throws ChaiOperationException, ChaiUnavailableException
{
    activityPreCheck();
    getInputValidator().createEntry( entryDN, baseObjectClasses, stringAttributes );

    final Attributes attrs = new BasicAttributes();

    //Put in the base object class an attribute
    final BasicAttribute objectClassAttr = new BasicAttribute( ChaiConstant.ATTR_LDAP_OBJECTCLASS );
    for ( final String loopClass : baseObjectClasses )
    {
        objectClassAttr.add( loopClass );
    }
    attrs.put( objectClassAttr );

    //Add each of the attributes required.
    for ( final Map.Entry<String, String> entry : stringAttributes.entrySet() )
    {
        attrs.put( entry.getKey(), entry.getValue() );
    }

    // Create the object.
    final DirContext ldapConnection = getLdapConnection();
    try
    {
        ldapConnection.createSubcontext( addJndiEscape( entryDN ), attrs );
    }
    catch ( NamingException e )
    {
        convertNamingException( e );
    }
}
 
Example 17
Source File: LdapTemplateBindUnbindITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private Attributes setupAttributes() {
	Attributes attributes = new BasicAttributes();
	BasicAttribute ocattr = new BasicAttribute("objectclass");
	ocattr.add("top");
	ocattr.add("person");
	attributes.put(ocattr);
	attributes.put("cn", "Some Person4");
	attributes.put("sn", "Person4");
	return attributes;
}
 
Example 18
Source File: LDAPIdentityStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private BasicAttribute createBasicAttribute(String attrName, Set<String> attrValue) {
    BasicAttribute attr = new BasicAttribute(attrName);

    for (String value : attrValue) {
        if (value == null || value.trim().length() == 0) {
            value = LDAPConstants.EMPTY_ATTRIBUTE_VALUE;
        }

        attr.add(value);
    }

    return attr;
}
 
Example 19
Source File: ModifyAttributesOperationRecorderTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCompensatingModificationItem_RemoveTwoAttributeValues()
        throws NamingException {
    BasicAttribute attribute = new BasicAttribute("someattr");
    attribute.add("value1");
    attribute.add("value2");
    attribute.add("value3");
    Attributes attributes = new BasicAttributes();
    attributes.put(attribute);

    BasicAttribute modificationAttribute = new BasicAttribute("someattr");
    modificationAttribute.add("value1");
    modificationAttribute.add("value2");
    ModificationItem originalItem = new ModificationItem(
            DirContext.REMOVE_ATTRIBUTE, modificationAttribute);

    // Perform test
    ModificationItem result = tested.getCompensatingModificationItem(
            attributes, originalItem);

    // Verify result
    assertThat(result.getModificationOp()).isEqualTo(DirContext.ADD_ATTRIBUTE);
    Attribute resultAttribute = result.getAttribute();
    assertThat(resultAttribute.getID()).isEqualTo("someattr");
    Object object = resultAttribute.get(0);
    assertThat(object).isEqualTo("value1");
    assertThat(resultAttribute.get(1)).isEqualTo("value2");
}
 
Example 20
Source File: LDAPServerStoreManager.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
private void constructBasicAttributes(BasicAttributes basicAttributes, String id, String principleName,
                                      Object credential, String commonName, String surName)
        throws DirectoryServerManagerException {

    // set the objectClass type for schema
    BasicAttribute objectClass = new BasicAttribute(LDAPServerManagerConstants.LDAP_OBJECT_CLASS);
    objectClass.add(LDAPServerManagerConstants.LDAP_INTET_ORG_PERSON);
    objectClass.add(LDAPServerManagerConstants.LDAP_ORG_PERSON);
    objectClass.add(LDAPServerManagerConstants.LDAP_PERSON);
    objectClass.add(LDAPServerManagerConstants.LDAP_TOP);

    // Add Kerberos specific object classes
    objectClass.add(LDAPServerManagerConstants.LDAP_KRB5_PRINCIPLE);
    objectClass.add(LDAPServerManagerConstants.LDAP_KRB5_KDC);
    objectClass.add(LDAPServerManagerConstants.LDAP_SUB_SCHEMA);

    basicAttributes.put(objectClass);

    BasicAttribute uid = new BasicAttribute(LDAPServerManagerConstants.LDAP_UID);
    uid.add(id);
    basicAttributes.put(uid);

    String principal = getFullyQualifiedPrincipalName(principleName);

    BasicAttribute principalAttribute = new BasicAttribute
            (LDAPServerManagerConstants.KRB5_PRINCIPAL_NAME_ATTRIBUTE);
    principalAttribute.add(principal);
    basicAttributes.put(principalAttribute);

    BasicAttribute versionNumberAttribute = new BasicAttribute
            (LDAPServerManagerConstants.KRB5_KEY_VERSION_NUMBER_ATTRIBUTE);
    versionNumberAttribute.add("0");
    basicAttributes.put(versionNumberAttribute);

    BasicAttribute userPassword = new BasicAttribute(LDAPServerManagerConstants.LDAP_PASSWORD);

    //Since we are using the KDC, we will always use plain text password.
    //KDC does not support other types of passwords
    String password = getPasswordToStore((String) credential,
                                         LDAPServerManagerConstants.PASSWORD_HASH_METHOD_PLAIN_TEXT);

    userPassword.add(password.getBytes());
    basicAttributes.put(userPassword);

    if (commonName == null || commonName.isEmpty()) {
        commonName = principleName + " Service";
    }

    BasicAttribute cn = new BasicAttribute(LDAPServerManagerConstants.LDAP_COMMON_NAME);
    cn.add(commonName);
    basicAttributes.put(cn);

    BasicAttribute sn = new BasicAttribute(LDAPServerManagerConstants.SERVER_PRINCIPAL_ATTRIBUTE_NAME);
    sn.add(surName);
    basicAttributes.put(sn);
}