Search in sources :

Example 6 with SortControl

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);
    }
}
Also used : SortControl(javax.naming.ldap.SortControl) KettleException(org.pentaho.di.core.exception.KettleException) Control(javax.naming.ldap.Control) SortControl(javax.naming.ldap.SortControl) PagedResultsControl(javax.naming.ldap.PagedResultsControl) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) BasicAttribute(javax.naming.directory.BasicAttribute) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) SearchControls(javax.naming.directory.SearchControls) KettleException(org.pentaho.di.core.exception.KettleException) NameNotFoundException(javax.naming.NameNotFoundException) PagedResultsControl(javax.naming.ldap.PagedResultsControl)

Aggregations

Control (javax.naming.ldap.Control)6 SortControl (javax.naming.ldap.SortControl)6 ArrayList (java.util.ArrayList)4 PagedResultsControl (javax.naming.ldap.PagedResultsControl)4 PagedResultsResponseControl (javax.naming.ldap.PagedResultsResponseControl)4 NamingException (javax.naming.NamingException)3 Attributes (javax.naming.directory.Attributes)3 SearchControls (javax.naming.directory.SearchControls)3 SearchResult (javax.naming.directory.SearchResult)3 NameNotFoundException (javax.naming.NameNotFoundException)2 Attribute (javax.naming.directory.Attribute)2 LdapContext (javax.naming.ldap.LdapContext)2 KettleException (org.pentaho.di.core.exception.KettleException)2 IOException (java.io.IOException)1 AuthenticationException (javax.naming.AuthenticationException)1 NamingEnumeration (javax.naming.NamingEnumeration)1 BasicAttribute (javax.naming.directory.BasicAttribute)1 SortKey (javax.naming.ldap.SortKey)1 BadUserRequestException (org.camunda.bpm.engine.BadUserRequestException)1 QueryOrderingProperty (org.camunda.bpm.engine.impl.QueryOrderingProperty)1