LDAP 程式庫

piJNDI 程式庫

package com.PowerIntegral;

import javax.naming.Context;

import javax.naming.directory.InitialDirContext;

import javax.naming.directory.DirContext;

import javax.naming.directory.Attributes;

import javax.naming.directory.*;

import javax.naming.NamingException;

import javax.naming.NamingEnumeration;

import java.util.*;

import com.PowerIntegral.*;

import java.io.Serializable;

public class piJNDI implements Serializable {

private DirContext _DirContext = null;

private boolean _OK = false;

private String _ErrorMessage = "";

private com.PowerIntegral.piBase64 _piBase64 = null;

private java.util.Properties _Configuration = null;

/**

* piJNDI: 建構子

*

*/

public piJNDI() {

}

/**

* piJNDI: 建構子

*

* @param ldapURL

* String => LDAP 描述子

* @param UID

* String => 帳號

* @param PWD

* String => 密碼

*/

public piJNDI(String ldapURL, String UID, String PWD) {

java.util.Properties _Properties = new java.util.Properties();

_Properties.put(Context.INITIAL_CONTEXT_FACTORY,

"com.sun.jndi.ldap.LdapCtxFactory");

_Properties.put(Context.PROVIDER_URL, ldapURL);

_Properties.put(Context.SECURITY_AUTHENTICATION, "SIMPLE");

_Properties.put(Context.SECURITY_PRINCIPAL, UID);

_Properties.put(Context.SECURITY_CREDENTIALS, PWD);

try {

this._piBase64 = new piBase64();

this._DirContext = new InitialDirContext(_Properties);

this._Configuration = _Properties;

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

}

/**

* piJNDI: 建構子, 系統內建日誌功能

*

* @param MyConfiguration

* Properties => LDAP 屬性物件

*/

public piJNDI(java.util.Properties MyConfiguration) {

try {

this._piBase64 = new piBase64(MyConfiguration);

this._DirContext = new InitialDirContext(MyConfiguration);

this._Configuration = MyConfiguration;

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

}

/**

* Init: 初始化

*

* @param ldapURL

* String => LDAP 描述子

* @param UID

* String => 帳號

* @param PWD

* String => 密碼

*/

public boolean Init(String ldapURL, String UID, String PWD) {

java.util.Properties _Properties = new java.util.Properties();

_Properties.put(Context.INITIAL_CONTEXT_FACTORY,

"com.sun.jndi.ldap.LdapCtxFactory");

_Properties.put(Context.PROVIDER_URL, ldapURL);

_Properties.put(Context.SECURITY_AUTHENTICATION, "SIMPLE");

_Properties.put(Context.SECURITY_PRINCIPAL, UID);

_Properties.put(Context.SECURITY_CREDENTIALS, PWD);

try {

this._piBase64 = new piBase64();

this._DirContext = new InitialDirContext(_Properties);

this._Configuration = _Properties;

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return this._OK;

}

/**

* getDirContext: 取得 JNDI 物件

*

* @return DirContext

*/

public DirContext getDirContext() {

return this._DirContext;

}

/**

* IsSignOn: 驗證 LDAP 帳號/密碼

*

* @param ldapURL

* String => LDAP 描述子

* @param UID

* String => 帳號

* @param PWD

* String => 密碼

* @return boolean

*/

public boolean IsSignOn(String ldapURL, String UID, String PWD) {

this._OK = false;

this._ErrorMessage = "";

java.util.Properties _Properties = new java.util.Properties();

_Properties.put(Context.INITIAL_CONTEXT_FACTORY,

"com.sun.jndi.ldap.LdapCtxFactory");

_Properties.put(Context.PROVIDER_URL, ldapURL);

_Properties.put(Context.SECURITY_AUTHENTICATION, "simple");

_Properties.put(Context.SECURITY_PRINCIPAL, UID);

_Properties.put(Context.SECURITY_CREDENTIALS, PWD);

try {

DirContext _DirContext = new InitialDirContext(_Properties);

this._OK = true;

} catch (javax.naming.AuthenticationException _AuthenticationException) {

this._ErrorMessage = _AuthenticationException.getMessage();

} catch (NamingException _NamingException) {

this._ErrorMessage = _NamingException.getMessage();

}

return this._OK;

}

/**

* getAtributes: 取得 LDAP 屬性群物件

*

* @param ldapEntry

* String => LDAP 物件描述子 (如: cn=MyName)

* @return Attributes => LDAP 屬性群物件

*/

public javax.naming.directory.Attributes getAtributes(String ldapEntry) {

javax.naming.directory.Attributes _Attributes = null;

this._OK = false;

this._ErrorMessage = "";

if (this._DirContext == null) {

return null;

}

try {

_Attributes = this._DirContext.getAttributes(ldapEntry);

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return _Attributes;

}

/**

* getAttributeValue: 取得 LDAP 屬性值

*

* @param MyAttributes

* javax.naming.directory.Attributes => LDAP 屬性物件

* @param AttributeName

* String => LDAP 屬性名稱

* @return String

*/

public String getAttributeValue(

javax.naming.directory.Attributes MyAttributes, String AttributeName) {

String _AttributeValue = "";

this._OK = false;

this._ErrorMessage = "";

if (this._DirContext == null || MyAttributes == null) {

return "";

}

try {

_AttributeValue = MyAttributes.get(AttributeName).toString();

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return _AttributeValue;

}

public class piAttributeValue implements Serializable {

String ArributeName;

Object AttributeValue;

}

/**

* getAttributeValues: 取得 LDAP 屬性物件中所有屬性物件群

*

* @param MyAttributes

* Attributes => LDAP 屬性物件

* @return ArrayList

*/

public ArrayList getAttributeValues(

javax.naming.directory.Attributes MyAttributes) {

this._OK = false;

this._ErrorMessage = "";

if (this._DirContext == null || MyAttributes == null) {

return null;

}

ArrayList _ArrayList = new ArrayList();

javax.naming.NamingEnumeration _NamingEnumeration = MyAttributes

.getAll();

try {

while (_NamingEnumeration.hasMore()) {

javax.naming.directory.Attribute _Attribute = (javax.naming.directory.Attribute) _NamingEnumeration

.next();

String _AttributeName = _Attribute.getID();

javax.naming.NamingEnumeration _NamingEnumerationAttribute = _Attribute

.getAll();

while (_NamingEnumerationAttribute.hasMore()) {

piAttributeValue _piArributeValue = new piAttributeValue();

_piArributeValue.ArributeName = _AttributeName;

_piArributeValue.AttributeValue = _NamingEnumerationAttribute

.next();

_ArrayList.add(_piArributeValue);

}

}

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return _ArrayList;

}

/**

* setAttributes: 異動 LDAP 屬性物件屬性內容

*

* @param ldapEntry

* String => 欲異動 LDAP 屬性物件描述子

* @param AttriButeName

* String => 欲異動 LDAP 屬性名稱

* @param AttributeValue

* String => 欲異動 LDAP 屬性值

* @param Operation

* int => 異動方式, 0:修改, 1:新增, 2: 刪除

* @return boolean

*/

public boolean setAttributes(String ldapEntry, String AttriButeName,

Object AttributeValue, int Operation) {

this._OK = false;

this._ErrorMessage = "";

if (this._DirContext == null) {

return false;

}

int _Operation = DirContext.REPLACE_ATTRIBUTE;

switch (Operation) {

case 0:

_Operation = DirContext.REPLACE_ATTRIBUTE;

break;

case 1:

_Operation = DirContext.ADD_ATTRIBUTE;

break;

case 2:

_Operation = DirContext.REMOVE_ATTRIBUTE;

break;

default:

_Operation = DirContext.REPLACE_ATTRIBUTE;

break;

}

try {

javax.naming.directory.ModificationItem[] _ModificationItem = new javax.naming.directory.ModificationItem[1];

_ModificationItem[0] = new javax.naming.directory.ModificationItem(

_Operation, new javax.naming.directory.BasicAttribute(

AttriButeName, AttributeValue));

this._OK = this.setAttributes(ldapEntry, _ModificationItem);

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return this._OK;

}

/**

* setAttributes: 異動 LDAP 屬性物件屬性內容

*

* @param ldapEntry

* String => 欲異動 LDAP 屬性物件描述子

* @param MyModificationItem

* javax.naming.directory.ModificationItem[] => 欲異動 LDAP 屬性修改陣列

* @return boolean

*/

public boolean setAttributes(String ldapEntry,

javax.naming.directory.ModificationItem[] MyModificationItem) {

this._OK = false;

this._ErrorMessage = "";

if (this._DirContext == null || MyModificationItem == null) {

return false;

}

try {

this._DirContext.modifyAttributes(ldapEntry, MyModificationItem);

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return this._OK;

}

/**

* AttributeBuilder: 建立屬性物件

*

* @param AttributeName

* String => 屬性名稱

* @param Attributes

* String[] => 屬性集合

* @return BasicAttribute

*/

public javax.naming.directory.BasicAttribute AttributeBuilder(

String AttributeName, String Attributes[]) {

javax.naming.directory.BasicAttribute _BasicAttribute = new javax.naming.directory.BasicAttribute(

AttributeName);

for (int i = 0; i < Attributes.length; i++) {

_BasicAttribute.add(Attributes[i]);

}

return _BasicAttribute;

}

/**

* CreateObject: 建立 LDAP 新節點

*

* @param ldapEntry

* String => 新 LDAP 節點描述子

* @param ClassName

* String => 新 LDAP 節點類別名稱

* @param ClassAttributes

* String[] => 新 LDAP 節點類別值

* @param Attributes

* Hashtable => 新 LDAP 節點屬性集合物件

* @return boolean

*/

public boolean CreateObject(String ldapEntry, String ClassName,

String ClassAttributes[], Hashtable Attributes) {

javax.naming.directory.BasicAttribute _ClassAttribute = this

.AttributeBuilder(ClassName, ClassAttributes);

javax.naming.directory.BasicAttributes _BasicAttributes = new javax.naming.directory.BasicAttributes();

_BasicAttributes.put(_ClassAttribute);

for (Enumeration _AttributeKey = Attributes.keys(); _AttributeKey

.hasMoreElements();) {

String _KeyName = (String) _AttributeKey.nextElement();

String _KeyValue = (String) Attributes.get(_KeyName);

_BasicAttributes.put(_KeyName, _KeyValue);

}

return this.CreateObject(ldapEntry, _BasicAttributes);

}

/**

* CreateObject: 建立 LDAP 新節點

*

* @param ldapEntry

* String => 新 LDAP 節點描述子

* @param MyBasicAttributes

* BasicAttributes => 新 LDAP 節點屬性集合物件

* @return boolean

*/

public boolean CreateObject(String ldapEntry,

javax.naming.directory.BasicAttributes MyBasicAttributes) {

this._OK = false;

this._ErrorMessage = "";

if (this._DirContext == null || MyBasicAttributes == null) {

return false;

}

try {

this._DirContext.createSubcontext(ldapEntry, MyBasicAttributes);

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return this._OK;

}

/**

* DeleteObject: 刪除 LDAP 節點

*

* @param ldapEntry

* String => LDAP 節點描述子

* @return boolean

*/

public boolean DeleteObject(String ldapEntry) {

this._OK = false;

this._ErrorMessage = "";

if (this._DirContext == null) {

return false;

}

try {

this._DirContext.destroySubcontext(ldapEntry);

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return this._OK;

}

/**

* OK: 取得最近函式調用狀態, 成功或失敗.

*

* @return boolean

*/

public boolean OK() {

return this._OK;

}

/**

* ErrorMessage: 取得最近函式調用錯誤說明

*

* @return String

*/

public String ErrorMessage() {

return this._ErrorMessage;

}

/**

* getXML: 取得 LDAP 屬性物件所有屬性值

*

* @param ldapEntry

* String

* @return org.jdom.Document

*/

public org.jdom.Document getXML(String ldapEntry) {

org.jdom.Document _xmlDocument = null;

javax.naming.directory.Attributes _Attributes = this

.getAtributes(ldapEntry);

if (!this._OK) {

System.out.println(this._ErrorMessage);

return null;

}

ArrayList _ArrayList = this.getAttributeValues(_Attributes);

String _ClassName = "", _AttributeValue = "";

try {

org.jdom.Element _xmlRoot = new org.jdom.Element(

"ldapAttributeValues");

_xmlDocument = new org.jdom.Document(_xmlRoot);

// JDOM 1.0

_xmlRoot.setAttribute("Base", this._Configuration

.getProperty("PROVIDER_URL"));

_xmlRoot.setAttribute("AttributePath", ldapEntry);

// JDOM Borland

// _xmlRoot.addAttribute("Base", AttributePath);

for (int i = 0; i < _ArrayList.size(); i++) {

piAttributeValue _piAttributeValue = (piAttributeValue) _ArrayList

.get(i);

try {

org.jdom.Element _xmlArributeValue = new org.jdom.Element(

_piAttributeValue.ArributeName.replaceAll(";", "_"));

_ClassName = _piAttributeValue.AttributeValue.getClass()

.toString();

_AttributeValue = "";

if (_piAttributeValue.AttributeValue.getClass().toString()

.equals("class java.lang.String")) {

_AttributeValue = _piAttributeValue.AttributeValue

.toString();

} else {

_AttributeValue = this._piBase64

.getObjectBase64((Serializable) _piAttributeValue.AttributeValue);

}

_xmlArributeValue.setText(_AttributeValue);

_xmlRoot.addContent(_xmlArributeValue);

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

}

this._OK = true;

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return _xmlDocument;

}

/**

* DisplayAttributeValues: 輸出 LDAP 物件所有屬性值

*

* @param AttributePath

* String => LDAP 物件描述子

* @param MyPrintStream

* PrintStream => 輸出串流

*/

public void DisplayAttributeValues(String AttributePath,

java.io.PrintStream MyPrintStream) {

javax.naming.directory.Attributes _Attributes = this

.getAtributes(AttributePath);

if (!this._OK) {

MyPrintStream.println(this._ErrorMessage);

return;

}

ArrayList _ArrayList = this.getAttributeValues(_Attributes);

MyPrintStream.println(AttributePath);

for (int i = 0; i < _ArrayList.size(); i++) {

piAttributeValue _piAttributeValue = (piAttributeValue) _ArrayList

.get(i);

String _ClassName = _piAttributeValue.AttributeValue.getClass()

.toString();

String _AttributeValue = "";

if (_piAttributeValue.AttributeValue.getClass().toString().equals(

"class java.lang.String")) {

_AttributeValue = _piAttributeValue.AttributeValue.toString();

} else {

_AttributeValue = this._piBase64

.getObjectBase64((Serializable) _piAttributeValue.AttributeValue);

}

MyPrintStream.println("\t" + _piAttributeValue.ArributeName + "="

+ _AttributeValue);

}

}

public ArrayList SearchEntries(String SearchBase, String SearchFilter) {

this._OK = false;

this._ErrorMessage = "";

if (this._DirContext == null) {

return null;

}

SearchControls _SearchControls = new SearchControls();

_SearchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);

ArrayList _Result = new ArrayList();

try {

NamingEnumeration _NamingEnumeration = this._DirContext.search(

SearchBase, SearchFilter, _SearchControls);

while (_NamingEnumeration != null && _NamingEnumeration.hasMore()) {

SearchResult _SearchResult = (SearchResult) _NamingEnumeration

.next();

String _ldapEntry = _SearchResult.getName() + "," + SearchBase;

_Result.add(_ldapEntry);

}

} catch (Exception ex) {

this._ErrorMessage = ex.getMessage();

}

return _Result;

}

}