Search in sources :

Example 46 with InvalidNameException

use of javax.naming.InvalidNameException in project teiid by teiid.

the class LDAPQueryExecution method next.

/**
 * Fetch the next batch of data from the LDAP searchEnumerationr result.
 * @return the next Batch of results.
 */
// GHH 20080326 - set all batches as last batch after an exception
// is thrown calling a method on the enumeration.  Per Javadoc for
// javax.naming.NamingEnumeration, enumeration is invalid after an
// exception is thrown - by setting last batch indicator we prevent
// it from being used again.
// GHH 20080326 - also added return of explanation for generic
// NamingException
public List<?> next() throws TranslatorException {
    try {
        if (unwrapIterator != null) {
            if (unwrapIterator.hasNext()) {
                return unwrapIterator.next();
            }
            unwrapIterator = null;
        }
        // The search has been executed, so process up to one batch of
        // results.
        List<?> result = null;
        while (result == null && searchEnumeration != null && searchEnumeration.hasMore()) {
            SearchResult searchResult = (SearchResult) searchEnumeration.next();
            try {
                result = getRow(searchResult);
            } catch (InvalidNameException e) {
            }
        }
        if (result == null && this.executionFactory.usePagination()) {
            byte[] cookie = null;
            Control[] controls = ldapCtx.getResponseControls();
            if (controls != null) {
                for (int i = 0; i < controls.length; i++) {
                    if (controls[i] instanceof PagedResultsResponseControl) {
                        PagedResultsResponseControl prrc = (PagedResultsResponseControl) controls[i];
                        cookie = prrc.getCookie();
                    }
                }
            }
            if (cookie == null) {
                return null;
            }
            setRequestControls(cookie);
            executeSearch();
            return next();
        }
        if (result != null) {
            resultCount++;
        }
        return result;
    } catch (SizeLimitExceededException e) {
        if (resultCount != searchDetails.getCountLimit()) {
            String msg = LDAPPlugin.Util.gs(LDAPPlugin.Event.TEIID12008);
            TranslatorException te = new TranslatorException(e, msg);
            if (executionFactory.isExceptionOnSizeLimitExceeded()) {
                throw te;
            }
            this.executionContext.addWarning(te);
            LogManager.logWarning(LogConstants.CTX_CONNECTOR, e, msg);
        }
        // GHH 20080326 - if size limit exceeded don't try to read more results
        return null;
    } catch (NamingException ne) {
        // $NON-NLS-1$
        throw new TranslatorException(ne, LDAPPlugin.Util.gs("ldap_error"));
    }
}
Also used : Control(javax.naming.ldap.Control) SortControl(javax.naming.ldap.SortControl) PagedResultsControl(javax.naming.ldap.PagedResultsControl) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) SizeLimitExceededException(javax.naming.SizeLimitExceededException) InvalidNameException(javax.naming.InvalidNameException) PagedResultsResponseControl(javax.naming.ldap.PagedResultsResponseControl) SearchResult(javax.naming.directory.SearchResult) TranslatorException(org.teiid.translator.TranslatorException) NamingException(javax.naming.NamingException)

Example 47 with InvalidNameException

use of javax.naming.InvalidNameException in project geronimo-xbean by apache.

the class AbstractContext method removeDeepBinding.

