Search in sources :

Example 1 with WebXml

use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.

the class ContextConfig method getDefaultWebXmlFragment.

private WebXml getDefaultWebXmlFragment() {
    // Host should never be null
    Host host = (Host) context.getParent();
    DefaultWebXmlCacheEntry entry = hostWebXmlCache.get(host);
    InputSource globalWebXml = getGlobalWebXmlSource();
    InputSource hostWebXml = getHostWebXmlSource();
    long globalTimeStamp = 0;
    long hostTimeStamp = 0;
    if (globalWebXml != null) {
        URLConnection uc = null;
        try {
            URL url = new URL(globalWebXml.getSystemId());
            uc = url.openConnection();
            globalTimeStamp = uc.getLastModified();
        } catch (IOException e) {
            globalTimeStamp = -1;
        } finally {
            if (uc != null) {
                try {
                    uc.getInputStream().close();
                } catch (IOException e) {
                    ExceptionUtils.handleThrowable(e);
                    globalTimeStamp = -1;
                }
            }
        }
    }
    if (hostWebXml != null) {
        URLConnection uc = null;
        try {
            URL url = new URL(hostWebXml.getSystemId());
            uc = url.openConnection();
            hostTimeStamp = uc.getLastModified();
        } catch (IOException e) {
            hostTimeStamp = -1;
        } finally {
            if (uc != null) {
                try {
                    uc.getInputStream().close();
                } catch (IOException e) {
                    ExceptionUtils.handleThrowable(e);
                    hostTimeStamp = -1;
                }
            }
        }
    }
    if (entry != null && entry.getGlobalTimeStamp() == globalTimeStamp && entry.getHostTimeStamp() == hostTimeStamp) {
        InputSourceUtil.close(globalWebXml);
        InputSourceUtil.close(hostWebXml);
        return entry.getWebXml();
    }
    // already be held on the host by another thread
    synchronized (host.getPipeline()) {
        entry = hostWebXmlCache.get(host);
        if (entry != null && entry.getGlobalTimeStamp() == globalTimeStamp && entry.getHostTimeStamp() == hostTimeStamp) {
            return entry.getWebXml();
        }
        WebXml webXmlDefaultFragment = createWebXml();
        webXmlDefaultFragment.setOverridable(true);
        // Set to distributable else every app will be prevented from being
        // distributable when the default fragment is merged with the main
        // web.xml
        webXmlDefaultFragment.setDistributable(true);
        // When merging, the default welcome files are only used if the app has
        // not defined any welcomes files.
        webXmlDefaultFragment.setAlwaysAddWelcomeFiles(false);
        // Parse global web.xml if present
        if (globalWebXml == null) {
            // This is unusual enough to log
            log.info(sm.getString("contextConfig.defaultMissing"));
        } else {
            parseWebXml(globalWebXml, webXmlDefaultFragment, false);
        }
        // Parse host level web.xml if present
        // Additive apart from welcome pages
        webXmlDefaultFragment.setReplaceWelcomeFiles(true);
        parseWebXml(hostWebXml, webXmlDefaultFragment, false);
        // Don't update the cache if an error occurs
        if (globalTimeStamp != -1 && hostTimeStamp != -1) {
            entry = new DefaultWebXmlCacheEntry(webXmlDefaultFragment, globalTimeStamp, hostTimeStamp);
            hostWebXmlCache.put(host, entry);
        }
        return webXmlDefaultFragment;
    }
}
Also used : WebXml(org.apache.catalina.deploy.WebXml) InputSource(org.xml.sax.InputSource) Host(org.apache.catalina.Host) StandardHost(org.apache.catalina.core.StandardHost) IOException(java.io.IOException) DirContextURLConnection(org.apache.naming.resources.DirContextURLConnection) JarURLConnection(java.net.JarURLConnection) URLConnection(java.net.URLConnection) URL(java.net.URL)

Example 2 with WebXml

use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.

the class SetOverrideRule method begin.

