Example usage for javax.naming.ldap LdapContext getNameInNamespace

List of usage examples for javax.naming.ldap LdapContext getNameInNamespace

Introduction

In this page you can find the example usage for javax.naming.ldap LdapContext getNameInNamespace.

Prototype

public String getNameInNamespace() throws NamingException;

Source Link

Document

Retrieves the full name of this context within its own namespace.

Usage

From source file:edu.vt.middleware.ldap.AbstractLdap.java

/**
 * This will return the matching attributes associated with the supplied dn.
 * If retAttrs is null then all attributes will be returned. If retAttrs is an
 * empty array then no attributes will be returned. See {@link
 * javax.naming.DirContext#getAttributes(String, String[])}.
 *
 * @param  dn  <code>String</code> named object in the LDAP
 * @param  retAttrs  <code>String[]</code> attributes to return
 * @param  handler  <code>AttributeHandler[]</code> to post process results
 *
 * @return  <code>Attributes</code>
 *
 * @throws  NamingException  if the LDAP returns an error
 *///from w ww  .j a  v  a  2s. c o  m
protected Attributes getAttributes(final String dn, final String[] retAttrs, final AttributeHandler... handler)
        throws NamingException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Attribute search with the following parameters:");
        this.logger.debug("  dn = " + dn);
        this.logger.debug("  retAttrs = " + (retAttrs == null ? "all attributes" : Arrays.toString(retAttrs)));
        this.logger.debug("  handler = " + Arrays.toString(handler));
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("  config = " + this.config.getEnvironment());
        }
    }

    LdapContext ctx = null;
    Attributes attrs = null;
    try {
        for (int i = 0; i <= this.config.getOperationRetry() || this.config.getOperationRetry() == -1; i++) {
            try {
                ctx = this.getContext();
                attrs = ctx.getAttributes(dn, retAttrs);

                if (handler != null && handler.length > 0) {
                    final SearchCriteria sc = new SearchCriteria();
                    if (ctx != null && !"".equals(ctx.getNameInNamespace())) {
                        sc.setDn(ctx.getNameInNamespace());
                    } else {
                        sc.setDn(dn);
                    }
                    for (int j = 0; j < handler.length; j++) {
                        attrs = AttributesProcessor.executeHandler(sc, attrs, handler[j],
                                this.config.getHandlerIgnoreExceptions());
                    }
                }

                break;
            } catch (NamingException e) {
                this.operationRetry(ctx, e, i);
            }
        }
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
    return attrs;
}

From source file:edu.vt.middleware.ldap.AbstractLdap.java

/**
 * This will query the LDAP for the supplied dn, matching attributes and
 * return attributes. This method will always perform a one level search. The
 * resulting <code>Iterator</code> is a deep copy of the original search
 * results. If matchAttrs is empty or null then all objects in the target
 * context are returned. If retAttrs is null then all attributes will be
 * returned. If retAttrs is an empty array then no attributes will be
 * returned. See {@link javax.naming.DirContext#search(String, Attributes,
 * String[])}.//ww  w.java2s. c o m
 *
 * @param  dn  <code>String</code> name to search in
 * @param  matchAttrs  <code>Attributes</code> attributes to match
 * @param  retAttrs  <code>String[]</code> attributes to return
 * @param  handler  <code>SearchResultHandler[]</code> to post process results
 *
 * @return  <code>Iterator</code> - of LDAP search results
 *
 * @throws  NamingException  if the LDAP returns an error
 */
