Skip to content

Commit

Permalink
Jetty 12.1.x jetty ee web xml (#11746)
Browse files Browse the repository at this point in the history
* Resolve jetty-eeX-web.xml before jetty-web.xml
  • Loading branch information
janbartel committed May 4, 2024
1 parent 74ac01f commit 4bbffa1
Show file tree
Hide file tree
Showing 15 changed files with 561 additions and 87 deletions.
Expand Up @@ -33,6 +33,7 @@ public class JettyWebXmlConfiguration extends AbstractConfiguration
public static final String PROPERTY_WEB_INF = "web-inf";
public static final String XML_CONFIGURATION = "org.eclipse.jetty.webapp.JettyWebXmlConfiguration";
public static final String JETTY_WEB_XML = "jetty-web.xml";
public static final String JETTY_EE10_WEB_XML = "jetty-ee10-web.xml";

public JettyWebXmlConfiguration()
{
Expand All @@ -53,44 +54,71 @@ public void configure(WebAppContext context) throws Exception
LOG.debug("Configuring web-jetty.xml");

Resource webInf = context.getWebInf();
// handle any WEB-INF descriptors
if (webInf != null && webInf.isDirectory())
// get the jetty-ee10-web.xml or jetty-web.xml
Resource jetty = resolveJettyWebXml(webInf);
if (Resources.isReadableFile(jetty))
{
// Attempt to load ancient jetty8-web.xml file
Resource jetty = webInf.resolve("jetty8-web.xml");
if (Resources.missing(jetty))
jetty = webInf.resolve(JETTY_WEB_XML);
if (Resources.missing(jetty))
jetty = webInf.resolve("web-jetty.xml");

if (Resources.isReadableFile(jetty))
{
if (LOG.isDebugEnabled())
LOG.debug("Configure: {}", jetty);
if (LOG.isDebugEnabled())
LOG.debug("Configure: {}", jetty);

Object xmlAttr = context.getAttribute(XML_CONFIGURATION);
context.removeAttribute(XML_CONFIGURATION);
final XmlConfiguration jetty_config = xmlAttr instanceof XmlConfiguration ? (XmlConfiguration)xmlAttr : new XmlConfiguration(jetty);
Object xmlAttr = context.getAttribute(XML_CONFIGURATION);
context.removeAttribute(XML_CONFIGURATION);
final XmlConfiguration jetty_config = xmlAttr instanceof XmlConfiguration ? (XmlConfiguration)xmlAttr : new XmlConfiguration(jetty);

setupXmlConfiguration(context, jetty_config, webInf);
setupXmlConfiguration(context, jetty_config, webInf);

try
{
WebAppClassLoader.runWithServerClassAccess(() ->
{
jetty_config.configure(context);
return null;
});
}
catch (Exception e)
try
{
WebAppClassLoader.runWithServerClassAccess(() ->
{
LOG.warn("Error applying {}", jetty);
throw e;
}
jetty_config.configure(context);
return null;
});
}
catch (Exception e)
{
LOG.warn("Error applying {}", jetty);
throw e;
}
}
}

/**
* Obtain a WEB-INF/jetty-ee9-web.xml, falling back to
* looking for WEB-INF/jetty-web.xml.
*
* @param webInf the WEB-INF of the context to search
* @return the file if it exists or null otherwise
*/
private Resource resolveJettyWebXml(Resource webInf)
{
String xmlFile = JETTY_EE10_WEB_XML;
try
{
if (webInf == null || !webInf.isDirectory())
return null;

//try to find jetty-ee10-web.xml
Resource jetty = webInf.resolve(xmlFile);
if (!Resources.missing(jetty))
return jetty;

xmlFile = JETTY_WEB_XML;
//failing that, look for jetty-web.xml
jetty = webInf.resolve(xmlFile);
if (!Resources.missing(jetty))
return jetty;

return null;
}
catch (Exception e)
{
if (LOG.isDebugEnabled())
LOG.debug("Error resolving WEB-INF/" + xmlFile, e);
return null;
}
}

/**
* Configures some well-known properties before the XmlConfiguration reads
* the configuration.
Expand Down
@@ -0,0 +1,73 @@
//
// ========================================================================
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//

package org.eclipse.jetty.ee10.webapp;

import java.nio.file.Files;
import java.nio.file.Path;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class JettyWebXmlConfigurationTest
{
Server _server;

@BeforeEach
public void setUp()
{
_server = new Server();
}

@AfterEach
public void tearDown() throws Exception
{
if (_server != null)
_server.stop();
}

@Test
public void testWithOnlyJettyWebXml() throws Exception
{
Path testWebappDir = MavenTestingUtils.getTargetPath("test-classes/webapp-with-jetty-web-xml");
assertTrue(Files.exists(testWebappDir));

WebAppContext context = new WebAppContext();
context.setContextPath("/banana");
_server.setHandler(context);
context.setWar(testWebappDir.toFile().getAbsolutePath());
_server.start();
assertThat(context.getContextPath(), is("/orange"));
}

@Test
public void testWithJettyEEWebXml() throws Exception
{
Path testWebappDir = MavenTestingUtils.getTargetPath("test-classes/webapp-with-jetty-ee10-web-xml");
assertTrue(Files.exists(testWebappDir));

WebAppContext context = new WebAppContext();
context.setContextPath("/banana");
_server.setHandler(context);
context.setWar(testWebappDir.toFile().getAbsolutePath());
_server.start();
assertThat(context.getContextPath(), is("/raspberry"));
}
}
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- // -->
<!-- // ======================================================================== -->
<!-- // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. -->
<!-- // -->
<!-- // This program and the accompanying materials are made available under the -->
<!-- // terms of the Eclipse Public License v. 2.0 which is available at -->
<!-- // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -->
<!-- // which is available at https://www.apache.org/licenses/LICENSE-2.0. -->
<!-- // -->
<!-- // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -->
<!-- // ======================================================================== -->
<!-- // -->

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.ee10.webapp.WebAppContext">
<Set name="contextPath">/raspberry</Set>
</Configure>

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- // -->
<!-- // ======================================================================== -->
<!-- // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. -->
<!-- // -->
<!-- // This program and the accompanying materials are made available under the -->
<!-- // terms of the Eclipse Public License v. 2.0 which is available at -->
<!-- // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -->
<!-- // which is available at https://www.apache.org/licenses/LICENSE-2.0. -->
<!-- // -->
<!-- // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -->
<!-- // ======================================================================== -->
<!-- // -->

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.ee10.webapp.WebAppContext">
<Set name="contextPath">/apple</Set>
</Configure>

@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- // -->
<!-- // ======================================================================== -->
<!-- // Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. -->
<!-- // -->
<!-- // This program and the accompanying materials are made available under the -->
<!-- // terms of the Eclipse Public License v. 2.0 which is available at -->
<!-- // https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 -->
<!-- // which is available at https://www.apache.org/licenses/LICENSE-2.0. -->
<!-- // -->
<!-- // SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 -->
<!-- // ======================================================================== -->
<!-- // -->

<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://eclipse.dev/jetty/configure_10_0.dtd">
<Configure class="org.eclipse.jetty.ee10.webapp.WebAppContext">
<Set name="contextPath">/orange</Set>
</Configure>

Expand Up @@ -33,6 +33,7 @@ public class JettyWebXmlConfiguration extends AbstractConfiguration
public static final String PROPERTY_WEB_INF = "web-inf";
public static final String XML_CONFIGURATION = "org.eclipse.jetty.webapp.JettyWebXmlConfiguration";
public static final String JETTY_WEB_XML = "jetty-web.xml";
public static final String JETTY_EE11_WEB_XML = "jetty-ee11-web.xml";

public JettyWebXmlConfiguration()
{
Expand All @@ -53,44 +54,71 @@ public void configure(WebAppContext context) throws Exception
LOG.debug("Configuring web-jetty.xml");

Resource webInf = context.getWebInf();
// handle any WEB-INF descriptors
if (webInf != null && webInf.isDirectory())
// get the jetty-ee11-web.xml or jetty-web.xml
Resource jetty = resolveJettyWebXml(webInf);
if (Resources.isReadableFile(jetty))
{
// Attempt to load ancient jetty8-web.xml file
Resource jetty = webInf.resolve("jetty8-web.xml");
if (Resources.missing(jetty))
jetty = webInf.resolve(JETTY_WEB_XML);
if (Resources.missing(jetty))
jetty = webInf.resolve("web-jetty.xml");

if (Resources.isReadableFile(jetty))
{
if (LOG.isDebugEnabled())
LOG.debug("Configure: {}", jetty);
if (LOG.isDebugEnabled())
LOG.debug("Configure: {}", jetty);

Object xmlAttr = context.getAttribute(XML_CONFIGURATION);
context.removeAttribute(XML_CONFIGURATION);
final XmlConfiguration jetty_config = xmlAttr instanceof XmlConfiguration ? (XmlConfiguration)xmlAttr : new XmlConfiguration(jetty);
Object xmlAttr = context.getAttribute(XML_CONFIGURATION);
context.removeAttribute(XML_CONFIGURATION);
final XmlConfiguration jetty_config = xmlAttr instanceof XmlConfiguration ? (XmlConfiguration)xmlAttr : new XmlConfiguration(jetty);

setupXmlConfiguration(context, jetty_config, webInf);
setupXmlConfiguration(context, jetty_config, webInf);

try
{
WebAppClassLoader.runWithServerClassAccess(() ->
{
jetty_config.configure(context);
return null;
});
}
catch (Exception e)
try
{
WebAppClassLoader.runWithServerClassAccess(() ->
{
LOG.warn("Error applying {}", jetty);
throw e;
}
jetty_config.configure(context);
return null;
});
}
catch (Exception e)
{
LOG.warn("Error applying {}", jetty);
throw e;
}
}
}

/**
* Obtain a WEB-INF/jetty-ee9-web.xml, falling back to
* looking for WEB-INF/jetty-web.xml.
*
* @param webInf the WEB-INF of the context to search
* @return the file if it exists or null otherwise
*/
private Resource resolveJettyWebXml(Resource webInf)
{
String xmlFile = JETTY_EE11_WEB_XML;
try
{
if (webInf == null || !webInf.isDirectory())
return null;

//try to find jetty-ee11-web.xml
Resource jetty = webInf.resolve(xmlFile);
if (!Resources.missing(jetty))
return jetty;

xmlFile = JETTY_WEB_XML;
//failing that, look for jetty-web.xml
jetty = webInf.resolve(xmlFile);
if (!Resources.missing(jetty))
return jetty;

return null;
}
catch (Exception e)
{
if (LOG.isDebugEnabled())
LOG.debug("Error resolving WEB-INF/" + xmlFile, e);
return null;
}
}

/**
* Configures some well-known properties before the XmlConfiguration reads
* the configuration.
Expand Down

0 comments on commit 4bbffa1

Please sign in to comment.