Skip to content

Commit c7a6613

Browse files
vaadin-botArtur-
andauthored
fix: Always get url mapping using helpers when using Spring Boot (#13791) (#13803)
The properties in application.properties use a relaxed mapping strategy in Spring Boot so all variants of "url-mapping","urlmapping","urlMapping" are mapped to the same value in VaadinConfigurationProperties. Fixes vaadin/spring#478 Co-authored-by: Artur <artur@vaadin.com>
1 parent 9c58563 commit c7a6613

8 files changed

Lines changed: 120 additions & 18 deletions

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/DevModeBrowserLauncher.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ static String getUrl(GenericWebApplicationContext app) {
9999
String host = "http://localhost:" + port;
100100

101101
String path = "/";
102-
String vaadinServletMapping = app.getEnvironment()
103-
.getProperty("vaadin.urlMapping");
102+
String vaadinServletMapping = RootMappedCondition
103+
.getUrlMapping(app.getEnvironment());
104+
104105
ServletContext servletContext = app.getServletContext();
105106
if (servletContext != null) {
106107
String contextPath = servletContext.getContextPath();

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/RootMappedCondition.java‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,17 @@
1515
*/
1616
package com.vaadin.flow.spring;
1717

18+
import java.lang.reflect.InvocationTargetException;
19+
20+
import com.vaadin.flow.server.VaadinServlet;
21+
22+
import org.slf4j.LoggerFactory;
1823
import org.springframework.context.annotation.Condition;
1924
import org.springframework.context.annotation.ConditionContext;
25+
import org.springframework.core.env.Environment;
2026
import org.springframework.core.type.AnnotatedTypeMetadata;
2127
import org.springframework.web.servlet.DispatcherServlet;
2228

23-
import com.vaadin.flow.server.VaadinServlet;
24-
2529
/**
2630
* Condition to check whether the Vaadin servlet is mapped to the root
2731
* ({@literal "/*"}).
@@ -43,8 +47,36 @@ public class RootMappedCondition implements Condition {
4347
@Override
4448
public boolean matches(ConditionContext context,
4549
AnnotatedTypeMetadata metadata) {
46-
return isRootMapping(
47-
context.getEnvironment().getProperty(URL_MAPPING_PROPERTY));
50+
51+
String urlMapping = getUrlMapping(context.getEnvironment());
52+
return isRootMapping(urlMapping);
53+
}
54+
55+
/**
56+
* Gets the url mapping in a way compatible with both plain Spring and
57+
* Spring Boot.
58+
*
59+
* @param environment
60+
* the application environment
61+
* @return the url mapping or null if none is defined
62+
*/
63+
public static String getUrlMapping(Environment environment) {
64+
if (SpringUtil.isSpringBoot()) {
65+
try {
66+
return (String) Class.forName(
67+
"com.vaadin.flow.spring.VaadinConfigurationProperties")
68+
.getMethod("getUrlMapping", Environment.class)
69+
.invoke(null, environment);
70+
} catch (IllegalAccessException | IllegalArgumentException
71+
| InvocationTargetException | NoSuchMethodException
72+
| SecurityException | ClassNotFoundException e) {
73+
LoggerFactory.getLogger(RootMappedCondition.class)
74+
.error("Unable to find urlMapping from properties", e);
75+
return null;
76+
}
77+
} else {
78+
return environment.getProperty(URL_MAPPING_PROPERTY);
79+
}
4880
}
4981

5082
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.vaadin.flow.spring;
2+
3+
/** Helpers related to Spring. */
4+
public class SpringUtil {
5+
6+
/**
7+
* Checks if this is Spring Boot and not plain Spring.
8+
*
9+
* @return true if this is Spring Boot, false if it is Spring without Boot
10+
*/
11+
public static boolean isSpringBoot() {
12+
Class<?> resourcesClass = resolveClass(
13+
SpringVaadinServletService.SPRING_BOOT_WEBPROPERTIES_CLASS);
14+
return (resourcesClass != null);
15+
}
16+
17+
private static Class<?> resolveClass(String clazzName) {
18+
try {
19+
return Class.forName(clazzName, false,
20+
SpringVaadinServletService.class.getClassLoader());
21+
} catch (LinkageError | ClassNotFoundException e) {
22+
return null;
23+
}
24+
}
25+
26+
}

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/SpringVaadinServletService.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class SpringVaadinServletService extends VaadinServletService {
4747

4848
private final Registration serviceDestroyRegistration;
4949

50-
private static final String SPRING_BOOT_WEBPROPROPERTIES_CLASS = "org.springframework.boot.autoconfigure.web.WebProperties";
50+
static final String SPRING_BOOT_WEBPROPERTIES_CLASS = "org.springframework.boot.autoconfigure.web.WebProperties";
5151

5252
/**
5353
* Creates an instance connected to the given servlet and using the given
@@ -161,8 +161,7 @@ private String getFullPath(String path, String prefix) {
161161
* ClassNotFound or similar exceptions in plain Spring.
162162
*/
163163
private boolean isSpringBootConfigured() {
164-
Class<?> resourcesClass = resolveClass(
165-
SPRING_BOOT_WEBPROPROPERTIES_CLASS);
164+
Class<?> resourcesClass = resolveClass(SPRING_BOOT_WEBPROPERTIES_CLASS);
166165
if (resourcesClass != null) {
167166
return context.getBeanNamesForType(resourcesClass).length != 0;
168167
}

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/VaadinConfigurationProperties.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.List;
2121

2222
import org.springframework.boot.context.properties.ConfigurationProperties;
23+
import org.springframework.boot.context.properties.bind.Binder;
24+
import org.springframework.core.env.Environment;
2325

2426
/**
2527
* Configuration properties for Vaadin Spring Boot.
@@ -31,6 +33,22 @@
3133
@ConfigurationProperties(prefix = "vaadin")
3234
public class VaadinConfigurationProperties {
3335

36+
/**
37+
* Gets the url mapping using the given environment.
38+
*
39+
* This is needed only when VaadinConfigurationProperties is not available
40+
* for injection, e.g. in a condition.
41+
*
42+
* @param environment
43+
* the application environment
44+
* @return the url mapping or null if none is defined
45+
*/
46+
public static String getUrlMapping(Environment environment) {
47+
return Binder.get(environment)
48+
.bind("vaadin", VaadinConfigurationProperties.class)
49+
.map(conf -> conf.getUrlMapping()).orElse(null);
50+
}
51+
3452
/**
3553
* Base URL mapping of the Vaadin servlet.
3654
*/

‎vaadin-spring/src/main/java/com/vaadin/flow/spring/VaadinMVCWebAppInitializer.java‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
*/
1616
package com.vaadin.flow.spring;
1717

18-
import javax.servlet.ServletContext;
19-
import javax.servlet.ServletException;
20-
import javax.servlet.ServletRegistration.Dynamic;
21-
2218
import java.util.Collection;
2319
import java.util.HashMap;
2420
import java.util.Map;
2521
import java.util.stream.Stream;
2622

23+
import javax.servlet.ServletContext;
24+
import javax.servlet.ServletException;
25+
import javax.servlet.ServletRegistration.Dynamic;
26+
27+
import com.vaadin.flow.server.Constants;
28+
2729
import org.springframework.core.env.Environment;
2830
import org.springframework.util.ClassUtils;
2931
import org.springframework.web.WebApplicationInitializer;
3032
import org.springframework.web.context.ContextLoaderListener;
3133
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
3234
import org.springframework.web.servlet.DispatcherServlet;
3335

34-
import com.vaadin.flow.server.Constants;
35-
3636
/**
3737
* Abstract Vaadin Spring MVC {@link WebApplicationInitializer}.
3838
* <p>
@@ -56,8 +56,10 @@ public void onStartup(ServletContext servletContext)
5656
context.refresh();
5757

5858
Environment env = context.getBean(Environment.class);
59-
String mapping = env
60-
.getProperty(RootMappedCondition.URL_MAPPING_PROPERTY, "/*");
59+
String mapping = RootMappedCondition.getUrlMapping(env);
60+
if (mapping == null) {
61+
mapping = "/*";
62+
}
6163

6264
boolean rootMapping = RootMappedCondition.isRootMapping(mapping);
6365

‎vaadin-spring/src/test/java/com/vaadin/flow/spring/DevModeBrowserLauncherVaadinAndServletMappingTest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import org.springframework.test.context.TestPropertySource;
2222

2323
@TestPropertySource(properties = { "server.port = 1234",
24-
"vaadin.urlMapping=/ui/*" })
24+
"vaadin.url-mapping=/ui/*" })
2525
public class DevModeBrowserLauncherVaadinAndServletMappingTest
2626
extends AbstractDevModeBrowserLauncherTest {
2727

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.vaadin.flow.spring;
2+
3+
import org.junit.Assert;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.mock.env.MockEnvironment;
6+
7+
public class RootMappedConditionTest {
8+
9+
@Test
10+
public void lookupProperty() {
11+
assertUrlMapping("url-mapping");
12+
assertUrlMapping("urlmapping");
13+
assertUrlMapping("urlMapping");
14+
assertUrlMapping("URL-mapping");
15+
assertUrlMapping("URLMAPPING");
16+
}
17+
18+
private void assertUrlMapping(String key) {
19+
MockEnvironment environment = new MockEnvironment();
20+
environment.setProperty("vaadin." + key, "abc");
21+
Assert.assertEquals("abc",
22+
RootMappedCondition.getUrlMapping(environment));
23+
}
24+
}

0 commit comments

Comments
 (0)