List of usage examples for javax.naming.directory DirContext list
public NamingEnumeration<NameClassPair> list(Name name) throws NamingException;
From source file:Create.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 www . j a v a2 s . c o m*/ // Create the initial context DirContext ctx = new InitialDirContext(env); // Create attributes to be associated with the new context Attributes attrs = new BasicAttributes(true); // case-ignore Attribute objclass = new BasicAttribute("objectclass"); objclass.add("top"); objclass.add("organizationalUnit"); attrs.put(objclass); // Create the context Context result = ctx.createSubcontext("ou=NewOu", attrs); // Check that it was created by listing its parent NamingEnumeration list = ctx.list(""); // Go through each item in list while (list.hasMore()) { NameClassPair nc = (NameClassPair) list.next(); System.out.println(nc); } // Close the contexts when we're done result.close(); ctx.close(); } catch (NamingException e) { System.out.println("Create failed: " + e); } }
From source file:ldap.SearchUtility.java
/** * recursively walks the tree to depth 'depth', and returns * a list of all names found at that depth. * @param treeNode//w ww .j a va 2 s. c om * @param depth * @return * @throws NamingException */ private List<LdapName> getElementNames(LdapName treeNode, int depth, DirContext context) throws NamingException { depth--; NamingEnumeration<NameClassPair> children = context.list(treeNode); List<LdapName> elementNames = new ArrayList<LdapName>(); // cycle through all the children we've found. while (children.hasMore()) { NameClassPair child = children.next(); LdapName childName = new LdapName(child.getNameInNamespace()); if (depth == 0) // return value - these are what we're looking for! elementNames.add(childName); else elementNames.addAll(getElementNames(childName, depth, context)); // keep going down! } return elementNames; }
From source file:catalina.startup.ContextConfig.java
/** * Accumulate and return a Set of resource paths to be analyzed for * tag library descriptors. Each element of the returned set will be * the context-relative path to either a tag library descriptor file, * or to a JAR file that may contain tag library descriptors in its * <code>META-INF</code> subdirectory. * * @exception IOException if an input/output error occurs while * accumulating the list of resource paths *///w w w . j ava 2 s . c o m private Set tldScanResourcePaths() throws IOException { if (debug >= 1) { log(" Accumulating TLD resource paths"); } Set resourcePaths = new HashSet(); // Accumulate resource paths explicitly listed in the web application // deployment descriptor if (debug >= 2) { log(" Scanning <taglib> elements in web.xml"); } String taglibs[] = context.findTaglibs(); for (int i = 0; i < taglibs.length; i++) { String resourcePath = context.findTaglib(taglibs[i]); // FIXME - Servlet 2.3 DTD implies that the location MUST be // a context-relative path starting with '/'? if (!resourcePath.startsWith("/")) { resourcePath = "/WEB-INF/web.xml/../" + resourcePath; } if (debug >= 3) { log(" Adding path '" + resourcePath + "' for URI '" + taglibs[i] + "'"); } resourcePaths.add(resourcePath); } // Scan TLDs in the /WEB-INF subdirectory of the web application if (debug >= 2) { log(" Scanning TLDs in /WEB-INF subdirectory"); } DirContext resources = context.getResources(); try { NamingEnumeration items = resources.list("/WEB-INF"); while (items.hasMoreElements()) { NameClassPair item = (NameClassPair) items.nextElement(); String resourcePath = "/WEB-INF/" + item.getName(); // FIXME - JSP 1.2 is not explicit about whether we should // scan subdirectories of /WEB-INF for TLDs also if (!resourcePath.endsWith(".tld")) { continue; } if (debug >= 3) { log(" Adding path '" + resourcePath + "'"); } resourcePaths.add(resourcePath); } } catch (NamingException e) { ; // Silent catch: it's valid that no /WEB-INF directory exists } // Scan JARs in the /WEB-INF/lib subdirectory of the web application if (debug >= 2) { log(" Scanning JARs in /WEB-INF/lib subdirectory"); } try { NamingEnumeration items = resources.list("/WEB-INF/lib"); while (items.hasMoreElements()) { NameClassPair item = (NameClassPair) items.nextElement(); String resourcePath = "/WEB-INF/lib/" + item.getName(); if (!resourcePath.endsWith(".jar")) { continue; } if (debug >= 3) { log(" Adding path '" + resourcePath + "'"); } resourcePaths.add(resourcePath); } } catch (NamingException e) { ; // Silent catch: it's valid that no /WEB-INF/lib directory exists } // Return the completed set return (resourcePaths); }
From source file:com.concursive.connect.web.webdav.servlets.WebdavServlet.java
/** * Copy a collection./*from ww w.j av a2s .com*/ * * @param resources Resources implementation to be used * @param errorList Hashtable containing the list of errors which occurred * during the copy operation * @param source Path of the resource to be copied * @param dest Destination path * @return Description of the Return Value */ private boolean copyResource(DirContext resources, Hashtable errorList, String source, String dest) { if (debug > 1) { System.out.println("Copy: " + source + " To: " + dest); } Object object = null; try { object = resources.lookup(source); } catch (NamingException e) { } if (object instanceof DirContext) { try { resources.createSubcontext(dest); } catch (NamingException e) { errorList.put(dest, new Integer(WebdavStatus.SC_CONFLICT)); return false; } try { NamingEnumeration enum1 = resources.list(source); while (enum1.hasMoreElements()) { NameClassPair ncPair = (NameClassPair) enum1.nextElement(); String childDest = dest; if (!childDest.equals("/")) { childDest += "/"; } childDest += ncPair.getName(); String childSrc = source; if (!childSrc.equals("/")) { childSrc += "/"; } childSrc += ncPair.getName(); copyResource(resources, errorList, childSrc, childDest); } } catch (NamingException e) { errorList.put(dest, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); return false; } } else { if (object instanceof Resource) { try { resources.bind(dest, object); } catch (NamingException e) { errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); return false; } } else { errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); return false; } } return true; }
From source file:com.concursive.connect.web.webdav.servlets.WebdavServlet.java
/** * Deletes a collection./*from www .j ava2s . c o m*/ * * @param resources Resources implementation associated with the context * @param path Path to the collection to be deleted * @param errorList Contains the list of the errors which occurred * @param req Description of the Parameter */ private void deleteCollection(HttpServletRequest req, DirContext resources, String path, Hashtable errorList) { if (debug > 1) { System.out.println("Delete:" + path); } if ((path.toUpperCase().startsWith("/WEB-INF")) || (path.toUpperCase().startsWith("/META-INF"))) { errorList.put(path, new Integer(WebdavStatus.SC_FORBIDDEN)); return; } String ifHeader = req.getHeader("If"); if (ifHeader == null) { ifHeader = ""; } String lockTokenHeader = req.getHeader("Lock-Token"); if (lockTokenHeader == null) { lockTokenHeader = ""; } Enumeration enum1 = null; try { enum1 = resources.list(path); } catch (NamingException e) { errorList.put(path, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); return; } while (enum1.hasMoreElements()) { NameClassPair ncPair = (NameClassPair) enum1.nextElement(); String childName = path; if (!childName.equals("/")) { childName += "/"; } childName += ncPair.getName(); if (isLocked(childName, ifHeader + lockTokenHeader)) { errorList.put(childName, new Integer(WebdavStatus.SC_LOCKED)); } else { try { Object object = resources.lookup(childName); if (object instanceof DirContext) { deleteCollection(req, resources, childName, errorList); } try { resources.unbind(childName); } catch (NamingException e) { if (!(object instanceof DirContext)) { // If it's not a collection, then it's an unknown // error errorList.put(childName, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); } } } catch (NamingException e) { errorList.put(childName, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); } } } }
From source file:nl.nn.adapterframework.ldap.LdapSender.java
/** * Return a list of all of the subcontexts of the current context, which is relative to parentContext. * @return an array of Strings containing a list of the subcontexts for a current context. */// www .j av a 2 s . c om public String[] getSubContextList(DirContext parentContext, String relativeContext, ParameterResolutionContext prc) { String[] retValue = null; try { // Create a vector object and add the names of all of the subcontexts // to it Vector n = new Vector(); NamingEnumeration list = parentContext.list(relativeContext); if (log.isDebugEnabled()) log.debug("getSubCOntextList(context) : context = " + relativeContext); for (int x = 0; list.hasMore(); x++) { NameClassPair nc = (NameClassPair) list.next(); n.addElement(nc); } // Create a string array of the same size as the vector object String contextList[] = new String[n.size()]; for (int x = 0; x < n.size(); x++) { // Add each name to the array contextList[x] = ((NameClassPair) (n.elementAt(x))).getName(); } retValue = contextList; } catch (NamingException e) { storeLdapException(e, prc); log.error("Exception in operation [" + getOperation() + "] ", e); } return retValue; }
From source file:org.apache.activemq.artemis.tests.integration.amqp.SaslKrb5LDAPSecurityTest.java
@Test public void testRunning() throws Exception { Hashtable<String, String> env = new Hashtable<>(); env.put(Context.PROVIDER_URL, "ldap://localhost:1024"); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.SECURITY_AUTHENTICATION, "simple"); env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL); env.put(Context.SECURITY_CREDENTIALS, CREDENTIALS); DirContext ctx = new InitialDirContext(env); HashSet<String> set = new HashSet<>(); NamingEnumeration<NameClassPair> list = ctx.list("ou=system"); while (list.hasMore()) { NameClassPair ncp = list.next(); set.add(ncp.getName());//from ww w . ja v a 2 s . c om } Assert.assertTrue(set.contains("uid=admin")); Assert.assertTrue(set.contains("ou=users")); Assert.assertTrue(set.contains("ou=groups")); Assert.assertTrue(set.contains("ou=configuration")); Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot")); ctx.close(); }
From source file:org.apache.activemq.artemis.tests.integration.amqp.SaslKrb5LDAPSecurityTest.java
@Test public void testSaslGssapiLdapAuth() throws Exception { final Hashtable<String, String> env = new Hashtable<>(); env.put(Context.PROVIDER_URL, "ldap://localhost:1024"); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI"); LoginContext loginContext = new LoginContext("broker-sasl-gssapi"); loginContext.login();/* w w w. ja v a 2 s.co m*/ try { Subject.doAs(loginContext.getSubject(), (PrivilegedExceptionAction<Object>) () -> { HashSet<String> set = new HashSet<>(); DirContext ctx = new InitialDirContext(env); NamingEnumeration<NameClassPair> list = ctx.list("ou=system"); while (list.hasMore()) { NameClassPair ncp = list.next(); set.add(ncp.getName()); } Assert.assertTrue(set.contains("uid=first")); Assert.assertTrue(set.contains("cn=users")); Assert.assertTrue(set.contains("ou=configuration")); Assert.assertTrue(set.contains("prefNodeName=sysPrefRoot")); ctx.close(); return null; }); } catch (PrivilegedActionException e) { throw e.getException(); } }
From source file:org.apache.catalina.startup.TldConfig.java
private void tldScanResourcePathsWebInf(DirContext resources, String rootPath, Set tldPaths) throws IOException { if (log.isTraceEnabled()) { log.trace(" Scanning TLDs in " + rootPath + " subdirectory"); }//from w w w . ja v a2 s . c o m try { NamingEnumeration items = resources.list(rootPath); while (items.hasMoreElements()) { NameClassPair item = (NameClassPair) items.nextElement(); String resourcePath = rootPath + "/" + item.getName(); if (!resourcePath.endsWith(".tld") && (resourcePath.startsWith("/WEB-INF/classes") || resourcePath.startsWith("/WEB-INF/lib"))) { continue; } if (resourcePath.endsWith(".tld")) { if (log.isTraceEnabled()) { log.trace(" Adding path '" + resourcePath + "'"); } tldPaths.add(resourcePath); } else { tldScanResourcePathsWebInf(resources, resourcePath, tldPaths); } } } catch (NamingException e) { ; // Silent catch: it's valid that no /WEB-INF directory exists } }
From source file:org.jboss.web.tomcat.tc4.SingleSignOnContextConfig.java
/** * Accumulate and return a Set of resource paths to be analyzed for * tag library descriptors. Each element of the returned set will be * the context-relative path to either a tag library descriptor file, * or to a JAR file that may contain tag library descriptors in its * <code>META-INF</code> subdirectory. * * @exception IOException if an input/output error occurs while * accumulating the list of resource paths *//* w w w .j a v a2 s .c om*/ private Set tldScanResourcePaths() throws IOException { if (debug >= 1) { log(" Accumulating TLD resource paths"); } Set resourcePaths = new HashSet(); // Accumulate resource paths explicitly listed in the web application // deployment descriptor if (debug >= 2) { log(" Scanning <taglib> elements in web.xml"); } String taglibs[] = context.findTaglibs(); for (int i = 0; i < taglibs.length; i++) { String resourcePath = context.findTaglib(taglibs[i]); // FIXME - Servlet 2.3 DTD implies that the location MUST be // a context-relative path starting with '/'? if (!resourcePath.startsWith("/")) { resourcePath = "/WEB-INF/" + resourcePath; } if (debug >= 3) { log(" Adding path '" + resourcePath + "' for URI '" + taglibs[i] + "'"); } resourcePaths.add(resourcePath); } // Scan TLDs in the /WEB-INF subdirectory of the web application if (debug >= 2) { log(" Scanning TLDs in /WEB-INF subdirectory"); } DirContext resources = context.getResources(); try { NamingEnumeration items = resources.list("/WEB-INF"); while (items.hasMoreElements()) { NameClassPair item = (NameClassPair) items.nextElement(); String resourcePath = "/WEB-INF/" + item.getName(); // FIXME - JSP 1.2 is not explicit about whether we should // scan subdirectories of /WEB-INF for TLDs also if (!resourcePath.endsWith(".tld")) { continue; } if (debug >= 3) { log(" Adding path '" + resourcePath + "'"); } resourcePaths.add(resourcePath); } } catch (NamingException e) { ; // Silent catch: it's valid that no /WEB-INF directory exists } // Scan JARs in the /WEB-INF/lib subdirectory of the web application if (debug >= 2) { log(" Scanning JARs in /WEB-INF/lib subdirectory"); } try { NamingEnumeration items = resources.list("/WEB-INF/lib"); while (items.hasMoreElements()) { NameClassPair item = (NameClassPair) items.nextElement(); String resourcePath = "/WEB-INF/lib/" + item.getName(); if (!resourcePath.endsWith(".jar")) { continue; } if (debug >= 3) { log(" Adding path '" + resourcePath + "'"); } resourcePaths.add(resourcePath); } } catch (NamingException e) { ; // Silent catch: it's valid that no /WEB-INF/lib directory exists } // Return the completed set return (resourcePaths); }