Java Code Examples for javax.naming.directory.Attributes#put()

The following examples show how to use javax.naming.directory.Attributes#put() . 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: LdapUtil.java    From herd-mdl with Apache License 2.0 5 votes vote down vote up
private static void createOu(String ou) throws NamingException {
    DirContext ldapContext = getLdapContext(User.getLdapAdminUser());
    Attributes attrs = new BasicAttributes(true);
    Attribute objclass = new BasicAttribute("objectClass");
    objclass.add("top");
    objclass.add("organizationalUnit");
    attrs.put(objclass);
    attrs.put("ou", ou);
    ldapContext.bind(constructOuDn(ou), null, attrs);
}
 
Example 2
Source File: TestLdapGroupsMapping.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Before
public void setupMocks() throws NamingException {
  mockContext = mock(DirContext.class);
  doReturn(mockContext).when(mappingSpy).getDirContext();
          
  SearchResult mockUserResult = mock(SearchResult.class);
  // We only ever call hasMoreElements once for the user NamingEnum, so 
  // we can just have one return value
  when(mockUserNamingEnum.hasMoreElements()).thenReturn(true);
  when(mockUserNamingEnum.nextElement()).thenReturn(mockUserResult);
  when(mockUserResult.getNameInNamespace()).thenReturn("CN=some_user,DC=test,DC=com");
  
  SearchResult mockGroupResult = mock(SearchResult.class);
  // We're going to have to define the loop here. We want two iterations,
  // to get both the groups
  when(mockGroupNamingEnum.hasMoreElements()).thenReturn(true, true, false);
  when(mockGroupNamingEnum.nextElement()).thenReturn(mockGroupResult);
  
  // Define the attribute for the name of the first group
  Attribute group1Attr = new BasicAttribute("cn");
  group1Attr.add(testGroups[0]);
  Attributes group1Attrs = new BasicAttributes();
  group1Attrs.put(group1Attr);
  
  // Define the attribute for the name of the second group
  Attribute group2Attr = new BasicAttribute("cn");
  group2Attr.add(testGroups[1]);
  Attributes group2Attrs = new BasicAttributes();
  group2Attrs.put(group2Attr);
  
  // This search result gets reused, so return group1, then group2
  when(mockGroupResult.getAttributes()).thenReturn(group1Attrs, group2Attrs);
}
 
Example 3
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 4
Source File: LdifAttributesReader.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * Parse an AttributeType/AttributeValue
 *
 * @param attributes The entry where to store the value
 * @param line The line to parse
 * @param lowerLine The same line, lowercased
 * @throws LdapLdifException If anything goes wrong
 */
private void parseAttribute( Attributes attributes, String line, String lowerLine ) throws LdapLdifException
{
    int colonIndex = line.indexOf( ':' );

    String attributeType = lowerLine.substring( 0, colonIndex );

    // We should *not* have a Dn twice
    if ( "dn".equals( attributeType ) )
    {
        LOG.error( I18n.err( I18n.ERR_13400_ENTRY_WITH_TWO_DNS ) );
        throw new LdapLdifException( I18n.err( I18n.ERR_13439_LDIF_ENTRY_WITH_TWO_DNS ) );
    }

    Object attributeValue = parseValue( attributeType, line, colonIndex );

    // Update the entry
    javax.naming.directory.Attribute attribute = attributes.get( attributeType );

    if ( attribute == null )
    {
        attributes.put( attributeType, attributeValue );
    }
    else
    {
        attribute.add( attributeValue );
    }
}
 
Example 5
Source File: LdapSender.java    From iaf with Apache License 2.0 5 votes vote down vote up
/**
 *Strips all the values from the attributes in <code>input</code>. This is performed to be able to delete 
 *the attributes without having to match the values. If values exist they must be exactly matched too in
 *order to delete the attribute.
 */
protected Attributes removeValuesFromAttributes(Attributes input) {
	Attributes result = new BasicAttributes(true);
	// ignore attribute name case
	NamingEnumeration enumeration = input.getIDs();
	while (enumeration.hasMoreElements()) {
		String attrId = (String) enumeration.nextElement();
		result.put(new BasicAttribute(attrId));
	}
	return result;
}
 
Example 6
Source File: DefaultIncrementalAttributesMapperTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private Attributes createAttributes(String attributeName, RangeOption range, int valueCnt) {
    Attributes attributes = new BasicAttributes();

    Attribute attribute = createRangeAttribute(attributeName, range, valueCnt);
    attributes.put(attribute);

    return attributes;
}
 
