Search in sources :

Example 1 with StandardJarScanner

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

the class CatalinaContainer method prepareContext.

private StandardContext prepareContext(Host host, ContainerConfig.Configuration configuration, ComponentConfig.WebappInfo appInfo, Property clusterProp) throws ContainerException {
    StandardContext context = new StandardContext();
    Tomcat.initWebappDefaults(context);
    String location = getWebappRootLocation(appInfo);
    boolean contextIsDistributable = isContextDistributable(configuration, location);
    context.setParent(host);
    context.setDocBase(location);
    context.setPath(getWebappMountPoint(appInfo));
    context.addLifecycleListener(new ContextConfig());
    context.setJ2EEApplication("OFBiz");
    context.setJ2EEServer("OFBiz Container");
    context.setLoader(new WebappLoader(Thread.currentThread().getContextClassLoader()));
    context.setDisplayName(appInfo.name);
    context.setDocBase(location);
    context.setReloadable(ContainerConfig.getPropertyValue(configuration, "apps-context-reloadable", false));
    context.setDistributable(contextIsDistributable);
    context.setCrossContext(ContainerConfig.getPropertyValue(configuration, "apps-cross-context", true));
    context.setPrivileged(appInfo.privileged);
    context.getServletContext().setAttribute("_serverId", appInfo.server);
    context.getServletContext().setAttribute("componentName", appInfo.componentConfig.getComponentName());
    if (clusterProp != null && contextIsDistributable) {
        context.setManager(prepareClusterManager(clusterProp));
    }
    StandardRoot resources = new StandardRoot(context);
    resources.setAllowLinking(true);
    context.setResources(resources);
    JarScanner jarScanner = context.getJarScanner();
    if (jarScanner instanceof StandardJarScanner) {
        StandardJarScanner standardJarScanner = (StandardJarScanner) jarScanner;
        standardJarScanner.setJarScanFilter(new FilterJars());
        standardJarScanner.setScanClassPath(true);
    }
    Map<String, String> initParameters = appInfo.getInitParameters();
    // request dumper filter
    if ("true".equals(initParameters.get("enableRequestDump"))) {
        FilterDef requestDumperFilterDef = new FilterDef();
        requestDumperFilterDef.setFilterClass(RequestDumperFilter.class.getName());
        requestDumperFilterDef.setFilterName("RequestDumper");
        context.addFilterDef(requestDumperFilterDef);
        FilterMap requestDumperFilterMap = new FilterMap();
        requestDumperFilterMap.setFilterName("RequestDumper");
        requestDumperFilterMap.addURLPattern("*");
        context.addFilterMap(requestDumperFilterMap);
    }
    // set the init parameters
    initParameters.entrySet().forEach(entry -> context.addParameter(entry.getKey(), entry.getValue()));
    return context;
}
Also used : FilterDef(org.apache.tomcat.util.descriptor.web.FilterDef) RequestDumperFilter(org.apache.catalina.filters.RequestDumperFilter) StandardRoot(org.apache.catalina.webresources.StandardRoot) JarScanner(org.apache.tomcat.JarScanner) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) ContextConfig(org.apache.catalina.startup.ContextConfig) StandardContext(org.apache.catalina.core.StandardContext) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) WebappLoader(org.apache.catalina.loader.WebappLoader)

Example 2 with StandardJarScanner

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

the class TestVirtualContext method testVirtualClassLoader.

