Welcome 微信登录

首页 / 软件开发 / JAVA / Spring LdapTemplate读取以及Freemark简单合成页面

Spring LdapTemplate读取以及Freemark简单合成页面2015-02-06客户近期频繁要求修改组织机构,维护部门苦不堪言。更新组织机构就要更新邮箱的通讯录,使用的应该是某流行的邮件系统,php的,版本挺老的,其中有个功能,在写收件人的时候输入的东西会autocomplate,这个东西维护部门需要维护一个超大的文件夹,如下图:一级目录是第一个字,二级目录是前两个字,三级目录是前三个字,以此类推,其中每个文件夹下包含一个result.php文件,内容为该级目录中所对应的全部人员。

那我需要做什么呢?对,帮助他们生成这个东西。缕了一下思路,需要用到ldap、freemark的知识。

Spring大家应该不陌生,这里ldap数据的读取,我选用的是Spring的ldapTemplate 1.3.1,配置如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=内置帐号,ou=系统,o=北京XX局"/><property name="password" value="111111"/></bean><bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"><constructor-arg ref="contextSource"/></bean>

读取Ldap这块我遇到一个难点,读取出来人如何得到这个人属于哪个组织机构,网上搜索无果,通过查看包里的类找靠谱的方法,最后终于找到了ContextMapper,可以通过获取dn来取得组织机构

public List<Person> getAllPersonNames() {// 正则表达式,为获取组织机构String regEx_script = "ou=([^,]*)?";final Pattern p = Pattern.compile(regEx_script, Pattern.DOTALL | Pattern.CASE_INSENSITIVE);// 修改模式@SuppressWarnings("unchecked")List<Person> persons = ldapTemplate.search("o=北京XX局", "(objectClass=Person)",new ContextMapper() {@Overridepublic Object mapFromContext(Object arg0) {DirContextAdapter adapter = (DirContextAdapter)arg0;Attributes attrs = adapter.getAttributes();Person bean = new Person();// 取值Attribute fullName = attrs.get("cn");Attribute mail = attrs.get("mail");Attribute title = attrs.get("title");Name dn = adapter.getDn();String org = "";// 取组织机构if(dn != null){Matcher m = p.matcher(dn.toString());MatchResult mr = null;// 获取匹配while (m.find()) {mr = m.toMatchResult();org = org + mr.group(1) + "/";}}// 给对象赋值try {bean.setFullName(fullName == null ? "" : fullName.get().toString());// ldap中有的邮箱有后缀有的没有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));} catch (NamingException e) {e.printStackTrace();}return bean;}});return persons;}