protected Iterator<SearchResult> searchAttributes(final String dn, final Attributes matchAttrs,
        final String[] retAttrs, final SearchResultHandler... handler) throws NamingException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("One level search with the following parameters:");
        this.logger.debug("  dn = " + dn);
        this.logger.debug("  matchAttrs = " + matchAttrs);
        this.logger.debug("  retAttrs = " + (retAttrs == null ? "all attributes" : Arrays.toString(retAttrs)));
        this.logger.debug("  handler = " + Arrays.toString(handler));
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("  config = " + this.config.getEnvironment());
        }
    }

    List<SearchResult> results = null;
    LdapContext ctx = null;
    NamingEnumeration<SearchResult> en = null;
    try {
        for (int i = 0; i <= this.config.getOperationRetry() || this.config.getOperationRetry() == -1; i++) {
            try {
                ctx = this.getContext();
                en = ctx.search(dn, matchAttrs, retAttrs);

                if (handler != null && handler.length > 0) {
                    final SearchCriteria sc = new SearchCriteria();
                    if (ctx != null && !"".equals(ctx.getNameInNamespace())) {
                        sc.setDn(ctx.getNameInNamespace());
                    } else {
                        sc.setDn(dn);
                    }
                    sc.setMatchAttrs(matchAttrs);
                    sc.setReturnAttrs(retAttrs);
                    if (handler != null && handler.length > 0) {
                        for (int j = 0; j < handler.length; j++) {
                            if (j == 0) {
                                results = handler[j].process(sc, en, this.config.getHandlerIgnoreExceptions());
                            } else {
                                results = handler[j].process(sc, results);
                            }
                        }
                    }
                } else {
                    results = SR_COPY_RESULT_HANDLER.process(null, en,
                            this.config.getHandlerIgnoreExceptions());
                }

                break;
            } catch (NamingException e) {
                this.operationRetry(ctx, e, i);
            }
        }
    } finally {
        if (en != null) {
            en.close();
        }
        if (ctx != null) {
            ctx.close();
        }
    }
    return results.iterator();
}

From source file:edu.vt.middleware.ldap.AbstractLdap.java

/**
 * This will query the LDAP with the supplied dn, filter, filter arguments,
 * and search controls. This method will perform a search whose scope is
 * defined in the search controls. The resulting <code>Iterator</code> is a
 * deep copy of the original search results. If filterArgs is null, then no
 * variable substitution will occur. See {@link
 * javax.naming.DirContext#search( String, String, Object[], SearchControls)}.
 *
 * @param  dn  <code>String</code> name to begin search at
 * @param  filter  <code>String</code> expression to use for the search
 * @param  filterArgs  <code>Object[]</code> to substitute for variables in
 * the filter/* w  w w.  j av  a 2  s .  c o  m*/
 * @param  searchControls  <code>SearchControls</code> to perform search with
 * @param  handler  <code>SearchResultHandler[]</code> to post process results
 *
 * @return  <code>Iterator</code> - of LDAP search results
 *
 * @throws  NamingException  if the LDAP returns an error
 */
protected Iterator<SearchResult> search(final String dn, final String filter, final Object[] filterArgs,
        final SearchControls searchControls, final SearchResultHandler... handler) throws NamingException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Search with the following parameters:");
        this.logger.debug("  dn = " + dn);
        this.logger.debug("  filter = " + filter);
        this.logger.debug("  filterArgs = " + Arrays.toString(filterArgs));
        this.logger.debug("  searchControls = " + searchControls);
        this.logger.debug("  handler = " + Arrays.toString(handler));
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("  config = " + this.config.getEnvironment());
        }
    }

    List<SearchResult> results = null;
    LdapContext ctx = null;
    NamingEnumeration<SearchResult> en = null;
    try {
        for (int i = 0; i <= this.config.getOperationRetry() || this.config.getOperationRetry() == -1; i++) {
            try {
                ctx = this.getContext();
                en = ctx.search(dn, filter, filterArgs, searchControls);

                if (handler != null && handler.length > 0) {
                    final SearchCriteria sc = new SearchCriteria();
                    if (ctx != null && !"".equals(ctx.getNameInNamespace())) {
                        sc.setDn(ctx.getNameInNamespace());
                    } else {
                        sc.setDn(dn);
                    }
                    sc.setFilter(filter);
                    sc.setFilterArgs(filterArgs);
                    if (searchControls != null) {
                        sc.setReturnAttrs(searchControls.getReturningAttributes());
                    }
                    for (int j = 0; j < handler.length; j++) {
                        if (j == 0) {
                            results = handler[j].process(sc, en, this.config.getHandlerIgnoreExceptions());
                        } else {
                            results = handler[j].process(sc, results);
                        }
                    }
                } else {
                    results = SR_COPY_RESULT_HANDLER.process(null, en,
                            this.config.getHandlerIgnoreExceptions());
                }

                break;
            } catch (NamingException e) {
                this.operationRetry(ctx, e, i);
            }
        }
    } finally {
        if (en != null) {
            en.close();
        }
        if (ctx != null) {
            ctx.close();
        }
    }
    return results.iterator();
}

