System Server Classes

This commit is contained in:
gregw 2024-03-24 19:47:41 +01:00
parent 2775c3233d
commit 17b20041ba
19 changed files with 387 additions and 204 deletions

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-core</artifactId>
<version>12.0.8-SNAPSHOT</version>
</parent>
<artifactId>jetty-ee</artifactId>
<name>Core :: EE Common</name>
<properties>
<bundle-symbolic-name>${project.groupId}.ee</bundle-symbolic-name>
<spotbugs.onlyAnalyze>org.eclipse.jetty.ee.*</spotbugs.onlyAnalyze>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.tests</groupId>
<artifactId>jetty-test-multipart</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
<artifactId>jetty-test-helper</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>@{argLine} ${jetty.surefire.argLine} --add-reads org.eclipse.jetty.ee=org.eclipse.jetty.logging</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,22 @@
//
// ========================================================================
// 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
// ========================================================================
//
module org.eclipse.jetty.ee
{
requires org.slf4j;
requires transitive org.eclipse.jetty.util;
requires transitive org.eclipse.jetty.server;
exports org.eclipse.jetty.ee;
}

View File

@ -0,0 +1,129 @@
package org.eclipse.jetty.ee;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.ClassMatcher;
import org.eclipse.jetty.util.component.Environment;
public class WebappProtectedClasses
{
public static final String SYSTEM_CLASSES_ATTRIBUTE = "org.eclipse.jetty.webapp.systemClasses";
public static final String SERVER_CLASSES_ATTRIBUTE = "org.eclipse.jetty.webapp.serverClasses";
// System classes are classes that cannot be replaced by
// the web application, and they are *always* loaded via
// system classloader.
public static final ClassMatcher DEFAULT_SYSTEM_CLASSES = new ClassMatcher(
"java.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2)
"javax.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2)
"jakarta.", // Jakarta classes (per servlet spec v5.0 / Section 15.2.1)
"org.xml.", // javax.xml
"org.w3c." // javax.xml
);
// Server classes are classes that are hidden from being
// loaded by the web application using system classloader,
// so if web application needs to load any of such classes,
// it has to include them in its distribution.
public static final ClassMatcher DEFAULT_SERVER_CLASSES = new ClassMatcher(
"org.eclipse.jetty." // hide jetty classes
);
public static ClassMatcher getSystemClasses(Server server)
{
return getClassMatcher(server, SYSTEM_CLASSES_ATTRIBUTE, null);
}
public static ClassMatcher getSystemClasses(Environment environment)
{
return getClassMatcher(environment, SYSTEM_CLASSES_ATTRIBUTE, null);
}
/**
* Add a System Class pattern to use for all WebAppContexts.
* @param patterns the patterns to use
*/
public static void addSystemClasses(String... patterns)
{
DEFAULT_SYSTEM_CLASSES.add(patterns);
}
/**
* Add a System Class pattern to use for all WebAppContexts of a given {@link Server}.
* @param server The {@link Server} instance to add classes to
* @param patterns the patterns to use
*/
public static void addSystemClasses(Server server, String... patterns)
{
addClasses(server, SYSTEM_CLASSES_ATTRIBUTE, DEFAULT_SYSTEM_CLASSES, patterns);
}
/**
* Add a System Class pattern to use for WebAppContexts of a given environment.
* @param environment The {@link Environment} instance to add classes to
* @param patterns the patterns to use
*/
public static void addSystemClasses(Environment environment, String... patterns)
{
addClasses(environment, SYSTEM_CLASSES_ATTRIBUTE, DEFAULT_SYSTEM_CLASSES, patterns);
}
public static ClassMatcher getServerClasses(Server server)
{
return getClassMatcher(server, SERVER_CLASSES_ATTRIBUTE, DEFAULT_SERVER_CLASSES);
}
public static ClassMatcher getServerClasses(Environment environment)
{
return getClassMatcher(environment, SERVER_CLASSES_ATTRIBUTE, DEFAULT_SERVER_CLASSES);
}
/**
* Add a Server Class pattern to use for all WebAppContexts of a given {@link Server}.
* @param patterns the patterns to use
*/
public static void addServerClasses(String... patterns)
{
DEFAULT_SERVER_CLASSES.add(patterns);
}
/**
* Add a Server Class pattern to use for all WebAppContexts of a given {@link Server}.
* @param server The {@link Server} instance to add classes to
* @param patterns the patterns to use
*/
public static void addServerClasses(Server server, String... patterns)
{
addClasses(server, SERVER_CLASSES_ATTRIBUTE, DEFAULT_SERVER_CLASSES, patterns);
}
/**
* Add a Server Class pattern to use for all ee9 WebAppContexts.
* @param environment The {@link Environment} instance to add classes to
* @param patterns the patterns to use
*/
public static void addServerClasses(Environment environment, String... patterns)
{
addClasses(environment, SERVER_CLASSES_ATTRIBUTE, DEFAULT_SERVER_CLASSES, patterns);
}
private static void addClasses(Attributes attributes, String attribute, ClassMatcher defaultPatterns, String... patterns)
{
ClassMatcher classMatcher = getClassMatcher(attributes, attribute, defaultPatterns);
if (patterns != null && patterns.length > 0)
classMatcher.add(patterns);
}
private static ClassMatcher getClassMatcher(Attributes attributes, String attribute, ClassMatcher defaultPatterns)
{
Object existing = attributes.getAttribute(attribute);
if (existing instanceof ClassMatcher cm)
return cm;
ClassMatcher classMatcher = (existing instanceof String[] stringArray)
? new ClassMatcher(stringArray) : new ClassMatcher(defaultPatterns);
attributes.setAttribute(attribute, classMatcher);
return classMatcher;
}
}

