Example usage for javax.naming NamingException NamingException

List of usage examples for javax.naming NamingException NamingException

Introduction

In this page you can find the example usage for javax.naming NamingException NamingException.

Prototype

public NamingException(String explanation) 

Source Link

Document

Constructs a new NamingException with an explanation.

Usage

From source file:org.apache.cloudstack.ldap.LdapUserManager.java

private LdapUser getUserForDn(String userdn, LdapContext context) throws NamingException {
    final SearchControls controls = new SearchControls();
    controls.setSearchScope(_ldapConfiguration.getScope());
    controls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());

    NamingEnumeration<SearchResult> result = context.search(userdn,
            "(objectClass=" + _ldapConfiguration.getUserObject() + ")", controls);
    if (result.hasMoreElements()) {
        return createUser(result.nextElement());
    } else {//from w  w  w  . j av a  2  s.co  m
        throw new NamingException("No user found for dn " + userdn);
    }
}

From source file:org.apache.cloudstack.ldap.OpenLdapUserManagerImpl.java

@Override
public LdapUser getUser(final String username, final LdapContext context) throws NamingException, IOException {
    List<LdapUser> result = searchUsers(username, context);
    if (result != null && result.size() == 1) {
        return result.get(0);
    } else {//from w  w w. ja  v a  2  s. co m
        throw new NamingException("No user found for username " + username);
    }
}

From source file:org.apache.cloudstack.ldap.OpenLdapUserManagerImpl.java

public LdapUser searchUser(final String basedn, final String searchString, final LdapContext context)
        throws NamingException, IOException {
    final SearchControls searchControls = new SearchControls();

    searchControls.setSearchScope(_ldapConfiguration.getScope());
    searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes());

    NamingEnumeration<SearchResult> results = context.search(basedn, searchString, searchControls);
    final List<LdapUser> users = new ArrayList<LdapUser>();
    while (results.hasMoreElements()) {
        final SearchResult result = results.nextElement();
        users.add(createUser(result));//from www .ja  va2s . c o m
    }

    if (users.size() == 1) {
        return users.get(0);
    } else {
        throw new NamingException("No user found for basedn " + basedn + " and searchString " + searchString);
    }
}

From source file:org.apache.directory.server.jndi.ServerContextFactory.java

/**
 * Starts up the LDAPS protocol provider to service LDAPS requests
 *
 * @throws NamingException if there are problems starting the LDAPS provider
 *//*w  w  w  . ja v a 2 s.com*/
private void startLDAPS(ServerStartupConfiguration cfg, Hashtable env) throws NamingException {
    // Skip if disabled
    if (!cfg.isEnableLdaps()) {
        return;
    }

    // We use the reflection API in case this is not running on JDK 1.5+.
    IoFilterChainBuilder chain;
    try {
        chain = (IoFilterChainBuilder) Class
                .forName("org.apache.directory.server.ssl.LdapsInitializer", true,
                        ServerContextFactory.class.getClassLoader())
                .getMethod("init", new Class[] { ServerStartupConfiguration.class })
                .invoke(null, new Object[] { cfg });
        ldapsStarted = true;
    } catch (InvocationTargetException e) {
        if (e.getCause() instanceof NamingException) {
            throw (NamingException) e.getCause();
        } else {
            throw (NamingException) new NamingException("Failed to load LDAPS initializer.")
                    .initCause(e.getCause());
        }
    } catch (Exception e) {
        throw (NamingException) new NamingException("Failed to load LDAPS initializer.").initCause(e);
    }

    startLDAP0(cfg, env, cfg.getLdapsPort(), chain);
}

From source file:org.apache.directory.studio.connection.core.io.api.DirectoryApiConnectionWrapper.java

/**
 * {@inheritDoc}/*from  ww w .  j  a  v a2s .  c o  m*/
 */
