public static void main(String[] args) { // set up environment to access the server Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://" + ldapServerName + "/" + rootContext); env.put(Context.SECURITY_PRINCIPAL, rootdn); env.put(Context.SECURITY_CREDENTIALS, rootpass); try { // obtain initial directory context using the environment DirContext ctx = new InitialDirContext(env); // create some random number to add to the directory Integer i = new Integer(28420); System.out.println("Adding " + i + " to directory..."); ctx.bind("cn=myRandomInt", i); i = new Integer(98765); System.out.println("i is now: " + i); i = (Integer) ctx.lookup("cn=myRandomInt"); System.out.println("Retrieved i from directory with value: " + i); } catch (NameAlreadyBoundException nabe) { System.err.println("value has already been bound!"); } catch (Exception e) { System.err.println(e); } }
/** * Connect to the DirContext, and retrieve the bound object, as well as its attributes. If no * object is bound with the name specified in the URL, then an IOException is thrown. * * @throws IOException Object not found */ @Override public void connect() throws IOException { if (!connected) { try { date = System.currentTimeMillis(); String path = URL_DECODER.convert(getURL().getFile(), false); if (context instanceof ProxyDirContext) { ProxyDirContext proxyDirContext = (ProxyDirContext) context; String hostName = proxyDirContext.getHostName(); String contextPath = proxyDirContext.getContextPath(); if (hostName != null) { if (!path.startsWith("/" + hostName + "/")) return; path = path.substring(hostName.length() + 1); } if (contextPath != null) { if (!path.startsWith(contextPath + "/")) { return; } path = path.substring(contextPath.length()); } } object = context.lookup(path); attributes = context.getAttributes(path); if (object instanceof Resource) resource = (Resource) object; if (object instanceof DirContext) collection = (DirContext) object; } catch (NamingException e) { // Object not found } connected = true; } }
/** * Return the URL to the resource that is mapped to a specified path. The path must begin with a * "/" and is interpreted as relative to the current context root. * * @param path The path to the desired resource * @exception MalformedURLException if the path is not given in the correct form */ public URL getResource(String path) throws MalformedURLException { DirContext resources = context.getResources(); if (resources != null) { String fullPath = context.getName() + path; // this is the problem. Host must not be null String hostName = context.getParent().getName(); try { resources.lookup(path); if (System.getSecurityManager() != null) { try { PrivilegedGetResource dp = new PrivilegedGetResource(hostName, fullPath, resources); return (URL) AccessController.doPrivileged(dp); } catch (PrivilegedActionException pe) { throw pe.getException(); } } else { return new URL( "jndi", null, 0, getJNDIUri(hostName, fullPath), new DirContextURLStreamHandler(resources)); } } catch (Exception e) { // e.printStackTrace(); } } return (null); }
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"); // Authenticate as S. User and password "mysecret" env.put(Context.SECURITY_AUTHENTICATION, "custom"); env.put(Context.SECURITY_PRINCIPAL, "cn=S. User, ou=NewHires, o=JNDITutorial"); env.put(Context.SECURITY_CREDENTIALS, "mysecret"); try { // Create initial context DirContext ctx = new InitialDirContext(env); System.out.println(ctx.lookup("ou=NewHires")); // do something useful with ctx // Close the context when we're done ctx.close(); } catch (NamingException e) { e.printStackTrace(); } }
/** * Return the requested resource as an <code>InputStream</code>. The path must be specified * according to the rules described under <code>getResource</code>. If no such resource can be * identified, return <code>null</code>. * * @param path The path to the desired resource. */ public InputStream getResourceAsStream(String path) { DirContext resources = context.getResources(); if (resources != null) { try { Object resource = resources.lookup(path); if (resource instanceof Resource) return (((Resource) resource).streamContent()); } catch (Exception e) { } } return (null); }
/** Get input stream. */ @Override public InputStream getInputStream() throws IOException { if (!connected) connect(); if (resource == null) { throw new FileNotFoundException(getURL() == null ? "null" : getURL().toString()); } // Reopen resource try { resource = (Resource) context.lookup(URL_DECODER.convert(getURL().getFile(), false)); } catch (NamingException e) { // Ignore } return (resource.streamContent()); }
public boolean bind(String user, String passwd) { /** * @param user * @param passwd */ Hashtable env = getDirectoryEnvironment(); env.put(Context.SECURITY_CREDENTIALS, passwd); env.put(Context.SECURITY_PRINCIPAL, "uid=" + user + "," + userBase); try { // Create initial context DirContext ctx = new InitialDirContext(env); // Check that the credentials work LOG.info(ctx.lookup("uid=" + user + "," + userBase)); ctx.close(); } catch (NamingException e) { LOG.warn("Invalid Credentials for user: uid=" + user + "," + userBase, e); return false; } LOG.warn("User " + user + ", bind successful."); return true; }
/* * Test for String get(String) */ public void testGetString() throws Exception { DirContext ctx = contextFactory.getDirContext("people"); assertNotNull(ctx); Attributes attrs = new BasicAttributes(true); ctx.createSubcontext("bar", attrs); DirContext dctx = (DirContext) ctx.lookup("bar"); assertNotNull(dctx); Parameters parameters = new DirectoryParameters(dctx); assertEquals(parameters.getParameterNames().length, 0); assertEquals(parameters.get("foo", "foo"), "foo"); try { parameters.get("foo"); fail("should throw the exception"); } catch (UndefinedParameterException e) { // ok! } parameters.add("foo", "bar"); assertEquals(parameters.get("foo", "foo"), "bar"); parameters.add("foo", "bar2"); try { parameters.get("foo"); fail("should throw the exception"); } catch (AmbiguousParameterException e) { // ok! } assertEquals(parameters.getStrings("foo").length, 2); assertEquals(parameters.getStrings("bar").length, 0); assertEquals(parameters.getParameterNames().length, 1); assertEquals(parameters.isDefined("foo"), true); assertEquals(parameters.isDefined("bar"), false); parameters.add("bar", "foo"); assertEquals(parameters.get("bar", "bar"), "foo"); parameters.remove("bar"); assertEquals(parameters.get("bar", "bar"), "bar"); parameters.add("bar", "foo"); assertEquals(parameters.get("bar", "bar"), "foo"); parameters.remove("bar", "foo"); assertEquals(parameters.get("bar", "bar"), "bar"); parameters.remove(); assertEquals(parameters.getParameterNames().length, 0); parameters.add("bar", "foo"); parameters.add("foo", "bar"); assertEquals(parameters.getParameterNames().length, 2); HashSet<String> set = new HashSet<String>(); set.add("foo"); parameters.remove(set); assertEquals(parameters.get("bar", "bar"), "foo"); assertEquals(parameters.get("foo", "foo"), "foo"); parameters.add("bar", "foo2"); parameters.add("foo", "bar"); parameters.removeExcept(set); assertEquals(parameters.get("bar", "bar"), "bar"); assertEquals(parameters.get("foo", "foo"), "bar"); parameters.remove(); parameters.add("foo", new String[] {"bar"}); parameters.add("foo", new String[] {"foo", "buz"}); parameters.add("bar", new String[] {"foo"}); assertEquals(parameters.getParameterNames().length, 2); assertEquals(parameters.getStrings("foo").length, 3); parameters.remove(); Parameters temp = new DefaultParameters(); temp.add("foo", 2); temp.add("bar", 2); parameters.add("foo", 1); parameters.add("bar", 1); parameters.add(temp, false); assertEquals(parameters.getInts("foo").length, 2); assertEquals(parameters.getInts("bar").length, 2); parameters.remove(); parameters.add("foo", 1); parameters.add("bar", 1); parameters.add(temp, true); assertEquals(parameters.getInts("foo").length, 1); assertEquals(parameters.getInts("bar").length, 1); assertEquals(parameters.getInt("foo"), 2); assertEquals(parameters.getInt("bar"), 2); parameters.remove(); parameters.add("foo", "bar"); parameters.set("foo", "foo"); assertEquals(parameters.get("foo", "bar"), "foo"); parameters.set("foo", new String[] {"foo", "buz"}); assertEquals(parameters.getStrings("foo").length, 2); parameters.set("foo", new boolean[] {true}); assertEquals(parameters.getBoolean("foo"), true); parameters.set("foo", new float[] {1.0F, 2.0F}); assertEquals(parameters.getStrings("foo").length, 2); parameters.set("foo", new int[] {1, 2}); assertEquals(parameters.getStrings("foo").length, 2); parameters.set("foo", new long[] {1, 2}); assertEquals(parameters.getStrings("foo").length, 2); parameters.toString(); Parameters params = parameters.getChild("bar"); assertEquals(params.getParameterNames().length, 0); }