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:com.adito.activedirectory.ActiveDirectoryUserDatabase.java

private User getAccountFromDN(String dn, InitialLdapContext context) throws NamingException {
    String actualDN = null;/*from  www  .  j  a v a 2  s  .  c o m*/
    for (StringTokenizer tokens = new StringTokenizer(dn, ","); tokens.hasMoreTokens();) {
        String elm = tokens.nextToken().trim();
        if (elm.toUpperCase().startsWith("CN") || elm.toUpperCase().startsWith("OU")
                || elm.toUpperCase().startsWith("DC")) {
            actualDN = (actualDN == null ? "" : actualDN + ",") + elm;
        }
    }

    try {
        Attributes attributes = context.getAttributes(actualDN, USER_ATTRS);
        return populateActiveDirectoryUser(dn, attributes);
    } catch (Exception e) {
        logger.error("Cannot locate user for DN " + dn, e);
        throw new NamingException("User not found for DN " + dn);
    }
}

From source file:com.adito.activedirectory.ActiveDirectoryUserDatabase.java

private ActiveDirectoryUser populateActiveDirectoryUser(String dn, Attributes attributes)
        throws NamingException, UserDatabaseException {
    if (attributes == null) {
        throw new NamingException("No attributes for " + dn);
    }//www.  ja va  2  s  .  co  m

    String username = getAttributeValue(attributes, SAM_ACCOUNT_NAME_ATTRIBUTE);
    String userPrincipalName = getAttributeValue(attributes, USER_PRINCIPAL_NAME_ATTRIBUTE);
    String defaultDomain = getConfiguration().getDomain();
    String email = getAttributeValue(attributes, MAIL_ATTRIBUTE);
    String fullName = getAttributeValue(attributes, DISPLAY_NAME_ATTRIBUTE);
    Date lastPasswordChange = isPasswordChangeAllowed(attributes) ? getPasswordLastSetDate(attributes)
            : new Date();
    ActiveDirectoryUser user = new ActiveDirectoryUser(username, userPrincipalName, defaultDomain, email,
            fullName, dn, getEscapedDn(dn), lastPasswordChange, getRealm());

    String homeDirectory = getAttributeValue(attributes, User.USER_ATTR_HOME_DIRECTORY);
    if (homeDirectory.length() != 0) {
        Property.setProperty(new UserAttributeKey(user, User.USER_ATTR_HOME_DIRECTORY), homeDirectory, null);
    }

    String homeDrive = getAttributeValue(attributes, User.USER_ATTR_HOME_DRIVE);
    if (homeDrive.length() != 0) {
        Property.setProperty(new UserAttributeKey(user, User.USER_ATTR_HOME_DRIVE), homeDrive, null);
    }

    ActiveDirectoryGroup[] groups = getGroups(user, attributes);
    if (logger.isDebugEnabled()) {
        logger.debug("User belongs to " + groups.length + " groups");
    }
    user.setRoles(groups);
    return user;
}

From source file:ldap.SearchUtility.java

/**
 * This searches through the list of pre-defined structure elements until it finds one that matches
 * the passed type parameter, and then returns the 'depth' of that element.  We will search this level
 * of the tree (under the Config.SEARCH_BASE dn).
 * @param type//from  w  w w .j  av  a 2  s.  c  o  m
 * @return
 * @throws NamingException
 */
public int getStructureLevel(String type) throws NamingException {
    int level;
    for (level = 0; level < structureTypes.size(); level++)
        if (structureTypes.get(level).equals(type))
            break;

    if (level == structureTypes.size())
        throw new NamingException("Unknown type: '" + type
                + "' - has it been initialised in the constructor structure definition?");

    return level;
}

From source file:com.adito.ldap.LdapUserDatabase.java

/**
 * Return the certificat store in LDAP server for a user (if exist)
 * @param user/*w  w  w. j a v a2 s.c o m*/
 * @return X509Certificate of the user or null id this certificate doesn't exist
 */
public X509Certificate getCertificate(User user) {

    if (logger.isInfoEnabled()) {
        logger.info("Get Certificat for " + user.getPrincipalName());
    }

    LdapTemplate ldapTemplate = new LdapTemplate();
    ldapTemplate.setContextSource(ldapContextSource);

    AndFilter filterS = new AndFilter();
    String dn = ((LdapUser) user).getDn();
    int ind = dn.indexOf(baseDn);
    String rdn = dn.substring(0, ind - 1);
    filterS.and(new EqualsFilter(OBJECT_CLASS_ATTRIBUTE, USERS_CLASS));
    filterS.and(new LikeFilter(UID_ATTRIBUTE, user.getPrincipalName()));
    List certificats = ldapTemplate.search(rdn, filterS.encode(), new AttributesMapper() {
        public Object mapFromAttributes(Attributes attrs) throws NamingException {

            try {
                CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");

                return certificateFactory.generateCertificate(
                        new ByteArrayInputStream((byte[]) attrs.get(CERTIFICATE_ATTRIBUTE).get()));

            } catch (Exception e) {

                throw new NamingException(e.toString());
            }

        }
    });

    if (certificats.size() == 0) {
        return null;
    } else {
        X509Certificate cert = (X509Certificate) certificats.get(0);
        return cert;
    }

}

