Search in sources :

Example 6 with StandardJarScanner

use of org.apache.tomcat.util.scan.StandardJarScanner in project tomcat by apache.

the class JspCServletContext method scanForFragments.

private Map<String, WebXml> scanForFragments(WebXmlParser webXmlParser) throws JasperException {
    StandardJarScanner scanner = new StandardJarScanner();
    // TODO - enabling this means initializing the classloader first in JspC
    scanner.setScanClassPath(false);
    // TODO - configure filter rules from Ant rather then system properties
    scanner.setJarScanFilter(new StandardJarScanFilter());
    FragmentJarScannerCallback callback = new FragmentJarScannerCallback(webXmlParser, false, true);
    scanner.scan(JarScanType.PLUGGABILITY, this, callback);
    if (!callback.isOk()) {
        throw new JasperException(Localizer.getMessage("jspc.error.invalidFragment"));
    }
    return callback.getFragments();
}
Also used : JasperException(org.apache.jasper.JasperException) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) FragmentJarScannerCallback(org.apache.tomcat.util.descriptor.web.FragmentJarScannerCallback) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter)

Example 7 with StandardJarScanner

use of org.apache.tomcat.util.scan.StandardJarScanner in project tomcat70 by apache.

the class TestCompiler method testBug55807.

@Test
public void testBug55807() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-3.0");
    Context context = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    ((StandardJarScanner) context.getJarScanner()).setScanAllDirectories(true);
    tomcat.start();
    ByteChunk res = new ByteChunk();
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    getUrl("http://localhost:" + getPort() + "/test/bug5nnnn/bug55807.jsp", res, headers);
    // Check request completed
    String result = res.toString();
    assertEcho(result, "OK");
    // Check the dependencies count
    Assert.assertTrue(result.contains("<p>DependenciesCount: 1</p>"));
    // Check the right timestamp was used in the dependency
    File tld = new File("test/webapp-3.0/WEB-INF/classes/META-INF/bug55807.tld");
    String expected = "/WEB-INF/classes/META-INF/bug55807.tld : " + tld.lastModified() + "</p>";
    Assert.assertTrue(result.contains(expected));
    // Check content type
    Assert.assertTrue(headers.get("Content-Type").get(0).startsWith("text/html"));
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) ByteChunk(org.apache.tomcat.util.buf.ByteChunk) HashMap(java.util.HashMap) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) List(java.util.List) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Example 8 with StandardJarScanner

use of org.apache.tomcat.util.scan.StandardJarScanner in project tomcat70 by apache.

the class JarScannerFactory method getJarScanner.

/**
 * Obtain the {@link JarScanner} associated with the specified {@link
 * ServletContext}. It is obtained via a context parameter.
 */
public static JarScanner getJarScanner(ServletContext ctxt) {
    JarScanner jarScanner = (JarScanner) ctxt.getAttribute(JarScanner.class.getName());
    if (jarScanner == null) {
        ctxt.log(Localizer.getMessage("jsp.warning.noJarScanner"));
        jarScanner = new StandardJarScanner();
    }
    return jarScanner;
}
Also used : StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) JarScanner(org.apache.tomcat.JarScanner)

Example 9 with StandardJarScanner

use of org.apache.tomcat.util.scan.StandardJarScanner in project tomcat by apache.

the class TomcatBaseTest method skipTldsForResourceJars.

public static void skipTldsForResourceJars(Context context) {
    StandardJarScanner scanner = (StandardJarScanner) context.getJarScanner();
    StandardJarScanFilter filter = (StandardJarScanFilter) scanner.getJarScanFilter();
    filter.setTldSkip(filter.getTldSkip() + ",resources*.jar");
}
Also used : StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter)

Example 10 with StandardJarScanner

use of org.apache.tomcat.util.scan.StandardJarScanner in project tomcat by apache.

the class TestVirtualContext method testVirtualClassLoader.