View File

@ -44,6 +44,7 @@
<module>jetty-util-ajax</module>
<module>jetty-websocket</module>
<module>jetty-xml</module>
<module>jetty-ee</module>
</modules>
<!-- FIXME: Remove once Servlet API usage is totally removed from jetty-core -->

View File

@ -25,6 +25,10 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-ee</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee10</groupId>
<artifactId>jetty-ee10-servlet</artifactId>

View File

@ -2,7 +2,8 @@
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call class="org.eclipse.jetty.ee10.webapp.WebAppContext" name="addSystemClasses">
<Call class="org.eclipse.jetty.ee.WebappProtectedClasses" name="addSystemClasses">
<Arg><Ref refid="Environment"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addSystemClasses"/></Arg>
@ -10,12 +11,12 @@
</Arg>
</Call>
<Call class="org.eclipse.jetty.ee10.webapp.WebAppContext" name="addServerClasses">
<Call class="org.eclipse.jetty.ee.WebappProtectedClasses" name="addServerClasses">
<Arg><Ref refid="Environment"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addServerClasses"/></Arg>
</Call>
</Arg>
</Call>
</Configure>

View File

@ -33,6 +33,7 @@ module org.eclipse.jetty.ee10.webapp
requires transitive org.eclipse.jetty.session;
requires transitive org.eclipse.jetty.ee10.servlet;
requires transitive org.eclipse.jetty.xml;
requires transitive org.eclipse.jetty.ee;
exports org.eclipse.jetty.ee10.webapp;

View File

