Example usage for javax.naming.directory SearchControls setReturningObjFlag

List of usage examples for javax.naming.directory SearchControls setReturningObjFlag

Introduction

In this page you can find the example usage for javax.naming.directory SearchControls setReturningObjFlag.

Prototype

public void setReturningObjFlag(boolean on) 

Source Link

Document

Enables/disables returning objects returned as part of the result.

Usage

From source file:org.lsc.jndi.JndiServices.java

private List<String> doGetDnList(final String base, final String filter, final int scope)
        throws NamingException {
    NamingEnumeration<SearchResult> ne = null;
    List<String> iist = new ArrayList<String>();
    try {/*from  w  w  w  .  j a v  a 2s  . co m*/
        SearchControls sc = new SearchControls();
        sc.setDerefLinkFlag(false);
        sc.setReturningAttributes(new String[] { "1.1" });
        sc.setSearchScope(scope);
        sc.setReturningObjFlag(true);
        ne = ctx.search(base, filter, sc);

        String completedBaseDn = "";
        if (base.length() > 0) {
            completedBaseDn = "," + base;
        }
        while (ne.hasMoreElements()) {
            iist.add(((SearchResult) ne.next()).getName() + completedBaseDn);
        }
    } catch (NamingException e) {
        LOGGER.error(e.toString());
        LOGGER.debug(e.toString(), e);
        throw e;
    }
    return iist;
}

From source file:org.lsc.jndi.JndiServices.java

/**
 * Retrieve a specific attribute from an object
 * /*  w ww.j ava  2s .  c  o m*/
 * @param objectDn
 * @param attribute
 * @return
 * @throws LscServiceException
 */
public List<String> getAttributeValues(String objectDn, String attribute) throws LscServiceException {
    List<String> values = null;
    try {
        // Setup search
        SearchControls sc = new SearchControls();
        sc.setDerefLinkFlag(false);
        sc.setReturningAttributes(new String[] { attribute });
        sc.setSearchScope(SearchControls.OBJECT_SCOPE);
        sc.setReturningObjFlag(true);

        // Retrieve attribute values
        SearchResult res = getEntry(objectDn, "objectClass=*", sc, SearchControls.OBJECT_SCOPE);
        Attribute attr = res.getAttributes().get(attribute);
        if (attr != null) {
            values = new ArrayList<String>();
            NamingEnumeration<?> enu = attr.getAll();
            while (enu.hasMoreElements()) {
                Object val = enu.next();
                values.add(val.toString());
            }
        }
    } catch (NamingException e) {
        throw new LscServiceException(e);
    }
    return values;
}

From source file:org.lsc.jndi.JndiServices.java

public Map<String, LscDatasets> doGetAttrsList(final String base, final String filter, final int scope,
        final List<String> attrsNames) throws NamingException {

    // sanity checks
    String searchBase = base == null ? "" : rewriteBase(base);
    String searchFilter = filter == null ? DEFAULT_FILTER : filter;

    Map<String, LscDatasets> res = new LinkedHashMap<String, LscDatasets>();

    if (attrsNames == null || attrsNames.size() == 0) {
        LOGGER.error("No attribute names to read! Check configuration.");
        return res;
    }/*from  w  w  w .  j  a  v a2s  .c  o m*/

    String[] attributes = new String[attrsNames.size()];
    attributes = attrsNames.toArray(attributes);

    SearchControls constraints = new SearchControls();
    constraints.setDerefLinkFlag(false);
    constraints.setReturningAttributes(attributes);
    constraints.setSearchScope(scope);
    constraints.setReturningObjFlag(true);

    try {
        boolean requestPagedResults = false;

        List<Control> extControls = new ArrayList<Control>();

        if (pageSize > 0) {
            requestPagedResults = true;
            LOGGER.debug("Using pagedResults control for {} entries at a time", pageSize);
        }

        if (requestPagedResults) {
            extControls.add(new PagedResultsControl(pageSize, Control.CRITICAL));
        }

        if (sortedBy != null) {
            extControls.add(new SortControl(sortedBy, Control.CRITICAL));
        }

        if (extControls.size() > 0) {
            ctx.setRequestControls(extControls.toArray(new Control[extControls.size()]));
        }

        byte[] pagedResultsResponse = null;
        do {
            NamingEnumeration<SearchResult> results = ctx.search(searchBase, searchFilter, constraints);

            if (results != null) {
                Map<String, Object> attrsValues = null;
                while (results.hasMoreElements()) {
                    attrsValues = new HashMap<String, Object>();

                    SearchResult ldapResult = (SearchResult) results.next();

                    // get the value for each attribute requested
                    for (String attributeName : attrsNames) {
                        Attribute attr = ldapResult.getAttributes().get(attributeName);
                        if (attr != null && attr.get() != null) {
                            attrsValues.put(attributeName, attr.get());
                        }
                    }

                    res.put(ldapResult.getNameInNamespace(), new LscDatasets(attrsValues));
                }
            }

            Control[] respCtls = ctx.getResponseControls();
            if (respCtls != null) {
                for (Control respCtl : respCtls) {
                    if (requestPagedResults && respCtl instanceof PagedResultsResponseControl) {
                        pagedResultsResponse = ((PagedResultsResponseControl) respCtl).getCookie();
                    }
                }
            }

            if (requestPagedResults && pagedResultsResponse != null) {
                ctx.setRequestControls(new Control[] {
                        new PagedResultsControl(pageSize, pagedResultsResponse, Control.CRITICAL) });
            }

        } while (pagedResultsResponse != null);

        // clear requestControls for future use of the JNDI context
        if (requestPagedResults) {
            ctx.setRequestControls(null);
        }
    } catch (CommunicationException e) {
        // Avoid handling the communication exception as a generic one
        throw e;
    } catch (ServiceUnavailableException e) {
        // Avoid handling the service unavailable exception as a generic one
        throw e;
    } catch (NamingException e) {
        // clear requestControls for future use of the JNDI context
        ctx.setRequestControls(null);
        LOGGER.error(e.toString());
        LOGGER.debug(e.toString(), e);

    } catch (IOException e) {
        // clear requestControls for future use of the JNDI context
        ctx.setRequestControls(null);
        LOGGER.error(e.toString());
        LOGGER.debug(e.toString(), e);
    }
    return res;
}

From source file:org.springframework.ldap.core.LdapTemplate.java

private SearchControls getDefaultSearchControls(int searchScope, boolean returningObjFlag, String[] attrs) {

    SearchControls controls = new SearchControls();
    controls.setSearchScope(searchScope);
    controls.setReturningObjFlag(returningObjFlag);
    controls.setReturningAttributes(attrs);
    return controls;
}

From source file:org.springframework.ldap.core.LdapTemplate.java

/**
 * Make sure the returnObjFlag is set in the supplied SearchControls. Set it
 * and log if it's not set./*  w  ww  .j a  v a 2s. c  om*/
 * 
 * @param controls the SearchControls to check.
 */
private void assureReturnObjFlagSet(SearchControls controls) {
    Validate.notNull(controls);
    if (!controls.getReturningObjFlag()) {
        log.info("The returnObjFlag of supplied SearchControls is not set"
                + " but a ContextMapper is used - setting flag to true");
        controls.setReturningObjFlag(true);
    }
}