Skip to content

Commit 25f758d

Browse files
committed
Fix: cleanup
Signed-off-by: anaconda875 <hflbtmax@gmail.com>
1 parent 983a8dc commit 25f758d

File tree

8 files changed

+140
-595
lines changed

8 files changed

+140
-595
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/MvcNamespaceUtils.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import org.springframework.web.servlet.DispatcherServlet;
3636
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
3737
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
38-
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
3938
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
4039
import org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter;
4140
import org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter;
@@ -72,14 +71,11 @@ public abstract class MvcNamespaceUtils {
7271

7372
private static final String CORS_CONFIGURATION_BEAN_NAME = "mvcCorsConfigurations";
7473

75-
private static final String HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME = "mvcHandlerMappingIntrospector";
76-
7774

7875
public static void registerDefaultComponents(ParserContext context, @Nullable Object source) {
7976
registerBeanNameUrlHandlerMapping(context, source);
8077
registerHttpRequestHandlerAdapter(context, source);
8178
registerSimpleControllerHandlerAdapter(context, source);
82-
registerHandlerMappingIntrospector(context, source);
8379
registerLocaleResolver(context, source);
8480
registerViewNameTranslator(context, source);
8581
registerFlashMapManager(context, source);
@@ -275,22 +271,6 @@ else if (corsConfigurations != null) {
275271
return new RuntimeBeanReference(CORS_CONFIGURATION_BEAN_NAME);
276272
}
277273

278-
/**
279-
* Registers an {@link HandlerMappingIntrospector} under a well-known name
280-
* unless already registered.
281-
*/
282-
@SuppressWarnings("removal")
283-
private static void registerHandlerMappingIntrospector(ParserContext context, @Nullable Object source) {
284-
if (!context.getRegistry().containsBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) {
285-
RootBeanDefinition beanDef = new RootBeanDefinition(HandlerMappingIntrospector.class);
286-
beanDef.setSource(source);
287-
beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
288-
beanDef.setLazyInit(true);
289-
context.getRegistry().registerBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME, beanDef);
290-
context.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME));
291-
}
292-
}
293-
294274
/**
295275
* Registers an {@link AcceptHeaderLocaleResolver} under a well-known name
296276
* unless already registered.

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/DefaultPreFlightRequestHandler.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,17 @@
1616

1717
package org.springframework.web.servlet.handler;
1818

19+
import java.io.IOException;
20+
import java.util.ArrayList;
21+
import java.util.Collections;
22+
import java.util.List;
23+
import java.util.Map;
24+
import java.util.Properties;
25+
1926
import jakarta.servlet.http.HttpServletRequest;
2027
import jakarta.servlet.http.HttpServletResponse;
2128
import org.jspecify.annotations.Nullable;
29+
2230
import org.springframework.beans.factory.BeanFactoryUtils;
2331
import org.springframework.beans.factory.InitializingBean;
2432
import org.springframework.context.ApplicationContext;
@@ -40,13 +48,6 @@
4048
import org.springframework.web.servlet.NoHandlerFoundException;
4149
import org.springframework.web.util.ServletRequestPathUtils;
4250

43-
import java.io.IOException;
44-
import java.util.ArrayList;
45-
import java.util.Collections;
46-
import java.util.List;
47-
import java.util.Map;
48-
import java.util.Properties;
49-
5051
/**
5152
* Default implementation of {@link PreFlightRequestHandler} that discovers all
5253
* {@link HandlerMapping} beans in the {@link ApplicationContext} and uses them to
@@ -57,6 +58,7 @@
5758
* {@link DispatcherServlet}, with fallback to the default mappings configured in
5859
* {@code DispatcherServlet.properties} if no mappings are found.
5960
*
61+
* @author Rossen Stoyanchev
6062
* @since 7.1
6163
* @see PreFlightRequestHandler
6264
* @see HandlerMapping

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/MatchableHandlerMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*
3232
* @author Rossen Stoyanchev
3333
* @since 4.3.1
34-
* @deprecated together with {@link HandlerMappingIntrospector} without a replacement.
34+
* @deprecated without a replacement.
3535
*/
3636
@Deprecated(since = "7.0", forRemoval = true)
3737
public interface MatchableHandlerMapping extends HandlerMapping {

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/PathPatternMatchableHandlerMapping.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/RequestMatchResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @author Rossen Stoyanchev
3636
* @since 4.3.1
37-
* @deprecated together with {@link HandlerMappingIntrospector} without a replacement.
37+
* @deprecated without a replacement.
3838
*/
3939
@SuppressWarnings("removal")
4040
@Deprecated(since = "7.0", forRemoval = true)

spring-webmvc/src/test/java/org/springframework/web/servlet/config/MvcNamespaceTests.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
9696
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
9797
import org.springframework.web.servlet.handler.ConversionServiceExposingInterceptor;
98-
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
9998
import org.springframework.web.servlet.handler.MappedInterceptor;
10099
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
101100
import org.springframework.web.servlet.handler.UserRoleAuthorizationInterceptor;
@@ -260,13 +259,6 @@ void defaultConfig() throws Exception {
260259
CompositeUriComponentsContributor.class);
261260

262261
assertThat(uriComponentsContributor).isNotNull();
263-
264-
String name = "mvcHandlerMappingIntrospector";
265-
HandlerMappingIntrospector introspector = this.appContext.getBean(name, HandlerMappingIntrospector.class);
266-
assertThat(introspector).isNotNull();
267-
assertThat(introspector.getHandlerMappings()).hasSize(2);
268-
assertThat(introspector.getHandlerMappings()).element(0).isSameAs(mapping);
269-
assertThat(introspector.getHandlerMappings().get(1).getClass()).isEqualTo(BeanNameUrlHandlerMapping.class);
270262
}
271263

