List of usage examples for javax.naming.directory Attributes get
Attribute get(String attrID);
From source file:LdapSearch.java
public static void main(String[] args) throws Exception { Hashtable env = new Hashtable(); String sp = "com.sun.jndi.ldap.LdapCtxFactory"; env.put(Context.INITIAL_CONTEXT_FACTORY, sp); String ldapUrl = "ldap://localhost:389/dc=yourName, dc=com"; env.put(Context.PROVIDER_URL, ldapUrl); DirContext dctx = new InitialDirContext(env); String base = "ou=People"; SearchControls sc = new SearchControls(); String[] attributeFilter = { "cn", "mail" }; sc.setReturningAttributes(attributeFilter); sc.setSearchScope(SearchControls.SUBTREE_SCOPE); String filter = "(&(sn=W*)(l=Criteria*))"; NamingEnumeration results = dctx.search(base, filter, sc); while (results.hasMore()) { SearchResult sr = (SearchResult) results.next(); Attributes attrs = sr.getAttributes(); Attribute attr = attrs.get("cn"); System.out.print(attr.get() + ": "); attr = attrs.get("mail"); System.out.println(attr.get()); }/*w w w . j a v a 2 s . c o m*/ dctx.close(); }
From source file:Main.java
public static void main(String[] argv) 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, "yourURL"); DirContext ctx = new InitialDirContext(env); // Get an attribute of that type Attributes attrs = ctx.getAttributes("cn=YourName, ou=People", new String[] { "cn" }); Attribute cnAttr = attrs.get("cn"); // Get its attribute definition DirContext cnSchema = cnAttr.getAttributeDefinition(); // Get cnSchema's attributes Attributes cnAttrs = cnSchema.getAttributes(""); }
From source file:Main.java
public static void main(String args[]) throws Exception { Hashtable<String, String> env = new Hashtable<String, String>(); env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory"); env.put("java.naming.provider.url", "dns://123.123.123.123/"); String dns_attributes[] = { "MX", "A", "HINFO" }; DirContext ctx = new InitialDirContext(env); Attributes attrsl = ctx.getAttributes("http://www.yourserver.com", dns_attributes); for (int z = 0; z < dns_attributes.length; z++) { Attribute attr = attrsl.get(dns_attributes[z]); if (attr != null) { System.out.print(dns_attributes[z] + ": "); for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) { System.out.println(vals.nextElement()); }// w w w. jav a 2 s . com } } }
From source file:Main.java
public static void main(String args[]) throws Exception { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, INITCTX); env.put(Context.PROVIDER_URL, MY_HOST); DirContext ctx = new InitialDirContext(env); SearchControls constraints = new SearchControls(); constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); NamingEnumeration results = ctx.search(MY_SEARCHBASE, MY_FILTER, constraints); while (results != null && results.hasMore()) { SearchResult sr = (SearchResult) results.next(); String dn = sr.getName() + ", " + MY_SEARCHBASE; System.out.println("Distinguished Name is " + dn); Attributes ar = ctx.getAttributes(dn, MY_ATTRS); if (ar == null) { System.out.println("Entry " + dn + " has none of the specified attributes\n"); return; }//from w w w . ja v a 2 s. c om for (int i = 0; i < MY_ATTRS.length; i++) { Attribute attr = ar.get(MY_ATTRS[i]); if (attr == null) { continue; } System.out.println(MY_ATTRS[i] + ":"); for (Enumeration vals = attr.getAll(); vals.hasMoreElements();) { System.out.println("\t" + vals.nextElement()); } } } }
From source file:pl.umk.mat.zawodyweb.ldap.LdapConnector.java
/** * Check user password and return that user * * Example of LDAP data:/*from w ww . ja v a 2 s. co m*/ * <pre> * dn: uid=faramir,ou=People,ou=int,dc=mat,dc=uni,dc=torun,dc=pl * objectClass: top * objectClass: account * objectClass: posixAccount * objectClass: shadowAccount * objectClass: radiusprofile * objectClass: sambaSamAccount * dialupAccess: yes * uid: faramir * cn: Marek Nowicki * loginShell: /bin/tcsh * uidNumber: 30030 * sambaSID: S-1-30030 * gecos: Marek Nowicki, doktorant Info. * gidNumber: 160 * homeDirectory: /studdok/faramir * radiusSimultaneousUse: 1</pre> * @param login login * @param pass user password * @return Users if user found and password is OK or null if anything failed */ public static Users retieveUser(String login, String pass) { if (pass == null || pass.isEmpty() || login == null || login.isEmpty() || login.contains(",")) { return null; } Hashtable<String, String> ldapEnv = new Hashtable<String, String>(11); String dn = String.format("uid=%s,%s", login, baseDN); ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); ldapEnv.put(Context.PROVIDER_URL, ldapURL); ldapEnv.put(Context.SECURITY_PRINCIPAL, dn); ldapEnv.put(Context.SECURITY_CREDENTIALS, pass); try { DirContext authContext = new InitialDirContext(ldapEnv); Attributes userAttributes = authContext.getAttributes(dn); if (userAttributes.get("uidNumber") == null) { return null; } Attribute cn = userAttributes.get("cn"); // commonName - eg. Marek Nowicki String name = ((String) cn.get()); String firstName = name; String lastName = "(LDAP)"; int index = name.lastIndexOf(" "); if (index > 0) { firstName = name.substring(0, index).trim(); lastName = name.substring(index + 1).trim(); } Users user = new Users(); user.setLogin(login); user.setFirstname(firstName); user.setLastname(lastName); user.setEmail(login + emailSuffix); return user; } catch (AuthenticationException ex) { } catch (NamingException ex) { } catch (NullPointerException ex) { } catch (ClassCastException ex) { } catch (Exception ex) { log.fatal("LDAP Exception:", ex); } return null; }
From source file:org.wso2.carbon.appfactory.userstore.internal.OTLDAPUtil.java
public static String getEmailFromUserId(String uid, LDAPConnectionContext connectionSource, String userSearchBase) throws UserStoreException { // check from cache String email = otEmailCache.getValueFromCache(uid); if (email != null && !email.isEmpty()) { return email; }/* w w w . j ava 2s . c om*/ // check from ldap and update the cache StringBuffer buff = new StringBuffer(); buff.append("(&(objectClass=inetOrgPerson)(uid=").append(uid).append("))"); if (log.isDebugEnabled()) { log.debug("Searching for " + buff.toString()); } DirContext dirContext = connectionSource.getContext(); NamingEnumeration<SearchResult> answer = null; try { String[] returnedAttributes = { "mail" }; answer = searchForUser(buff.toString(), returnedAttributes, dirContext, userSearchBase); int count = 0; SearchResult userObj = null; while (answer.hasMoreElements()) { SearchResult sr = (SearchResult) answer.next(); if (count > 0) { log.error("More than one user exist for the same name"); } count++; userObj = sr; } if (userObj != null) { Attributes attributes = userObj.getAttributes(); Attribute mailAttribute = attributes.get("mail"); if (mailAttribute != null) { email = mailAttribute.getID(); } } otEmailCache.addToCache(uid, email); return email; } catch (Exception e) { log.error(e.getMessage(), e); throw new UserStoreException(e.getMessage(), e); } finally { JNDIUtil.closeNamingEnumeration(answer); JNDIUtil.closeContext(dirContext); } }
From source file:org.trypticon.xmpp.util.SrvLookup.java
/** * Resolves a domain name to a host and port, by looking up the SRV record. * * @param domain the domain to look up. * @param service the service to look up. * @param protocol the protocol to look up. * @return the address, or <code>null</code> if it couldn't be resolved. *///from w w w. j a va 2 s .c om private static InetSocketAddress resolveSRV(String domain, String service, String protocol) { if (context == null) { return null; } try { Attributes dnsLookup = context.getAttributes("_" + service + "._" + protocol + "." + domain); Attribute srvAttribute = dnsLookup.get("SRV"); // The attribute is null if there was no record in DNS. if (srvAttribute == null) { return null; } String srvRecord = (String) srvAttribute.get(); // The attribute value is null if there was somehow a record with a null value. if (srvRecord == null) { return null; } String[] srvRecordEntries = srvRecord.split(" "); String host = srvRecordEntries[srvRecordEntries.length - 1]; int port = Integer.parseInt(srvRecordEntries[srvRecordEntries.length - 2]); return new InetSocketAddress(host, port); } catch (NamingException e) { log.warn("Problem looking up SRV record for domain " + domain + ", service " + service + ", protocol " + protocol, e); return null; } }
From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java
public static String getStringAttribute(Attributes attrs, String name) { Attribute attr = attrs.get(name); if (attr == null) return null; String s = attr.toString();//w w w . j a v a 2s.c om if (StringUtils.isEmpty(s)) return s; int i = s.indexOf(':'); if (i == -1) throw new IllegalArgumentException(StringUtils.fastConcat("Wrong returned value: ", s)); return s.substring(i + 1); }
From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java
public static String getObjectSID(Attributes attrs) throws NamingException { Attribute attr = attrs.get("objectsid"); if (attr == null) throw new NamingException("No ObjectSID attribute"); Object attrObject = attr.get(); if (attrObject == null) throw new NamingException("ObjectSID is empty"); if (attrObject instanceof String) { String attrString = (String) attrObject; if (attrString.startsWith("S-")) return attrString; return decodeSID(attrString.getBytes()); } else if (attrObject instanceof byte[]) { return decodeSID((byte[]) attrObject); } else/*from w w w.j a va 2 s . c o m*/ throw new NamingException("Unknown attribute type: " + attrObject.getClass().getName()); }
From source file:com.jaeksoft.searchlib.util.ActiveDirectory.java
public static void collectMemberOf(Attributes attrs, Collection<ADGroup> groups) throws NamingException { Attribute tga = attrs.get("memberOf"); if (tga == null) return;//w ww . ja v a 2 s . c o m NamingEnumeration<?> membersOf = tga.getAll(); while (membersOf.hasMore()) { Object memberObject = membersOf.next(); groups.add(new ADGroup(memberObject.toString())); } membersOf.close(); }