org.apache.catalina.WebResourceSet Java Examples

The following examples show how to use org.apache.catalina.WebResourceSet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public Set<String> listWebAppPaths(String path) {
    path = validate(path);

    // Set because we don't want duplicates
    HashSet<String> result = new HashSet<>();
    for (List<WebResourceSet> list : allResources) {
        for (WebResourceSet webResourceSet : list) {
            if (!webResourceSet.getClassLoaderOnly()) {
                result.addAll(webResourceSet.listWebAppPaths(path));
            }
        }
    }
    if (result.size() == 0) {
        return null;
    }
    return result;
}
 
Example #2
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
protected void initInternal() throws LifecycleException {
    super.initInternal();

    cacheJmxName = register(cache, getObjectNameKeyProperties() + ",name=Cache");

    registerURLStreamHandlerFactory();

    if (context == null) {
        throw new IllegalStateException(
                sm.getString("standardRoot.noContext"));
    }

    for (List<WebResourceSet> list : allResources) {
        for (WebResourceSet webResourceSet : list) {
            webResourceSet.init();
        }
    }
}
 
Example #3
Source File: TestDirResourceSetVirtual.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public WebResourceRoot getWebResourceRoot() {
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    WebResourceSet webResourceSet =
            new DirResourceSet(new TesterWebResourceRoot(), "/",
                    getBaseDir().getAbsolutePath(), "/");
    root.setMainResources(webResourceSet);

    WebResourceSet f1 = new FileResourceSet(root, "/f1.txt",
            dir1.getAbsolutePath() + "/f1.txt", "/");
    root.addPreResources(f1);

    WebResourceSet f2 = new FileResourceSet(root, "/f2.txt",
            dir1.getAbsolutePath() + "/f2.txt", "/");
    root.addPreResources(f2);

    WebResourceSet d1 = new DirResourceSet(root, "/d1",
            dir1.getAbsolutePath() + "/d1", "/");
    root.addPreResources(d1);

    WebResourceSet d2 = new DirResourceSet(root, "/d2",
            dir1.getAbsolutePath() + "/d2", "/");
    root.addPreResources(d2);

    return root;
}
 
Example #4
Source File: TestResourceJars.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testNonStaticResources() {
    File empty = new File("test/webresources/dir3");
    File jar = new File("test/webresources/non-static-resources.jar");

    TesterWebResourceRoot root = new TesterWebResourceRoot();

    // Use empty dir for root of web app.
    WebResourceSet webResourceSet = new DirResourceSet(root, "/", empty.getAbsolutePath(), "/");
    root.setMainResources(webResourceSet);

    // If this JAR was in a web application, this is equivalent to how it
    // would be added
    JarResourceSet test =
            new JarResourceSet(root, "/", jar.getAbsolutePath(), "/META-INF/resources");
    test.setStaticOnly(true);
    root.addJarResources(test);

    WebResource resource = root.getClassLoaderResource("/org/apache/tomcat/unittest/foo.txt");

    Assert.assertFalse(resource.exists());
}
 
Example #5
Source File: ArmeriaWebResourceRoot.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Override
protected WebResourceSet createMainResourceSet() {
    final Path docBase = config.docBase();
    assert docBase.isAbsolute();

    final String docBaseStr = docBase.toString();
    getContext().setDocBase(docBaseStr);

    if (Files.isDirectory(docBase)) {
        return new DirResourceSet(this, "/", docBaseStr, "/");
    }

    final String jarRoot = config.jarRoot();
    if (jarRoot != null) { // If docBase is a JAR file
        if ("/".equals(jarRoot)) {
            return new JarResourceSet(this, "/", docBaseStr, "/");
        } else {
            return new JarSubsetResourceSet(this, "/", docBaseStr, "/", jarRoot);
        }
    }

    throw new IllegalArgumentException(sm.getString("standardRoot.startInvalidMain", docBaseStr));
}
 
Example #6
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
protected WebResource[] getResourcesInternal(String path,
        boolean useClassLoaderResources) {
    List<WebResource> result = new ArrayList<>();
    for (List<WebResourceSet> list : allResources) {
        for (WebResourceSet webResourceSet : list) {
            if (useClassLoaderResources || !webResourceSet.getClassLoaderOnly()) {
                WebResource webResource = webResourceSet.getResource(path);
                if (webResource.exists()) {
                    result.add(webResource);
                }
            }
        }
    }

    if (result.size() == 0) {
        result.add(main.getResource(path));
    }

    return result.toArray(new WebResource[result.size()]);
}
 
