Move Servlet high-level infrastructure to spring-boot-servlet

This commit is contained in:
Stéphane Nicoll
2025-06-03 09:01:03 +02:00
committed by Phillip Webb
parent 9ee79c1bbc
commit b379bc0deb
53 changed files with 184 additions and 91 deletions

View File

@@ -1,56 +0,0 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.web.mappings.servlet;
import java.util.Collection;
import jakarta.servlet.FilterRegistration;
/**
* A {@link RegistrationMappingDescription} derived from a {@link FilterRegistration}.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
public class FilterRegistrationMappingDescription extends RegistrationMappingDescription<FilterRegistration> {
/**
* Creates a new {@code FilterRegistrationMappingDescription} derived from the given
* {@code filterRegistration}.
* @param filterRegistration the filter registration
*/
public FilterRegistrationMappingDescription(FilterRegistration filterRegistration) {
super(filterRegistration);
}
/**
* Returns the servlet name mappings for the registered filter.
* @return the mappings
*/
public Collection<String> getServletNameMappings() {
return getRegistration().getServletNameMappings();
}
/**
* Returns the URL pattern mappings for the registered filter.
* @return the mappings
*/
public Collection<String> getUrlPatternMappings() {
return getRegistration().getUrlPatternMappings();
}
}

View File

@@ -1,74 +0,0 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.web.mappings.servlet;
import java.util.Collections;
import java.util.List;
import jakarta.servlet.Filter;
import jakarta.servlet.ServletContext;
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.boot.actuate.web.mappings.MappingDescriptionProvider;
import org.springframework.boot.actuate.web.mappings.servlet.FiltersMappingDescriptionProvider.FiltersMappingDescriptionProviderRuntimeHints;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.web.context.WebApplicationContext;
/**
* A {@link MappingDescriptionProvider} that describes that mappings of any {@link Filter
* Filters} registered with a {@link ServletContext}.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
@ImportRuntimeHints(FiltersMappingDescriptionProviderRuntimeHints.class)
public class FiltersMappingDescriptionProvider implements MappingDescriptionProvider {
@Override
public List<FilterRegistrationMappingDescription> describeMappings(ApplicationContext context) {
if (context instanceof WebApplicationContext webApplicationContext) {
return webApplicationContext.getServletContext()
.getFilterRegistrations()
.values()
.stream()
.map(FilterRegistrationMappingDescription::new)
.toList();
}
return Collections.emptyList();
}
@Override
public String getMappingName() {
return "servletFilters";
}
static class FiltersMappingDescriptionProviderRuntimeHints implements RuntimeHintsRegistrar {
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
this.bindingRegistrar.registerReflectionHints(hints.reflection(),
FilterRegistrationMappingDescription.class);
}
}
}

View File

@@ -1,65 +0,0 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.web.mappings.servlet;
import jakarta.servlet.Registration;
/**
* A mapping description derived from a {@link Registration}.
*
* @param <T> type of the registration
* @author Andy Wilkinson
* @since 2.0.0
*/
public class RegistrationMappingDescription<T extends Registration> {
private final T registration;
/**
* Creates a new {@link RegistrationMappingDescription} derived from the given
* {@code registration} and with the given {@code predicate}.
* @param registration the registration
*/
public RegistrationMappingDescription(T registration) {
this.registration = registration;
}
/**
* Returns the name of the registered Filter or Servlet.
* @return the name
*/
public String getName() {
return this.registration.getName();
}
/**
* Returns the class name of the registered Filter or Servlet.
* @return the class name
*/
public String getClassName() {
return this.registration.getClassName();
}
/**
* Returns the registration that is being described.
* @return the registration
*/
protected final T getRegistration() {
return this.registration;
}
}

View File

@@ -1,48 +0,0 @@
/*
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.web.mappings.servlet;
import java.util.Collection;
import jakarta.servlet.ServletRegistration;
/**
* A mapping description derived from a {@link ServletRegistration}.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
public class ServletRegistrationMappingDescription extends RegistrationMappingDescription<ServletRegistration> {
/**
* Creates a new {@code ServletRegistrationMappingDescription} derived from the given
* {@code servletRegistration}.
* @param servletRegistration the servlet registration
*/
public ServletRegistrationMappingDescription(ServletRegistration servletRegistration) {
super(servletRegistration);
}
/**
* Returns the mappings for the registered servlet.
* @return the mappings
*/
public Collection<String> getMappings() {
return getRegistration().getMappings();
}
}

View File

@@ -1,74 +0,0 @@
/*
* Copyright 2012-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.web.mappings.servlet;
import java.util.Collections;
import java.util.List;
import jakarta.servlet.Servlet;
import jakarta.servlet.ServletContext;
import org.springframework.aot.hint.BindingReflectionHintsRegistrar;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.boot.actuate.web.mappings.MappingDescriptionProvider;
import org.springframework.boot.actuate.web.mappings.servlet.ServletsMappingDescriptionProvider.ServletsMappingDescriptionProviderRuntimeHints;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.web.context.WebApplicationContext;
/**
* A {@link MappingDescriptionProvider} that describes that mappings of any {@link Servlet
* Servlets} registered with a {@link ServletContext}.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
@ImportRuntimeHints(ServletsMappingDescriptionProviderRuntimeHints.class)
public class ServletsMappingDescriptionProvider implements MappingDescriptionProvider {
@Override
public List<ServletRegistrationMappingDescription> describeMappings(ApplicationContext context) {
if (context instanceof WebApplicationContext webApplicationContext) {
return webApplicationContext.getServletContext()
.getServletRegistrations()
.values()
.stream()
.map(ServletRegistrationMappingDescription::new)
.toList();
}
return Collections.emptyList();
}
@Override
public String getMappingName() {
return "servlets";
}
static class ServletsMappingDescriptionProviderRuntimeHints implements RuntimeHintsRegistrar {
private final BindingReflectionHintsRegistrar bindingRegistrar = new BindingReflectionHintsRegistrar();
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
this.bindingRegistrar.registerReflectionHints(hints.reflection(),
ServletRegistrationMappingDescription.class);
}
}
}

View File

@@ -1,20 +0,0 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Actuator servlet request mappings support.
*/
package org.springframework.boot.actuate.web.mappings.servlet;

View File

@@ -1,44 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.web.mappings.servlet;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.boot.actuate.web.mappings.servlet.FiltersMappingDescriptionProvider.FiltersMappingDescriptionProviderRuntimeHints;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link FiltersMappingDescriptionProvider}.
*
* @author Moritz Halbritter
*/
class FiltersMappingDescriptionProviderTests {
@Test
void shouldRegisterHints() {
RuntimeHints runtimeHints = new RuntimeHints();
new FiltersMappingDescriptionProviderRuntimeHints().registerHints(runtimeHints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.reflection()
.onType(FilterRegistrationMappingDescription.class)
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(runtimeHints);
}
}

View File

@@ -1,44 +0,0 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.web.mappings.servlet;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates;
import org.springframework.boot.actuate.web.mappings.servlet.ServletsMappingDescriptionProvider.ServletsMappingDescriptionProviderRuntimeHints;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link ServletsMappingDescriptionProvider}.
*
* @author Moritz Halbritter
*/
class ServletsMappingDescriptionProviderTests {
@Test
void shouldRegisterHints() {
RuntimeHints runtimeHints = new RuntimeHints();
new ServletsMappingDescriptionProviderRuntimeHints().registerHints(runtimeHints, getClass().getClassLoader());
assertThat(RuntimeHintsPredicates.reflection()
.onType(ServletRegistrationMappingDescription.class)
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(runtimeHints);
}
}