@ -0,0 +1,42 @@
//
// ========================================================================
// 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;
/**
* @deprecated Use org.eclipse.jetty.util.ClassMatcher
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public class ClassMatcher extends org.eclipse.jetty.util.ClassMatcher
{
public ClassMatcher()
{
super();
}
public ClassMatcher(org.eclipse.jetty.util.ClassMatcher patterns)
{
super(patterns);
}
public ClassMatcher(String... patterns)
{
super(patterns);
}
public ClassMatcher(String pattern)
{
super(pattern);
}
}

View File

@ -38,6 +38,7 @@ import jakarta.servlet.http.HttpSessionAttributeListener;
import jakarta.servlet.http.HttpSessionBindingListener;
import jakarta.servlet.http.HttpSessionIdListener;
import jakarta.servlet.http.HttpSessionListener;
import org.eclipse.jetty.ee.WebappProtectedClasses;
import org.eclipse.jetty.ee10.servlet.ErrorHandler;
import org.eclipse.jetty.ee10.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
@ -83,32 +84,25 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
static final Logger LOG = LoggerFactory.getLogger(WebAppContext.class);
public static final String WEB_DEFAULTS_XML = "org/eclipse/jetty/ee10/webapp/webdefault-ee10.xml";
public static final String SERVER_SYS_CLASSES = "org.eclipse.jetty.webapp.systemClasses";
public static final String SERVER_SRV_CLASSES = "org.eclipse.jetty.webapp.serverClasses";
public static final String SERVER_SYS_CLASSES = WebappProtectedClasses.SYSTEM_CLASSES_ATTRIBUTE;
public static final String SERVER_SRV_CLASSES = WebappProtectedClasses.SERVER_CLASSES_ATTRIBUTE;
private static String[] __dftProtectedTargets = {"/WEB-INF", "/META-INF"};
private static final String[] __dftProtectedTargets = {"/WEB-INF", "/META-INF"};
// System classes are classes that cannot be replaced by
// the web application, and they are *always* loaded via
// system classloader.
public static final ClassMatcher __dftSystemClasses = new ClassMatcher(
"java.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2)
"javax.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2)
"jakarta.", // Jakarta classes (per servlet spec v5.0 / Section 15.2.1)
"org.xml.", // javax.xml
"org.w3c." // javax.xml
);
/**
* @deprecated use {@link WebappProtectedClasses#DEFAULT_SYSTEM_CLASSES}
*/
@Deprecated
public static final ClassMatcher __dftSystemClasses = WebappProtectedClasses.DEFAULT_SYSTEM_CLASSES;
// Server classes are classes that are hidden from being
// loaded by the web application using system classloader,
// so if web application needs to load any of such classes,
// it has to include them in its distribution.
public static final ClassMatcher __dftServerClasses = new ClassMatcher(
"org.eclipse.jetty." // hide jetty classes
);
/**
* @deprecated use {@link WebappProtectedClasses#DEFAULT_SERVER_CLASSES}
*/
@Deprecated
public static final ClassMatcher __dftServerClasses = WebappProtectedClasses.DEFAULT_SERVER_CLASSES;
private final ClassMatcher _systemClasses = new ClassMatcher(__dftSystemClasses);
private final ClassMatcher _serverClasses = new ClassMatcher(__dftServerClasses);
private final ClassMatcher _systemClasses = new ClassMatcher(WebappProtectedClasses.getSystemClasses(ServletContextHandler.ENVIRONMENT));
private final ClassMatcher _serverClasses = new ClassMatcher(WebappProtectedClasses.getServerClasses(ServletContextHandler.ENVIRONMENT));
private Configurations _configurations;
private String _defaultsDescriptor = WEB_DEFAULTS_XML;
@ -727,23 +721,8 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
super.setServer(server);
if (server != null)
{
if (__dftSystemClasses.equals(_systemClasses))
{
Object systemClasses = ServletContextHandler.ENVIRONMENT.getAttribute(SERVER_SYS_CLASSES);
if (systemClasses instanceof String[] patterns)
_systemClasses.add(patterns);
if (systemClasses instanceof ClassMatcher classMatcher)
_systemClasses.add(classMatcher.getPatterns());
}
if (__dftServerClasses.equals(_serverClasses))
{
Object serverClasses = ServletContextHandler.ENVIRONMENT.getAttribute(SERVER_SRV_CLASSES);
if (serverClasses instanceof String[] patterns)
_serverClasses.add(patterns);
if (serverClasses instanceof ClassMatcher classMatcher)
_serverClasses.add(classMatcher.getPatterns());
}
_systemClasses.add(WebappProtectedClasses.getSystemClasses(server).getPatterns());
_serverClasses.add(WebappProtectedClasses.getServerClasses(server).getPatterns());
}
}
@ -1403,75 +1382,30 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
}
/**
* @param server ignored.
* @param patterns the patterns to add
* @deprecated use {@link #addServerClasses(String...)} instead, will be removed in Jetty 12.1.0
* Add a Server Class pattern to use for all ee9 WebAppContexts.
* @param server The {@link Server} instance to add classes to
* @param patterns the patterns to use
* @see #getServerClassMatcher()
* @see #getServerClasses()
* @deprecated use {@link WebappProtectedClasses#addSystemClasses(Server, String...)}
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public static void addServerClasses(Server server, String... patterns)
{
addServerClasses(patterns);
WebappProtectedClasses.addServerClasses(server, patterns);
}
/**
* Add a Server Class pattern to use for all ee10 WebAppContexts.
* Add a System Class pattern to use for all ee9 WebAppContexts.
* @param server The {@link Server} instance to add classes to
* @param patterns the patterns to use
* @see #getServerClassMatcher()
* @see #getServerClasses()
*/
public static void addServerClasses(String... patterns)
{
if (LOG.isDebugEnabled())
LOG.debug("Adding {} ServerClasses: {}", ServletContextHandler.ENVIRONMENT, String.join(", ", patterns));
addClasses(__dftServerClasses, SERVER_SRV_CLASSES, patterns);
}
/**
* @param server ignored.
* @param patterns the patterns to add
* @deprecated use {@link #addSystemClasses(String...)} instead, will be removed in Jetty 12.1.0
* @see #getSystemClassMatcher()
* @see #getSystemClasses()
* @deprecated use {@link WebappProtectedClasses#addServerClasses(Server, String...)}
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public static void addSystemClasses(Server server, String... patterns)
{
addSystemClasses(patterns);
}
/**
* Add a System Class pattern to use for all ee10 WebAppContexts.
* @param patterns the patterns to use
* @see #getSystemClassMatcher()
* @see #getSystemClasses()
*/
public static void addSystemClasses(String... patterns)
{
if (LOG.isDebugEnabled())
LOG.debug("Adding {} SystemClasses: {}", ServletContextHandler.ENVIRONMENT, String.join(", ", patterns));
addClasses(__dftSystemClasses, SERVER_SYS_CLASSES, patterns);
}
private static void addClasses(ClassMatcher matcher, String attributeKey, String... pattern)
{
if (pattern == null || pattern.length == 0)
return;
// look for a ClassMatcher attribute with the list of Server / System classes
// to apply to every ee10 web application. If not present, use our defaults.
Object o = ServletContextHandler.ENVIRONMENT.getAttribute(attributeKey);
if (o instanceof ClassMatcher)
{
((ClassMatcher)o).add(pattern);
return;
}
String[] classes;
if (o instanceof String[])
classes = (String[])o;
else
classes = matcher.getPatterns();
int l = classes.length;
classes = Arrays.copyOf(classes, l + pattern.length);
System.arraycopy(pattern, 0, classes, l, pattern.length);
ServletContextHandler.ENVIRONMENT.setAttribute(attributeKey, classes);
WebappProtectedClasses.addSystemClasses(server, patterns);
}
}