@Override
public void begin(String namespace, String name, Attributes attributes) throws Exception {
    WebXml webXml = (WebXml) digester.peek(digester.getCount() - 1);
    // If we have a public ID, this is not a 2.4 or later webapp
    boolean havePublicId = (webXml.getPublicId() != null);
    // havePublicId and isServlet24OrLater should be mutually exclusive
    if (havePublicId == isServlet24OrLater) {
        throw new IllegalArgumentException("taglib definition not consistent with specification version");
    }
}
Also used : WebXml(org.apache.catalina.deploy.WebXml)

Example 3 with WebXml

use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.

the class SetOverrideRule method end.

@Override
public void end(String namespace, String name) throws Exception {
    Object[] params = (Object[]) digester.peekParams();
    if (params != null && params.length == 2) {
        WebXml webXml = (WebXml) digester.peek();
        if (postConstruct) {
            if (webXml.getPostConstructMethods().containsKey(params[0])) {
                throw new IllegalArgumentException(WebRuleSet.sm.getString("webRuleSet.postconstruct.duplicate", params[0]));
            }
        } else {
            if (webXml.getPreDestroyMethods().containsKey(params[0])) {
                throw new IllegalArgumentException(WebRuleSet.sm.getString("webRuleSet.predestroy.duplicate", params[0]));
            }
        }
    }
    super.end(namespace, name);
}
Also used : WebXml(org.apache.catalina.deploy.WebXml)

Example 4 with WebXml

use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.

the class TestContextConfigAnnotation method testOverwriteAnnotation.

@Test
public void testOverwriteAnnotation() throws Exception {
    WebXml webxml = new WebXml();
    ServletDef servletDef = new ServletDef();
    servletDef.setServletName("param");
    servletDef.setServletClass("org.apache.catalina.startup.ParamServlet");
    servletDef.addInitParameter("foo", "tomcat");
    servletDef.setDescription("Description");
    servletDef.setDisplayName("DisplayName");
    servletDef.setLargeIcon("LargeIcon");
    servletDef.setSmallIcon("SmallIcon");
    servletDef.setAsyncSupported("true");
    servletDef.setLoadOnStartup("1");
    webxml.addServlet(servletDef);
    webxml.addServletMapping("/param", "param");
    ContextConfig config = new ContextConfig();
    File pFile = paramClassResource("org/apache/catalina/startup/ParamServlet");
    Assert.assertTrue(pFile.exists());
    config.processAnnotationsFile(pFile, webxml, false);
    Assert.assertEquals(servletDef, webxml.getServlets().get("param"));
    Assert.assertEquals("tomcat", servletDef.getParameterMap().get("foo"));
    Assert.assertEquals("param", webxml.getServletMappings().get("/param"));
    // annotation mapping not added s. Servlet Spec 3.0 (Nov 2009)
    // 8.2.3.3.iv page 81
    Assert.assertNull(webxml.getServletMappings().get("/annotation/overwrite"));
    Assert.assertEquals("Description", servletDef.getDescription());
    Assert.assertEquals("DisplayName", servletDef.getDisplayName());
    Assert.assertEquals("LargeIcon", servletDef.getLargeIcon());
    Assert.assertEquals("SmallIcon", servletDef.getSmallIcon());
    Assert.assertEquals(Boolean.TRUE, servletDef.getAsyncSupported());
    Assert.assertEquals(Integer.valueOf(1), servletDef.getLoadOnStartup());
    Assert.assertNull(servletDef.getEnabled());
    Assert.assertNull(servletDef.getJspFile());
}
Also used : WebXml(org.apache.catalina.deploy.WebXml) ServletDef(org.apache.catalina.deploy.ServletDef) File(java.io.File) Test(org.junit.Test)

Example 5 with WebXml

use of org.apache.catalina.deploy.WebXml in project tomcat70 by apache.

the class TestStandardContextResources method testResourcesAbsoluteOrdering.