protected void removeDeepBinding(Name name, boolean pruneEmptyContexts, boolean removeNotEmptyContext) throws NamingException {
    if (name == null)
        throw new NullPointerException("name is null");
    if (name.isEmpty()) {
        throw new InvalidNameException("Name is empty");
    }
    if (name.size() == 1) {
        removeBinding(name.get(0), removeNotEmptyContext);
        return;
    }
    if (!pruneEmptyContexts) {
        Context context = lookupFinalContext(name);
        context.unbind(name.getSuffix(name.size() - 1));
    } else {
        // we serch the tree for a target context and name to remove
        // this is normally the last context in the tree and the final name part, but
        // it may be farther up the path if the intervening nodes are empty
        Context targetContext = this;
        String targetName = name.get(0);
        Context currentContext = this;
        for (int i = 0; i < name.size(); i++) {
            String part = name.get(i);
            // empty path parts are not allowed
            if (part.length() == 0) {
                throw new InvalidNameException("Name part " + i + " is empty: " + name);
            }
            // update targets
            if (getSize(currentContext) > 1) {
                targetContext = currentContext;
                targetName = part;
            }
            // Is this the last element in the name?
            if (i == name.size() - 1) {
                // we're at the end... unbind value
                unbind(targetContext, targetName, true);
                // all done... this is redundant but makes the code more readable
                break;
            } else {
                Object currentValue = getBinding(currentContext, part);
                if (currentValue == null) {
                    // path not found we are done, but first prune the empty contexts
                    if (targetContext != currentContext) {
                        unbind(targetContext, targetName, false);
                    }
                    break;
                } else {
                    // the current value must be a context
                    if (!(currentValue instanceof Context)) {
                        throw new NotContextException("Expected an instance of context to be bound at " + part + " but found an instance of " + currentValue.getClass().getName());
                    }
                    currentContext = (Context) currentValue;
                // now we recurse into the current context
                }
            }
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NotContextException(javax.naming.NotContextException) InvalidNameException(javax.naming.InvalidNameException)

Example 48 with InvalidNameException

use of javax.naming.InvalidNameException in project geronimo-xbean by apache.

the class AbstractContext method addDeepBinding.

// 
// Add Binding
// 
protected void addDeepBinding(Name name, Object value, boolean rebind, boolean createIntermediateContexts) throws NamingException {
    if (name == null)
        throw new NullPointerException("name is null");
    if (value == null)
        throw new NullPointerException("value is null for name: " + name);
    if (name.isEmpty()) {
        throw new InvalidNameException("Name is empty");
    }
    if (name.size() == 1) {
        addBinding(name.get(0), value, rebind);
        return;
    }
    if (!createIntermediateContexts) {
        Context context = lookupFinalContext(name);
        String lastSegment = name.get(name.size() - 1);
        addBinding(context, lastSegment, value, rebind);
    } else {
        Context currentContext = this;
        for (int i = 0; i < name.size(); i++) {
            String part = name.get(i);
            // empty path parts are not allowed
            if (part.length() == 0) {
                // this could be supported but it would be tricky
                throw new InvalidNameException("Name part " + i + " is empty: " + name);
            }
            // Is this the last element in the name?
            if (i == name.size() - 1) {
                // we're at the end... (re)bind the value into the parent context
                addBinding(currentContext, part, value, rebind);
                // all done... this is redundant but makes the code more readable
                break;
            } else {
                Object currentValue = getBinding(currentContext, part);
                if (currentValue == null) {
                    // the next step in the tree is not present, so create everything down
                    // and add it to the current bindings
                    Context subcontext = createSubcontextTree(name.getPrefix(i).toString(), name.getSuffix(i), value);
                    addBinding(currentContext, part, subcontext, rebind);
                    // all done
                    break;
                } else {
                    // the current value must be a nested subcontext
                    if (!(currentValue instanceof Context)) {
                        throw new NotContextException("Expected an instance of context to be bound at " + part + " but found an instance of " + currentValue.getClass().getName());
                    }
                    currentContext = (Context) currentValue;
                // now we recurse into the current context
                }
            }
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NotContextException(javax.naming.NotContextException) InvalidNameException(javax.naming.InvalidNameException)

Example 49 with InvalidNameException

use of javax.naming.InvalidNameException in project geronimo-xbean by apache.

the class AbstractContext method createSubcontextTree.

/**
 * Creates a context tree which will be rooted at the specified path and contain a single entry located down
 * a path specified by the name.  All necessary intermediate contexts will be created using the createContext method.
 * @param path the path to the context that will contains this context
 * @param name the name under which the value should be bound
 * @param value the value
 * @return a context with the value bound at the specified name
 * @throws NamingException if a problem occurs while creating the subcontext tree
 */
protected Context createSubcontextTree(String path, Name name, Object value) throws NamingException {
    if (path == null)
        throw new NullPointerException("path is null");
    if (name == null)
        throw new NullPointerException("name is null");
    if (name.size() < 2)
        throw new InvalidNameException("name must have at least 2 parts " + name);
    if (path.length() > 0 && !path.endsWith("/"))
        path += "/";
    for (int i = name.size() - 1; i > 0; i--) {
        String fullPath = path + name.getPrefix(i);
        String key = name.get(i);
        value = createNestedSubcontext(fullPath, Collections.singletonMap(key, value));
    }
    return (Context) value;
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) InvalidNameException(javax.naming.InvalidNameException)

Example 50 with InvalidNameException

use of javax.naming.InvalidNameException in project i2p.i2p by i2p.

the class DefaultHostnameVerifier method extractCN.

static String extractCN(final String subjectPrincipal) throws SSLException {
    if (subjectPrincipal == null) {
        return null;
    }
    try {
        final LdapName subjectDN = new LdapName(subjectPrincipal);
        final List<Rdn> rdns = subjectDN.getRdns();
        for (int i = rdns.size() - 1; i >= 0; i--) {
            final Rdn rds = rdns.get(i);
            final Attributes attributes = rds.toAttributes();
            final Attribute cn = attributes.get("cn");
            if (cn != null) {
                try {
                    final Object value = cn.get();
                    if (value != null) {
                        return value.toString();
                    }
                } catch (NoSuchElementException ignore) {
                } catch (NamingException ignore) {
                }
            }
        }
        return null;
    } catch (InvalidNameException e) {
        throw new SSLException(subjectPrincipal + " is not a valid X500 distinguished name");
    }
}
Also used : InvalidNameException(javax.naming.InvalidNameException) Attribute(javax.naming.directory.Attribute) Attributes(javax.naming.directory.Attributes) NamingException(javax.naming.NamingException) Rdn(javax.naming.ldap.Rdn) SSLException(javax.net.ssl.SSLException) NoSuchElementException(java.util.NoSuchElementException) LdapName(javax.naming.ldap.LdapName)

Aggregations

InvalidNameException (javax.naming.InvalidNameException)68 LdapName (javax.naming.ldap.LdapName)30 Rdn (javax.naming.ldap.Rdn)24 CompositeName (javax.naming.CompositeName)12 NamingException (javax.naming.NamingException)11 ArrayList (java.util.ArrayList)9 Name (javax.naming.Name)8 NameNotFoundException (javax.naming.NameNotFoundException)8 Context (javax.naming.Context)7 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)6 NotContextException (javax.naming.NotContextException)6 Attribute (javax.naming.directory.Attribute)6 X509Certificate (java.security.cert.X509Certificate)5 NoSuchElementException (java.util.NoSuchElementException)5 OperationNotSupportedException (javax.naming.OperationNotSupportedException)5 Attributes (javax.naming.directory.Attributes)5 IOException (java.io.IOException)4 URISyntaxException (java.net.URISyntaxException)4 SSLException (javax.net.ssl.SSLException)4 HashMap (java.util.HashMap)3