swapped defaults to environment
added unit tests
This commit is contained in:
gregw 2024-03-30 10:14:37 +01:00
parent 210b068bf9
commit 7f432235da
3 changed files with 245 additions and 23 deletions

View File

@ -54,8 +54,8 @@ public class WebAppClassLoading
public static final String HIDDEN_CLASSES_ATTRIBUTE = "org.eclipse.jetty.webapp.serverClasses";
/**
* The default protected (system) classes used by a web application, which can be overridden by {@link Server},
* {@link Environment} or {@link org.eclipse.jetty.server.handler.ContextHandler Context} configuration.
* The default protected (system) classes used by a web application, which will be applied to the {@link ClassMatcher}s created
* by {@link #getProtectedClasses(Environment)}.
*/
public static final ClassMatcher DEFAULT_PROTECTED_CLASSES = new ClassMatcher(
"java.", // Java SE classes (per servlet spec v2.5 / SRV.9.7.2)
@ -66,8 +66,8 @@ public class WebAppClassLoading
);
/**
* The default hidden (server) classes used by a web application, which can be overridden by {@link Server},
* {@link Environment} or {@link org.eclipse.jetty.server.handler.ContextHandler Context} configuration.
* The default hidden (server) classes used by a web application, which can be applied to the {@link ClassMatcher}s created
* by {@link #getHiddenClasses(Environment)}.
*/
public static final ClassMatcher DEFAULT_HIDDEN_CLASSES = new ClassMatcher(
"org.eclipse.jetty." // hide jetty classes
@ -76,21 +76,21 @@ public class WebAppClassLoading
/**
* Get the default protected (system) classes for a {@link Server}
* @param server The {@link Server} for the defaults
* @return The default protected (system) classes for the {@link Server}, which will be the {@link #DEFAULT_PROTECTED_CLASSES} if not previously configured.
* @return The default protected (system) classes for the {@link Server}, which will be empty if not previously configured.
*/
public static ClassMatcher getProtectedClasses(Server server)
{
return getClassMatcher(server, PROTECTED_CLASSES_ATTRIBUTE, DEFAULT_PROTECTED_CLASSES);
return getClassMatcher(server, PROTECTED_CLASSES_ATTRIBUTE, null);
}
/**
* Get the default protected (system) classes for an {@link Environment}
* @param environment The {@link Server} for the defaults
* @return The default protected (system) classes for the {@link Environment}, which will be empty if not previously configured.
* @return The default protected (system) classes for the {@link Environment}, which will be the {@link #DEFAULT_PROTECTED_CLASSES} if not previously configured.
*/
public static ClassMatcher getProtectedClasses(Environment environment)
{
return getClassMatcher(environment, PROTECTED_CLASSES_ATTRIBUTE, null);
return getClassMatcher(environment, PROTECTED_CLASSES_ATTRIBUTE, DEFAULT_PROTECTED_CLASSES);
}
/**
@ -109,7 +109,8 @@ public class WebAppClassLoading
*/
public static void addProtectedClasses(Server server, String... patterns)
{
addClasses(server, PROTECTED_CLASSES_ATTRIBUTE, DEFAULT_PROTECTED_CLASSES, patterns);
if (patterns != null && patterns.length > 0)
getClassMatcher(server, PROTECTED_CLASSES_ATTRIBUTE, null).add(patterns);
}
/**
@ -119,28 +120,29 @@ public class WebAppClassLoading
*/
public static void addProtectedClasses(Environment environment, String... patterns)
{
addClasses(environment, PROTECTED_CLASSES_ATTRIBUTE, DEFAULT_PROTECTED_CLASSES, patterns);
if (patterns != null && patterns.length > 0)
getClassMatcher(environment, PROTECTED_CLASSES_ATTRIBUTE, DEFAULT_PROTECTED_CLASSES).add(patterns);
}
/**
* Get the default hidden (server) classes for a {@link Server}
* @param server The {@link Server} for the defaults
* @return The default hidden (server) classes for the {@link Server}, which will be the
* {@link #DEFAULT_PROTECTED_CLASSES} if not previously configured.
* @return The default hidden (server) classes for the {@link Server}, which will be empty if not previously configured.
*
*/
public static ClassMatcher getHiddenClasses(Server server)
{
return getClassMatcher(server, HIDDEN_CLASSES_ATTRIBUTE, DEFAULT_HIDDEN_CLASSES);
return getClassMatcher(server, HIDDEN_CLASSES_ATTRIBUTE, null);
}
/**
* Get the default hidden (server) classes for an {@link Environment}
* @param environment The {@link Server} for the defaults
* @return The default hidden (server) classes for the {@link Environment}, which will be empty if not previously configured.
* @return The default hidden (server) classes for the {@link Environment}, which will be {@link #DEFAULT_PROTECTED_CLASSES} if not previously configured.
*/
public static ClassMatcher getHiddenClasses(Environment environment)
{
return getClassMatcher(environment, HIDDEN_CLASSES_ATTRIBUTE, null);
return getClassMatcher(environment, HIDDEN_CLASSES_ATTRIBUTE, DEFAULT_HIDDEN_CLASSES);
}
/**
@ -159,7 +161,8 @@ public class WebAppClassLoading
*/
public static void addHiddenClasses(Server server, String... patterns)
{
addClasses(server, HIDDEN_CLASSES_ATTRIBUTE, DEFAULT_HIDDEN_CLASSES, patterns);
if (patterns != null && patterns.length > 0)
getClassMatcher(server, HIDDEN_CLASSES_ATTRIBUTE, null).add(patterns);
}
/**
@ -169,14 +172,8 @@ public class WebAppClassLoading
*/
public static void addHiddenClasses(Environment environment, String... patterns)
{
addClasses(environment, HIDDEN_CLASSES_ATTRIBUTE, DEFAULT_HIDDEN_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);
getClassMatcher(environment, HIDDEN_CLASSES_ATTRIBUTE, DEFAULT_HIDDEN_CLASSES).add(patterns);
}
private static ClassMatcher getClassMatcher(Attributes attributes, String attribute, ClassMatcher defaultPatterns)

View File

@ -0,0 +1,222 @@
//
// ========================================================================
// 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 orge.eclipse.jetty.ee;
import java.util.Arrays;
import org.eclipse.jetty.ee.WebAppClassLoading;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.ClassMatcher;
import org.eclipse.jetty.util.component.Environment;
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.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
public class WebAppClassLoadingTest
{
@BeforeEach
public void beforeEach()
{
Environment.ensure("Test");
}
@AfterEach
public void afterEach()
{
Environment.ensure("Test").clearAttributes();
}
@Test
public void testServerDefaults()
{
Server server = new Server();
ClassMatcher protect = WebAppClassLoading.getProtectedClasses(server);
assertThat(protect.size(), is(0));
assertThat(server.getAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE), sameInstance(protect));
ClassMatcher hide = WebAppClassLoading.getHiddenClasses(server);
assertThat(hide.size(), is(0));
assertThat(server.getAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE), sameInstance(hide));
}
@Test
public void testServerAttributeDefaults()
{
Server server = new Server();
ClassMatcher protect = new ClassMatcher("org.protect.");
server.setAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE, protect);
ClassMatcher hide = new ClassMatcher("org.hide.");
server.setAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE, hide);
assertThat(WebAppClassLoading.getProtectedClasses(server), sameInstance(protect));
assertThat(WebAppClassLoading.getHiddenClasses(server), sameInstance(hide));
}
@Test
public void testServerStringAttributeDefaults()
{
Server server = new Server();
server.setAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE, new String[] {"org.protect."});
server.setAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE, new String[] {"org.hide."});
ClassMatcher protect = WebAppClassLoading.getProtectedClasses(server);
assertThat(protect.size(), is(1));
assertThat(Arrays.asList(protect.getPatterns()), contains("org.protect."));
assertThat(server.getAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE), sameInstance(protect));
ClassMatcher hide = WebAppClassLoading.getHiddenClasses(server);
assertThat(hide.size(), is(1));
assertThat(Arrays.asList(hide.getPatterns()), contains("org.hide."));
assertThat(server.getAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE), sameInstance(hide));
}
@Test
public void testServerProgrammaticDefaults()
{
Server server = new Server();
WebAppClassLoading.addProtectedClasses(server, "org.protect.");
WebAppClassLoading.addHiddenClasses(server, "org.hide.");
ClassMatcher protect = WebAppClassLoading.getProtectedClasses(server);
assertThat(protect.size(), is(1));
assertThat(Arrays.asList(protect.getPatterns()), contains("org.protect."));
assertThat(server.getAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE), sameInstance(protect));
ClassMatcher hide = WebAppClassLoading.getHiddenClasses(server);
assertThat(hide.size(), is(1));
assertThat(Arrays.asList(hide.getPatterns()), contains("org.hide."));
assertThat(server.getAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE), sameInstance(hide));
}
@Test
public void testServerAddPatterns()
{
Server server = new Server();
ClassMatcher protect = WebAppClassLoading.getProtectedClasses(server);
ClassMatcher hide = WebAppClassLoading.getHiddenClasses(server);
assertThat(protect.size(), is(0));
assertThat(hide.size(), is(0));
WebAppClassLoading.addProtectedClasses(server, "org.protect.", "com.protect.");
WebAppClassLoading.addHiddenClasses(server, "org.hide.", "com.hide.");
assertThat(protect.size(), is(2));
assertThat(Arrays.asList(protect.getPatterns()), containsInAnyOrder("org.protect.", "com.protect."));
assertThat(server.getAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE), sameInstance(protect));
assertThat(hide.size(), is(2));
assertThat(Arrays.asList(hide.getPatterns()), containsInAnyOrder("org.hide.", "com.hide."));
assertThat(server.getAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE), sameInstance(hide));
}
@Test
public void testEnvironmentDefaults()
{
Environment environment = Environment.get("Test");
ClassMatcher protect = WebAppClassLoading.getProtectedClasses(environment);
assertThat(protect, equalTo(WebAppClassLoading.DEFAULT_PROTECTED_CLASSES));
assertThat(environment.getAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE), sameInstance(protect));
ClassMatcher hide = WebAppClassLoading.getHiddenClasses(environment);
assertThat(hide, equalTo(WebAppClassLoading.DEFAULT_HIDDEN_CLASSES));
assertThat(environment.getAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE), sameInstance(hide));
}
@Test
public void testEnvironmentAttributeDefaults()
{
Environment environment = Environment.get("Test");
ClassMatcher protect = new ClassMatcher("org.protect.");
environment.setAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE, protect);
ClassMatcher hide = new ClassMatcher("org.hide.");
environment.setAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE, hide);
assertThat(WebAppClassLoading.getProtectedClasses(environment), sameInstance(protect));
assertThat(WebAppClassLoading.getHiddenClasses(environment), sameInstance(hide));
}
@Test
public void testEnvironmentStringAttributeDefaults()
{
Environment environment = Environment.get("Test");
environment.setAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE, new String[] {"org.protect."});
environment.setAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE, new String[] {"org.hide."});
ClassMatcher protect = WebAppClassLoading.getProtectedClasses(environment);
assertThat(protect.size(), is(1));
assertThat(Arrays.asList(protect.getPatterns()), contains("org.protect."));
assertThat(environment.getAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE), sameInstance(protect));
ClassMatcher hide = WebAppClassLoading.getHiddenClasses(environment);
assertThat(hide.size(), is(1));
assertThat(Arrays.asList(hide.getPatterns()), contains("org.hide."));
assertThat(environment.getAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE), sameInstance(hide));
}
@Test
public void testEnvironmentProgrammaticDefaults()
{
Environment environment = Environment.get("Test");
WebAppClassLoading.addProtectedClasses(environment, "org.protect.");
WebAppClassLoading.addHiddenClasses(environment, "org.hide.");
ClassMatcher protect = WebAppClassLoading.getProtectedClasses(environment);
ClassMatcher hide = WebAppClassLoading.getHiddenClasses(environment);
assertThat(protect.size(), is(WebAppClassLoading.DEFAULT_PROTECTED_CLASSES.size() + 1));
assertThat(protect.getPatterns(), hasItemInArray("org.protect."));
for (String pattern : WebAppClassLoading.DEFAULT_PROTECTED_CLASSES)
assertThat(protect.getPatterns(), hasItemInArray(pattern));
assertThat(environment.getAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE), sameInstance(protect));
assertThat(hide.size(), is(WebAppClassLoading.DEFAULT_HIDDEN_CLASSES.size() + 1));
assertThat(hide.getPatterns(), hasItemInArray("org.hide."));
for (String pattern : WebAppClassLoading.DEFAULT_HIDDEN_CLASSES)
assertThat(hide.getPatterns(), hasItemInArray(pattern));
assertThat(environment.getAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE), sameInstance(hide));
}
@Test
public void testEnvironmentAddPatterns()
{
Environment environment = Environment.get("Test");
ClassMatcher protect = WebAppClassLoading.getProtectedClasses(environment);
ClassMatcher hide = WebAppClassLoading.getHiddenClasses(environment);
assertThat(protect, equalTo(WebAppClassLoading.DEFAULT_PROTECTED_CLASSES));
assertThat(hide, equalTo(WebAppClassLoading.DEFAULT_HIDDEN_CLASSES));
WebAppClassLoading.addProtectedClasses(environment, "org.protect.", "com.protect.");
WebAppClassLoading.addHiddenClasses(environment, "org.hide.", "com.hide.");
assertThat(protect.size(), is(WebAppClassLoading.DEFAULT_PROTECTED_CLASSES.size() + 2));
assertThat(protect.getPatterns(), hasItemInArray("org.protect."));
assertThat(protect.getPatterns(), hasItemInArray("com.protect."));
for (String pattern : WebAppClassLoading.DEFAULT_PROTECTED_CLASSES)
assertThat(protect.getPatterns(), hasItemInArray(pattern));
assertThat(environment.getAttribute(WebAppClassLoading.PROTECTED_CLASSES_ATTRIBUTE), sameInstance(protect));
assertThat(hide.size(), is(WebAppClassLoading.DEFAULT_HIDDEN_CLASSES.size() + 2));
assertThat(hide.getPatterns(), hasItemInArray("org.hide."));
assertThat(hide.getPatterns(), hasItemInArray("com.hide."));
for (String pattern : WebAppClassLoading.DEFAULT_HIDDEN_CLASSES)
assertThat(hide.getPatterns(), hasItemInArray(pattern));
assertThat(environment.getAttribute(WebAppClassLoading.HIDDEN_CLASSES_ATTRIBUTE), sameInstance(hide));
}
}

View File

@ -871,6 +871,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 defaults", serverClasses, hasItem("org.eclipse.jetty."));
assertThat("Should have pattern from JaasConfiguration", serverClasses, hasItem("-org.eclipse.jetty.security.jaas."));
for (String defaultServerClass: WebAppContext.__dftServerClasses)
assertThat("Should have default patterns", serverClasses, hasItem(defaultServerClass));
@ -898,6 +899,8 @@ 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 defaults", systemClasses, hasItem("javax."));
assertThat("Should have pattern from defaults", systemClasses, hasItem("jakarta."));
assertThat("Should have pattern from JaasConfiguration", systemClasses, hasItem("org.eclipse.jetty.security.jaas."));
for (String defaultSystemClass : WebAppContext.__dftSystemClasses)
{