@Test
public void testResourcesAbsoluteOrdering() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp-3.0-fragments");
    // app dir is relative to server home
    StandardContext ctx = (StandardContext) tomcat.addWebapp(null, "/test", appDir.getAbsolutePath());
    LifecycleListener[] listener = ctx.findLifecycleListeners();
    Assert.assertEquals(3, listener.length);
    Assert.assertTrue(listener[1] instanceof ContextConfig);
    ContextConfig config = new ContextConfig() {

        @Override
        protected WebXml createWebXml() {
            WebXml wxml = new WebXml();
            wxml.addAbsoluteOrdering("resources");
            wxml.addAbsoluteOrdering("resources2");
            return wxml;
        }
    };
    // prevent it from looking ( if it finds one - it'll have dup error )
    config.setDefaultWebXml(Constants.NoDefaultWebXml);
    listener[1] = config;
    Tomcat.addServlet(ctx, "getresource", new GetResourceServlet());
    ctx.addServletMapping("/getresource", "getresource");
    tomcat.start();
    assertPageContains("/test/getresource?path=/resourceF.jsp", "<p>resourceF.jsp in resources2.jar</p>");
    assertPageContains("/test/getresource?path=/resourceB.jsp", "<p>resourceB.jsp in resources.jar</p>");
    // Check ordering, for BZ 54391
    Assert.assertEquals(Arrays.asList("resources.jar", "resources2.jar"), ctx.getServletContext().getAttribute(ServletContext.ORDERED_LIBS));
    ctx.stop();
    LifecycleListener[] listener1 = ctx.findLifecycleListeners();
    // change ordering and reload
    ContextConfig config1 = new ContextConfig() {

        @Override
        protected WebXml createWebXml() {
            WebXml wxml = new WebXml();
            wxml.addAbsoluteOrdering("resources2");
            wxml.addAbsoluteOrdering("resources");
            return wxml;
        }
    };
    // prevent it from looking ( if it finds one - it'll have dup error )
    config1.setDefaultWebXml(Constants.NoDefaultWebXml);
    listener1[1] = config1;
    // Need to init since context won't call init
    config1.lifecycleEvent(new LifecycleEvent(ctx, Lifecycle.AFTER_INIT_EVENT, null));
    Tomcat.addServlet(ctx, "getresource", new GetResourceServlet());
    ctx.addServletMapping("/getresource", "getresource");
    ctx.start();
    assertPageContains("/test/getresource?path=/resourceF.jsp", "<p>resourceF.jsp in resources2.jar</p>");
    assertPageContains("/test/getresource?path=/resourceB.jsp", "<p>resourceB.jsp in resources2.jar</p>");
    // Check ordering, for BZ 54391
    Assert.assertEquals(Arrays.asList("resources2.jar", "resources.jar"), ctx.getServletContext().getAttribute(ServletContext.ORDERED_LIBS));
}
Also used : ContextConfig(org.apache.catalina.startup.ContextConfig) WebXml(org.apache.catalina.deploy.WebXml) Tomcat(org.apache.catalina.startup.Tomcat) LifecycleEvent(org.apache.catalina.LifecycleEvent) LifecycleListener(org.apache.catalina.LifecycleListener) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

WebXml (org.apache.catalina.deploy.WebXml)26 Test (org.junit.Test)20 File (java.io.File)17 ServletDef (org.apache.catalina.deploy.ServletDef)5 WebRuleSet (org.apache.catalina.startup.WebRuleSet)5 XmlErrorHandler (org.apache.tomcat.util.descriptor.XmlErrorHandler)5 Digester (org.apache.tomcat.util.digester.Digester)5 URL (java.net.URL)4 FilterDef (org.apache.catalina.deploy.FilterDef)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 LinkedHashSet (java.util.LinkedHashSet)2 NamingException (javax.naming.NamingException)2 ServletContainerInitializer (javax.servlet.ServletContainerInitializer)2 StandardContext (org.apache.catalina.core.StandardContext)2 FilterMap (org.apache.catalina.deploy.FilterMap)2 FileDirContext (org.apache.naming.resources.FileDirContext)2 InputSource (org.xml.sax.InputSource)2 JarURLConnection (java.net.JarURLConnection)1 MalformedURLException (java.net.MalformedURLException)1