List of usage examples for javax.naming NameClassPair getName
public String getName()
From source file:com.concursive.connect.web.webdav.servlets.WebdavServlet.java
/** * Deletes a collection./*from w ww .j a v a 2s. 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:com.concursive.connect.web.webdav.servlets.WebdavServlet.java
/** * Copy a collection.//www . ja v a 2 s.c o m * * @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
/** * PROPFIND Method.//from ww w . jav a 2s . c om * * @param context Description of the Parameter * @throws javax.servlet.ServletException Description of the Exception * @throws java.io.IOException Description of the Exception */ protected void doPropfind(ActionContext context) throws ServletException, IOException { String path = getRelativePath(context.getRequest()); //fix for windows clients if (path.equals("/files")) { path = ""; } if (path.endsWith("/")) { path = path.substring(0, path.length() - 1); } if ((path.toUpperCase().startsWith("/WEB-INF")) || (path.toUpperCase().startsWith("/META-INF"))) { context.getResponse().sendError(WebdavStatus.SC_FORBIDDEN); return; } // Properties which are to be displayed. Vector properties = null; // Propfind depth int depth = INFINITY; // Propfind type int type = FIND_ALL_PROP; String depthStr = context.getRequest().getHeader("Depth"); if (depthStr == null) { depth = INFINITY; } else { if (depthStr.equals("0")) { depth = 0; } else if (depthStr.equals("1")) { depth = 1; } else if (depthStr.equals("infinity")) { depth = INFINITY; } } /* * Read the request xml and determine all the properties */ /* Node propNode = null; DocumentBuilder documentBuilder = getDocumentBuilder(); try { Document document = documentBuilder.parse (new InputSource(context.getRequest().getInputStream())); // Get the root element of the document Element rootElement = document.getDocumentElement(); NodeList childList = rootElement.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node currentNode = childList.item(i); switch (currentNode.getNodeType()) { case Node.TEXT_NODE: break; case Node.ELEMENT_NODE: if (currentNode.getNodeName().endsWith("prop")) { type = FIND_BY_PROPERTY; propNode = currentNode; } if (currentNode.getNodeName().endsWith("propname")) { type = FIND_PROPERTY_NAMES; } if (currentNode.getNodeName().endsWith("allprop")) { type = FIND_ALL_PROP; } break; } } } catch (Exception e) { // Most likely there was no content : we use the defaults. // TODO : Enhance that ! e.printStackTrace(System.out); } if (type == FIND_BY_PROPERTY) { properties = new Vector(); NodeList childList = propNode.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node currentNode = childList.item(i); switch (currentNode.getNodeType()) { case Node.TEXT_NODE: break; case Node.ELEMENT_NODE: String nodeName = currentNode.getNodeName(); String propertyName = null; if (nodeName.indexOf(':') != -1) { propertyName = nodeName.substring (nodeName.indexOf(':') + 1); } else { propertyName = nodeName; } // href is a live property which is handled differently properties.addElement(propertyName); break; } } } */ // Properties have been determined // Retrieve the resources Connection db = null; boolean exists = true; boolean status = true; Object object = null; ModuleContext resources = null; StringBuffer xmlsb = new StringBuffer(); try { db = this.getConnection(context); resources = getCFSResources(db, context); if (resources == null) { context.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } object = resources.lookup(db, path); } catch (NamingException e) { //e.printStackTrace(System.out); exists = false; int slash = path.lastIndexOf('/'); if (slash != -1) { String parentPath = path.substring(0, slash); Vector currentLockNullResources = (Vector) lockNullResources.get(parentPath); if (currentLockNullResources != null) { Enumeration lockNullResourcesList = currentLockNullResources.elements(); while (lockNullResourcesList.hasMoreElements()) { String lockNullPath = (String) lockNullResourcesList.nextElement(); if (lockNullPath.equals(path)) { context.getResponse().setStatus(WebdavStatus.SC_MULTI_STATUS); context.getResponse().setContentType("text/xml; charset=UTF-8"); // Create multistatus object XMLWriter generatedXML = new XMLWriter(context.getResponse().getWriter()); generatedXML.writeXMLHeader(); generatedXML.writeElement(null, "multistatus" + generateNamespaceDeclarations(), XMLWriter.OPENING); parseLockNullProperties(context.getRequest(), generatedXML, lockNullPath, type, properties); generatedXML.writeElement(null, "multistatus", XMLWriter.CLOSING); generatedXML.sendData(); //e.printStackTrace(System.out); return; } } } } } catch (SQLException e) { e.printStackTrace(System.out); context.getResponse().sendError(SQLERROR, e.getMessage()); status = false; } finally { this.freeConnection(db, context); } if (!status) { return; } if (!exists) { context.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND, path); return; } context.getResponse().setStatus(WebdavStatus.SC_MULTI_STATUS); context.getResponse().setContentType("text/xml; charset=UTF-8"); // Create multistatus object ////System.out.println("Creating Multistatus Object"); XMLWriter generatedXML = new XMLWriter(context.getResponse().getWriter()); generatedXML.writeXMLHeader(); generatedXML.writeElement(null, "multistatus" + generateNamespaceDeclarations(), XMLWriter.OPENING); //System.out.println("Depth: " + depth); if (depth == 0) { parseProperties(context, resources, generatedXML, path, type, properties); } else { // The stack always contains the object of the current level Stack stack = new Stack(); stack.push(path); // Stack of the objects one level below Stack stackBelow = new Stack(); while ((!stack.isEmpty()) && (depth >= 0)) { String currentPath = (String) stack.pop(); if (!currentPath.equals(path)) { parseProperties(context, resources, generatedXML, currentPath, type, properties); } try { db = this.getConnection(context); object = resources.lookup(db, currentPath); } catch (NamingException e) { continue; } catch (SQLException e) { //e.printStackTrace(System.out); context.getResponse().sendError(SQLERROR, e.getMessage()); status = false; } finally { this.freeConnection(db, context); } if (!status) { return; } if ((object instanceof ModuleContext) && depth > 0) { // Get a list of all the resources at the current path and store them // in the stack try { NamingEnumeration enum1 = resources.list(currentPath); int count = 0; while (enum1.hasMoreElements()) { NameClassPair ncPair = (NameClassPair) enum1.nextElement(); String newPath = currentPath; if (!(newPath.endsWith("/"))) { newPath += "/"; } newPath += ncPair.getName(); stackBelow.push(newPath); count++; } if (currentPath.equals(path) && count == 0) { // This directory does not have any files or folders. parseProperties(context, resources, generatedXML, properties); } } catch (NamingException e) { //e.printStackTrace(System.out); context.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } // Displaying the lock-null resources present in that collection String lockPath = currentPath; if (lockPath.endsWith("/")) { lockPath = lockPath.substring(0, lockPath.length() - 1); } Vector currentLockNullResources = (Vector) lockNullResources.get(lockPath); if (currentLockNullResources != null) { Enumeration lockNullResourcesList = currentLockNullResources.elements(); while (lockNullResourcesList.hasMoreElements()) { String lockNullPath = (String) lockNullResourcesList.nextElement(); System.out.println("Lock null path: " + lockNullPath); parseLockNullProperties(context.getRequest(), generatedXML, lockNullPath, type, properties); } } } if (stack.isEmpty()) { depth--; stack = stackBelow; stackBelow = new Stack(); } xmlsb.append(generatedXML.toString()); //System.out.println("xml : " + generatedXML.toString()); generatedXML.sendData(); } } generatedXML.writeElement(null, "multistatus", XMLWriter.CLOSING); xmlsb.append(generatedXML.toString()); generatedXML.sendData(); //System.out.println("xml: " + xmlsb.toString()); }
From source file:com.zeroio.webdav.WebdavServlet.java
/** * Copy a collection./*from w w w . ja v a2 s . c o m*/ * * @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 * @param thisSystem Description of the Parameter * @param db Description of the Parameter * @return Description of the Return Value * @throws SQLException Description of the Exception * @throws FileNotFoundException Description of the Exception */ private boolean copyResource(ActionContext context, SystemStatus thisSystem, Connection db, ModuleContext resources, Hashtable errorList, String source, String dest) throws SQLException, FileNotFoundException { if (debug > 1) { System.out.println("Copy: " + source + " To: " + dest); } Object object = null; try { object = resources.lookup(thisSystem, db, source); } catch (NamingException e) { e.printStackTrace(System.out); } if (object instanceof ModuleContext) { try { resources.createSubcontext(thisSystem, db, dest); } catch (NamingException e) { e.printStackTrace(System.out); errorList.put(dest, new Integer(WebdavStatus.SC_CONFLICT)); return false; } try { //System.out.println("Listing resources at path: " + source); //System.out.println("OBJECT: " + object); object = resources.lookup(thisSystem, db, source); NamingEnumeration enum1 = resources.list(source); while (enum1.hasMoreElements()) { //System.out.println("COPYING CHILD CONTEXT........................."); 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(context, thisSystem, db, resources, errorList, childSrc, childDest); } } catch (NamingException e) { e.printStackTrace(System.out); errorList.put(dest, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); return false; } } else { if (object instanceof Resource) { try { Object resource = resources.copyResource(thisSystem, db, dest, object); if (resource != null) { processInsertHook(context, resource); } } catch (NamingException e) { errorList.put(source, new Integer(WebdavStatus.SC_INTERNAL_SERVER_ERROR)); return false; } catch (IOException io) { 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.zeroio.webdav.WebdavServlet.java
/** * Deletes a collection.//from w ww . jav a2s. 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 * @param db Description of the Parameter * @throws SQLException Description of the Exception */ private void deleteCollection(SystemStatus thisSystem, Connection db, HttpServletRequest req, ModuleContext resources, String path, Hashtable errorList) throws SQLException { 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) { e.printStackTrace(System.out); 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("/")) { if (!childName.endsWith("/")) { 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 ModuleContext) { deleteCollection(thisSystem, db, req, resources, childName, errorList); } try { resources.unbind(thisSystem, db, childName); } catch (NamingException e) { if (!(object instanceof ModuleContext)) { // 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:com.zeroio.webdav.WebdavServlet.java
/** * PROPFIND Method./* w w w. j ava 2 s .c om*/ * * @param context Description of the Parameter * @throws ServletException Description of the Exception * @throws IOException Description of the Exception */ protected void doPropfind(ActionContext context) throws ServletException, IOException { String path = getRelativePath(context.getRequest()); //fix for windows clients if (path.equals("/files")) { path = ""; } if (path.endsWith("/")) { path = path.substring(0, path.length() - 1); } if ((path.toUpperCase().startsWith("/WEB-INF")) || (path.toUpperCase().startsWith("/META-INF"))) { context.getResponse().sendError(WebdavStatus.SC_FORBIDDEN); return; } if (path.indexOf("/.") > -1 || path.indexOf(".DS_Store") > -1) { //Fix for MACOSX finder. Do not allow requests for files starting with a period return; } //System.out.println("METHOD PROPFIND....PATH: " + path); // Properties which are to be displayed. Vector properties = null; // Propfind depth by default 1 for performance reasons int depth = 1; // Propfind type int type = FIND_ALL_PROP; String depthStr = context.getRequest().getHeader("Depth"); if (depthStr == null) { depth = INFINITY; } else { if (depthStr.equals("0")) { depth = 0; } else if (depthStr.equals("1")) { depth = 1; } else if (depthStr.equals("infinity")) { depth = INFINITY; } } /* * Read the request xml and determine all the properties */ Node propNode = null; DocumentBuilder documentBuilder = getDocumentBuilder(); try { Document document = documentBuilder.parse(new InputSource(context.getRequest().getInputStream())); // Get the root element of the document Element rootElement = document.getDocumentElement(); NodeList childList = rootElement.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node currentNode = childList.item(i); switch (currentNode.getNodeType()) { case Node.TEXT_NODE: break; case Node.ELEMENT_NODE: if (currentNode.getNodeName().endsWith("prop")) { type = FIND_BY_PROPERTY; propNode = currentNode; } if (currentNode.getNodeName().endsWith("propname")) { type = FIND_PROPERTY_NAMES; } if (currentNode.getNodeName().endsWith("allprop")) { type = FIND_ALL_PROP; } break; } } } catch (Exception e) { // Most likely there was no content : we use the defaults. // TODO : Enhance that ! //e.printStackTrace(System.out); } if (type == FIND_BY_PROPERTY) { properties = new Vector(); if (!properties.contains("creationdate")) { //If the request did not contain creationdate property then add this to requested properties //to make the information available for clients properties.addElement("creationdate"); } NodeList childList = propNode.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { Node currentNode = childList.item(i); switch (currentNode.getNodeType()) { case Node.TEXT_NODE: break; case Node.ELEMENT_NODE: String nodeName = currentNode.getNodeName(); String propertyName = null; if (nodeName.indexOf(':') != -1) { propertyName = nodeName.substring(nodeName.indexOf(':') + 1); } else { propertyName = nodeName; } // href is a live property which is handled differently properties.addElement(propertyName); break; } } } // Properties have been determined // Retrieve the resources Connection db = null; boolean exists = true; boolean status = true; Object current = null; Object child = null; ModuleContext resources = null; SystemStatus thisSystem = null; StringBuffer xmlsb = new StringBuffer(); try { db = this.getConnection(context); resources = getCFSResources(db, context); if (resources == null) { context.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return; } thisSystem = this.getSystemStatus(context); current = resources.lookup(thisSystem, db, path); if (current instanceof ModuleContext) { //System.out.println( ((ModuleContext) current).toString()); } } catch (NamingException e) { //e.printStackTrace(System.out); exists = false; int slash = path.lastIndexOf('/'); if (slash != -1) { String parentPath = path.substring(0, slash); Vector currentLockNullResources = (Vector) lockNullResources.get(parentPath); if (currentLockNullResources != null) { Enumeration lockNullResourcesList = currentLockNullResources.elements(); while (lockNullResourcesList.hasMoreElements()) { String lockNullPath = (String) lockNullResourcesList.nextElement(); if (lockNullPath.equals(path)) { context.getResponse().setStatus(WebdavStatus.SC_MULTI_STATUS); context.getResponse().setContentType("text/xml; charset=UTF-8"); // Create multistatus object XMLWriter generatedXML = new XMLWriter(context.getResponse().getWriter()); generatedXML.writeXMLHeader(); generatedXML.writeElement(null, "multistatus" + generateNamespaceDeclarations(), XMLWriter.OPENING); parseLockNullProperties(context.getRequest(), generatedXML, lockNullPath, type, properties); generatedXML.writeElement(null, "multistatus", XMLWriter.CLOSING); generatedXML.sendData(); //e.printStackTrace(System.out); return; } } } } } catch (SQLException e) { e.printStackTrace(System.out); context.getResponse().sendError(CFS_SQLERROR, e.getMessage()); status = false; } finally { this.freeConnection(db, context); } if (!status) { return; } if (!exists) { context.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND, path); return; } context.getResponse().setStatus(WebdavStatus.SC_MULTI_STATUS); context.getResponse().setContentType("text/xml; charset=UTF-8"); // Create multistatus object ////System.out.println("Creating Multistatus Object"); XMLWriter generatedXML = new XMLWriter(context.getResponse().getWriter()); generatedXML.writeXMLHeader(); generatedXML.writeElement(null, "multistatus" + generateNamespaceDeclarations(), XMLWriter.OPENING); //System.out.println("Depth: " + depth); if (depth == 0) { parseProperties(context, resources, generatedXML, path, type, properties); } else { // The stack always contains the object of the current level Stack stack = new Stack(); stack.push(path); // Stack of the objects one level below Stack stackBelow = new Stack(); while ((!stack.isEmpty()) && (depth >= 0)) { String currentPath = (String) stack.pop(); try { if (!currentPath.equals(path)) { //object at url currentPath not yet looked up. so perform lookup at url currentPath child = resources.lookup(currentPath); parseProperties(context, resources, generatedXML, currentPath, type, properties); } } catch (NamingException e) { e.printStackTrace(System.out); continue; } if (!status) { return; } if ((current instanceof ModuleContext) && depth > 0) { // Get a list of all the resources at the current path and store them // in the stack try { NamingEnumeration enum1 = ((ModuleContext) current).list(""); int count = 0; while (enum1.hasMoreElements()) { NameClassPair ncPair = (NameClassPair) enum1.nextElement(); String newPath = currentPath; if (!(newPath.endsWith("/"))) { newPath += "/"; } newPath += ncPair.getName(); //System.out.println("STACKING CHILD: " + newPath); stackBelow.push(newPath); count++; } if (currentPath.equals(path) && count == 0) { // This directory does not have any files or folders. //System.out.println("DIRECTORY HAS NO FILES OR FOLDERS..."); parseProperties(context, resources, generatedXML, properties); } } catch (NamingException e) { //e.printStackTrace(System.out); context.getResponse().sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, path); return; } // Displaying the lock-null resources present in that collection String lockPath = currentPath; if (lockPath.endsWith("/")) { lockPath = lockPath.substring(0, lockPath.length() - 1); } Vector currentLockNullResources = (Vector) lockNullResources.get(lockPath); if (currentLockNullResources != null) { Enumeration lockNullResourcesList = currentLockNullResources.elements(); while (lockNullResourcesList.hasMoreElements()) { String lockNullPath = (String) lockNullResourcesList.nextElement(); System.out.println("Lock null path: " + lockNullPath); parseLockNullProperties(context.getRequest(), generatedXML, lockNullPath, type, properties); } } } if (stack.isEmpty()) { depth--; stack = stackBelow; stackBelow = new Stack(); } xmlsb.append(generatedXML.toString()); //System.out.println("xml : " + generatedXML.toString()); generatedXML.sendData(); } } Iterator locks = lockNullResources.keySet().iterator(); while (locks.hasNext()) { String lockpath = (String) locks.next(); //System.out.println("LOCK PATH: " + lockpath); } generatedXML.writeElement(null, "multistatus", XMLWriter.CLOSING); xmlsb.append(generatedXML.toString()); generatedXML.sendData(); //System.out.println("xml: " + xmlsb.toString()); }
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()); }// ww w. ja v a2 s. c o m 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();// ww w .j a 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.loader.WebappClassLoader.java
/** * Have one or more classes or resources been modified so that a reload * is appropriate?//from w w w. ja va 2 s . c o m */ public boolean modified() { if (log.isDebugEnabled()) log.debug("modified()"); // Checking for modified loaded resources int length = paths.length; // A rare race condition can occur in the updates of the two arrays // It's totally ok if the latest class added is not checked (it will // be checked the next time int length2 = lastModifiedDates.length; if (length > length2) length = length2; for (int i = 0; i < length; i++) { try { long lastModified = ((ResourceAttributes) resources.getAttributes(paths[i])).getLastModified(); if (lastModified != lastModifiedDates[i]) { if (log.isDebugEnabled()) log.debug(" Resource '" + paths[i] + "' was modified; Date is now: " + new java.util.Date(lastModified) + " Was: " + new java.util.Date(lastModifiedDates[i])); return (true); } } catch (NamingException e) { log.error(" Resource '" + paths[i] + "' is missing"); return (true); } } length = jarNames.length; // Check if JARs have been added or removed if (getJarPath() != null) { try { NamingEnumeration enum_ = resources.listBindings(getJarPath()); int i = 0; while (enum_.hasMoreElements() && (i < length)) { NameClassPair ncPair = (NameClassPair) enum_.nextElement(); String name = ncPair.getName(); // Ignore non JARs present in the lib folder if (!name.endsWith(".jar")) continue; if (!name.equals(jarNames[i])) { // Missing JAR log.info(" Additional JARs have been added : '" + name + "'"); return (true); } i++; } if (enum_.hasMoreElements()) { while (enum_.hasMoreElements()) { NameClassPair ncPair = (NameClassPair) enum_.nextElement(); String name = ncPair.getName(); // Additional non-JAR files are allowed if (name.endsWith(".jar")) { // There was more JARs log.info(" Additional JARs have been added"); return (true); } } } else if (i < jarNames.length) { // There was less JARs log.info(" Additional JARs have been added"); return (true); } } catch (NamingException e) { if (log.isDebugEnabled()) log.debug(" Failed tracking modifications of '" + getJarPath() + "'"); } catch (ClassCastException e) { log.error(" Failed tracking modifications of '" + getJarPath() + "' : " + e.getMessage()); } } // No classes have been modified return (false); }
From source file:org.apache.catalina.servlets.DefaultServlet.java
/** * Return an InputStream to an HTML representation of the contents * of this directory.//from ww w .j a va2s . co m * * @param contextPath Context path to which our internal paths are * relative * @param resourceInfo Description of the Parameter * @param xsltInputStream Description of the Parameter * @return Description of the Return Value */ protected InputStream renderXml(String contextPath, ResourceInfo resourceInfo, InputStream xsltInputStream) { StringBuffer sb = new StringBuffer(); sb.append("<?xml version=\"1.0\"?>"); sb.append("<listing "); sb.append(" contextPath='"); sb.append(contextPath); sb.append("'"); sb.append(" directory='"); sb.append(resourceInfo.path); sb.append("' "); sb.append(" hasParent='").append(!resourceInfo.path.equals("/")); sb.append("'>"); sb.append("<entries>"); try { // Render the directory entries within this directory DirContext directory = resourceInfo.directory; NamingEnumeration enum1 = resourceInfo.resources.list(resourceInfo.path); while (enum1.hasMoreElements()) { NameClassPair ncPair = (NameClassPair) enum1.nextElement(); String resourceName = ncPair.getName(); ResourceInfo childResourceInfo = new ResourceInfo(resourceName, directory); String trimmed = resourceName; if (trimmed.equalsIgnoreCase("WEB-INF") || trimmed.equalsIgnoreCase("META-INF") || trimmed.equalsIgnoreCase(localXsltFile)) { continue; } sb.append("<entry"); sb.append(" type='").append(childResourceInfo.collection ? "dir" : "file").append("'"); sb.append(" urlPath='").append(rewriteUrl(contextPath)) .append(rewriteUrl(resourceInfo.path + resourceName)) .append(childResourceInfo.collection ? "/" : "").append("'"); if (!childResourceInfo.collection) { sb.append(" size='").append(renderSize(childResourceInfo.length)).append("'"); } sb.append(" date='").append(childResourceInfo.httpDate).append("'"); sb.append(">"); sb.append(trimmed); if (childResourceInfo.collection) { sb.append("/"); } sb.append("</entry>"); } } catch (NamingException e) { // Something went wrong e.printStackTrace(); } sb.append("</entries>"); String readme = getReadme(resourceInfo.directory); if (readme != null) { sb.append("<readme><![CDATA["); sb.append(readme); sb.append("]]></readme>"); } sb.append("</listing>"); try { TransformerFactory tFactory = TransformerFactory.newInstance(); Source xmlSource = new StreamSource(new StringReader(sb.toString())); Source xslSource = new StreamSource(xsltInputStream); Transformer transformer = tFactory.newTransformer(xslSource); ByteArrayOutputStream stream = new ByteArrayOutputStream(); OutputStreamWriter osWriter = new OutputStreamWriter(stream, "UTF8"); StreamResult out = new StreamResult(osWriter); transformer.transform(xmlSource, out); osWriter.flush(); return (new ByteArrayInputStream(stream.toByteArray())); } catch (Exception e) { log("directory transform failure: " + e.getMessage()); return renderHtml(contextPath, resourceInfo); } }