From source file:edu.vt.middleware.ldap.AbstractLdap.java

/**
 * This will query the LDAP with the supplied dn, filter, filter arguments,
 * and search controls. See {@link #search(String, String, Object[],
 * SearchControls, SearchResultHandler...)}. The PagedResultsControl is used
 * in conjunction with {@link LdapConfig#getPagedResultsSize()} to produce the
 * results.//from   w  w  w  .ja  v a  2  s . c o  m
 *
 * @param  dn  <code>String</code> name to begin search at
 * @param  filter  <code>String</code> expression to use for the search
 * @param  filterArgs  <code>Object[]</code> to substitute for variables in
 * the filter
 * @param  searchControls  <code>SearchControls</code> to perform search with
 * @param  handler  <code>SearchResultHandler[]</code> to post process results
 *
 * @return  <code>Iterator</code> - of LDAP search results
 *
 * @throws  NamingException  if the LDAP returns an error
 */
protected Iterator<SearchResult> pagedSearch(final String dn, final String filter, final Object[] filterArgs,
        final SearchControls searchControls, final SearchResultHandler... handler) throws NamingException {
    if (this.logger.isDebugEnabled()) {
        this.logger.debug("Paginated search with the following parameters:");
        this.logger.debug("  dn = " + dn);
        this.logger.debug("  filter = " + filter);
        this.logger.debug("  filterArgs = " + Arrays.toString(filterArgs));
        this.logger.debug("  searchControls = " + searchControls);
        this.logger.debug("  handler = " + Arrays.toString(handler));
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("  config = " + this.config.getEnvironment());
        }
    }

    final List<SearchResult> results = new ArrayList<SearchResult>();
    LdapContext ctx = null;
    NamingEnumeration<SearchResult> en = null;
    try {
        for (int i = 0; i <= this.config.getOperationRetry() || this.config.getOperationRetry() == -1; i++) {
            try {
                byte[] cookie = null;
                ctx = this.getContext();
                ctx.setRequestControls(new Control[] {
                        new PagedResultsControl(this.config.getPagedResultsSize(), Control.CRITICAL), });
                do {
                    List<SearchResult> pagedResults = null;
                    en = ctx.search(dn, filter, filterArgs, searchControls);

                    if (handler != null && handler.length > 0) {
                        final SearchCriteria sc = new SearchCriteria();
                        if (ctx != null && !"".equals(ctx.getNameInNamespace())) {
                            sc.setDn(ctx.getNameInNamespace());
                        } else {
                            sc.setDn(dn);
                        }
                        sc.setFilter(filter);
                        sc.setFilterArgs(filterArgs);
                        if (searchControls != null) {
                            sc.setReturnAttrs(searchControls.getReturningAttributes());
                        }
                        for (int j = 0; j < handler.length; j++) {
                            if (j == 0) {
                                pagedResults = handler[j].process(sc, en,
                                        this.config.getHandlerIgnoreExceptions());
                            } else {
                                pagedResults = handler[j].process(sc, pagedResults);
                            }
                        }
                    } else {
                        pagedResults = SR_COPY_RESULT_HANDLER.process(null, en,
                                this.config.getHandlerIgnoreExceptions());
                    }

                    results.addAll(pagedResults);

                    final Control[] controls = ctx.getResponseControls();
                    if (controls != null) {
                        for (int j = 0; j < controls.length; j++) {
                            if (controls[j] instanceof PagedResultsResponseControl) {
                                final PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[j];
                                cookie = prrc.getCookie();
                            }
                        }
                    }

                    // re-activate paged results
                    ctx.setRequestControls(
                            new Control[] { new PagedResultsControl(this.config.getPagedResultsSize(), cookie,
                                    Control.CRITICAL), });

                } while (cookie != null);

                break;
            } catch (NamingException e) {
                this.operationRetry(ctx, e, i);
            } catch (IOException e) {
                if (this.logger.isErrorEnabled()) {
                    this.logger.error("Could not encode page size into control", e);
                }
                throw new NamingException(e.getMessage());
            }
        }
    } finally {
        if (en != null) {
            en.close();
        }
        if (ctx != null) {
            ctx.close();
        }
    }
    return results.iterator();
}