public StudioNamingEnumeration search(final String searchBase, final String filter,
        final SearchControls searchControls, final AliasDereferencingMethod aliasesDereferencingMethod,
        final ReferralHandlingMethod referralsHandlingMethod, final Control[] controls,
        final StudioProgressMonitor monitor, final ReferralsInfo referralsInfo) {
    final long requestNum = searchRequestNum++;

    InnerRunnable runnable = new InnerRunnable() {
        public void run() {
            try {
                // Preparing the search request
                SearchRequest request = new SearchRequestImpl();
                request.setBase(new Dn(searchBase));
                ExprNode node = FilterParser.parse(filter, true);
                request.setFilter(node);
                request.setScope(convertSearchScope(searchControls));
                if (searchControls.getReturningAttributes() != null) {
                    request.addAttributes(searchControls.getReturningAttributes());
                }
                request.addAllControls(convertControls(controls));
                request.setSizeLimit(searchControls.getCountLimit());
                request.setTimeLimit(searchControls.getTimeLimit());
                request.setDerefAliases(convertAliasDerefMode(aliasesDereferencingMethod));

                // Performing the search operation
                SearchCursor cursor = ldapConnection.search(request);

                // Returning the result of the search
                namingEnumeration = new CursorStudioNamingEnumeration(connection, cursor, searchBase, filter,
                        searchControls, aliasesDereferencingMethod, referralsHandlingMethod, controls,
                        requestNum, monitor, referralsInfo);
            } catch (Exception e) {
                exception = e;
            }

            NamingException ne = null;
            if (exception != null) {
                ne = new NamingException(exception.getMessage());
            }

            for (IJndiLogger logger : getJndiLoggers()) {
                if (namingEnumeration != null) {
                    logger.logSearchRequest(connection, searchBase, filter, searchControls,
                            aliasesDereferencingMethod, controls, requestNum, ne);
                } else {
                    logger.logSearchRequest(connection, searchBase, filter, searchControls,
                            aliasesDereferencingMethod, controls, requestNum, ne);
                    logger.logSearchResultDone(connection, 0, requestNum, ne);
                }
            }
        }
    };

    try {
        checkConnectionAndRunAndMonitor(runnable, monitor);
    } catch (Exception e) {
        monitor.reportError(e);
        return null;
    }

    if (runnable.isCanceled()) {
        monitor.setCanceled(true);
    }
    if (runnable.getException() != null) {
        monitor.reportError(runnable.getException());
        return null;
    } else {
        return runnable.getResult();
    }
}

From source file:org.apache.directory.studio.connection.core.io.api.DirectoryApiConnectionWrapper.java

/**
 * {@inheritDoc}//from  ww w  .  j  a va  2s  .c o  m
 */
public void modifyEntry(final String dn, final ModificationItem[] modificationItems, final Control[] controls,
        final StudioProgressMonitor monitor, final ReferralsInfo referralsInfo) {
    if (connection.isReadOnly()) {
        monitor.reportError(
                new Exception(NLS.bind(Messages.error__connection_is_readonly, connection.getName())));
        return;
    }

    InnerRunnable runnable = new InnerRunnable() {
        public void run() {
            try {
                // Preparing the modify request
                ModifyRequest request = new ModifyRequestImpl();
                request.setName(new Dn(dn));
                Modification[] modifications = convertModificationItems(modificationItems);
                if (modifications != null) {
                    for (Modification modification : modifications) {
                        request.addModification(modification);
                    }
                }
                request.addAllControls(convertControls(controls));

                // Performing the modify operation
                ModifyResponse modifyResponse = ldapConnection.modify(request);

                // Handle referral
                Consumer<ReferralHandlingData> consumer = referralHandlingData -> referralHandlingData.connectionWrapper
                        .modifyEntry(referralHandlingData.referralDn, modificationItems, controls, monitor,
                                referralHandlingData.newReferralsInfo);

                if (checkAndHandleReferral(modifyResponse, monitor, referralsInfo, consumer)) {
                    return;
                }

                // Checking the response
                checkResponse(modifyResponse);
            } catch (Exception e) {
                exception = e;
            }

            NamingException ne = null;
            if (exception != null) {
                ne = new NamingException(exception.getMessage());
            }

            for (IJndiLogger logger : getJndiLoggers()) {
                logger.logChangetypeModify(connection, dn, modificationItems, controls, ne);
            }
        }
    };

    try {
        checkConnectionAndRunAndMonitor(runnable, monitor);
    } catch (Exception e) {
        monitor.reportError(e);
    }

    if (runnable.isCanceled()) {
        monitor.setCanceled(true);
    }
    if (runnable.getException() != null) {
        monitor.reportError(runnable.getException());
    }
}