Example 7
Source File: Rdn.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieves the {@link javax.naming.directory.Attributes Attributes}
 * view of the type/value mappings contained in this Rdn.
 *
 * @return  The non-null attributes containing the type/value
 *          mappings of this Rdn.
 */
public Attributes toAttributes() {
    Attributes attrs = new BasicAttributes(true);
    for (int i = 0; i < entries.size(); i++) {
        RdnEntry entry = entries.get(i);
        Attribute attr = attrs.put(entry.getType(), entry.getValue());
        if (attr != null) {
            attr.add(entry.getValue());
            attrs.put(attr);
        }
    }
    return attrs;
}
 
Example 8
Source File: DefaultDirObjectFactoryTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetObjectInstance_ObjectNotContext() throws Exception {
	Attributes expectedAttributes = new NameAwareAttributes();
	expectedAttributes.put("someAttribute", "someValue");

	DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(new Object(), DN, null,
			new Hashtable(), expectedAttributes);

	assertThat(adapter.getDn()).isEqualTo(DN);
	assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes);
}
 
Example 9
Source File: LdapUtil.java    From herd-mdl with Apache License 2.0 5 votes vote down vote up
/**
 * create ldap user with provided user id and user password
 *
 * @param user new ldap user to create
 * @throws NamingException
 */
public static void addEntry(User user) throws NamingException {
    String username = user.getUsername();

    Attribute userCn = new BasicAttribute("cn", user.getUsername());
    Attribute userSn = new BasicAttribute("sn", "null");
    Attribute uid = new BasicAttribute("uid", user.getUsername());

    Attribute uidNumber = new BasicAttribute("uidNumber", String.valueOf(listEntries() + 1));
    Attribute gidNumber = new BasicAttribute("gidNumber", String.valueOf(1001));
    Attribute homeDirectory = new BasicAttribute("homeDirectory", "/home/" + username);
    Attribute mail = new BasicAttribute("mail", username + "@" + DOMAIN_NAME);
    Attribute loginShell = new BasicAttribute("loginShell", "/bin/bash");

    Attribute userUserPassword = new BasicAttribute("userPassword", user.getPassword());
    //ObjectClass attributes
    Attribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("inetOrgPerson");
    objectClass.add("posixAccount");

    Attributes entry = new BasicAttributes();
    entry.put(userCn);
    entry.put(userSn);
    entry.put(userUserPassword);
    entry.put(objectClass);
    entry.put(uid);

    entry.put(uidNumber);
    entry.put(gidNumber);
    entry.put(homeDirectory);
    entry.put(mail);
    entry.put(loginShell);

    String ou = user.getOu() == null ? "People" : user.getOu();
    String entryDN = constructEntryCn(user.getUsername(), ou);
    DirContext ldapContext = getLdapContext(User.getLdapAdminUser());
    ldapContext.createSubcontext(entryDN, entry);
    LOGGER.info("Added Entry :" + entryDN);
}
 
Example 10
Source File: DefaultDirObjectFactoryTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetObjectInstance_nullObject() throws Exception {
	Attributes expectedAttributes = new NameAwareAttributes();
	expectedAttributes.put("someAttribute", "someValue");

	DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(null, DN, null, new Hashtable(),
			expectedAttributes);

	assertThat(adapter.getDn()).isEqualTo(DN);
	assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes);
}
 
Example 11
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a ServerEntry into a BasicAttributes. The Dn is lost
 * during this conversion, as the Attributes object does not store
 * this element.
 *
 * @return An instance of a AttributesImpl() object
 */
public static Attributes toBasicAttributes( Entry entry )
{
    if ( entry == null )
    {
        return null;
    }

    Attributes attributes = new BasicAttributes( true );

    for ( Attribute attribute : entry.getAttributes() )
    {
        AttributeType attributeType = attribute.getAttributeType();
        Attribute attr = entry.get( attributeType );

        // Deal with a special case : an entry without any ObjectClass
        if ( attributeType.getOid().equals( SchemaConstants.OBJECT_CLASS_AT_OID ) && attr.size() == 0 )
        {
            // We don't have any objectClass, just dismiss this element
            continue;
        }

        attributes.put( toBasicAttribute( attr ) );
    }

    return attributes;
}
 
Example 12
Source File: LdapName.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
Attributes toAttributes() {
    Attributes attrs = new BasicAttributes(true);
    TypeAndValue tv;
    Attribute attr;

    for (int i = 0; i < tvs.size(); i++) {
        tv = tvs.elementAt(i);
        if ((attr = attrs.get(tv.getType())) == null) {
            attrs.put(tv.getType(), tv.getUnescapedValue());
        } else {
            attr.add(tv.getUnescapedValue());
        }
    }
    return attrs;
}
 