Example #7
Source File: TomEEWebappClassLoader.java    From tomee with Apache License 2.0 6 votes vote down vote up
@Override
public void setResources(final WebResourceRoot resources) {
    this.resources = resources;
    if (StandardRoot.class.isInstance(resources)) {
        final List<WebResourceSet> jars = (List<WebResourceSet>) Reflections.get(resources, "jarResources");
        if (jars != null && !jars.isEmpty()) {
            final Iterator<WebResourceSet> jarIt = jars.iterator();
            while (jarIt.hasNext()) {
                final WebResourceSet set = jarIt.next();
                if (set.getBaseUrl() == null) {
                    continue;
                }
                final File file = URLs.toFile(set.getBaseUrl());
                try {
                    if (file.exists() && (!TomEEClassLoaderEnricher.validateJarFile(file) || !jarIsAccepted(file))) {
                        // need to remove this resource
                        LOGGER.warning("Removing " + file.getAbsolutePath() + " since it is offending");
                        jarIt.remove();
                    }
                } catch (final IOException e) {
                    // ignore
                }
            }
        }
    }
}
 
Example #8
Source File: MockWebResourceRoot.java    From joinfaces with Apache License 2.0 5 votes vote down vote up
@Override
public void addJarResources(WebResourceSet webResourceSet) {
	if (this.jarResources == null) {
		this.jarResources = new ArrayList<>();
	}
	this.jarResources.add(webResourceSet);
}
 
Example #9
Source File: JsfTomcatApplicationListener.java    From joinfaces with Apache License 2.0 5 votes vote down vote up
private URL mainFile(WebResourceRoot resources) {
	URL result = null;
	for (WebResourceSet resourceSet :resources.getJarResources()) {
		if (resourceSet instanceof JarWarResourceSet) {
			result = resourceSet.getBaseUrl();
			break;
		}
	}
	return result;
}
 
Example #10
Source File: JsfTomcatApplicationListener.java    From joinfaces with Apache License 2.0 5 votes vote down vote up
private JarWarResourceSet getFirstJarWarResourceSetAtJarResources(WebResourceRoot resources) {
	JarWarResourceSet result = null;
	for (WebResourceSet resourceSet :resources.getJarResources()) {
		if (resourceSet instanceof JarWarResourceSet) {
			result = (JarWarResourceSet) resourceSet;
			break;
		}
	}
	return result;
}
 
