Example usage for javax.naming.directory Attributes getAll

List of usage examples for javax.naming.directory Attributes getAll

Introduction

In this page you can find the example usage for javax.naming.directory Attributes getAll.

Prototype

NamingEnumeration<? extends Attribute> getAll();

Source Link

Document

Retrieves an enumeration of the attributes in the attribute set.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "ldap://localhost/o=JNDITutorial";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);

    DirContext ctx = new InitialDirContext(env);
    String[] attrIDs = { "sn", "number", "value", "mail" };

    Attributes answer = ctx.getAttributes("cn=yourName, ou=People", attrIDs);

    NamingEnumeration e = answer.getAll();
    while (e.hasMore()) {
        Attribute attr = (Attribute) e.next();
        System.out.println(attr.getID());
    }/*from   w w w.  j  a  v a 2s .  co  m*/
}

From source file:AttributeExample.java

public static void main(String[] argc) throws Exception {
    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");

    env.put(Context.PROVIDER_URL, "ldap://MyHost/o=JNDIExample");
    DirContext dctx = new InitialDirContext(env);
    Attributes attrs = new BasicAttributes(true);
    attrs.put(new BasicAttribute("email"));
    attrs.put(new BasicAttribute("website", "www.pri.com"));

    NamingEnumeration result = dctx.search("ou=People", attrs);

    while (result.hasMore()) {
        SearchResult sr = (SearchResult) result.next();
        System.out.println("Result = " + sr.getName());
        Attributes srchAttrs = sr.getAttributes();

        NamingEnumeration attributes = srchAttrs.getAll();

        while (attributes.hasMore()) {
            Attribute attr = (Attribute) attributes.next();
            System.out.println("Attribute: " + attr.getID());
            NamingEnumeration values = attr.getAll();
            while (values.hasMore()) {
                Object value = values.next();
                System.out.println("Value = " + value);
            }/*from   w w  w  .  j a  v a2  s . co  m*/
        }
    }
}

From source file:org.apache.cxf.sts.claims.LdapUtils.java

public static List<String> getAttributeOfEntries(LdapTemplate ldapTemplate, String baseDN, String objectClass,
        String filterAttributeName, String filterAttributeValue, String searchAttribute) {

    List<String> ldapAttributes = null;

    AttributesMapper mapper = new AttributesMapper() {
        public Object mapFromAttributes(Attributes attrs) throws NamingException {
            NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
            while (attrEnum.hasMore()) {
                return (String) attrEnum.next().get();
            }/*  w ww.j av  a 2  s. co m*/
            return null;
        }
    };

    String[] searchAttributes = new String[] { searchAttribute };

    List<?> result = null;
    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("objectclass", objectClass))
            .and(new EqualsFilter(filterAttributeName, filterAttributeValue));

    result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(),
            SearchControls.SUBTREE_SCOPE, searchAttributes, mapper);
    if (result != null && result.size() > 0) {
        ldapAttributes = CastUtils.cast((List<?>) result);
    }

    return ldapAttributes;
}

From source file:ModAttrs.java

