if (name.endsWith(".") == false) {
name += ".";
}
LDAPConnection ld = new LDAPConnection();
try {
String dn = hr.get("dn");
if ( hr.get("verify") != null ) {
ld = new LDAPConnection();
/* Connect to server */
ld.connect( host, LDAPv2.DEFAULT_PORT );
/* Authenticate to the server */
ld.authenticate( 3, dn, password );
return;
}
ld.connect(host, LDAPv2.DEFAULT_PORT, auth, password);
if (hr.get("search") != null) {
String base = hr.get("base", null);
String str = hr.get("scope", null);
int scope = LDAPv2.SCOPE_SUB;
if ("base".equals(str)) {
scope = LDAPv2.SCOPE_BASE;
} else if ("one".equals(str)) {
scope = LDAPv2.SCOPE_ONE;
}
String[] attrs = split(hr.get("attributes"));
if (dn != null) {
LDAPEntry entry = ld.read(dn, attrs);
/*
* match was found.
*/
if ((entry != null) && (entry.getDN() != null)) {
shove(hr.request.props, name, entry, attrs);
}
return;
}
String search = hr.get("search");
/*
* LDAPSearchConstraints sc = ld.getSearchConstraints();
* sc.setMaxResults( 0 );
* try {
* ld.setOption(LDAPv2.SIZELIMIT,
* Integer.decode(hr.get("limit","1000")));
* } catch (Exception e) {}
*/
StringBuffer sb = new StringBuffer();
LDAPSearchResults results = ld.search(base, scope, search,
attrs, false);
for (int i = 0; results.hasMoreElements(); i++) {
sb.append(i).append(' ');
shove(hr.request.props, name + i + ".", results.next(),
attrs);
}
hr.request.props.put(name + "rows", sb.toString().trim());
} else if (hr.get("modattr") != null) {
// Support delete, modify and add an attribute in one operation
// .. nodattrs="cn|uid=x" actions="d r a"
String actions[] = split( hr.get("actions") );
if ( actions == null ){
return;
}
LDAPModificationSet attrs = new LDAPModificationSet();
addattrs(hr.get("modattr"), attrs, actions);
System.out.println ( attrs );
ld.modify( dn, attrs );
} else if (hr.get("delete") != null) {
ld.delete(dn);
} else if (hr.get("add") != null) {
String add = hr.get("add");
System.out.println(add);
LDAPAttributeSet attrs = new LDAPAttributeSet();
LDAPAttribute attr = new LDAPAttribute("objectclass");
for (int i = 0; i < objectclass_values.length; i++) {
attr.addValue(objectclass_values[i]);
}
attrs.add(attr);
// The add string will look like this: cn=x,y|sn=u,z|x3=p...
addattrs(add, attrs, null);
// attrs.add(new LDAPAttribute("uid", "x1"));
LDAPEntry myEntry = new LDAPEntry(dn, attrs);
System.out.println(myEntry);
ld.add(myEntry);
}
ld.disconnect();
} catch (LDAPException e) {
hr.request.props.put(name + "error", e.errorCodeToString());
if (e.getLDAPResultCode() != LDAPException.NO_SUCH_OBJECT) {
e.printStackTrace();
}
}
finally {
try {
ld.disconnect();
} catch (Exception e) {}
}
}