View File

@ -30,6 +30,7 @@ import jakarta.servlet.GenericServlet;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.eclipse.jetty.ee.WebappProtectedClasses;
import org.eclipse.jetty.ee10.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.ee10.servlet.ServletContextHandler;
import org.eclipse.jetty.http.HttpStatus;
@ -812,7 +813,7 @@ public class WebAppContextTest
String testPattern = "org.eclipse.jetty.ee10.webapp.test.";
WebAppContext.addServerClasses(testPattern);
WebAppContext.addServerClasses(server, testPattern);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
@ -825,7 +826,7 @@ public class WebAppContextTest
List<String> serverClasses = List.of(context.getServerClasses());
assertThat("Should have environment specific test pattern", serverClasses, hasItem(testPattern));
assertThat("Should have pattern from JaasConfiguration", serverClasses, hasItem("-org.eclipse.jetty.security.jaas."));
for (String defaultServerClass: WebAppContext.__dftServerClasses)
for (String defaultServerClass: WebappProtectedClasses.DEFAULT_SERVER_CLASSES)
assertThat("Should have default patterns", serverClasses, hasItem(defaultServerClass));
}
@ -836,7 +837,7 @@ public class WebAppContextTest
String testPattern = "org.eclipse.jetty.ee10.webapp.test.";
WebAppContext.addSystemClasses(testPattern);
WebAppContext.addSystemClasses(server, testPattern);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
@ -849,7 +850,7 @@ public class WebAppContextTest
List<String> systemClasses = List.of(context.getSystemClasses());
assertThat("Should have environment specific test pattern", systemClasses, hasItem(testPattern));
assertThat("Should have pattern from JaasConfiguration", systemClasses, hasItem("org.eclipse.jetty.security.jaas."));
for (String defaultSystemClass: WebAppContext.__dftSystemClasses)
for (String defaultSystemClass: WebappProtectedClasses.DEFAULT_SYSTEM_CLASSES)
assertThat("Should have default patterns", systemClasses, hasItem(defaultSystemClass));
}
}

