Example usage for javax.naming NamingException getCause

List of usage examples for javax.naming NamingException getCause

Introduction

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

Prototype

public Throwable getCause() 

Source Link

Document

Returns the cause of this exception.

Usage

From source file:com.ca.dvs.app.dvs_servlet.resources.RAML.java

/**
 * Get the servlet configuration/*  ww  w  .jav a 2  s .  c  om*/
 * <p>
 * @return HTTP response containing the Servlet configuration in JSON format
 */
@GET
@Path("config")
@Produces(MediaType.APPLICATION_JSON)
public Response getConfig() {

    log.info("GET raml/config");

    Map<String, Object> configMap = new LinkedHashMap<String, Object>();

    try {
        Context initialContext = new InitialContext();
        Context envContext = (Context) initialContext.lookup("java:comp/env");

        configMap.put("vseServerUrl", envContext.lookup("vseServerUrl"));
        configMap.put("vseServicePortRange", envContext.lookup("vseServicePortRange"));
        configMap.put("vseServiceReadyWaitSeconds", envContext.lookup("vseServiceReadyWaitSeconds"));

    } catch (NamingException e) {
        e.printStackTrace();
        log.error("Failed to obtain servlet configuration", e.getCause());
    }

    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.setPrettyPrinting();
    gsonBuilder.serializeNulls();

    Gson gson = gsonBuilder.create();

    Response response = Response.status(200).entity(gson.toJson(configMap)).build();

    return response;
}

From source file:hudson.plugins.active_directory.ActiveDirectoryUnixAuthenticationProvider.java

public GroupDetails loadGroupByGroupname(final String groupname) {
    try {/*from  w w  w  .  ja  va2 s  .c  o m*/
        return groupCache.get(groupname, new Callable<ActiveDirectoryGroupDetails>() {
            public ActiveDirectoryGroupDetails call() {
                for (ActiveDirectoryDomain domain : domains) {
                    if (domain == null) {
                        throw new UserMayOrMayNotExistException(
                                "Unable to retrieve group information without bind DN/password configured");
                    }
                    // when we use custom socket factory below, every LDAP operations result
                    // in a classloading via context classloader, so we need it to resolve.
                    ClassLoader ccl = Thread.currentThread().getContextClassLoader();
                    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
                    try {
                        DirContext context = descriptor.bind(domain.getBindName(),
                                domain.getBindPassword().getPlainText(), obtainLDAPServers(domain));

                        try {
                            final String domainDN = toDC(domain.getName());

                            Attributes group = new LDAPSearchBuilder(context, domainDN).subTreeScope()
                                    .searchOne("(& (cn={0})(objectCategory=group))", groupname);
                            if (group == null) {
                                // failed to find it. Fall back to sAMAccountName.
                                // see http://www.nabble.com/Re%3A-Hudson-AD-plug-in-td21428668.html
                                LOGGER.log(Level.FINE, "Failed to find {0} in cn. Trying sAMAccountName",
                                        groupname);
                                group = new LDAPSearchBuilder(context, domainDN).subTreeScope()
                                        .searchOne("(& (sAMAccountName={0})(objectCategory=group))", groupname);
                                if (group == null) {
                                    // Group not found in this domain, try next
                                    continue;
                                }
                            }
                            LOGGER.log(Level.FINE, "Found group {0} : {1}", new Object[] { groupname, group });
                            return new ActiveDirectoryGroupDetails(groupname);
                        } catch (NamingException e) {
                            LOGGER.log(Level.WARNING,
                                    String.format("Failed to retrieve user information for %s", groupname), e);
                            throw new BadCredentialsException(
                                    "Failed to retrieve user information for " + groupname, e);
                        } finally {
                            closeQuietly(context);
                        }
                    } catch (UsernameNotFoundException e) {
                        // everything worked OK but we just didn't find it. This could be just a typo in group name.
                        LOGGER.log(Level.WARNING, String.format("Failed to find the group %s in %s domain",
                                groupname, domain.getName()), e);
                    } catch (AuthenticationException e) {
                        // something went wrong talking to the server. This should be reported
                        LOGGER.log(Level.WARNING, String.format("Failed to find the group %s in %s domain",
                                groupname, domain.getName()), e);
                    } finally {
                        Thread.currentThread().setContextClassLoader(ccl);
                    }
                }
                LOGGER.log(Level.WARNING,
                        "Exhausted all configured domains and could not authenticate against any");
                throw new UserMayOrMayNotExistException(groupname);
            }
        });
    } catch (UncheckedExecutionException e) {
        Throwable t = e.getCause();
        if (t instanceof AuthenticationException) {
            AuthenticationException authenticationException = (AuthenticationException) t;
            throw authenticationException;
        } else {
            throw new CacheAuthenticationException(
                    "Authentication failed because there was a problem caching group " + groupname, e);
        }
    } catch (ExecutionException e) {
        LOGGER.log(Level.SEVERE, String.format("There was a problem caching group %s", groupname), e);
        throw new CacheAuthenticationException(
                "Authentication failed because there was a problem caching group " + groupname, e);
    }
}