Example #11
Source File: LoaderHidingWebResourceSet.java    From spring-graalvm-native with Apache License 2.0 5 votes vote down vote up
protected LoaderHidingWebResourceSet(WebResourceSet delegate) {
	this.delegate = delegate;
	try {
		this.initInternal = LifecycleBase.class.getDeclaredMethod("initInternal");
		this.initInternal.setAccessible(true);
	}
	catch (Exception ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #12
Source File: TestJarResourceSetMount.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public WebResourceRoot getWebResourceRoot() {
    File f = new File("test/webresources/dir1.jar");
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    WebResourceSet webResourceSet =
            new JarResourceSet(root, getMount(), f.getAbsolutePath(), "/");
    root.setMainResources(webResourceSet);
    return root;
}
 
Example #13
Source File: TestDirResourceSetInternal.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public WebResourceRoot getWebResourceRoot() {
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    WebResourceSet webResourceSet =
            new DirResourceSet(root, "/", tempDir.toAbsolutePath().toString(), "/dir1");
    root.setMainResources(webResourceSet);
    return root;
}
 
Example #14
Source File: TestDirResourceSetMount.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public WebResourceRoot getWebResourceRoot() {
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    WebResourceSet webResourceSet =
            new DirResourceSet(new TesterWebResourceRoot(), getMount(),
                    getBaseDir().getAbsolutePath(), "/");
    root.setMainResources(webResourceSet);
    return root;
}
 
Example #15
Source File: TestJarResourceSet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public WebResourceRoot getWebResourceRoot() {
    File f = new File("test/webresources/dir1.jar");
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    WebResourceSet webResourceSet =
            new JarResourceSet(root, "/", f.getAbsolutePath(), "/");
    root.setMainResources(webResourceSet);
    return root;
}
 
Example #16
Source File: AbstractTestFileResourceSet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public WebResourceRoot getWebResourceRoot() {
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    WebResourceSet webResourceSet = new DirResourceSet(root, "/", getBaseDir().getAbsolutePath(), "/");
    webResourceSet.setReadOnly(readOnly);
    root.setMainResources(webResourceSet);

    WebResourceSet f1 = new FileResourceSet(root, "/f1.txt",
            "test/webresources/dir1/f1.txt", "/");
    f1.setReadOnly(readOnly);
    root.addPreResources(f1);

    WebResourceSet f2 = new FileResourceSet(root, "/f2.txt",
            "test/webresources/dir1/f2.txt", "/");
    f2.setReadOnly(readOnly);
    root.addPreResources(f2);

    WebResourceSet d1f1 = new FileResourceSet(root, "/d1/d1-f1.txt",
            "test/webresources/dir1/d1/d1-f1.txt", "/");
    d1f1.setReadOnly(readOnly);
    root.addPreResources(d1f1);

    WebResourceSet d2f1 = new FileResourceSet(root, "/d2/d2-f1.txt",
            "test/webresources/dir1/d2/d2-f1.txt", "/");
    d2f1.setReadOnly(readOnly);
    root.addPreResources(d2f1);

    return root;
}
 
Example #17
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private boolean preResourceExists(String path) {
    for (WebResourceSet webResourceSet : preResources) {
        WebResource webResource = webResourceSet.getResource(path);
        if (webResource.exists()) {
            return true;
        }
    }
    return false;
}
 
Example #18
Source File: TestJarResourceSetInternal.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public WebResourceRoot getWebResourceRoot() {
    File f = new File("test/webresources/dir1-internal.jar");
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    WebResourceSet webResourceSet =
            new JarResourceSet(root, "/", f.getAbsolutePath(), "/dir1");
    root.setMainResources(webResourceSet);
    return root;
}
 
Example #19
Source File: TestDirResourceSet.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public WebResourceRoot getWebResourceRoot() {
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    WebResourceSet webResourceSet =
            new DirResourceSet(root, "/", getBaseDir().getAbsolutePath(), "/");
    webResourceSet.setReadOnly(false);
    root.setMainResources(webResourceSet);
    return root;
}
 
Example #20
Source File: TestDirResourceSetReadOnly.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public WebResourceRoot getWebResourceRoot() {
    TesterWebResourceRoot root = new TesterWebResourceRoot();
    WebResourceSet webResourceSet =
            new DirResourceSet(root, "/", getBaseDir().getAbsolutePath(), "/");
    webResourceSet.setReadOnly(true);
    root.setMainResources(webResourceSet);
    return root;
}
 
Example #21
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected void destroyInternal() throws LifecycleException {
    for (List<WebResourceSet> list : allResources) {
        for (WebResourceSet webResourceSet : list) {
            webResourceSet.destroy();
        }
    }

    unregister(cacheJmxName);

    super.destroyInternal();
}
 
Example #22
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
protected void startInternal() throws LifecycleException {
    mainResources.clear();

    main = createMainResourceSet();

    mainResources.add(main);

    for (List<WebResourceSet> list : allResources) {
        // Skip class resources since they are started below
        if (list != classResources) {
            for (WebResourceSet webResourceSet : list) {
                webResourceSet.start();
            }
        }
    }

    // This has to be called after the other resources have been started
    // else it won't find all the matching resources
    processWebInfLib();
    // Need to start the newly found resources
    for (WebResourceSet classResource : classResources) {
        classResource.start();
    }

    cache.enforceObjectMaxSizeLimit();

    setState(LifecycleState.STARTING);
}
 
Example #23
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public List<URL> getBaseUrls() {
    List<URL> result = new ArrayList<>();
    for (List<WebResourceSet> list : allResources) {
        for (WebResourceSet webResourceSet : list) {
            if (!webResourceSet.getClassLoaderOnly()) {
                URL url = webResourceSet.getBaseUrl();
                if (url != null) {
                    result.add(url);
                }
            }
        }
    }
    return result;
}
 
Example #24
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void gc() {
    for (List<WebResourceSet> list : allResources) {
        for (WebResourceSet webResourceSet : list) {
            webResourceSet.gc();
        }
    }
}
 
Example #25
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
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;
}
 
Example #26
Source File: StandardRoot.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Override
public void createWebResourceSet(ResourceSetType type, String webAppMount,
        String base, String archivePath, String internalPath) {
    List<WebResourceSet> resourceList;
    WebResourceSet resourceSet;

    switch (type) {
        case PRE:
            resourceList = preResources;
            break;
        case CLASSES_JAR:
            resourceList = classResources;
            break;
        case RESOURCE_JAR:
            resourceList = jarResources;
            break;
        case POST:
            resourceList = postResources;
            break;
        default:
            throw new IllegalArgumentException(
                    sm.getString("standardRoot.createUnknownType", type));
    }

    // This implementation assumes that the base for all resources will be a
    // file.
    File file = new File(base);

    if (file.isFile()) {
        if (archivePath != null) {
            // Must be a JAR nested inside a WAR if archivePath is non-null
            resourceSet = new JarWarResourceSet(this, webAppMount, base,
                    archivePath, internalPath);
        } else if (file.getName().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
            resourceSet = new JarResourceSet(this, webAppMount, base,
                    internalPath);
        } else {
            resourceSet = new FileResourceSet(this, webAppMount, base,
                    internalPath);
        }
    } else if (file.isDirectory()) {
        resourceSet =
                new DirResourceSet(this, webAppMount, base, internalPath);
    } else {
        throw new IllegalArgumentException(
                sm.getString("standardRoot.createInvalidFile", file));
    }

    if (type.equals(ResourceSetType.CLASSES_JAR)) {
        resourceSet.setClassLoaderOnly(true);
    } else if (type.equals(ResourceSetType.RESOURCE_JAR)) {
        resourceSet.setStaticOnly(true);
    }

    resourceList.add(resourceSet);
}
 
Example #27
Source File: TomcatWebAppBuilder.java    From tomee with Apache License 2.0 4 votes vote down vote up
private static DeploymentLoader.ExternalConfiguration configuredClasspath(final StandardContext standardContext) {
    Loader loader = standardContext.getLoader();
    if (loader != null && LazyStopLoader.class.isInstance(loader)) {
        loader = LazyStopLoader.class.cast(loader).getDelegateLoader();
    }
    if (loader != null) {
        final ClassLoader cl = standardContext.getLoader().getClassLoader();
        if (cl == null) {
            return null;
        }

        final Collection<String> cp = new LinkedList<>();

        final WebResourceRoot webResources = standardContext.getResources();
        if (webResources != null) { // to enhance
            for (final WebResourceSet[] sets : asList(webResources.getPreResources(), webResources.getPostResources(), webResources.getJarResources())) {
                for (final WebResourceSet wr : sets) {
                    final URL base = wr.getBaseUrl();
                    if (base != null) {
                        final File baseFile = URLs.toFile(base);
                        if (baseFile.isDirectory()) {
                            final String[] libs = wr.list("/WEB-INF/lib/");
                            if (libs != null) {
                                for (final String resource : libs) {
                                    cp.add(new File(baseFile, resource).getAbsolutePath());
                                }
                            }

                            final WebResource classes = wr.getResource("/WEB-INF/classes/");
                            if (classes != null) {
                                final String path = classes.getCanonicalPath();
                                if (path != null) {
                                    cp.add(path);
                                }
                            }
                        } else if (baseFile.exists() && baseFile.getName().endsWith(".jar") && wr.getResource("/WEB-INF/classes/").exists()) {
                            try {
                                cp.add(baseFile.getCanonicalPath());
                            } catch (final IOException e) {
                                throw new IllegalStateException(e);
                            }
                        }
                    }
                }
            }
        }

        if (!cp.isEmpty()) {
            return new DeploymentLoader.ExternalConfiguration(cp.toArray(new String[cp.size()]), null /*for now doesnt make sense, todo: configure*/);
        }
    }
    return null;
}
 
Example #28
Source File: OrderedWebResourceRoot.java    From tomcat-classloader-ordered with Apache License 2.0 4 votes vote down vote up
@Override
public void addPreResources(WebResourceSet webResourceSet) {
	delegate.addPreResources(webResourceSet);
}
 
Example #29
Source File: OrderedWebResourceRoot.java    From tomcat-classloader-ordered with Apache License 2.0 4 votes vote down vote up
@Override
public WebResourceSet[] getPreResources() {
	return delegate.getPreResources();
}
 
Example #30
Source File: OrderedWebResourceRoot.java    From tomcat-classloader-ordered with Apache License 2.0 4 votes vote down vote up
@Override
public void addJarResources(WebResourceSet webResourceSet) {
	delegate.addJarResources(webResourceSet);
}