List of usage examples for javax.naming.directory DirContext getAttributes
public Attributes getAttributes(String name) throws NamingException;
From source file:UsePool.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); // Enable connection pooling env.put("com.sun.jndi.ldap.connect.pool", "true"); try {//from w w w .ja va2s .com // Create one initial context (Get connection from pool) DirContext ctx = new InitialDirContext(env); System.out.println(ctx.getAttributes("ou=NewHires")); // do something useful with ctx // Close the context when we're done ctx.close(); // Return connection to pool // Create another initial context (Get connection from pool) DirContext ctx2 = new InitialDirContext(env); System.out.println(ctx2.getAttributes("ou=People")); // do something useful with ctx2 // Close the context when we're done ctx2.close(); // Return connection to pool } catch (NamingException e) { e.printStackTrace(); } }
From source file:GetAllAttrs.java
public static void main(String[] args) { // Set up the environment for creating the initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/*from ww w .j a v a 2 s . co m*/ // Create the initial context DirContext ctx = new InitialDirContext(env); // Get all the attributes of named object Attributes answer = ctx.getAttributes("cn=Ted Geisel, ou=People"); // Print the answer printAttrs(answer); // Close the context when we're done ctx.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:RenameKeepRDN.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); // Set property to keep RDN env.put("java.naming.ldap.deleteRDN", "false"); try {//from w ww. ja va 2 s. c o m // Create initial context DirContext ctx = new InitialDirContext(env); // Perform rename ctx.rename("cn=C. User, ou=NewHires", "cn=Claude User,ou=NewHires"); // Check that it worked System.out.println(ctx.getAttributes("cn=Claude User,ou=NewHires")); // Revert change // Make sure new name doesn't get converted into attribute ctx.removeFromEnvironment("java.naming.ldap.deleteRDN"); ctx.rename("cn=Claude User, ou=NewHires", "cn=C. User,ou=NewHires"); // Check that we are back at our original setup System.out.println(ctx.getAttributes("cn=C. User,ou=NewHires")); // Close the context when we're done ctx.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:NewConn.java
public static void main(String[] args) { // Set up environment for creating initial context Hashtable<String, Object> env = new Hashtable<String, Object>(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial"); try {/*ww w . j a v a 2 s.c o m*/ // Create initial context (first connection) DirContext ctx = new InitialDirContext(env); // Get a copy of the same context DirContext ctx2 = (DirContext) ctx.lookup(""); // Change authentication properties in ctx2 ctx2.addToEnvironment(Context.SECURITY_PRINCIPAL, "cn=C. User, ou=NewHires, o=JNDITutorial"); ctx2.addToEnvironment(Context.SECURITY_CREDENTIALS, "mysecret"); // Method on ctx2 will use new connection System.out.println(ctx2.getAttributes("ou=NewHires")); // Close the contexts when we're done ctx.close(); ctx2.close(); } catch (NamingException e) { e.printStackTrace(); } }
From source file:eu.europa.ec.markt.dss.validation.crl.OnlineCRLSource.java
/** * Downloads a CRL from given LDAP url, e.g. ldap://ldap.infonotary.com/dc=identity-ca,dc=infonotary,dc=com * * @throws CertificateException/* www . j a va 2s. c o m*/ * @throws CRLException */ private static X509CRL downloadCRLFromLDAP_(final String ldapURL) throws DSSException { final 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); try { final DirContext ctx = new InitialDirContext(env); final Attributes attributes = ctx.getAttributes(""); final javax.naming.directory.Attribute attribute = attributes.get("certificateRevocationList;binary"); final byte[] val = (byte[]) attribute.get(); if (val == null || val.length == 0) { throw new DSSException("Can not download CRL from: " + ldapURL); } final InputStream inStream = new ByteArrayInputStream(val); return DSSUtils.loadCRL(inStream); } catch (Exception e) { LOG.warning(e.getMessage()); e.printStackTrace(); } return null; }
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 + ", "); }/*from w w w. j a v a 2s . c o m*/ } System.out.println(); } }
From source file:org.archone.ad.authentication.ShoadRealm.java
@Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { UsernamePasswordToken upToken = (UsernamePasswordToken) token; String username = upToken.getUsername(); Assert.notNull(username, "Null usernames are not allowed by this realm."); String password = new String(upToken.getPassword()); Assert.hasLength(password, "Empty passwords are not allowed by this realm."); DirContext ctx = null; try {/*from w ww. ja v a 2 s . c om*/ String userDn = getUserDn(username); ctx = contextSource.getContext(userDn, password); Attributes attrs = ctx.getAttributes(userDn); DirContextAdapter result = new DirContextAdapter(attrs, new DistinguishedName(userDn)); return new SimpleAuthenticationInfo(result, password.toCharArray(), getName()); } catch (javax.naming.NamingException ex) { throw new AuthenticationException(); } catch (NamingException ex) { throw new AuthenticationException(); } }
From source file:alpine.auth.LdapConnectionWrapper.java
/** * Retrieves an attribute by its name for the specified dn. * @param ctx the DirContext to use//from www. j a v a 2 s . c om * @param dn the distinguished name of the entry to obtain the attribute value for * @param attributeName the name of the attribute to return * @return the value of the attribute, or null if not found * @throws NamingException if an exception is thrown * @since 1.4.0 */ public String getAttribute(DirContext ctx, String dn, String attributeName) throws NamingException { final Attributes attributes = ctx.getAttributes(dn); return getAttribute(attributes, attributeName); }
From source file:org.keycloak.testsuite.federation.kerberos.AbstractKerberosTest.java
protected String invokeLdap(GSSCredential gssCredential, String username) throws NamingException { Hashtable env = new Hashtable(11); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://localhost:10389"); if (gssCredential != null) { env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI"); env.put(Sasl.CREDENTIALS, gssCredential); }/*from w w w . j a va 2 s. c o m*/ DirContext ctx = new InitialDirContext(env); try { Attributes attrs = ctx.getAttributes("uid=" + username + ",ou=People,dc=keycloak,dc=org"); String cn = (String) attrs.get("cn").get(); String sn = (String) attrs.get("sn").get(); return cn + " " + sn; } finally { ctx.close(); } }
From source file:com.evolveum.midpoint.pwdfilter.opendj.PasswordPusher.java
private void readConfig() throws InitializationException { String configFile = "/opt/midpoint/opendj-pwdpusher.xml"; if (System.getProperty("config") != null) { configFile = System.getProperty("config"); }// w w w. ja va2s.co m File f = new File(configFile); if (!f.exists() || !f.canRead()) { throw new IllegalArgumentException("Config file " + configFile + " does not exist or is not readable"); } try { XMLConfiguration config = new XMLConfiguration(f); String notifierDN = "cn=" + config.getString("passwordpusher.statusNotifierName") + ",cn=Account Status Notification Handlers"; String ldapURL = config.getString("passwordpusher.ldapServerURL"); boolean ldapSSL = config.getBoolean("passwordpusher.ldapServerSSL"); String ldapUsername = config.getString("passwordpusher.ldapServerUsername"); String ldapPassword = config.getString("passwordpusher.ldapServerPassword"); Hashtable<Object, Object> env = new Hashtable<Object, Object>(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, ldapURL + "/cn=config"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, ldapUsername); env.put(Context.SECURITY_CREDENTIALS, ldapPassword); if (ldapSSL) { env.put(Context.SECURITY_PROTOCOL, "ssl"); } try { DirContext context = new InitialDirContext(env); Attributes attr = context.getAttributes(notifierDN); this.endPoint = attr.get("ds-cfg-referrals-url").get(0).toString(); this.username = attr.get("ds-cfg-midpoint-username").get(0).toString(); this.password = attr.get("ds-cfg-midpoint-password").get(0).toString(); this.pwdChangeDirectory = attr.get("ds-cfg-midpoint-passwordcachedir").get(0).toString(); } catch (NamingException ne) { throw new InitializationException( ERR_MIDPOINT_PWDSYNC_READING_CONFIG_FROM_LDAP.get(ne.getMessage()), ne); } } catch (ConfigurationException ce) { throw new InitializationException(ERR_MIDPOINT_PWDSYNC_PARSING_XML_CONFIG.get(ce.getMessage()), ce); } }