272264
@Test // gh-25290
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright 2002-present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.handler;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.context.annotation.Bean;
22+
import org.springframework.context.annotation.Configuration;
23+
import org.springframework.http.HttpHeaders;
24+
import org.springframework.stereotype.Controller;
25+
import org.springframework.web.bind.annotation.CrossOrigin;
26+
import org.springframework.web.bind.annotation.PostMapping;
27+
import org.springframework.web.context.WebApplicationContext;
28+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
29+
import org.springframework.web.cors.PreFlightRequestHandler;
30+
import org.springframework.web.servlet.NoHandlerFoundException;
31+
import org.springframework.web.servlet.function.RouterFunction;
32+
import org.springframework.web.servlet.function.RouterFunctions;
33+
import org.springframework.web.servlet.function.ServerResponse;
34+
import org.springframework.web.servlet.function.support.RouterFunctionMapping;
35+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
36+
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
37+
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
38+
39+
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
41+
42+
/**
43+
* Tests for {@link DefaultPreFlightRequestHandler}.
44+
*
45+
* @since 7.1
46+
*/
47+
class DefaultPreFlightRequestHandlerTests {
48+
49+
@Test
50+
void handlePreFlight() throws Exception {
51+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
52+
context.register(TestConfig.class);
53+
context.refresh();
54+
55+
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/path");
56+
request.addHeader("Origin", "http://localhost:9000");
57+
request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST");
58+
MockHttpServletResponse response = new MockHttpServletResponse();
59+
60+
initHandler(context).handlePreFlight(request, response);
61+
62+
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://localhost:9000");
63+
assertThat(response.getHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)).containsExactly("POST");
64+
}
65+
66+
@Test
67+
void handlePreFlightWithNoHandlerFoundException() {
68+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
69+
context.register(TestConfig.class);
70+
context.refresh();
71+
72+
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/unknownPath");
73+
request.addHeader("Origin", "http://localhost:9000");
74+
request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST");
75+
MockHttpServletResponse response = new MockHttpServletResponse();
76+
77+
assertThatThrownBy(() -> initHandler(context).handlePreFlight(request, response))
78+
.isInstanceOf(NoHandlerFoundException.class);
79+
80+
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isNull();
81+
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)).isNull();
82+
}
83+
84+
private static PreFlightRequestHandler initHandler(WebApplicationContext context) {
85+
DefaultPreFlightRequestHandler preFlightRequestHandler = new DefaultPreFlightRequestHandler();
86+
preFlightRequestHandler.setApplicationContext(context);
87+
preFlightRequestHandler.afterPropertiesSet();
88+
return preFlightRequestHandler;
89+
}
90+
91+
92+
@Configuration
93+
static class TestConfig {
94+
95+
@Bean
96+
public RouterFunctionMapping routerFunctionMapping() {
97+
RouterFunctionMapping mapping = new RouterFunctionMapping();
98+
mapping.setOrder(1);
99+
return mapping;
100+
}
101+
102+
@Bean
103+
public RequestMappingHandlerMapping handlerMapping() {
104+
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
105+
mapping.setOrder(2);
106+
return mapping;
107+
}
108+
109+
@Bean
110+
TestController testController() {
111+
return new TestController();
112+
}
113+
114+
@Bean
115+
public RouterFunction<?> routerFunction() {
116+
return RouterFunctions.route().GET("/fn-path", request -> ServerResponse.ok().build()).build();
117+
}
118+
}
119+
120+
121+
@CrossOrigin("http://localhost:9000")
122+
@Controller
123+
private static class TestController {
124+
125+
@PostMapping("/path")
126+
void handle() {
127+
}
128+
}
129+
}

0 commit comments

Comments
 (0)