@Test
public void testVirtualClassLoader() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-virtual-webapp/src/main/webapp-a");
    // app dir is relative to server home
    StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    ctx.setResources(new StandardRoot(ctx));
    File f1 = new File("test/webapp-virtual-webapp/target/classes");
    File f2 = new File("test/webapp-virtual-library/target/WEB-INF");
    File f3 = new File("test/webapp-virtual-webapp/src/main/webapp-a/WEB-INF/classes");
    File f4 = new File("test/webapp-virtual-webapp/src/main/webapp-b/WEB-INF/classes");
    File f5 = new File("test/webapp-virtual-webapp/src/main/misc");
    File f6 = new File("test/webapp-virtual-webapp/src/main/webapp-b");
    ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/classes", f1.getAbsolutePath(), null, "/");
    ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF", f2.getAbsolutePath(), null, "/");
    ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/classes", f3.getAbsolutePath(), null, "/");
    ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/classes", f4.getAbsolutePath(), null, "/");
    ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/other", f5.getAbsolutePath(), null, "/");
    ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/", f6.getAbsolutePath(), null, "/");
    StandardJarScanner jarScanner = new StandardJarScanner();
    jarScanner.setScanAllDirectories(true);
    ctx.setJarScanner(jarScanner);
    ctx.setAddWebinfClassesResources(true);
    tomcat.start();
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=nonexistent", "resourceAInWebInfClasses=true", 404);
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceA.properties", "resourceAInWebInfClasses=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceA.properties", "resourceAInWebInfClasses=true");
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceB.properties", "resourceBInTargetClasses=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceB.properties", "resourceBInTargetClasses=true");
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceC.properties", "resourceCInDependentLibraryTargetClasses=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceC.properties", "resourceCInDependentLibraryTargetClasses=true");
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceD.properties", "resourceDInPackagedJarInWebInfLib=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceD.properties", "resourceDInPackagedJarInWebInfLib=true");
    assertPageContains("/test/classpathGetResourceAsStream.jsp?path=rsrc/resourceG.properties", "resourceGInWebInfClasses=true");
    assertPageContains("/test/classpathGetResourceUrlThenGetStream.jsp?path=rsrc/resourceG.properties", "resourceGInWebInfClasses=true");
    // test listing all possible paths for a classpath resource
    String allUrls = getUrl("http://localhost:" + getPort() + "/test/classpathGetResources.jsp?path=rsrc/").toString();
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-virtual-webapp/src/main/webapp-a/WEB-INF/classes/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-virtual-webapp/src/main/webapp-b/WEB-INF/classes/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-virtual-webapp/src/main/webapp-a/WEB-INF/lib/rsrc.jar!/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-virtual-webapp/target/classes/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-virtual-library/target/WEB-INF/classes/rsrc") > 0);
    // check that there's no duplicate in the URLs
    String[] allUrlsArray = allUrls.split("\\s+");
    Assert.assertEquals(new HashSet<>(Arrays.asList(allUrlsArray)).size(), allUrlsArray.length);
    String allRsrsc2ClasspathUrls = getUrl("http://localhost:" + getPort() + "/test/classpathGetResources.jsp?path=rsrc-2/").toString();
    Assert.assertTrue(allRsrsc2ClasspathUrls, allRsrsc2ClasspathUrls.indexOf("/test/webapp-virtual-webapp/src/main/webapp-b/WEB-INF/classes/rsrc-2") > 0);
    // tests context.getRealPath
    // the following fails because getRealPath always return a non-null path
    // even if there's no such resource
    // assertPageContains("/test/contextGetRealPath.jsp?path=nonexistent",
    // "resourceAInWebInfClasses=true", 404);
    // Real paths depend on the OS and this test has to work on all
    // platforms so use File to convert the path to a platform specific form
    File f = new File("test/webapp-virtual-webapp/src/main/webapp-a/rsrc/resourceF.properties");
    assertPageContains("/test/contextGetRealPath.jsp?path=/rsrc/resourceF.properties", f.getPath());
    // tests context.getResource then the content
    assertPageContains("/test/contextGetResource.jsp?path=/nonexistent", "resourceAInWebInfClasses=true", 404);
    assertPageContains("/test/contextGetResource.jsp?path=/WEB-INF/classes/rsrc/resourceA.properties", "resourceAInWebInfClasses=true");
    assertPageContains("/test/contextGetResource.jsp?path=/WEB-INF/classes/rsrc/resourceG.properties", "resourceGInWebInfClasses=true");
    assertPageContains("/test/contextGetResource.jsp?path=/rsrc/resourceE.properties", "resourceEInDependentLibraryTargetClasses=true");
    assertPageContains("/test/contextGetResource.jsp?path=/other/resourceI.properties", "resourceIInWebapp=true");
    assertPageContains("/test/contextGetResource.jsp?path=/rsrc-2/resourceJ.properties", "resourceJInWebapp=true");
    String allRsrcPaths = getUrl("http://localhost:" + getPort() + "/test/contextGetResourcePaths.jsp?path=/rsrc/").toString();
    Assert.assertTrue(allRsrcPaths, allRsrcPaths.indexOf("/rsrc/resourceF.properties") > 0);
    Assert.assertTrue(allRsrcPaths, allRsrcPaths.indexOf("/rsrc/resourceE.properties") > 0);
    Assert.assertTrue(allRsrcPaths, allRsrcPaths.indexOf("/rsrc/resourceH.properties") > 0);
    // check that there's no duplicate in the URLs
    String[] allRsrcPathsArray = allRsrcPaths.split("\\s+");
    Assert.assertEquals(new HashSet<>(Arrays.asList(allRsrcPathsArray)).size(), allRsrcPathsArray.length);
    String allRsrc2Paths = getUrl("http://localhost:" + getPort() + "/test/contextGetResourcePaths.jsp?path=/rsrc-2/").toString();
    Assert.assertTrue(allRsrc2Paths, allRsrc2Paths.indexOf("/rsrc-2/resourceJ.properties") > 0);
    assertPageContains("/test/testTlds.jsp", "worldA");
    assertPageContains("/test/testTlds.jsp", "worldB");
    assertPageContains("/test/testTlds.jsp", "worldC");
    assertPageContains("/test/testTlds.jsp", "worldD");
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) StandardContext(org.apache.catalina.core.StandardContext) StandardRoot(org.apache.catalina.webresources.StandardRoot) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) File(java.io.File) HashSet(java.util.HashSet) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

StandardJarScanner (org.apache.tomcat.util.scan.StandardJarScanner)11 File (java.io.File)5 Tomcat (org.apache.catalina.startup.Tomcat)4 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)4 StandardJarScanFilter (org.apache.tomcat.util.scan.StandardJarScanFilter)4 Test (org.junit.Test)4 Context (org.apache.catalina.Context)3 StandardContext (org.apache.catalina.core.StandardContext)3 StandardRoot (org.apache.catalina.webresources.StandardRoot)3 JarScanner (org.apache.tomcat.JarScanner)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)2 ServletContext (jakarta.servlet.ServletContext)1 RequestDumperFilter (org.apache.catalina.filters.RequestDumperFilter)1 WebappLoader (org.apache.catalina.loader.WebappLoader)1 ContextConfig (org.apache.catalina.startup.ContextConfig)1 JasperException (org.apache.jasper.JasperException)1 VirtualDirContext (org.apache.naming.resources.VirtualDirContext)1