View File

@ -6,7 +6,7 @@
<Configure class="org.eclipse.jetty.ee8.webapp.WebAppContext">
<Call name="addServerClassMatcher">
<Arg>
<New class="org.eclipse.jetty.ee8.webapp.ClassMatcher">
<New class="org.eclipse.jetty.util.ClassMatcher">
<Arg>
<Array type="java.lang.String">
<Item>-org.eclipse.jetty.util.Decorator</Item>

View File

@ -3,6 +3,7 @@
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call class="org.eclipse.jetty.ee8.webapp.WebAppContext" name="addSystemClasses">
<Arg><Ref refid="Server"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addSystemClasses"/></Arg>
@ -11,6 +12,7 @@
</Call>
<Call class="org.eclipse.jetty.ee8.webapp.WebAppContext" name="addServerClasses">
<Arg><Ref refid="Server"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addServerClasses"/></Arg>

View File

@ -6,7 +6,7 @@
<Configure class="org.eclipse.jetty.ee9.webapp.WebAppContext">
<Call name="addServerClassMatcher">
<Arg>
<New class="org.eclipse.jetty.ee9.webapp.ClassMatcher">
<New class="org.eclipse.jetty.util.ClassMatcher">
<Arg>
<Array type="java.lang.String">
<Item>-org.eclipse.jetty.util.Decorator</Item>

View File

@ -21,6 +21,10 @@
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-xml</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-ee</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.ee9</groupId>
<artifactId>jetty-ee9-servlet</artifactId>

View File

@ -2,7 +2,8 @@
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call class="org.eclipse.jetty.ee9.webapp.WebAppContext" name="addSystemClasses">
<Call class="org.eclipse.jetty.ee.WebappProtectedClasses" name="addSystemClasses">
<Arg><Ref refid="Environment"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addSystemClasses"/></Arg>
@ -10,7 +11,8 @@
</Arg>
</Call>
<Call class="org.eclipse.jetty.ee9.webapp.WebAppContext" name="addServerClasses">
<Call class="org.eclipse.jetty.ee.WebappProtectedClasses" name="addServerClasses">
<Arg><Ref refid="Environment"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addServerClasses"/></Arg>

View File

@ -32,6 +32,7 @@ module org.eclipse.jetty.ee9.webapp
requires transitive java.instrument;
requires transitive org.eclipse.jetty.ee9.servlet;
requires transitive org.eclipse.jetty.xml;
requires transitive org.eclipse.jetty.ee;
exports org.eclipse.jetty.ee9.webapp;

View File

