use of org.apache.catalina.webresources.StandardRoot in project nutzboot by nutzam.
the class TomcatStarter method prepareContext.
private void prepareContext() {
File docBase = Files.findFile(getStaticPath());
docBase = (docBase != null && docBase.isDirectory()) ? docBase : createTempDir("tomcat-docbase");
this.tomcatContext = new StandardContext();
this.tomcatContext.setDocBase(docBase.getAbsolutePath());
this.tomcatContext.setName(getContextPath());
this.tomcatContext.setPath(getContextPath());
this.tomcatContext.setDelegate(false);
this.tomcatContext.addLifecycleListener(new Tomcat.FixContextListener());
this.tomcatContext.setParentClassLoader(classLoader);
this.tomcatContext.setSessionTimeout(getSessionTimeout() / 60);
this.tomcatContext.addLifecycleListener(new StoreMergedWebXmlListener());
StandardRoot sr = new StandardRoot(this.tomcatContext);
sr.addPreResources(new ClasspathResourceSet(sr, "static/"));
this.tomcatContext.setResources(sr);
try {
this.tomcatContext.setUseRelativeRedirects(false);
} catch (NoSuchMethodError ex) {
// Tomcat is < 8.0.30. Continue
}
for (String welcomeFile : Arrays.asList("index.html", "index.htm", "index.jsp", "index.do")) {
this.tomcatContext.addWelcomeFile(welcomeFile);
}
// 注册defaultServlet
addDefaultServlet();
addNutzSupport();
this.tomcat.getHost().addChild(tomcatContext);
}
use of org.apache.catalina.webresources.StandardRoot in project tomcat by apache.
the class TestVirtualWebappLoader method testLoaderInstance.
@Test
public void testLoaderInstance() throws Exception {
WebappLoader loader = new WebappLoader();
Assert.assertNull(loader.getClassLoader());
WebappClassLoader cl = new WebappClassLoader();
loader.setLoaderInstance(cl);
Assert.assertSame(cl, loader.getClassLoader());
Assert.assertEquals(WebappClassLoader.class.getName(), loader.getLoaderClass());
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp");
StandardContext ctx = (StandardContext) tomcat.addContext("", appDir.getAbsolutePath());
loader.setContext(ctx);
ctx.setLoader(loader);
ctx.setResources(new StandardRoot(ctx));
ctx.resourcesStart();
loader.start();
Assert.assertSame(cl, loader.getClassLoader());
Assert.assertEquals(WebappClassLoader.class.getName(), loader.getLoaderClass());
loader.stop();
Assert.assertNull(loader.getClassLoader());
Assert.assertEquals(WebappClassLoader.class.getName(), loader.getLoaderClass());
}
use of org.apache.catalina.webresources.StandardRoot in project tomcat by apache.
the class AbstractTestTag method setUp.
@Override
@Before
public void setUp() throws Exception {
super.setUp();
Tomcat tomcat = getTomcatInstance();
File appDir = new File("test/webapp");
Context ctx = tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
ctx.setResources(new StandardRoot(ctx));
// Add the JSTL (we need the TLD)
File lib = new File("webapps/examples/WEB-INF/lib");
ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", lib.getAbsolutePath(), null, "/");
// Configure the use of the plug-in rather than the standard impl
File plugin = new File("java/org/apache/jasper/tagplugins/jstl/tagPlugins.xml");
Assert.assertTrue(plugin.isFile());
ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/tagPlugins.xml", plugin.getAbsolutePath(), null, "/");
tomcat.start();
}
use of org.apache.catalina.webresources.StandardRoot 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");
}
use of org.apache.catalina.webresources.StandardRoot in project tomcat by apache.
the class TestVirtualContext method testAdditionalWebInfClassesPaths.
@Test
public void testAdditionalWebInfClassesPaths() 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());
File tempFile = File.createTempFile("virtualWebInfClasses", null);
File additionWebInfClasses = new File(tempFile.getAbsolutePath() + ".dir");
Assert.assertTrue(additionWebInfClasses.mkdirs());
File targetPackageForAnnotatedClass = new File(additionWebInfClasses, MyAnnotatedServlet.class.getPackage().getName().replace('.', '/'));
Assert.assertTrue(targetPackageForAnnotatedClass.mkdirs());
try (InputStream annotatedServletClassInputStream = this.getClass().getResourceAsStream(MyAnnotatedServlet.class.getSimpleName() + ".class");
FileOutputStream annotatedServletClassOutputStream = new FileOutputStream(new File(targetPackageForAnnotatedClass, MyAnnotatedServlet.class.getSimpleName() + ".class"))) {
IOUtils.copy(annotatedServletClassInputStream, annotatedServletClassOutputStream);
}
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/classes");
ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/classes", f1.getAbsolutePath(), null, "/");
ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/classes", f2.getAbsolutePath(), null, "/");
tomcat.start();
// first test that without the setting on StandardContext the annotated
// servlet is not detected
assertPageContains("/test/annotatedServlet", MyAnnotatedServlet.MESSAGE, 404);
tomcat.stop();
// then test that if we configure StandardContext with the additional
// path, the servlet is detected
ctx.setResources(new StandardRoot(ctx));
ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/classes", f1.getAbsolutePath(), null, "/");
ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/classes", f2.getAbsolutePath(), null, "/");
ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/classes", additionWebInfClasses.getAbsolutePath(), null, "/");
tomcat.start();
assertPageContains("/test/annotatedServlet", MyAnnotatedServlet.MESSAGE);
tomcat.stop();
FileUtils.deleteDirectory(additionWebInfClasses);
Assert.assertTrue("Failed to clean up [" + tempFile + "]", tempFile.delete());
}
Aggregations