Search in sources :

Example 11 with NameClassPair

use of javax.naming.NameClassPair in project tomcat70 by apache.

the class WebappLoader method copyDir.

/**
 * Copy directory.
 */
private boolean copyDir(DirContext srcDir, File destDir) {
    try {
        NamingEnumeration<NameClassPair> enumeration = srcDir.list("");
        while (enumeration.hasMoreElements()) {
            NameClassPair ncPair = enumeration.nextElement();
            String name = ncPair.getName();
            Object object = srcDir.lookup(name);
            File currentFile = new File(destDir, name);
            if (object instanceof Resource) {
                InputStream is = ((Resource) object).streamContent();
                OutputStream os = new FileOutputStream(currentFile);
                if (!copy(is, os))
                    return false;
            } else if (object instanceof InputStream) {
                OutputStream os = new FileOutputStream(currentFile);
                if (!copy((InputStream) object, os))
                    return false;
            } else if (object instanceof DirContext) {
                if (!currentFile.isDirectory() && !currentFile.mkdir())
                    return false;
                if (!copyDir((DirContext) object, currentFile))
                    return false;
            }
        }
    } catch (NamingException e) {
        return false;
    } catch (IOException e) {
        return false;
    }
    return true;
}
Also used : NameClassPair(javax.naming.NameClassPair) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Resource(org.apache.naming.resources.Resource) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 12 with NameClassPair

use of javax.naming.NameClassPair in project tomcat70 by apache.

the class WebdavStatus method copyResource.

/**
 * Copy a collection.
 *
 * @param dirContext 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
 */
