Skip to content

Commit 498d009

Browse files
Josh Ghilonigregturn
Josh Ghiloni
authored andcommitted
#361 - Add support for resolving values in request mappings
Original pull-request: #391
1 parent 05f687e commit 498d009

8 files changed

+295
-38
lines changed

src/main/java/org/springframework/hateoas/core/AnnotationMappingDiscoverer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public String getMapping(Class<?> type) {
7575
return mapping.length == 0 ? null : mapping[0];
7676
}
7777

78-
/*
78+
/*
7979
* (non-Javadoc)
8080
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.reflect.Method)
8181
*/
@@ -86,7 +86,7 @@ public String getMapping(Method method) {
8686
return getMapping(method.getDeclaringClass(), method);
8787
}
8888

89-
/*
89+
/*
9090
* (non-Javadoc)
9191
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.Class, java.lang.reflect.Method)
9292
*/

src/main/java/org/springframework/hateoas/core/MappingDiscoverer.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* Strategy interface to discover a URI mapping for either a given type or method.
2222
*
2323
* @author Oliver Gierke
24+
* @author Josh Ghiloni
25+
* @author Greg Turnquist
2426
*/
2527
public interface MappingDiscoverer {
2628

@@ -36,7 +38,7 @@ public interface MappingDiscoverer {
3638
* Returns the mapping associated with the given {@link Method}. This will include the type-level mapping.
3739
*
3840
* @param method must not be {@literal null}.
39-
* @return the method mapping including the type-level one or {@literal null} if neither of them present.
41+
* @return the method mapping including the type-level one or {@literal null} if neither are present.
4042
*/
4143
String getMapping(Method method);
4244

@@ -49,4 +51,5 @@ public interface MappingDiscoverer {
4951
* @return the method mapping including the type-level one or {@literal null} if neither of them present.
5052
*/
5153
String getMapping(Class<?> type, Method method);
54+
5255
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2017 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+
* http://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+
package org.springframework.hateoas.core;
17+
18+
import java.lang.reflect.Method;
19+
20+
import org.springframework.core.env.PropertyResolver;
21+
import org.springframework.util.Assert;
22+
23+
/**
24+
* Take any other {@link MappingDiscoverer} and wrap with an attempt to resolve properties via {@link PropertyResolver}.
25+
*
26+
* @author Greg Turnquist
27+
*/
28+
public class PropertyResolvingDiscoverer implements MappingDiscoverer {
29+
30+
private final MappingDiscoverer discoverer;
31+
private final PropertyResolver resolver;
32+
33+
public PropertyResolvingDiscoverer(MappingDiscoverer discoverer, PropertyResolver resolver) {
34+
35+
Assert.notNull(discoverer, "MappingDiscoverer must not be null!");
36+
Assert.notNull(resolver, "PropertyResolver must not be null!");
37+
38+
this.discoverer = discoverer;
39+
this.resolver = resolver;
40+
}
41+
42+
/**
43+
* Returns the mapping associated with the given type.
44+
*
45+
* @param type must not be {@literal null}.
46+
* @return the type-level mapping or {@literal null} in case none is present.
47+
*/
48+
@Override
49+
public String getMapping(Class<?> type) {
50+
return attemptToResolve(resolver, this.discoverer.getMapping(type));
51+
}
52+
53+
/**
54+
* Returns the mapping associated with the given {@link Method}. This will include the type-level mapping.
55+
*
56+
* @param method must not be {@literal null}.
57+
* @return the method mapping including the type-level one or {@literal null} if neither are present.
58+
*/
59+
@Override
60+
public String getMapping(Method method) {
61+
return attemptToResolve(resolver, this.discoverer.getMapping(method));
62+
}
63+
64+
/**
65+
* Returns the mapping for the given {@link Method} invoked on the given type. This can be used to calculate the
66+
* mapping for a super type method being invoked on a sub-type with a type mapping.
67+
*
68+
* @param type must not be {@literal null}.
69+
* @param method must not be {@literal null}.
70+
* @return the method mapping including the type-level one or {@literal null} if neither of them present.
71+
*/
72+
@Override
73+
public String getMapping(Class<?> type, Method method) {
74+
return attemptToResolve(resolver, this.discoverer.getMapping(type, method));
75+
}
76+
77+
/**
78+
* Use the {@link PropertyResolver} to substitute values into the link.
79+
*
80+
* @param resolver
81+
* @param value
82+
* @return
83+
*/
84+
private String attemptToResolve(PropertyResolver resolver, String value) {
85+
return resolver.resolvePlaceholders(value);
86+
}
87+
88+
89+
}

src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilder.java

+31-30
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717

1818
import static org.springframework.util.StringUtils.*;
1919

20-
import lombok.RequiredArgsConstructor;
21-
import lombok.experimental.Delegate;
22-
2320
import java.lang.reflect.Method;
2421
import java.net.URI;
2522
import java.util.Map;
@@ -53,18 +50,21 @@
5350
* @author Kevin Conaway
5451
* @author Andrew Naydyonock
5552
* @author Oliver Trosien
53+
* @author Josh Ghiloni
5654
* @author Greg Turnquist
5755
*/
5856
public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuilder> {
5957

6058
private static final String REQUEST_ATTRIBUTES_MISSING = "Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?";
61-
private static final CachingAnnotationMappingDiscoverer DISCOVERER = new CachingAnnotationMappingDiscoverer(
62-
new AnnotationMappingDiscoverer(RequestMapping.class));
59+
private static final AnnotationMappingDiscoverer DISCOVERER = new AnnotationMappingDiscoverer(RequestMapping.class);
6360
private static final ControllerLinkBuilderFactory FACTORY = new ControllerLinkBuilderFactory();
6461
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler();
62+
private static final Map<String, UriTemplate> TEMPLATES = new ConcurrentReferenceHashMap<String, UriTemplate>();
6563

6664
private final TemplateVariables variables;
6765

66+
private static MappingDiscoverer discovererOverride = null;
67+
6868
/**
6969
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponentsBuilder}.
7070
*
@@ -78,19 +78,29 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
7878
}
7979

8080
/**
81-
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponents}.
81+
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponents}, {@link TemplateVariables}, and
82+
* {@link MappingDiscoverer}.
8283
*
8384
* @param uriComponents must not be {@literal null}.
8485
*/
85-
ControllerLinkBuilder(UriComponents uriComponents) {
86-
this(uriComponents, TemplateVariables.NONE);
87-
}
88-
89-
ControllerLinkBuilder(UriComponents uriComponents, TemplateVariables variables) {
86+
ControllerLinkBuilder(UriComponents uriComponents, TemplateVariables variables, MappingDiscoverer discoverer) {
9087

9188
super(uriComponents);
9289

9390
this.variables = variables;
91+
this.discovererOverride = discoverer;
92+
}
93+
94+
public static void setDiscoverer(MappingDiscoverer discoverer) {
95+
discovererOverride = discoverer;
96+
}
97+
98+
public static void clearDiscoverer() {
99+
discovererOverride = null;
100+
}
101+
102+
private static MappingDiscoverer getDiscoverer() {
103+
return (discovererOverride != null ? discovererOverride : DISCOVERER);
94104
}
95105

96106
/**
@@ -117,7 +127,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Object... parame
117127
Assert.notNull(controller, "Controller must not be null!");
118128
Assert.notNull(parameters, "Parameters must not be null!");
119129

120-
String mapping = DISCOVERER.getMapping(controller);
130+
String mapping = getDiscoverer().getMapping(controller);
121131

122132
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
123133
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
@@ -139,7 +149,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Map<String, ?> p
139149
Assert.notNull(controller, "Controller must not be null!");
140150
Assert.notNull(parameters, "Parameters must not be null!");
141151

142-
String mapping = DISCOVERER.getMapping(controller);
152+
String mapping = getDiscoverer().getMapping(controller);
143153

144154
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
145155
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
@@ -162,7 +172,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Method method, O
162172
Assert.notNull(controller, "Controller type must not be null!");
163173
Assert.notNull(method, "Method must not be null!");
164174

165-
UriTemplate template = DISCOVERER.getMappingAsUriTemplate(controller, method);
175+
UriTemplate template = getUriTemplate(getDiscoverer().getMapping(controller, method));
166176
URI uri = template.expand(parameters);
167177

168178
return new ControllerLinkBuilder(getBuilder()).slash(uri);
@@ -301,25 +311,16 @@ private static HttpServletRequest getCurrentRequest() {
301311
return servletRequest;
302312
}
303313

304-
@RequiredArgsConstructor
305-
private static class CachingAnnotationMappingDiscoverer implements MappingDiscoverer {
306-
307-
private final @Delegate AnnotationMappingDiscoverer delegate;
308-
private final Map<String, UriTemplate> templates = new ConcurrentReferenceHashMap<String, UriTemplate>();
309-
310-
public UriTemplate getMappingAsUriTemplate(Class<?> type, Method method) {
314+
private static UriTemplate getUriTemplate(String mapping) {
311315

312-
String mapping = delegate.getMapping(type, method);
316+
UriTemplate template = TEMPLATES.get(mapping);
313317

314-
UriTemplate template = templates.get(mapping);
315-
316-
if (template == null) {
317-
template = new UriTemplate(mapping);
318-
templates.put(mapping, template);
319-
}
320-
321-
return template;
318+
if (template == null) {
319+
template = new UriTemplate(mapping);
320+
TEMPLATES.put(mapping, template);
322321
}
322+
323+
return template;
323324
}
324325

325326
private static class CustomUriTemplateHandler extends DefaultUriTemplateHandler {

src/main/java/org/springframework/hateoas/mvc/ControllerLinkBuilderFactory.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,26 @@
6464
* @author Oemer Yildiz
6565
* @author Kevin Conaway
6666
* @author Andrew Naydyonock
67+
* @author Josh Ghiloni
68+
* @author Greg Turnquist
6769
*/
6870
public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<ControllerLinkBuilder> {
6971

70-
private static final MappingDiscoverer DISCOVERER = new AnnotationMappingDiscoverer(RequestMapping.class);
7172
private static final AnnotatedParametersParameterAccessor PATH_VARIABLE_ACCESSOR = new AnnotatedParametersParameterAccessor(
7273
new AnnotationAttribute(PathVariable.class));
7374
private static final AnnotatedParametersParameterAccessor REQUEST_PARAM_ACCESSOR = new RequestParamParameterAccessor();
7475

76+
private MappingDiscoverer discoverer;
7577
private List<UriComponentsContributor> uriComponentsContributors = new ArrayList<UriComponentsContributor>();
7678

79+
public ControllerLinkBuilderFactory() {
80+
this.discoverer = new AnnotationMappingDiscoverer(RequestMapping.class);
81+
}
82+
83+
public ControllerLinkBuilderFactory(MappingDiscoverer discoverer) {
84+
this.discoverer = discoverer;
85+
}
86+
7787
/**
7888
* Configures the {@link UriComponentsContributor} to be used when building {@link Link} instances from method
7989
* invocations.
@@ -135,7 +145,7 @@ public ControllerLinkBuilder linkTo(Object invocationValue) {
135145
Iterator<Object> classMappingParameters = invocations.getObjectParameters();
136146
Method method = invocation.getMethod();
137147

138-
String mapping = DISCOVERER.getMapping(invocation.getTargetType(), method);
148+
String mapping = this.discoverer.getMapping(invocation.getTargetType(), method);
139149
UriComponentsBuilder builder = ControllerLinkBuilder.getBuilder().path(mapping);
140150

141151
UriTemplate template = new UriTemplate(mapping);
@@ -183,7 +193,7 @@ public ControllerLinkBuilder linkTo(Object invocationValue) {
183193
variables = variables.concat(variable);
184194
}
185195

186-
return new ControllerLinkBuilder(components, variables);
196+
return new ControllerLinkBuilder(components, variables, discoverer);
187197
}
188198

189199
/*

src/test/java/org/springframework/hateoas/core/AnnotationMappingDiscovererUnitTest.java

+49-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,13 @@
1919
import static org.junit.Assert.*;
2020

2121
import java.lang.reflect.Method;
22+
import java.util.HashMap;
23+
import java.util.Map;
2224

2325
import org.junit.Test;
26+
27+
import org.springframework.core.env.MapPropertySource;
28+
import org.springframework.core.env.StandardEnvironment;
2429
import org.springframework.web.bind.annotation.GetMapping;
2530
import org.springframework.web.bind.annotation.RequestMapping;
2631

@@ -30,10 +35,12 @@
3035
* @author Oliver Gierke
3136
* @author Kevin Conaway
3237
* @author Mark Paluch
38+
* @author Josh Ghiloni
39+
* @author Greg Turnquist
3340
*/
3441
public class AnnotationMappingDiscovererUnitTest {
3542

36-
MappingDiscoverer discoverer = new AnnotationMappingDiscoverer(RequestMapping.class);
43+
AnnotationMappingDiscoverer discoverer = new AnnotationMappingDiscoverer(RequestMapping.class);
3744

3845
@Test(expected = IllegalArgumentException.class)
3946
public void rejectsNullAnnotation() {
@@ -151,6 +158,36 @@ public void discoversMethodLevelMappingUsingComposedAnnotation() throws Exceptio
151158
assertThat(discoverer.getMapping(method), is("/type/otherMethod"));
152159
}
153160

161+
/**
162+
* @see #361
163+
*/
164+
@Test
165+
public void resolvesVariablesInMappings() throws NoSuchMethodException {
166+
167+
Map<String, Object> source = new HashMap<String, Object>();
168+
source.put("test.variable", "dynamicparent");
169+
source.put("test.child", "dynamicchild");
170+
171+
StandardEnvironment env = new StandardEnvironment();
172+
env.getPropertySources().addLast(new MapPropertySource("mapping-env", source));
173+
174+
Method method = DynamicEndpointControllerWithMethod.class.getMethod("method");
175+
176+
// Test regression first
177+
assertThat(discoverer.getMapping(DynamicEndpointController.class), is("/${test.variable}"));
178+
assertThat(discoverer.getMapping(DynamicEndpointControllerWithMethod.class, method),
179+
is("/${test.variable}/${test.child}"));
180+
181+
// Test property substitution
182+
PropertyResolvingDiscoverer propertyResolvingDiscoverer = new PropertyResolvingDiscoverer(discoverer, env);
183+
184+
assertThat(propertyResolvingDiscoverer.getMapping(DynamicEndpointController.class), is("/dynamicparent"));
185+
assertThat(propertyResolvingDiscoverer.getMapping(method), is("/dynamicparent/dynamicchild"));
186+
187+
assertThat(propertyResolvingDiscoverer.getMapping(DynamicEndpointControllerWithMethod.class, method),
188+
is("/dynamicparent/dynamicchild"));
189+
}
190+
154191
@RequestMapping("/type")
155192
interface MyController {
156193

@@ -230,4 +267,15 @@ interface MultipleMappingsController {
230267
@RequestMapping({ "/method", "/methodAlias" })
231268
void method();
232269
}
270+
271+
@RequestMapping("/${test.variable}")
272+
interface DynamicEndpointController {}
273+
274+
@RequestMapping("/${test.variable}")
275+
interface DynamicEndpointControllerWithMethod {
276+
277+
@RequestMapping("/${test.child}")
278+
void method();
279+
}
280+
233281
}

0 commit comments

Comments
 (0)