2013-08-28 48 views
0

我有一個存儲多個值的LDAP屬性 即用戶詳細信息(如名字,姓氏,電子郵件地址)存儲在一個帶有鍵值對的屬性中。LDAP中多值屬性中的單值

例如,屬性name ='Testuser'。這個'Testuser'屬性如下多值:firstname = test,lastname = testing [email protected]像這樣。

現在我想用java代碼單獨修改firstname值。 (找遍了很多網站在那裏我能找到使用ModificationItem單一屬性的變化)

這裏我的代碼片段:

DirContext ctx = new InitialDirContext(env); 
        SearchControls ctls = new SearchControls(); 
       ctls.setReturningObjFlag(true); 
       ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
           String filter; 
       filter = "(&(objectClass=myobjectclass)(apn=" + userName + "))"; 

           NamingEnumeration answer = ctx.search("o=mydomain", filter, ctls); 



while (answer.hasMore()) { 
    SearchResult sr = (SearchResult)answer.next(); 
     Attributes attrs = sr.getAttributes(); 
     String givenName = " "; 
try { 
       for (NamingEnumeration e = attrs.getAll(); e.hasMore();) { 

        Attribute attr = (Attribute) e.next(); 

        System.out.println("Attribute name: " + attr.getID()); 



        for (NamingEnumeration n = attr.getAll(); n.hasMore(); System.out 

          .println("value: " + n.next())); 
}} catch (Exception err) { 
givenName = " "; 
} 
    } 

我得到以下輸出:

Attribute name: apn 
value: testuser 
Attribute name: appropertycollection 
value: Profile.Contact.ZipCode=46784157 
value: Profile.Contact.State=7 
value: Profile.Contact.MobileNum=4564545455 
value: Profile.Contact.Password=12345 
value: Profile.Contact.FirstName=David 
value: Profile.Contact.Address=TestAddress456 
value: [email protected] 
value: Profile.Contact.LastName=lastname 

現在我想修改「屬性集合屬性」中的「Profile.Contact.FirstName = David」值。

幫助真的很感激。

謝謝!

回答