@ -0,0 +1,42 @@
//
// ========================================================================
// 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.ee9.webapp;
/**
* @deprecated Use org.eclipse.jetty.util.ClassMatcher
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public class ClassMatcher extends org.eclipse.jetty.util.ClassMatcher
{
public ClassMatcher()
{
super();
}
public ClassMatcher(org.eclipse.jetty.util.ClassMatcher patterns)
{
super(patterns);
}
public ClassMatcher(String... patterns)
{
super(patterns);
}
public ClassMatcher(String pattern)
{
super(pattern);
}
}

View File

@ -40,6 +40,7 @@ import jakarta.servlet.http.HttpSessionAttributeListener;
import jakarta.servlet.http.HttpSessionBindingListener;
import jakarta.servlet.http.HttpSessionIdListener;
import jakarta.servlet.http.HttpSessionListener;
import org.eclipse.jetty.ee.WebappProtectedClasses;
import org.eclipse.jetty.ee9.nested.ContextHandler;
import org.eclipse.jetty.ee9.nested.ErrorHandler;
import org.eclipse.jetty.ee9.nested.HandlerWrapper;
@ -55,7 +56,6 @@ import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Deployable;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.Attributes;
import org.eclipse.jetty.util.ClassMatcher;
import org.eclipse.jetty.util.ExceptionUtil;
import org.eclipse.jetty.util.IO;
@ -91,32 +91,25 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
public static final String WEB_DEFAULTS_XML = "org/eclipse/jetty/ee9/webapp/webdefault-ee9.xml";
public static final String ERROR_PAGE = "org.eclipse.jetty.server.error_page";
public static final String SERVER_SYS_CLASSES = "org.eclipse.jetty.ee9.webapp.systemClasses";
public static final String SERVER_SRV_CLASSES = "org.eclipse.jetty.ee9.webapp.serverClasses";
public static final String SERVER_SYS_CLASSES = WebappProtectedClasses.SYSTEM_CLASSES_ATTRIBUTE;
public static final String SERVER_SRV_CLASSES = WebappProtectedClasses.SERVER_CLASSES_ATTRIBUTE;
private static String[] __dftProtectedTargets = {"/WEB-INF", "/META-INF"};
private static final String[] __dftProtectedTargets = {"/WEB-INF", "/META-INF"};
// System classes are classes that cannot be replaced by
// the web application, and they are *always* loaded via
// system classloader.
public static final ClassMatcher __dftSystemClasses = new ClassMatcher(
"java.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2)
"javax.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2)
"jakarta.", // Jakarta classes (per servlet spec v5.0 / Section 15.2.1)
"org.xml.", // javax.xml
"org.w3c." // javax.xml
);
/**
* @deprecated use {@link WebappProtectedClasses#DEFAULT_SYSTEM_CLASSES}
*/
@Deprecated
public static final ClassMatcher __dftSystemClasses = WebappProtectedClasses.DEFAULT_SYSTEM_CLASSES;
// Server classes are classes that are hidden from being
// loaded by the web application using system classloader,
// so if web application needs to load any of such classes,
// it has to include them in its distribution.
public static final ClassMatcher __dftServerClasses = new ClassMatcher(
"org.eclipse.jetty." // hide jetty classes
);
/**
* @deprecated use {@link WebappProtectedClasses#DEFAULT_SERVER_CLASSES}
*/
@Deprecated
public static final ClassMatcher __dftServerClasses = WebappProtectedClasses.DEFAULT_SERVER_CLASSES;
private final ClassMatcher _systemClasses = new ClassMatcher(__dftSystemClasses);
private final ClassMatcher _serverClasses = new ClassMatcher(__dftServerClasses);
private final ClassMatcher _systemClasses = new ClassMatcher(WebappProtectedClasses.getSystemClasses(ServletContextHandler.ENVIRONMENT));
private final ClassMatcher _serverClasses = new ClassMatcher(WebappProtectedClasses.getServerClasses(ServletContextHandler.ENVIRONMENT));
private Configurations _configurations;
private String _defaultsDescriptor = WEB_DEFAULTS_XML;
@ -779,23 +772,8 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
super.setServer(server);
if (server != null)
{
if (__dftSystemClasses.equals(_systemClasses))
{
Object systemClasses = ServletContextHandler.ENVIRONMENT.getAttribute(SERVER_SYS_CLASSES);
if (systemClasses instanceof String[] patterns)
_systemClasses.add(patterns);
if (systemClasses instanceof ClassMatcher classMatcher)
_systemClasses.add(classMatcher.getPatterns());
}
if (__dftServerClasses.equals(_serverClasses))
{
Object serverClasses = ServletContextHandler.ENVIRONMENT.getAttribute(SERVER_SRV_CLASSES);
if (serverClasses instanceof String[] patterns)
_serverClasses.add(patterns);
if (serverClasses instanceof ClassMatcher classMatcher)
_serverClasses.add(classMatcher.getPatterns());
}
_systemClasses.add(WebappProtectedClasses.getSystemClasses(server).getPatterns());
_serverClasses.add(WebappProtectedClasses.getServerClasses(server).getPatterns());
}
}
@ -1493,75 +1471,29 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
/**
* Add a Server Class pattern to use for all ee9 WebAppContexts.
* @param server The {@link Server} instance to add classes to
* @param patterns the patterns to use
* @see #getServerClassMatcher()
* @see #getServerClasses()
*/
public static void addServerClasses(String... patterns)
{
if (LOG.isDebugEnabled())
LOG.debug("Adding {} ServerClasses: {}", ServletContextHandler.ENVIRONMENT, String.join(", ", patterns));
addClasses(__dftServerClasses, SERVER_SRV_CLASSES, patterns);
}
/**
* @param attributes ignored.
* @param patterns the patterns to add
* @deprecated use {@link #addServerClasses(String...)} instead. will be removed in Jetty 12.1.0
* @deprecated use {@link WebappProtectedClasses#addSystemClasses(Server, String...)}
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public static void addServerClasses(Attributes attributes, String... patterns)
public static void addServerClasses(Server server, String... patterns)
{
addServerClasses(patterns);
WebappProtectedClasses.addServerClasses(server, patterns);
}
/**
* Add a System Class pattern to use for all ee9 WebAppContexts.
* @param server The {@link Server} instance to add classes to
* @param patterns the patterns to use
* @see #getSystemClassMatcher()
* @see #getSystemClasses()
*/
public static void addSystemClasses(String... patterns)
{
if (LOG.isDebugEnabled())
LOG.debug("Adding {} SystemClasses: {}", ServletContextHandler.ENVIRONMENT, String.join(", ", patterns));
addClasses(__dftSystemClasses, SERVER_SYS_CLASSES, patterns);
}
/**
* @param attributes ignored.
* @param patterns the patterns to add
* @deprecated use {@link #addSystemClasses(String...)} instead. will be removed in Jetty 12.1.0
* @deprecated use {@link WebappProtectedClasses#addServerClasses(Server, String...)}
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public static void addSystemClasses(Attributes attributes, String... patterns)
public static void addSystemClasses(Server server, String... patterns)
{
addSystemClasses(patterns);
}
private static void addClasses(ClassMatcher matcher, String attributeKey, String... pattern)
{
if (pattern == null || pattern.length == 0)
return;
// look for a ClassMatcher attribute with the list of Server / System classes
// to apply to every ee9 web application. If not present, use our defaults.
Object o = ServletContextHandler.ENVIRONMENT.getAttribute(attributeKey);
if (o instanceof ClassMatcher)
{
((ClassMatcher)o).add(pattern);
return;
}
String[] classes;
if (o instanceof String[])
classes = (String[])o;
else
classes = matcher.getPatterns();
int l = classes.length;
classes = Arrays.copyOf(classes, l + pattern.length);
System.arraycopy(pattern, 0, classes, l, pattern.length);
ServletContextHandler.ENVIRONMENT.setAttribute(attributeKey, classes);
WebappProtectedClasses.addSystemClasses(server, patterns);
}
}

View File

@ -813,7 +813,7 @@ public class WebAppContextTest
String testPattern = "org.eclipse.jetty.ee9.webapp.test.";
WebAppContext.addServerClasses(testPattern);
WebAppContext.addServerClasses(server, testPattern);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
@ -837,7 +837,7 @@ public class WebAppContextTest
String testPattern = "org.eclipse.jetty.ee9.webapp.test.";
WebAppContext.addSystemClasses(testPattern);
WebAppContext.addSystemClasses(server, testPattern);
WebAppContext context = new WebAppContext();
context.setContextPath("/");