use of javax.naming.ldap.SortControl in project pentaho-kettle by pentaho.
the class LDAPConnection method search.
public void search(String searchBase, String filter, int limitRows, String[] attributeReturned, int searchScope) throws KettleException {
// Set the Search base.This is the place where the search will
setSearchBase(searchBase);
setFilter(Const.NVL(correctFilter(filter), DEFAUL_FILTER_STRING));
try {
if (Utils.isEmpty(getSearchBase())) {
// get Search Base
Attributes attrs = getInitialContext().getAttributes("", new String[] { "namingContexts" });
Attribute attr = attrs.get("namingContexts");
setSearchBase(attr.get().toString());
if (log.isDetailed()) {
log.logDetailed(BaseMessages.getString(PKG, "LDAPInput.SearchBaseFound", getSearchBase()));
}
}
this.controls = new SearchControls();
if (limitRows > 0) {
this.controls.setCountLimit(limitRows);
}
// Time Limit
if (getTimeLimit() > 0) {
this.controls.setTimeLimit(getTimeLimit() * 1000);
}
// Specify the attributes to return
if (attributeReturned != null) {
this.controls.setReturningAttributes(attributeReturned);
}
// Specify the search scope
switch(searchScope) {
case SEARCH_SCOPE_OBJECT_SCOPE:
this.controls.setSearchScope(SearchControls.OBJECT_SCOPE);
break;
case SEARCH_SCOPE_ONELEVEL_SCOPE:
this.controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
break;
default:
this.controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
break;
}
Control ctlp = null;
Control ctlk = null;
int nrCtl = 0;
// Set the sort search?
if (isSortingAttributes()) {
// Create a sort control that sorts based on attributes
setSortingAttributesKeys(getSortingAttributes().toArray(new String[getSortingAttributes().size()]));
ctlk = new SortControl(getSortingAttributesKeys(), Control.NONCRITICAL);
nrCtl++;
if (log.isDebug()) {
log.logDebug(BaseMessages.getString("LDAPInput.Log.SortingKeys", Arrays.toString(getSortingAttributesKeys())));
}
}
// Set the page size?
if (isPagingUsed()) {
// paging is activated
// Request the paged results control
ctlp = new PagedResultsControl(GetPagingSize(), Control.CRITICAL);
nrCtl++;
if (log.isDebug()) {
log.logDebug(BaseMessages.getString("LDAPInput.Log.PageSize", String.valueOf(GetPagingSize())));
}
}
if (nrCtl > 0) {
Control[] ctls = new Control[nrCtl];
int index = 0;
if (ctlk != null) {
ctls[index++] = ctlk;
}
if (ctlp != null) {
ctls[index++] = ctlp;
}
getInitialContext().setRequestControls(ctls);
}
// Search for objects using the filter
this.results = getInitialContext().search(getSearchBase(), getFilter(), getSearchControls());
} catch (Exception e) {
throw new KettleException(BaseMessages.getString("LDAPConnection.Error.Search"), e);
}
}
Aggregations