下面列出了javax.naming.directory.BasicAttribute#add ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
@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");
}
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;
}
};
}
};
}
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;
}
};
}
};
}
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;
}
};
}
};
}
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;
}
};
}
};
}
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;
}
};
}
};
}
@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");
}
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;
}
};
}
};
}
@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");
}
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;
}
};
}
};
}
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;
}
};
}
};
}
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;
}
};
}
};
}
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;
}
@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");
}
@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 );
}
}
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;
}
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;
}
@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");
}
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);
}