LdapOperUtils 為靜態工具類(私有建構子),封裝 LDAP 所有高層業務操作,輸入輸出統一使用 Map<String, Object>(單值用 String,多值用 List)、List<Map>(搜尋結果集),呼叫端零 JNDI API 依賴。
private static DirContext getLdapDirContext(Env env) throws BaseException {
if (env == null) throw new BaseException("error.common.parameter.empty", new String[]{"env"});
DirContext ctx = LdapConnectionFactory.getDirContext(env);
if (ctx == null) throw new BaseException("error.common.dao.ldap.getcontext", null);
return ctx;
}
private static void closeLdapDirContext(DirContext ctx) throws BaseException {
if (ctx == null) throw new BaseException("error.common.parameter.empty", new String[]{"dirContext"});
try { ctx.close(); }
catch (NamingException ex) {
log.debug("First close failed, retry...", ex);
try { ctx.close(); }
catch (NamingException ex2) {
throw new BaseException("error.common.dao.ldap.closedircontext", null);
}
}
}
public static void addContext(DirContext context, String cn, Map attMap)
throws NamingException, BaseException {
validateNotNull(context, "context");
validateNotEmpty(cn, "cn");
validateNotNull(attMap, "attMap");
if (attMap.isEmpty()) return;
Attributes attrs = new BasicAttributes();
for (Map.Entry entry : attMap.entrySet()) {
String key = entry.getKey();
Object valueObj = entry.getValue();
Attribute att;
if (valueObj instanceof String) {
att = new BasicAttribute(key, valueObj); // 單值
} else if (valueObj instanceof List) {
att = new BasicAttribute(key); // 多值
for (Object v : (List>) valueObj) att.add(v);
} else {
att = new BasicAttribute(key, valueObj); // 其他(如 byte[] 密碼)
}
attrs.put(att);
}
context.createSubcontext(cn, attrs);
}
public static void deleteContext(DirContext context, String cn)
throws NamingException, BaseException {
validateNotNull(context, "context");
validateNotEmpty(cn, "cn");
context.destroySubcontext(cn);
}
public static void reNameContext(DirContext context, String cn, String newCn)
throws NamingException, BaseException {
validateNotNull(context, "context");
validateNotEmpty(cn, "cn");
validateNotEmpty(newCn, "newCn");
context.rename(cn, newCn);
}
public static void addAttributes(DirContext context, String cn, Map attMap)
throws NamingException, BaseException {
validateParams(context, cn, attMap);
Attributes attrs = buildAttributes(attMap);
context.modifyAttributes(cn, DirContext.ADD_ATTRIBUTE, attrs);
}
public static void deleteAttributes(DirContext context, String cn, List attList)
throws NamingException, BaseException {
validateParams(context, cn, attList);
Attributes attrs = new BasicAttributes();
for (String name : attList) attrs.put(new BasicAttribute(name));
context.modifyAttributes(cn, DirContext.REMOVE_ATTRIBUTE, attrs);
}
public static void modifyAttributes(DirContext context, String cn, Map attMap)
throws NamingException, BaseException {
validateParams(context, cn, attMap);
Attributes attrs = buildAttributes(attMap);
context.modifyAttributes(cn, DirContext.REPLACE_ATTRIBUTE, attrs);
}
public static Map getAttributes(DirContext context, String cn, List attNameList)
throws NamingException {
Map attsMap = new HashMap<>();
String[] attArray = attNameList.toArray(new String[0]);
Attributes results = context.getAttributes(cn, attArray);
for (String name : attNameList) {
Attribute attr = results.get(name);
if (attr == null || attr.size() == 0) continue;
NamingEnumeration> vals = attr.getAll();
if (vals == null) continue;
Object first = vals.nextElement();
if (first == null) continue;
List
public static List
private static void validateNotNull(Object obj, String paramName) throws BaseException {
if (obj == null) throw new BaseException("error.common.parameter.empty", new String[]{paramName});
}
private static void validateNotEmpty(String str, String paramName) throws BaseException {
if (StringUtils.isEmpty(str)) throw new BaseException("error.common.parameter.empty", new String[]{paramName});
}
private static void validateParams(DirContext ctx, String cn, Object param) throws BaseException {
validateNotNull(ctx, "context");
validateNotEmpty(cn, "cn");
validateNotNull(param, param instanceof Map ? "attMap" : "attList");
}
private static Attributes buildAttributes(Map attMap) {
Attributes attrs = new BasicAttributes();
for (Map.Entry e : attMap.entrySet()) {
Attribute att;
Object v = e.getValue();
if (v instanceof List) {
att = new BasicAttribute(e.getKey());
for (Object o : (List>) v) att.add(o);
} else {
att = new BasicAttribute(e.getKey(), v);
}
attrs.put(att);
}
return attrs;
}
Attribute、NamingEnumeration。NamingException 向上拋出,業務層統一捕獲轉換為 BaseException。attNameList.toArray(new String[0]) 避免手動迴圈複製陣列。下一篇將介紹進階搜尋功能(單層/子樹、指定回傳屬性)、群組成員維護與認證整合。