Search in sources :

Example 46 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class ResolverImpl method resolveResource.

@Override
public boolean resolveResource(int type, String name) {
    WebResourceRoot resources = request.getContext().getResources();
    WebResource resource = resources.getResource(name);
    if (!resource.exists()) {
        return false;
    } else {
        switch(type) {
            case 0:
                return resource.isDirectory();
            case 1:
                return resource.isFile();
            case 2:
                return resource.isFile() && resource.getContentLength() > 0;
            default:
                return false;
        }
    }
}
Also used : WebResource(org.apache.catalina.WebResource) WebResourceRoot(org.apache.catalina.WebResourceRoot)

Example 47 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class ExtractingRoot method processWebInfLib.

@Override
protected void processWebInfLib() throws LifecycleException {
    // packed WAR file.
    if (!super.isPackedWarFile()) {
        super.processWebInfLib();
        return;
    }
    File expansionTarget = getExpansionTarget();
    if (!expansionTarget.isDirectory()) {
        if (!expansionTarget.mkdirs()) {
            throw new LifecycleException(sm.getString("extractingRoot.targetFailed", expansionTarget));
        }
    }
    WebResource[] possibleJars = listResources("/WEB-INF/lib", false);
    for (WebResource possibleJar : possibleJars) {
        if (possibleJar.isFile() && possibleJar.getName().endsWith(".jar")) {
            try {
                File dest = new File(expansionTarget, possibleJar.getName());
                dest = dest.getCanonicalFile();
                try (InputStream sourceStream = possibleJar.getInputStream();
                    OutputStream destStream = new FileOutputStream(dest)) {
                    IOTools.flow(sourceStream, destStream);
                }
                createWebResourceSet(ResourceSetType.CLASSES_JAR, "/WEB-INF/classes", dest.toURI().toURL(), "/");
            } catch (IOException ioe) {
                throw new LifecycleException(sm.getString("extractingRoot.jarFailed", possibleJar.getName()), ioe);
            }
        }
    }
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) WebResource(org.apache.catalina.WebResource) IOException(java.io.IOException) File(java.io.File)

Example 48 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class StandardRoot method getResourceInternal.

protected final WebResource getResourceInternal(String path, boolean useClassLoaderResources) {
    WebResource result = null;
    WebResource virtual = null;
    WebResource mainEmpty = null;
    for (List<WebResourceSet> list : allResources) {
        for (WebResourceSet webResourceSet : list) {
            if (!useClassLoaderResources && !webResourceSet.getClassLoaderOnly() || useClassLoaderResources && !webResourceSet.getStaticOnly()) {
                result = webResourceSet.getResource(path);
                if (result.exists()) {
                    return result;
                }
                if (virtual == null) {
                    if (result.isVirtual()) {
                        virtual = result;
                    } else if (main.equals(webResourceSet)) {
                        mainEmpty = result;
                    }
                }
            }
        }
    }
    // Use the first virtual result if no real result was found
    if (virtual != null) {
        return virtual;
    }
    // Default is empty resource in main resources
    return mainEmpty;
}
Also used : WebResourceSet(org.apache.catalina.WebResourceSet) TrackedWebResource(org.apache.catalina.TrackedWebResource) WebResource(org.apache.catalina.WebResource)

Example 49 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class AbstractTestResourceSet method testGetCanonicalPathExists.

// ------------------------------------------------------ getCanonicalPath()
@Test
public final void testGetCanonicalPathExists() {
    WebResource exists = resourceRoot.getResource(getMount() + "/d1/d1-f1.txt");
    String existsCanonicalPath = exists.getCanonicalPath();
    URL existsUrl = exists.getURL();
    if ("file".equals(existsUrl.getProtocol())) {
        // Should have a canonical path
        Assert.assertNotNull(existsCanonicalPath);
    } else {
        Assert.assertNull(existsCanonicalPath);
    }
}
Also used : WebResource(org.apache.catalina.WebResource) URL(java.net.URL) Test(org.junit.Test)

Example 50 with WebResource

use of org.apache.catalina.WebResource in project tomcat by apache.

the class AbstractTestResourceSet method testGetResourceTraversal.

@Test
public final void testGetResourceTraversal() {
    WebResource webResource = null;
    try {
        webResource = resourceRoot.getResource(getMount() + "/../");
    } catch (IllegalArgumentException iae) {
        // Expected if mount point is zero length
        Assert.assertTrue(getMount().length() == 0);
        return;
    }
    Assert.assertFalse(webResource.exists());
}
Also used : WebResource(org.apache.catalina.WebResource) Test(org.junit.Test)

Aggregations

WebResource (org.apache.catalina.WebResource)57 Test (org.junit.Test)17 File (java.io.File)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InputStream (java.io.InputStream)11 IOException (java.io.IOException)8 URL (java.net.URL)7 BufferedInputStream (java.io.BufferedInputStream)6 InputSource (org.xml.sax.InputSource)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 FileInputStream (java.io.FileInputStream)5 OutputStreamWriter (java.io.OutputStreamWriter)5 PrintWriter (java.io.PrintWriter)4 RandomAccessFile (java.io.RandomAccessFile)4 Manifest (java.util.jar.Manifest)4 StreamSource (javax.xml.transform.stream.StreamSource)3 WebResourceRoot (org.apache.catalina.WebResourceRoot)3 WebResourceSet (org.apache.catalina.WebResourceSet)3 StringReader (java.io.StringReader)2 StringWriter (java.io.StringWriter)2