From source file:org.alfresco.repo.security.authentication.ldap.LDAPInitialDirContextFactoryImpl.java

private InitialDirContext buildInitialDirContext(Hashtable<String, String> env, int pageSize,
        AuthenticationDiagnostic diagnostic) throws AuthenticationException {
    String securityPrincipal = env.get(Context.SECURITY_PRINCIPAL);
    String providerURL = env.get(Context.PROVIDER_URL);

    if (isSSLSocketFactoryRequired()) {
        KeyStore trustStore = initTrustStore();
        AlfrescoSSLSocketFactory.initTrustedSSLSocketFactory(trustStore);
        env.put("java.naming.ldap.factory.socket", AlfrescoSSLSocketFactory.class.getName());
    }/*from  w w w .ja v  a 2 s .co  m*/

    if (diagnostic == null) {
        diagnostic = new AuthenticationDiagnostic();
    }
    try {
        // If a page size has been requested, use LDAP v3 paging
        if (pageSize > 0) {
            InitialLdapContext ctx = new InitialLdapContext(env, null);
            ctx.setRequestControls(new Control[] { new PagedResultsControl(pageSize, Control.CRITICAL) });
            return ctx;
        } else {
            InitialDirContext ret = new InitialDirContext(env);
            Object[] args = { providerURL, securityPrincipal };
            diagnostic.addStep(AuthenticationDiagnostic.STEP_KEY_LDAP_CONNECTED, true, args);
            return ret;
        }
    } catch (javax.naming.AuthenticationException ax) {
        Object[] args1 = { securityPrincipal };
        Object[] args = { providerURL, securityPrincipal };
        diagnostic.addStep(AuthenticationDiagnostic.STEP_KEY_LDAP_CONNECTED, true, args);
        diagnostic.addStep(AuthenticationDiagnostic.STEP_KEY_LDAP_AUTHENTICATION, false, args1);

        // wrong user/password - if we get this far the connection is O.K
        Object[] args2 = { securityPrincipal, ax.getLocalizedMessage() };
        throw new AuthenticationException("authentication.err.authentication", diagnostic, args2, ax);
    } catch (CommunicationException ce) {
        Object[] args1 = { providerURL };
        diagnostic.addStep(AuthenticationDiagnostic.STEP_KEY_LDAP_CONNECTING, false, args1);

        StringBuffer message = new StringBuffer();

        message.append(ce.getClass().getName() + ", " + ce.getMessage());

        Throwable cause = ce.getCause();
        while (cause != null) {
            message.append(", ");
            message.append(cause.getClass().getName() + ", " + cause.getMessage());
            cause = cause.getCause();
        }

        // failed to connect
        Object[] args = { providerURL, message.toString() };
        throw new AuthenticationException("authentication.err.communication", diagnostic, args, cause);
    } catch (NamingException nx) {
        Object[] args = { providerURL };
        diagnostic.addStep(AuthenticationDiagnostic.STEP_KEY_LDAP_CONNECTING, false, args);

        StringBuffer message = new StringBuffer();

        message.append(nx.getClass().getName() + ", " + nx.getMessage());

        Throwable cause = nx.getCause();
        while (cause != null) {
            message.append(", ");
            message.append(cause.getClass().getName() + ", " + cause.getMessage());
            cause = cause.getCause();
        }

        // failed to connect
        Object[] args1 = { providerURL, message.toString() };
        throw new AuthenticationException("authentication.err.connection", diagnostic, args1, nx);
    } catch (IOException e) {
        Object[] args = { providerURL, securityPrincipal };
        diagnostic.addStep(AuthenticationDiagnostic.STEP_KEY_LDAP_CONNECTED, true, args);

        throw new AuthenticationException("Unable to encode LDAP v3 request controls", e);
    }
}