From source file:org.apache.directory.studio.connection.core.io.api.DirectoryApiConnectionWrapper.java

/**
 * {@inheritDoc}//from  w  w  w. j  a v a  2  s  . c o m
 */
public void renameEntry(final String oldDn, final String newDn, final boolean deleteOldRdn,
        final Control[] controls, final StudioProgressMonitor monitor, final ReferralsInfo referralsInfo) {
    if (connection.isReadOnly()) {
        monitor.reportError(
                new Exception(NLS.bind(Messages.error__connection_is_readonly, connection.getName())));
        return;
    }

    InnerRunnable runnable = new InnerRunnable() {
        public void run() {
            try {
                // Preparing the rename request
                ModifyDnRequest request = new ModifyDnRequestImpl();
                request.setName(new Dn(oldDn));
                request.setDeleteOldRdn(deleteOldRdn);
                Dn newName = new Dn(newDn);
                request.setNewRdn(newName.getRdn());
                request.setNewSuperior(newName.getParent());
                request.addAllControls(convertControls(controls));

                // Performing the rename operation
                ModifyDnResponse modifyDnResponse = ldapConnection.modifyDn(request);

                // Handle referral
                Consumer<ReferralHandlingData> consumer = referralHandlingData -> referralHandlingData.connectionWrapper
                        .renameEntry(oldDn, newDn, deleteOldRdn, controls, monitor,
                                referralHandlingData.newReferralsInfo);

                if (checkAndHandleReferral(modifyDnResponse, monitor, referralsInfo, consumer)) {
                    return;
                }

                // Checking the response
                checkResponse(modifyDnResponse);
            } catch (Exception e) {
                exception = e;
            }

            NamingException ne = null;
            if (exception != null) {
                ne = new NamingException(exception.getMessage());
            }

            for (IJndiLogger logger : getJndiLoggers()) {
                logger.logChangetypeModDn(connection, oldDn, newDn, deleteOldRdn, controls, ne);
            }
        }
    };

    try {
        checkConnectionAndRunAndMonitor(runnable, monitor);
    } catch (Exception e) {
        monitor.reportError(e);
    }

    if (runnable.isCanceled()) {
        monitor.setCanceled(true);
    }
    if (runnable.getException() != null) {
        monitor.reportError(runnable.getException());
    }
}

From source file:org.apache.directory.studio.connection.core.io.api.DirectoryApiConnectionWrapper.java

/**
 * {@inheritDoc}/*w w  w.j  a  v  a 2  s. co  m*/
 */