private boolean copyResource(DirContext dirContext, Hashtable<String, Integer> errorList, String source, String dest) {
    if (debug > 1)
        log("Copy: " + source + " To: " + dest);
    Object object = null;
    try {
        object = dirContext.lookup(source);
    } catch (NamingException e) {
    // Ignore
    }
    if (object instanceof DirContext) {
        try {
            dirContext.createSubcontext(dest);
        } catch (NamingException e) {
            errorList.put(dest, Integer.valueOf(WebdavStatus.SC_CONFLICT));
            return false;
        }
        try {
            NamingEnumeration<NameClassPair> enumeration = dirContext.list(source);
            while (enumeration.hasMoreElements()) {
                NameClassPair ncPair = enumeration.nextElement();
                String childDest = dest;
                if (!childDest.equals("/"))
                    childDest += "/";
                childDest += ncPair.getName();
                String childSrc = source;
                if (!childSrc.equals("/"))
                    childSrc += "/";
                childSrc += ncPair.getName();
                copyResource(dirContext, errorList, childSrc, childDest);
            }
        } catch (NamingException e) {
            errorList.put(dest, Integer.valueOf(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
            return false;
        }
    } else {
        if (object instanceof Resource) {
            try {
                dirContext.bind(dest, object);
            } catch (NamingException e) {
                if (e.getCause() instanceof FileNotFoundException) {
                    // We know the source exists so it must be the
                    // destination dir that can't be found
                    errorList.put(source, Integer.valueOf(WebdavStatus.SC_CONFLICT));
                } else {
                    errorList.put(source, Integer.valueOf(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
                }
                return false;
            }
        } else {
            errorList.put(source, Integer.valueOf(WebdavStatus.SC_INTERNAL_SERVER_ERROR));
            return false;
        }
    }
    return true;
}
Also used : NameClassPair(javax.naming.NameClassPair) Resource(org.apache.naming.resources.Resource) FileNotFoundException(java.io.FileNotFoundException) NamingException(javax.naming.NamingException) DirContext(javax.naming.directory.DirContext)

Example 13 with NameClassPair

use of javax.naming.NameClassPair in project tomcat70 by apache.

the class DefaultServlet method renderHtml.

/**
 * Return an InputStream to an HTML representation of the contents
 * of this directory.
 *
 * @param contextPath Context path to which our internal paths are
 *  relative
 */
protected InputStream renderHtml(String contextPath, CacheEntry cacheEntry) throws IOException, ServletException {
    String name = cacheEntry.name;
    // Prepare a writer to a buffered area
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    OutputStreamWriter osWriter = new OutputStreamWriter(stream, "UTF8");
    PrintWriter writer = new PrintWriter(osWriter);
    StringBuilder sb = new StringBuilder();
    // rewriteUrl(contextPath) is expensive. cache result for later reuse
    String rewrittenContextPath = rewriteUrl(contextPath);
    // Render the page header
    sb.append("<html>\r\n");
    sb.append("<head>\r\n");
    sb.append("<title>");
    sb.append(sm.getString("directory.title", name));
    sb.append("</title>\r\n");
    sb.append("<STYLE><!--");
    sb.append(org.apache.catalina.util.TomcatCSS.TOMCAT_CSS);
    sb.append("--></STYLE> ");
    sb.append("</head>\r\n");
    sb.append("<body>");
    sb.append("<h1>");
    sb.append(sm.getString("directory.title", name));
    // Render the link to our parent (if required)
    String parentDirectory = name;
    if (parentDirectory.endsWith("/")) {
        parentDirectory = parentDirectory.substring(0, parentDirectory.length() - 1);
    }
    int slash = parentDirectory.lastIndexOf('/');
    if (slash >= 0) {
        String parent = name.substring(0, slash);
        sb.append(" - <a href=\"");
        sb.append(rewrittenContextPath);
        if (parent.equals(""))
            parent = "/";
        sb.append(rewriteUrl(parent));
        if (!parent.endsWith("/"))
            sb.append("/");
        sb.append("\">");
        sb.append("<b>");
        sb.append(sm.getString("directory.parent", parent));
        sb.append("</b>");
        sb.append("</a>");
    }
    sb.append("</h1>");
    sb.append("<HR size=\"1\" noshade=\"noshade\">");
    sb.append("<table width=\"100%\" cellspacing=\"0\"" + " cellpadding=\"5\" align=\"center\">\r\n");
    // Render the column headings
    sb.append("<tr>\r\n");
    sb.append("<td align=\"left\"><font size=\"+1\"><strong>");
    sb.append(sm.getString("directory.filename"));
    sb.append("</strong></font></td>\r\n");
    sb.append("<td align=\"center\"><font size=\"+1\"><strong>");
    sb.append(sm.getString("directory.size"));
    sb.append("</strong></font></td>\r\n");
    sb.append("<td align=\"right\"><font size=\"+1\"><strong>");
    sb.append(sm.getString("directory.lastModified"));
    sb.append("</strong></font></td>\r\n");
    sb.append("</tr>");
    try {
        // Render the directory entries within this directory
        NamingEnumeration<NameClassPair> enumeration = resources.list(cacheEntry.name);
        boolean shade = false;
        while (enumeration.hasMoreElements()) {
            NameClassPair ncPair = enumeration.nextElement();
            String resourceName = ncPair.getName();
            String trimmed = resourceName;
            if (trimmed.equalsIgnoreCase("WEB-INF") || trimmed.equalsIgnoreCase("META-INF"))
                continue;
            CacheEntry childCacheEntry = resources.lookupCache(cacheEntry.name + resourceName);
            if (!childCacheEntry.exists) {
                continue;
            }
            sb.append("<tr");
            if (shade)
                sb.append(" bgcolor=\"#eeeeee\"");
            sb.append(">\r\n");
            shade = !shade;
            sb.append("<td align=\"left\">&nbsp;&nbsp;\r\n");
            sb.append("<a href=\"");
            sb.append(rewrittenContextPath);
            resourceName = rewriteUrl(name + resourceName);
            sb.append(resourceName);
            if (childCacheEntry.context != null)
                sb.append("/");
            sb.append("\"><tt>");
            sb.append(RequestUtil.filter(trimmed));
            if (childCacheEntry.context != null)
                sb.append("/");
            sb.append("</tt></a></td>\r\n");
            sb.append("<td align=\"right\"><tt>");
            if (childCacheEntry.context != null)
                sb.append("&nbsp;");
            else
                sb.append(renderSize(childCacheEntry.attributes.getContentLength()));
            sb.append("</tt></td>\r\n");
            sb.append("<td align=\"right\"><tt>");
            sb.append(childCacheEntry.attributes.getLastModifiedHttp());
            sb.append("</tt></td>\r\n");
            sb.append("</tr>\r\n");
        }
    } catch (NamingException e) {
        // Something went wrong
        throw new ServletException("Error accessing resource", e);
    }
    // Render the page footer
    sb.append("</table>\r\n");
    sb.append("<HR size=\"1\" noshade=\"noshade\">");
    String readme = getReadme(cacheEntry.context);
    if (readme != null) {
        sb.append(readme);
        sb.append("<HR size=\"1\" noshade=\"noshade\">");
    }
    if (showServerInfo) {
        sb.append("<h3>").append(ServerInfo.getServerInfo()).append("</h3>");
    }
    sb.append("</body>\r\n");
    sb.append("</html>\r\n");
    // Return an input stream to the underlying bytes
    writer.write(sb.toString());
    writer.flush();
    return (new ByteArrayInputStream(stream.toByteArray()));
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheEntry(org.apache.naming.resources.CacheEntry) ServletException(javax.servlet.ServletException) ByteArrayInputStream(java.io.ByteArrayInputStream) NameClassPair(javax.naming.NameClassPair) OutputStreamWriter(java.io.OutputStreamWriter) NamingException(javax.naming.NamingException) PrintWriter(java.io.PrintWriter)

Example 14 with NameClassPair

use of javax.naming.NameClassPair in project tomcat70 by apache.

the class DirContextURLConnection method list.

// --------------------------------------------------------- Public Methods
/**
 * List children of this collection. The names given are relative to this
 * URI's path. The full uri of the children is then : path + "/" + name.
 */
public Enumeration<String> list() throws IOException {
    if (!connected) {
        connect();
    }
    if ((resource == null) && (collection == null)) {
        throw new FileNotFoundException(getURL() == null ? "null" : getURL().toString());
    }
    Vector<String> result = new Vector<String>();
    if (collection != null) {
        try {
            NamingEnumeration<NameClassPair> enumeration = collection.list("/");
            UEncoder urlEncoder = new UEncoder(UEncoder.SafeCharsSet.WITH_SLASH);
            while (enumeration.hasMoreElements()) {
                NameClassPair ncp = enumeration.nextElement();
                String s = ncp.getName();
                result.addElement(urlEncoder.encodeURL(s, 0, s.length()).toString());
            }
        } catch (NamingException e) {
            // Unexpected exception
            throw new FileNotFoundException(getURL() == null ? "null" : getURL().toString());
        }
    }
    return result.elements();
}
Also used : NameClassPair(javax.naming.NameClassPair) FileNotFoundException(java.io.FileNotFoundException) UEncoder(org.apache.tomcat.util.buf.UEncoder) NamingException(javax.naming.NamingException) Vector(java.util.Vector)

Example 15 with NameClassPair

use of javax.naming.NameClassPair in project wildfly by wildfly.

the class NamingContextTestCase method checkListWithContinuationsResults.

private void checkListWithContinuationsResults(NamingEnumeration<? extends NameClassPair> results) throws NamingException {
    final Set<String> expected = new HashSet<String>(Arrays.asList("test", "testTwo", "testThree"));
    while (results.hasMore()) {
        NameClassPair result = results.next();
        final String resultName = result.getName();
        if ("test".equals(resultName) || "testTwo".equals(resultName) || "testThree".equals(resultName)) {
            assertEquals(Object.class.getName(), result.getClassName());
        } else {
            fail("Unknown result name: " + resultName);
        }
        expected.remove(resultName);
    }
    assertTrue("Not all expected results were returned", expected.isEmpty());
}
Also used : NameClassPair(javax.naming.NameClassPair) HashSet(java.util.HashSet)

Aggregations

NameClassPair (javax.naming.NameClassPair)59 NamingException (javax.naming.NamingException)27 DirContext (javax.naming.directory.DirContext)19 Test (org.junit.Test)17 HashSet (java.util.HashSet)13 Context (javax.naming.Context)11 InitialContext (javax.naming.InitialContext)10 Hashtable (java.util.Hashtable)9 NamingEnumeration (javax.naming.NamingEnumeration)8 InitialDirContext (javax.naming.directory.InitialDirContext)8 IOException (java.io.IOException)7 FileNotFoundException (java.io.FileNotFoundException)5 HashMap (java.util.HashMap)5 Resource (org.apache.naming.resources.Resource)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 Vector (java.util.Vector)4 CompositeName (javax.naming.CompositeName)4 File (java.io.File)3