Example 13
Source File: Rdn.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieves the {@link javax.naming.directory.Attributes Attributes}
 * view of the type/value mappings contained in this Rdn.
 *
 * @return  The non-null attributes containing the type/value
 *          mappings of this Rdn.
 */
public Attributes toAttributes() {
    Attributes attrs = new BasicAttributes(true);
    for (int i = 0; i < entries.size(); i++) {
        RdnEntry entry = entries.get(i);
        Attribute attr = attrs.put(entry.getType(), entry.getValue());
        if (attr != null) {
            attr.add(entry.getValue());
            attrs.put(attr);
        }
    }
    return attrs;
}
 
Example 14
Source File: Rdn.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieves the {@link javax.naming.directory.Attributes Attributes}
 * view of the type/value mappings contained in this Rdn.
 *
 * @return  The non-null attributes containing the type/value
 *          mappings of this Rdn.
 */
public Attributes toAttributes() {
    Attributes attrs = new BasicAttributes(true);
    for (int i = 0; i < entries.size(); i++) {
        RdnEntry entry = entries.get(i);
        Attribute attr = attrs.put(entry.getType(), entry.getValue());
        if (attr != null) {
            attr.add(entry.getValue());
            attrs.put(attr);
        }
    }
    return attrs;
}
 
Example 15
Source File: LdapName.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
Attributes toAttributes() {
    Attributes attrs = new BasicAttributes(true);
    TypeAndValue tv;
    Attribute attr;

    for (int i = 0; i < tvs.size(); i++) {
        tv = tvs.elementAt(i);
        if ((attr = attrs.get(tv.getType())) == null) {
            attrs.put(tv.getType(), tv.getUnescapedValue());
        } else {
            attr.add(tv.getUnescapedValue());
        }
    }
    return attrs;
}
 
Example 16
Source File: Rdn.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieves the {@link javax.naming.directory.Attributes Attributes}
 * view of the type/value mappings contained in this Rdn.
 *
 * @return  The non-null attributes containing the type/value
 *          mappings of this Rdn.
 */
public Attributes toAttributes() {
    Attributes attrs = new BasicAttributes(true);
    for (int i = 0; i < entries.size(); i++) {
        RdnEntry entry = entries.get(i);
        Attribute attr = attrs.put(entry.getType(), entry.getValue());
        if (attr != null) {
            attr.add(entry.getValue());
            attrs.put(attr);
        }
    }
    return attrs;
}
 
Example 17
Source File: LdapName.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
Attributes toAttributes() {
    Attributes attrs = new BasicAttributes(true);
    TypeAndValue tv;
    Attribute attr;

    for (int i = 0; i < tvs.size(); i++) {
        tv = tvs.elementAt(i);
        if ((attr = attrs.get(tv.getType())) == null) {
            attrs.put(tv.getType(), tv.getUnescapedValue());
        } else {
            attr.add(tv.getUnescapedValue());
        }
    }
    return attrs;
}
 
Example 18
Source File: Rdn.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Retrieves the {@link javax.naming.directory.Attributes Attributes}
 * view of the type/value mappings contained in this Rdn.
 *
 * @return  The non-null attributes containing the type/value
 *          mappings of this Rdn.
 */
public Attributes toAttributes() {
    Attributes attrs = new BasicAttributes(true);
    for (int i = 0; i < entries.size(); i++) {
        RdnEntry entry = entries.get(i);
        Attribute attr = attrs.put(entry.getType(), entry.getValue());
        if (attr != null) {
            attr.add(entry.getValue());
            attrs.put(attr);
        }
    }
    return attrs;
}
 
Example 19
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetStringAttributeWhenAttributeDoesExistButWithNoValue() throws Exception {
	final Attributes attrs = new BasicAttributes();
	attrs.put(new BasicAttribute("abc"));
	class TestableDirContextAdapter extends DirContextAdapter {
		public TestableDirContextAdapter() {
			super(attrs, null);
		}
	}
	tested = new TestableDirContextAdapter();
	String s = tested.getStringAttribute("abc");
	assertThat(s).isNull();
}
 
Example 20
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetStringAttributeWhenAttributeExists() throws Exception {
	final Attributes attrs = new BasicAttributes();
	attrs.put(new BasicAttribute("abc", "def"));
	class TestableDirContextAdapter extends DirContextAdapter {
		public TestableDirContextAdapter() {
			super(attrs, null);
		}
	}
	tested = new TestableDirContextAdapter();
	String s = tested.getStringAttribute("abc");
	assertThat(s).isEqualTo("def");
}