public void createEntry(final String dn, final Attributes attributes, final Control[] controls,
        final StudioProgressMonitor monitor, final ReferralsInfo referralsInfo) {
    if (connection.isReadOnly()) {
        monitor.reportError(
                new Exception(NLS.bind(Messages.error__connection_is_readonly, connection.getName())));
        return;
    }

    InnerRunnable runnable = new InnerRunnable() {
        public void run() {
            try {
                // Preparing the add request
                AddRequest request = new AddRequestImpl();
                request.setEntryDn(new Dn(dn));
                request.setEntry(AttributeUtils.toEntry(attributes, new Dn(dn)));
                request.addAllControls(convertControls(controls));

                // Performing the add operation
                AddResponse addResponse = ldapConnection.add(request);

                // Handle referral
                Consumer<ReferralHandlingData> consumer = referralHandlingData -> referralHandlingData.connectionWrapper
                        .createEntry(referralHandlingData.referralDn, attributes, controls, monitor,
                                referralHandlingData.newReferralsInfo);

                if (checkAndHandleReferral(addResponse, monitor, referralsInfo, consumer)) {
                    return;
                }

                // Checking the response
                checkResponse(addResponse);
            } catch (Exception e) {
                exception = e;
            }

            NamingException ne = null;
            if (exception != null) {
                ne = new NamingException(exception.getMessage());
            }

            for (IJndiLogger logger : getJndiLoggers()) {
                logger.logChangetypeAdd(connection, dn, attributes, controls, ne);
            }
        }
    };

    try {
        checkConnectionAndRunAndMonitor(runnable, monitor);
    } catch (Exception e) {
        monitor.reportError(e);
    }

    if (runnable.isCanceled()) {
        monitor.setCanceled(true);
    }
    if (runnable.getException() != null) {
        monitor.reportError(runnable.getException());
    }
}

From source file:org.apache.directory.studio.connection.core.io.api.DirectoryApiConnectionWrapper.java

/**
 * {@inheritDoc}/*  ww  w.j a v  a2 s .  c om*/
 */
public void deleteEntry(final String dn, final Control[] controls, final StudioProgressMonitor monitor,
        final ReferralsInfo referralsInfo) {
    if (connection.isReadOnly()) {
        monitor.reportError(
                new Exception(NLS.bind(Messages.error__connection_is_readonly, connection.getName())));
        return;
    }

    InnerRunnable runnable = new InnerRunnable() {
        public void run() {
            try {
                // Preparing the delete request
                DeleteRequest request = new DeleteRequestImpl();
                request.setName(new Dn(dn));
                request.addAllControls(convertControls(controls));

                // Performing the delete operation
                DeleteResponse deleteResponse = ldapConnection.delete(request);

                // Handle referral
                Consumer<ReferralHandlingData> consumer = referralHandlingData -> referralHandlingData.connectionWrapper
                        .deleteEntry(referralHandlingData.referralDn, controls, monitor,
                                referralHandlingData.newReferralsInfo);

                if (checkAndHandleReferral(deleteResponse, monitor, referralsInfo, consumer)) {
                    return;
                }

                // Checking the response
                checkResponse(deleteResponse);
            } catch (Exception e) {
                exception = e;
            }

            NamingException ne = null;
            if (exception != null) {
                ne = new NamingException(exception.getMessage());
            }

            for (IJndiLogger logger : getJndiLoggers()) {
                logger.logChangetypeDelete(connection, dn, controls, ne);
            }
        }
    };

    try {
        checkConnectionAndRunAndMonitor(runnable, monitor);
    } catch (Exception e) {
        monitor.reportError(e);
    }

    if (runnable.isCanceled()) {
        monitor.setCanceled(true);
    }
    if (runnable.getException() != null) {
        monitor.reportError(runnable.getException());
    }
}

From source file:org.apache.directory.studio.connection.core.io.api.DirectoryApiConnectionWrapper.java

private void checkConnectionAndRunAndMonitor(final InnerRunnable runnable, final StudioProgressMonitor monitor)
        throws Exception {
    // check connection
    if (!isConnected || ldapConnection == null) {
        doConnect(monitor);/*from w w  w .  jav a  2  s  .c  o m*/
        doBind(monitor);
    }
    if (ldapConnection == null) {
        throw new NamingException(Messages.DirectoryApiConnectionWrapper_NoConnection);
    }

    // loop for reconnection
    for (int i = 0; i <= 1; i++) {
        runAndMonitor(runnable, monitor);

        // check reconnection
        if ((i == 0) && (runnable.getException() instanceof InvalidConnectionException)) {
            doConnect(monitor);
            doBind(monitor);
            runnable.reset();
        } else {
            break;
        }
    }
}