@Test
public void testVirtualClassLoader() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-3.0-virtual-webapp/src/main/webapp");
    // app dir is relative to server home
    StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    VirtualWebappLoader loader = new VirtualWebappLoader(ctx.getParentClassLoader());
    // 
    loader.setVirtualClasspath(// 
    "test/webapp-3.0-virtual-webapp/target/classes;" + // 
    "test/webapp-3.0-virtual-library/target/classes;" + // 
    "test/webapp-3.0-virtual-webapp/src/main/webapp/WEB-INF/classes;" + "test/webapp-3.0-virtual-webapp/src/main/webapp2/WEB-INF/classes");
    ctx.setLoader(loader);
    String extraMappings = // 
    "/=test/webapp-3.0-virtual-webapp/src/main/webapp2" + // 
    ",/other=test/webapp-3.0-virtual-webapp/src/main/misc" + ",/WEB-INF/classes=test/webapp-3.0-virtual-webapp/target/classes";
    VirtualDirContext resources = new VirtualDirContext();
    resources.setExtraResourcePaths(extraMappings);
    ctx.setResources(resources);
    StandardJarScanner jarScanner = new StandardJarScanner();
    jarScanner.setScanAllDirectories(true);
    ctx.setJarScanner(jarScanner);
    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-3.0-virtual-webapp/src/main/webapp/WEB-INF/classes/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-3.0-virtual-webapp/src/main/webapp2/WEB-INF/classes/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-3.0-virtual-webapp/src/main/webapp/WEB-INF/lib/rsrc.jar!/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-3.0-virtual-webapp/target/classes/rsrc") > 0);
    Assert.assertTrue(allUrls, allUrls.indexOf("/test/webapp-3.0-virtual-library/target/classes/rsrc") > 0);
    // check that there's no duplicate in the URLs
    String[] allUrlsArray = allUrls.split("\\s+");
    Assert.assertEquals(new HashSet<String>(Arrays.asList(allUrlsArray)).size(), allUrlsArray.length);
    String allRsrsc2ClasspathUrls = getUrl("http://localhost:" + getPort() + "/test/classpathGetResources.jsp?path=rsrc2/").toString();
    Assert.assertTrue(allRsrsc2ClasspathUrls, allRsrsc2ClasspathUrls.indexOf("/test/webapp-3.0-virtual-webapp/src/main/webapp2/WEB-INF/classes/rsrc2") > 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-3.0-virtual-webapp/src/main/webapp/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=/rsrc2/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<String>(Arrays.asList(allRsrcPathsArray)).size(), allRsrcPathsArray.length);
    String allRsrc2Paths = getUrl("http://localhost:" + getPort() + "/test/contextGetResourcePaths.jsp?path=/rsrc2/").toString();
    Assert.assertTrue(allRsrc2Paths, allRsrc2Paths.indexOf("/rsrc2/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) VirtualDirContext(org.apache.naming.resources.VirtualDirContext) StandardContext(org.apache.catalina.core.StandardContext) 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)

Example 3 with StandardJarScanner

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

the class JarScannerFactory method getJarScanner.

/**
 * Obtain the {@link JarScanner} associated with the specified {@link
 * ServletContext}. It is obtained via a context parameter.
 * @param ctxt The Servlet context
 * @return a scanner instance
 */
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 4 with StandardJarScanner

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

the class TomcatBaseTest method getTomcatInstanceTestWebapp.

/**
 * Make the Tomcat instance preconfigured with test/webapp available to
 * sub-classes.
 * @param addJstl Should JSTL support be added to the test webapp
 * @param start   Should the Tomcat instance be started
 *
 * @return A Tomcat instance pre-configured with the web application located
 *         at test/webapp
 *
 * @throws LifecycleException If a problem occurs while starting the
 *                            instance
 */
public Tomcat getTomcatInstanceTestWebapp(boolean addJstl, boolean start) throws LifecycleException {
    File appDir = new File("test/webapp");
    Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    StandardJarScanner scanner = (StandardJarScanner) ctx.getJarScanner();
    StandardJarScanFilter filter = (StandardJarScanFilter) scanner.getJarScanFilter();
    filter.setTldSkip(filter.getTldSkip() + ",testclasses");
    filter.setPluggabilitySkip(filter.getPluggabilitySkip() + ",testclasses");
    if (addJstl) {
        File lib = new File("webapps/examples/WEB-INF/lib");
        ctx.setResources(new StandardRoot(ctx));
        ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", lib.getAbsolutePath(), null, "/");
    }
    if (start) {
        tomcat.start();
    }
    return tomcat;
}
Also used : Context(org.apache.catalina.Context) ServletContext(jakarta.servlet.ServletContext) StandardRoot(org.apache.catalina.webresources.StandardRoot) StandardJarScanner(org.apache.tomcat.util.scan.StandardJarScanner) File(java.io.File) StandardJarScanFilter(org.apache.tomcat.util.scan.StandardJarScanFilter)

Example 5 with StandardJarScanner

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

the class TestTldScanner method testBug55807.

@Test
public void testBug55807() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp");
    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<>();
    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/WEB-INF/classes/META-INF/bug55807.tld");
    String expected = "<p>/WEB-INF/classes/META-INF/bug55807.tld : " + tld.lastModified() + "</p>";
    Assert.assertTrue(result.contains(expected));
    // Check content type
    String contentType = getSingleHeader("Content-Type", headers);
    Assert.assertTrue(contentType.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)

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