Customers recently frequent requests to modify organizational structure, maintenance department miserable. Update the organization to update the mail address Book, use should be a popular mail system, PHP, the version is very old, which has a function, in writing the recipient when the input will be autocomplate, this thing maintenance department needs to maintain a very large folder, The following figure: the first level of the table of contents is a word, the two level directory is the first two words, three level directory is the first three words, and so on, where each folder contains a result.php file, the content of the level of the directory in the corresponding all the people.
So what do I need to do? Yes, help them build this thing. Wisp of thought, need to use LDAP, Freemark knowledge.
Spring everyone should be no stranger, here the reading of LDAP data, I chose the spring ldaptemplate 1.3.1, configuration such as JdbcTemplate:
<bean id= "Contextsource"
class= "Org.springframework.ldap.core.support.LdapContextSource" >
< Property name= "url" value= "ldap://192.168.101.179:389" />
<property name= "base" value= "" />
<property name= "UserDN" value= cn=platoptuser,ou= built-in account number, ou= system, o= Beijing xx Bureau " /> <property name=
" Password "value=" 111111 " />
</bean>
<bean id=" ldaptemplate "class=" Org.springframework.ldap.core.LdapTemplate ">
<constructor-arg ref=" Contextsource " />
</bean>
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
Read LDAP This piece I met a difficult point, read out how people get this person belongs to which organization, online search without fruit, by looking at the class in the bag to find a reliable method, finally found the contextmapper, can obtain the DN to obtain the organizational structure
Public list<person> Getallpersonnames () {//Regular expression, for getting organization String regex_script = "ou= ([^,]*)?"; Final pattern p = pattern.compile (Regex_script, Pattern.dotall | pattern.case_insensitive)//Modify mode @SuppressWarnings ("unchecked") list<person> persons = Ldapt
Emplate.search ("o= Beijing xx Bureau", "(Objectclass=person)", New Contextmapper () {@Override public Object Mapfromcontext (object arg0) {Dircontextadapter adapter = (Dircontextadapter) A
rg0;
Attributes attrs = Adapter.getattributes ();
Person bean = new person ();
Take value Attribute fullName = Attrs.get ("cn");
Attribute mail = Attrs.get ("Mail");
Attribute title = Attrs.get ("title");
Name DN = Adapter.getdn ();
String org = "";
Access to organizational structures if (DN!= null) {Matcher m = P.matcher (Dn.tostring ());
Matchresult Mr = NULL;
Gets a matching while (M.find ()) {Mr = M.tomatchresult ();
org = org + mr.group (1) + "/"; Assign a value to the object try {bean.setful}}// LName (fullName = null?)
"": Fullname.get (). toString ()); Some mailboxes in LDAP have suffixes and no String mailstr = Mail = = null?
"": Mail.get (). toString ();
Bean.setmail (MAILSTR); Bean.settitle (title = null?)
"": Title.get (). toString ());
Bean.setorg (Org.equals ("") "": org.substring (0, Org.length ()-1)); The catch (Namingexception e) {e.Printstacktrace ();
} return bean;
}
});
return persons; }