LDAP操作
以下代码实现了XML的读写,LDAP的连接查找,返回结果为XML字符串,所有public方法参数均为字符串。
因为要用Applet进行LDAP的直接操作,考虑到综合效率问题,所以我没有一些现成的库,比如DOM4J。
DirContext环境设置XML:
<?xml version="1.0" encoding="UTF-8"?>
<environment>
<factory>com.sun.jndi.ldap.LdapCtxFactory</factory>
<providerURL</providerURL>
<principal></principal>
<authentication></authentication>
<credentials></credentials>
<version></version>
</environment>
SearchControls设置XML:
<?xml version="1.0" encoding="UTF-8"?>
<searchControls>
<countLimit></countLimit>
<derefLinkFlag></derefLinkFlag>
<returningAttributes></returningAttributes>
<returningObjFlag></returningObjFlag>
<searchScope></searchScope>
<timeLimit></timeLimit>
</searchControls>
1
import
java.applet.
*
;
2 import javax.naming. * ;
3 import javax.naming.directory. * ;
4 import java.util. * ;
5 import javax.xml.parsers. * ;
6 import org.w3c.dom. * ;
7 import java.io. * ;
8 import org.xml.sax. * ;
9 import javax.xml.transform. * ;
10 import javax.xml.transform.dom. * ;
11 import javax.xml.transform.stream. * ;
12
13 public class LdapUtil {
14 /** *//**
15 * 把XML格式字符串转换为Document
16 * @param xml XML格式字符串
17 * @return org.w3c.dom.Document
18 * @throws Exception
19 */
20 private Document lodaXmlString(String xml) throws Exception{
21 DocumentBuilder docBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
22
23 StringReader strReader=new StringReader(xml);
24 InputSource inSrc=new InputSource(strReader);
25
26 Document xmlDoc=docBuilder.parse(inSrc);
27
28 return xmlDoc;
29 }
30
31 /** *//**
32 * 把Document转换为XML格式字符串
33 * @param xmlDoc Document
34 * @return String
35 * @throws Exception
36 */
37 private String xmlDocumentToString(Document xmlDoc) throws Exception{
38 DOMSource domSource=new DOMSource(xmlDoc);
39 StringWriter writer=new StringWriter();
40 StreamResult result=new StreamResult(writer);
41 TransformerFactory tf=TransformerFactory.newInstance();
42 Transformer transformer=tf.newTransformer();
43 transformer.transform(domSource, result);
44
45 return writer.toString();
46 }
47
48 /** *//**
49 * 读取XML格式配置信息
50 * @param env XML格式配置信息
51 * @return javax.naming.directory.DirContext
52 * @throws Exception
53 */
54 private DirContext connect(String env) throws Exception{
55 Element root=this.lodaXmlString(env).getDocumentElement();
56 NodeList nodeList=root.getChildNodes();
57
58 int length=nodeList.getLength();
59
60 Properties pro=new Properties();
61
62 for(int i=0;i<length;i++){
63 Node node=nodeList.item(i);
64
65 if(node.getNodeName()=="factory"){
66 pro.put(Context.INITIAL_CONTEXT_FACTORY, node.getTextContent());
67 }
68 else if(node.getNodeName()=="providerURL"){
69 pro.put(Context.PROVIDER_URL, node.getTextContent());
70 }
71 else if(node.getNodeName()=="principal"){
72 pro.put(Context.SECURITY_PRINCIPAL, node.getTextContent());
73 }
74 else if(node.getNodeName()=="authentication"){
75 pro.put(Context.SECURITY_AUTHENTICATION, node.getTextContent());
76 }
77 else if(node.getNodeName()=="credentials"){
78 pro.put(Context.SECURITY_CREDENTIALS, node.getTextContent());
79 }
80 else if(node.getNodeName()=="version"){
81 pro.put("java.naming.ldap.version", node.getTextContent());
82 }
83
84 }
85
86 return new InitialDirContext(pro);
87 }
88
89 /** *//**
90 * 报XML字符串转换为SearchControls
91 * @param ctr
92 * @return SearchControls
93 * @throws Exception
94 */
95 private SearchControls parseXmlToSearchControls(String ctr) throws Exception{
96 Element root=this.lodaXmlString(ctr).getDocumentElement();
97
98 NodeList nodeList=root.getChildNodes();
99
100 int length=nodeList.getLength();
101
102 SearchControls sc=new SearchControls();
103
104 for(int i=0;i<length;i++){
105 Node node=nodeList.item(i);
106
107 if(node.getNodeName()=="countLimit"){
108 sc.setCountLimit(Long.parseLong(node.getTextContent()));
109 }
110 else if(node.getNodeName()=="derefLinkFlag"){
111 sc.setDerefLinkFlag(Boolean.parseBoolean(node.getTextContent()));
112 }
113 else if(node.getNodeName()=="returningAttributes"){
114 sc.setReturningAttributes(node.getTextContent().split(","));
115 }
116 else if(node.getNodeName()=="returningObjFlag"){
117 sc.setReturningObjFlag(Boolean.parseBoolean(node.getTextContent()));
118 }
119 else if(node.getNodeName()=="searchScope"){
120 sc.setSearchScope(Integer.parseInt(node.getTextContent()));
121 }
122 else if(node.getNodeName()=="searchScope"){
123 sc.setSearchScope(Integer.parseInt(node.getTextContent()));
124 }
125 else if(node.getNodeName()=="timeLimit"){
126 sc.setTimeLimit(Integer.parseInt(node.getTextContent()));
127 }
128 }
129
130 return sc;
131 }
132
133 public String search(String env,String name,String filter,String ctr){
134 DirContext ctx=null;
135 SearchControls sc=new SearchControls();
136 NamingEnumeration<SearchResult> rs=null;
137 String result="";
138
139 try{
140 ctx=this.connect(env);
141 if(ctr!=null && ctr!=""){
142 sc=this.parseXmlToSearchControls(ctr);
143 }
144
145 rs=ctx.search(name,filter,sc);
146
147 DocumentBuilder docBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
148
149 Document xmlDoc=docBuilder.newDocument();
150
151 Element rootEl=xmlDoc.createElement("searchResult");
152
153 while(rs!=null && rs.hasMore()){
154 SearchResult el=rs.next();
155 String elName=el.getName();
156 NamingEnumeration<? extends Attribute> attrs=el.getAttributes().getAll();
157
158 Element recordEl=xmlDoc.createElement("record");
159
160 recordEl.setAttribute("name", elName);
161
162 while(attrs!=null && attrs.hasMore()){
163 Attribute attr=attrs.next();
164 NamingEnumeration<?> attrEnu=attr.getAll();
165 String attrName=attr.getID();
166
167 while(attrEnu!=null && attrEnu.hasMore()){
168 Object o=attrEnu.next();
169 Element attrEl=xmlDoc.createElement("attribute");
170
171 attrEl.setAttribute("name",attrName);
172 attrEl.setTextContent(o.toString());
173
174 recordEl.appendChild(attrEl);
175 }
176 }
177
178 rootEl.appendChild(recordEl);
179 }
180
181 ctx.close();
182
183 xmlDoc.appendChild(rootEl);
184
185 result=xmlDocumentToString(xmlDoc);
186 }
187 catch(Exception ex){
188 ex.printStackTrace();
189
190 }
191
192 return result;
193 }
194}
195
2 import javax.naming. * ;
3 import javax.naming.directory. * ;
4 import java.util. * ;
5 import javax.xml.parsers. * ;
6 import org.w3c.dom. * ;
7 import java.io. * ;
8 import org.xml.sax. * ;
9 import javax.xml.transform. * ;
10 import javax.xml.transform.dom. * ;
11 import javax.xml.transform.stream. * ;
12
13 public class LdapUtil {
14 /** *//**
15 * 把XML格式字符串转换为Document
16 * @param xml XML格式字符串
17 * @return org.w3c.dom.Document
18 * @throws Exception
19 */
20 private Document lodaXmlString(String xml) throws Exception{
21 DocumentBuilder docBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
22
23 StringReader strReader=new StringReader(xml);
24 InputSource inSrc=new InputSource(strReader);
25
26 Document xmlDoc=docBuilder.parse(inSrc);
27
28 return xmlDoc;
29 }
30
31 /** *//**
32 * 把Document转换为XML格式字符串
33 * @param xmlDoc Document
34 * @return String
35 * @throws Exception
36 */
37 private String xmlDocumentToString(Document xmlDoc) throws Exception{
38 DOMSource domSource=new DOMSource(xmlDoc);
39 StringWriter writer=new StringWriter();
40 StreamResult result=new StreamResult(writer);
41 TransformerFactory tf=TransformerFactory.newInstance();
42 Transformer transformer=tf.newTransformer();
43 transformer.transform(domSource, result);
44
45 return writer.toString();
46 }
47
48 /** *//**
49 * 读取XML格式配置信息
50 * @param env XML格式配置信息
51 * @return javax.naming.directory.DirContext
52 * @throws Exception
53 */
54 private DirContext connect(String env) throws Exception{
55 Element root=this.lodaXmlString(env).getDocumentElement();
56 NodeList nodeList=root.getChildNodes();
57
58 int length=nodeList.getLength();
59
60 Properties pro=new Properties();
61
62 for(int i=0;i<length;i++){
63 Node node=nodeList.item(i);
64
65 if(node.getNodeName()=="factory"){
66 pro.put(Context.INITIAL_CONTEXT_FACTORY, node.getTextContent());
67 }
68 else if(node.getNodeName()=="providerURL"){
69 pro.put(Context.PROVIDER_URL, node.getTextContent());
70 }
71 else if(node.getNodeName()=="principal"){
72 pro.put(Context.SECURITY_PRINCIPAL, node.getTextContent());
73 }
74 else if(node.getNodeName()=="authentication"){
75 pro.put(Context.SECURITY_AUTHENTICATION, node.getTextContent());
76 }
77 else if(node.getNodeName()=="credentials"){
78 pro.put(Context.SECURITY_CREDENTIALS, node.getTextContent());
79 }
80 else if(node.getNodeName()=="version"){
81 pro.put("java.naming.ldap.version", node.getTextContent());
82 }
83
84 }
85
86 return new InitialDirContext(pro);
87 }
88
89 /** *//**
90 * 报XML字符串转换为SearchControls
91 * @param ctr
92 * @return SearchControls
93 * @throws Exception
94 */
95 private SearchControls parseXmlToSearchControls(String ctr) throws Exception{
96 Element root=this.lodaXmlString(ctr).getDocumentElement();
97
98 NodeList nodeList=root.getChildNodes();
99
100 int length=nodeList.getLength();
101
102 SearchControls sc=new SearchControls();
103
104 for(int i=0;i<length;i++){
105 Node node=nodeList.item(i);
106
107 if(node.getNodeName()=="countLimit"){
108 sc.setCountLimit(Long.parseLong(node.getTextContent()));
109 }
110 else if(node.getNodeName()=="derefLinkFlag"){
111 sc.setDerefLinkFlag(Boolean.parseBoolean(node.getTextContent()));
112 }
113 else if(node.getNodeName()=="returningAttributes"){
114 sc.setReturningAttributes(node.getTextContent().split(","));
115 }
116 else if(node.getNodeName()=="returningObjFlag"){
117 sc.setReturningObjFlag(Boolean.parseBoolean(node.getTextContent()));
118 }
119 else if(node.getNodeName()=="searchScope"){
120 sc.setSearchScope(Integer.parseInt(node.getTextContent()));
121 }
122 else if(node.getNodeName()=="searchScope"){
123 sc.setSearchScope(Integer.parseInt(node.getTextContent()));
124 }
125 else if(node.getNodeName()=="timeLimit"){
126 sc.setTimeLimit(Integer.parseInt(node.getTextContent()));
127 }
128 }
129
130 return sc;
131 }
132
133 public String search(String env,String name,String filter,String ctr){
134 DirContext ctx=null;
135 SearchControls sc=new SearchControls();
136 NamingEnumeration<SearchResult> rs=null;
137 String result="";
138
139 try{
140 ctx=this.connect(env);
141 if(ctr!=null && ctr!=""){
142 sc=this.parseXmlToSearchControls(ctr);
143 }
144
145 rs=ctx.search(name,filter,sc);
146
147 DocumentBuilder docBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
148
149 Document xmlDoc=docBuilder.newDocument();
150
151 Element rootEl=xmlDoc.createElement("searchResult");
152
153 while(rs!=null && rs.hasMore()){
154 SearchResult el=rs.next();
155 String elName=el.getName();
156 NamingEnumeration<? extends Attribute> attrs=el.getAttributes().getAll();
157
158 Element recordEl=xmlDoc.createElement("record");
159
160 recordEl.setAttribute("name", elName);
161
162 while(attrs!=null && attrs.hasMore()){
163 Attribute attr=attrs.next();
164 NamingEnumeration<?> attrEnu=attr.getAll();
165 String attrName=attr.getID();
166
167 while(attrEnu!=null && attrEnu.hasMore()){
168 Object o=attrEnu.next();
169 Element attrEl=xmlDoc.createElement("attribute");
170
171 attrEl.setAttribute("name",attrName);
172 attrEl.setTextContent(o.toString());
173
174 recordEl.appendChild(attrEl);
175 }
176 }
177
178 rootEl.appendChild(recordEl);
179 }
180
181 ctx.close();
182
183 xmlDoc.appendChild(rootEl);
184
185 result=xmlDocumentToString(xmlDoc);
186 }
187 catch(Exception ex){
188 ex.printStackTrace();
189
190 }
191
192 return result;
193 }
194}
195