Skip to content

Commit a09ecf1

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

File tree

8 files changed

+166
-595
lines changed

8 files changed

+166
-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: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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 jakarta.servlet.http.HttpServletRequest;
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.http.HttpHeaders;
25+
import org.springframework.stereotype.Controller;
26+
import org.springframework.web.bind.annotation.CrossOrigin;
27+
import org.springframework.web.bind.annotation.PostMapping;
28+
import org.springframework.web.context.WebApplicationContext;
29+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
30+
import org.springframework.web.cors.PreFlightRequestHandler;
31+
import org.springframework.web.servlet.NoHandlerFoundException;
32+
import org.springframework.web.servlet.function.RouterFunction;
33+
import org.springframework.web.servlet.function.RouterFunctions;
34+
import org.springframework.web.servlet.function.ServerResponse;
35+
import org.springframework.web.servlet.function.support.RouterFunctionMapping;
36+
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
37+
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
38+
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
39+
40+
import static org.assertj.core.api.Assertions.assertThat;
41+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
42+
43+
/**
44+
* Tests for {@link DefaultPreFlightRequestHandler}.
45+
*
46+
* @since 7.1
47+
*/
48+
class DefaultPreFlightRequestHandlerTests {
49+
50+
@Test
51+
void handlePreFlight() throws Exception {
52+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
53+
context.register(TestConfig.class);
54+
context.refresh();
55+
56+
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/path");
57+
request.addHeader("Origin", "http://localhost:9000");
58+
request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST");
59+
MockHttpServletResponse response = new MockHttpServletResponse();
60+
61+
initHandler(context).handlePreFlight(request, response);
62+
63+
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isEqualTo("http://localhost:9000");
64+
assertThat(response.getHeaders(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)).containsExactly("POST");
65+
}
66+
67+
@Test
68+
void handlePreFlightWithNoHandlerFoundException() {
69+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
70+
context.register(TestConfig.class);
71+
context.refresh();
72+
73+
MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/unknownPath");
74+
request.addHeader("Origin", "http://localhost:9000");
75+
request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST");
76+
MockHttpServletResponse response = new MockHttpServletResponse();
77+
78+
assertThatThrownBy(() -> initHandler(context).handlePreFlight(request, response))
79+
.isInstanceOf(NoHandlerFoundException.class);
80+
81+
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN)).isNull();
82+
assertThat(response.getHeader(HttpHeaders.ACCESS_CONTROL_ALLOW_METHODS)).isNull();
83+
}
84+
85+
private static PreFlightRequestHandler initHandler(WebApplicationContext context) {
86+
DefaultPreFlightRequestHandler preFlightRequestHandler = new DefaultPreFlightRequestHandler();
87+
preFlightRequestHandler.setApplicationContext(context);
88+
preFlightRequestHandler.afterPropertiesSet();
89+
return preFlightRequestHandler;
90+
}
91+
92+
93+
@Configuration
94+
static class TestConfig {
95+
96+
@Bean
97+
public RouterFunctionMapping routerFunctionMapping() {
98+
RouterFunctionMapping mapping = new RouterFunctionMapping();
99+
mapping.setOrder(1);
100+
return mapping;
101+
}
102+
103+
@Bean
104+
public RequestMappingHandlerMapping handlerMapping() {
105+
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
106+
mapping.setOrder(2);
107+
return mapping;
108+
}
109+
110+
@Bean
111+
public TestController testController() {
112+
return new TestController();
113+
}
114+
115+
@Bean
116+
public RouterFunction<?> routerFunction() {
117+
return RouterFunctions.route().GET("/fn-path", request -> ServerResponse.ok().build()).build();
118+
}
119+
}
120+
121+
122+
@CrossOrigin("http://localhost:9000")
123+
@Controller
124+
private static class TestController {
125+
126+
@PostMapping("/path")
127+
void handle() {
128+
}
129+
}
130+
131+
private static class TestMatchableHandlerMapping extends SimpleUrlHandlerMapping {
132+
133+
private int invocationCount;
134+
135+
private int matchCount;
136+
137+
public int getInvocationCount() {
138+
return this.invocationCount;
139+
}
140+
141+
public int getMatchCount() {
142+
return this.matchCount;
143+
}
144+
145+
@Override
146+
protected Object getHandlerInternal(HttpServletRequest request) throws Exception {
147+
this.invocationCount++;
148+
Object handler = super.getHandlerInternal(request);
149+
if (handler != null) {
150+
this.matchCount++;
151+
}
152+
return handler;
153+
}
154+
}
155+
}

0 commit comments

Comments
 (0)