static void printAttrs(Attributes attrs) {
    if (attrs == null) {
        System.out.println("No attributes");
    } else {//  www .ja  v  a 2s  .  c  om
        /* Print each attribute */
        try {
            for (NamingEnumeration ae = attrs.getAll(); ae.hasMore();) {
                Attribute attr = (Attribute) ae.next();
                System.out.println("attribute: " + attr.getID());

                /* print each value */
                for (NamingEnumeration e = attr.getAll(); e.hasMore(); System.out.println("value: " + e.next()))
                    ;
            }
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

From source file:org.apache.cxf.sts.claims.LdapUtils.java

public static Map<String, Attribute> getAttributesOfEntry(LdapTemplate ldapTemplate, String baseDN,
        String objectClass, String filterAttributeName, String filterAttributeValue,
        String[] searchAttributes) {

    Map<String, Attribute> ldapAttributes = null;

    AttributesMapper mapper = new AttributesMapper() {
        public Object mapFromAttributes(Attributes attrs) throws NamingException {
            Map<String, Attribute> map = new HashMap<String, Attribute>();
            NamingEnumeration<? extends Attribute> attrEnum = attrs.getAll();
            while (attrEnum.hasMore()) {
                Attribute att = attrEnum.next();
                map.put(att.getID(), att);
            }//w  w w  .  j  a  v  a2  s . c  om
            return map;
        }
    };

    List<?> result = null;
    AndFilter filter = new AndFilter();
    filter.and(new EqualsFilter("objectclass", objectClass))
            .and(new EqualsFilter(filterAttributeName, filterAttributeValue));

    result = ldapTemplate.search((baseDN == null) ? "" : baseDN, filter.toString(),
            SearchControls.SUBTREE_SCOPE, searchAttributes, mapper);
    if (result != null && result.size() > 0) {
        ldapAttributes = CastUtils.cast((Map<?, ?>) result.get(0));
    }

    return ldapAttributes;
}

From source file:ldap.UserAccountImpl.java

public UserAccountImpl(Attributes atts) throws NamingException {
    NamingEnumeration attList = atts.getAll();
    while (attList.hasMore()) {
        Attribute att = (Attribute) attList.next();
        put(att);/*w  ww .  j  a  v a2  s  .  co  m*/
    }
}

From source file:cyrille.jndi.LdapTest.java

@Test
public void test() throws Exception {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "uid=admin,ou=system");
    env.put(Context.SECURITY_CREDENTIALS, "secret");
    DirContext dirContext = new InitialDirContext(env);

    Attributes attributes = dirContext.getAttributes("uid=aeinstein,ou=Users,dc=example,dc=com");
    for (NamingEnumeration<Attribute> attributesEnumeration = (NamingEnumeration<Attribute>) attributes
            .getAll(); attributesEnumeration.hasMore();) {
        Attribute attribute = attributesEnumeration.next();
        System.out.print(attribute.getID() + "=");

        for (NamingEnumeration<?> attributeValues = attribute.getAll(); attributeValues.hasMore();) {
            Object value = attributeValues.next();
            if (value instanceof byte[] && "userpassword".equals(attribute.getID())) {
                byte[] bytes = (byte[]) value;
                System.out.print(new String(bytes) + ", ");
            } else {
                System.out.print(value + ", ");
            }//  ww  w  .  ja  v  a2  s .c o  m
        }
        System.out.println();
    }
}

From source file:com.swdouglass.joid.server.DirectoryUserManagerImpl.java

@Override
public User getUser(String username) {
    User user = null;/*from w  ww .j a  va  2  s.  co  m*/
    try {
        Attributes attrs = findAttributes(username, initialCtx);
        if (attrs != null) {
            if (log.isDebugEnabled()) {
                NamingEnumeration ne = attrs.getAll();
                while (ne.hasMore()) {
                    log.debug(ne.next());
                }
            }
            // create the user, password very likely to be in binary form...
            user = new User(username, DirectoryUtil.getAttributeValue(attrs, PASSWORD_ATTRIBUTE_PROP,
                    PASSWORD_ATTRIBUTE_PROP_DEFAULT));

            // set the list of OpenIDs
            Attribute openIDattr = attrs
                    .get(DirectoryUtil.getProperty(OPENID_OBJECTCLASS_PROP, OPENID_OBJECTCLASS_PROP_DEFAULT));
            Enumeration e = openIDattr.getAll();
            Set<String> openIDs = new HashSet<String>();
            while (e.hasMoreElements()) {
                openIDs.add((String) e.nextElement());
            }
            user.setOpenIDs(openIDs);
        }
    } catch (NamingException ex) {
        log.warn("Error in finding the userame=" + username, ex);
    }
    return user;
}

From source file:ldap.Entry.java

/**
 *
 * @param entryName the unique distinguished name of the entry.  may be null.
 * @param entryAtts the attributes to add to this entry... may be null.
 *//*from ww  w.j av a 2s . c o  m*/
public Entry(LdapName entryName, Attributes entryAtts) {
    super(true);

    name = entryName;

    if (entryAtts != null) {
        Enumeration newAtts = entryAtts.getAll();
        while (newAtts.hasMoreElements())
            this.put((Attribute) newAtts.nextElement());
    }
}

From source file:com.dianping.cat.system.page.login.service.SessionManager.java

public SessionManager() {
    super();//from  w w  w. ja  va  2s  . c om
    AuthType type = AuthType.valueOf(CatPropertyProvider.INST.getProperty("CAT_AUTH_TYPE", "ADMIN_PWD"));
    switch (type) {
    case NOP:
        tokenCreator = new Function<Credential, Token>() {
            @Override
            public Token apply(Credential credential) {
                String account = credential.getAccount();
                return new Token(account, account);
            }
        };
        break;
    case LDAP:
        final String ldapUrl = CatPropertyProvider.INST.getProperty("CAT_LDAP_URL", null);
        if (StringUtils.isBlank(ldapUrl)) {
            throw new IllegalArgumentException("required CAT_LDAP_URL");
        }
        final String userDnTpl = CatPropertyProvider.INST.getProperty("CAT_LDAP_USER_DN_TPL", null);
        if (StringUtils.isBlank(userDnTpl)) {
            throw new IllegalArgumentException("required CAT_LDAP_USER_DN_TPL");
        }
        final String userDisplayAttr = CatPropertyProvider.INST.getProperty("CAT_LDAP_USER_DISPLAY_ATTR", null);
        final Pattern pattern = Pattern.compile("\\{0}");
        final Matcher userDnTplMatcher = pattern.matcher(userDnTpl);
        final String[] attrs = userDisplayAttr == null ? null : new String[] { userDisplayAttr };
        tokenCreator = new Function<Credential, Token>() {
            @Override
            public Token apply(Credential credential) {
                final String account = credential.getAccount();
                final String pwd = credential.getPassword();
                if (StringUtils.isEmpty(account) || StringUtils.isEmpty(pwd)) {
                    return null;
                }
                Hashtable<String, String> env = new Hashtable<String, String>();
                env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
                env.put(Context.PROVIDER_URL, ldapUrl);// LDAP server
                String userDn = userDnTplMatcher.replaceAll(account);
                env.put(Context.SECURITY_PRINCIPAL, pwd);
                env.put(Context.SECURITY_CREDENTIALS, pwd);
                try {
                    InitialLdapContext context = new InitialLdapContext(env, null);
                    final String baseDn = context.getNameInNamespace();
                    if (userDn.endsWith(baseDn)) {
                        userDn = userDn.substring(0, userDn.length() - baseDn.length() - 1);
                    }
                    String displayName = null;
                    if (attrs != null) {
                        final Attributes attributes = context.getAttributes(userDn, attrs);
                        if (attributes.size() > 0) {
                            displayName = attributes.getAll().next().get().toString();
                        }
                    }

                    return new Token(account, displayName == null ? account : displayName);
                } catch (Exception e) {
                    Cat.logError(e);
                    return null;
                }
            }

        };
        break;
    case ADMIN_PWD:
        final String p = CatPropertyProvider.INST.getProperty("CAT_ADMIN_PWD", "admin");

        tokenCreator = new Function<Credential, Token>() {
            @Override
            public Token apply(Credential credential) {
                String account = credential.getAccount();

                if ("admin".equals(account) && p.equals(credential.getPassword())) {
                    return new Token(account, account);
                }
                return null;
            }

        };
        break;
    }
}