G
gimme_this_gimme_that
In our AD system
The value lastLogon is a LargeInteger
The value whenCreated is a UTC Coded Time
The value group is a multi valued list Email Distribution Lists.
I'd need a tip so I can see how to fetch these values. Your help is
appreciated.
Here is a clip of code:
Note that only the String attributes such as
cn,givenName,sn,mail,sAMAccountName are fetched.
Note the comments.
If you're wondering about the JDOM I'm trying to write a reusable AD
data fetcher that returns the result of a query in XML. That should
sort of explain the method layout.
Again. Thanks.
//This is the calling function in a Servlet.
public Vector getDataFromAD(String searchText) {
String searchFilter = "(&(objectCategory=user)
(sAMAccountName="+searchText+"*))";
searchFilter = "(&(objectClass=user)(sAMAccountName=frog*))";
AdXml adXml = (AdXml)SpringContext.getContext().getBean("AdXml");
String[] attributes = {"cn","givenName", "sn",
"mail","sAMAccountName","group","lastLogon","logonCount"};
Vector v = null;
try {
v = adXml.search(attributes, searchFilter, 1000); // Here is
the call to search shown below
if ( null == v ) {
System.out.println("no search results returning null");
}
} catch (Exception ex) {
ex.printStackTrace();
}
return v;
}
These are from AdXml:
public Vector search(String[] aAttributes, String searchFilter, int
countLimit)
throws Exception {
DirContext ctx = getDirContext();
try {
SearchControls sc = new SearchControls();
sc.setCountLimit(countLimit);
sc.setReturningAttributes(aAttributes);
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration searchEnum = ctx.search(baseDN, searchFilter,
sc);
Vector results = new Vector ();
while(searchEnum != null && searchEnum.hasMore()) {
results.add(getAttributes((SearchResult)searchEnum.next()));
}
return results;
} catch ( Exception e){
e.printStackTrace();
} finally {
close(ctx);
}
}
public Element getAttributes(SearchResult result) throws BobException
{
Element elm = new Element("all");
try {
Attributes attrs = result.getAttributes();
NamingEnumeration enum = attrs.getIDs();
while ( enum.hasMoreElements() ) {
String key = (String) enum.nextElement(); // OK to assume all keys
are Strings?
Attribute attr = attrs.get(key);
getAttributesHelper(elm,key,attr);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return elm;
}
private void getAttributesHelper(Element elm, String key, Attribute
attr) throws NamingException {
Object obj = attr.get();
if ("class java.lang.String".equals(obj.getClass().toString())) {
elm.addContent(new Element(key)).addContent((obj.toString()));
} else { // This statement never gets executed I expected an
enumeration for multiple valued attributes
System.out.println(obj.getClass().toString());
}
}
The value lastLogon is a LargeInteger
The value whenCreated is a UTC Coded Time
The value group is a multi valued list Email Distribution Lists.
I'd need a tip so I can see how to fetch these values. Your help is
appreciated.
Here is a clip of code:
Note that only the String attributes such as
cn,givenName,sn,mail,sAMAccountName are fetched.
Note the comments.
If you're wondering about the JDOM I'm trying to write a reusable AD
data fetcher that returns the result of a query in XML. That should
sort of explain the method layout.
Again. Thanks.
//This is the calling function in a Servlet.
public Vector getDataFromAD(String searchText) {
String searchFilter = "(&(objectCategory=user)
(sAMAccountName="+searchText+"*))";
searchFilter = "(&(objectClass=user)(sAMAccountName=frog*))";
AdXml adXml = (AdXml)SpringContext.getContext().getBean("AdXml");
String[] attributes = {"cn","givenName", "sn",
"mail","sAMAccountName","group","lastLogon","logonCount"};
Vector v = null;
try {
v = adXml.search(attributes, searchFilter, 1000); // Here is
the call to search shown below
if ( null == v ) {
System.out.println("no search results returning null");
}
} catch (Exception ex) {
ex.printStackTrace();
}
return v;
}
These are from AdXml:
public Vector search(String[] aAttributes, String searchFilter, int
countLimit)
throws Exception {
DirContext ctx = getDirContext();
try {
SearchControls sc = new SearchControls();
sc.setCountLimit(countLimit);
sc.setReturningAttributes(aAttributes);
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration searchEnum = ctx.search(baseDN, searchFilter,
sc);
Vector results = new Vector ();
while(searchEnum != null && searchEnum.hasMore()) {
results.add(getAttributes((SearchResult)searchEnum.next()));
}
return results;
} catch ( Exception e){
e.printStackTrace();
} finally {
close(ctx);
}
}
public Element getAttributes(SearchResult result) throws BobException
{
Element elm = new Element("all");
try {
Attributes attrs = result.getAttributes();
NamingEnumeration enum = attrs.getIDs();
while ( enum.hasMoreElements() ) {
String key = (String) enum.nextElement(); // OK to assume all keys
are Strings?
Attribute attr = attrs.get(key);
getAttributesHelper(elm,key,attr);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return elm;
}
private void getAttributesHelper(Element elm, String key, Attribute
attr) throws NamingException {
Object obj = attr.get();
if ("class java.lang.String".equals(obj.getClass().toString())) {
elm.addContent(new Element(key)).addContent((obj.toString()));
} else { // This statement never gets executed I expected an
enumeration for multiple valued attributes
System.out.println(obj.getClass().toString());
}
}