From source file:net.testdriven.psiprobe.beans.JBossResourceResolverBean.java

public boolean resetResource(Context context, String resourceName) throws NamingException {
    try {// w  ww .j av a2  s  .c om
        ObjectName poolOName = new ObjectName("jboss.jca:service=ManagedConnectionPool,name=" + resourceName);
        MBeanServer server = getMBeanServer();
        if (server != null) {
            try {
                server.invoke(poolOName, "stop", null, null);
                server.invoke(poolOName, "start", null, null);
                return true;
            } catch (Exception e) {
                logger.error("Could not reset resource \"" + resourceName + "\"", e);
            }
        }
        return false;
    } catch (MalformedObjectNameException e) {
        throw new NamingException("Resource name: \"" + resourceName + "\" makes a malformed ObjectName");
    }
}

From source file:nl.nn.adapterframework.jms.JMSFacade.java

public Destination getDestination() throws NamingException, JMSException, JmsException {
    if (destination == null) {
        String destinationName = getDestinationName();
        if (StringUtils.isEmpty(destinationName)) {
            throw new NamingException("no destinationName specified");
        }/*w  w w  .  ja  va 2 s. c  o  m*/
        if (isLookupDestination()) {
            if (!useTopicFunctions || getPersistent()) {
                destination = getDestination(destinationName);
            } else {
                TopicSession session = null;
                try {
                    session = (TopicSession) createSession();
                    destination = session.createTopic(destinationName);
                } finally {
                    closeSession(session);
                }
            }
        } else {
            destination = getJmsMessagingSource().createDestination(destinationName);
        }
        if (destination == null) {
            throw new NamingException("cannot get Destination from [" + destinationName + "]");
        }
    }
    return destination;
}

From source file:nl.nn.adapterframework.jms.JNDIBase.java

protected Hashtable getJndiEnv() throws NamingException {
    Properties jndiEnv = new Properties();

    if (StringUtils.isNotEmpty(getJndiProperties())) {
        URL url = ClassUtils.getResourceURL(classLoader, getJndiProperties());
        if (url == null) {
            throw new NamingException("cannot find jndiProperties from [" + getJndiProperties() + "]");
        }/*from  w w w  .j  a va2s  .c  o m*/
        try {
            jndiEnv.load(url.openStream());
        } catch (IOException e) {
            throw new NamingException("cannot load jndiProperties [" + getJndiProperties() + "] from url ["
                    + url.toString() + "]");
        }
    }
    if (getInitialContextFactoryName() != null)
        jndiEnv.put(Context.INITIAL_CONTEXT_FACTORY, getInitialContextFactoryName());
    if (getProviderURL() != null)
        jndiEnv.put(Context.PROVIDER_URL, getProviderURL());
    if (getAuthentication() != null)
        jndiEnv.put(Context.SECURITY_AUTHENTICATION, getAuthentication());
    if (getPrincipal() != null || getCredentials() != null || getJndiAuthAlias() != null) {
        CredentialFactory jndiCf = new CredentialFactory(getJndiAuthAlias(), getPrincipal(), getCredentials());
        if (StringUtils.isNotEmpty(jndiCf.getUsername()))
            jndiEnv.put(Context.SECURITY_PRINCIPAL, jndiCf.getUsername());
        if (StringUtils.isNotEmpty(jndiCf.getPassword()))
            jndiEnv.put(Context.SECURITY_CREDENTIALS, jndiCf.getPassword());
    }
    if (getUrlPkgPrefixes() != null)
        jndiEnv.put(Context.URL_PKG_PREFIXES, getUrlPkgPrefixes());
    if (getSecurityProtocol() != null)
        jndiEnv.put(Context.SECURITY_PROTOCOL, getSecurityProtocol());

    if (log.isDebugEnabled()) {
        for (Iterator it = jndiEnv.keySet().iterator(); it.hasNext();) {
            String key = (String) it.next();
            String value = jndiEnv.getProperty(key);
            log.debug("jndiEnv [" + key + "] = [" + value + "]");
        }
    }
    return jndiEnv;
}

From source file:org.adeptnet.auth.kerberos.Krb5.java

private static String recurseResolveToA(final Nameserver ns, final Set<String> checked, final String host)
        throws NamingException {
    if (checked.contains(host)) {
        throw new NamingException(String.format("Recursive Name Lookup: %s", checked));
    }/*from w w w .  ja va  2s  . c om*/
    final String[] clookup = ns.lookup(host, "cname");
    if (clookup.length != 0) {
        checked.add(host);
        return recurseResolveToA(ns, checked, normalize(clookup[0]));
    }

    return host;
}

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

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 {//  ww w  .  j  av a 2s.c om
        throw new NamingException("No user found for username " + username);
    }
}