Skip to content

Commit 518c1d4

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 518c1d4

8 files changed

+318
-18
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,86 @@
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 it 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+
this.discoverer = discoverer;
38+
this.resolver = resolver;
39+
}
40+
41+
/**
42+
* Returns the mapping associated with the given type.
43+
*
44+
* @param type must not be {@literal null}.
45+
* @return the type-level mapping or {@literal null} in case none is present.
46+
*/
47+
@Override
48+
public String getMapping(Class<?> type) {
49+
return attemptToResolve(resolver, this.discoverer.getMapping(type));
50+
}
51+
52+
/**
53+
* Returns the mapping associated with the given {@link Method}. This will include the type-level mapping.
54+
*
55+
* @param method must not be {@literal null}.
56+
* @return the method mapping including the type-level one or {@literal null} if neither of them present.
57+
*/
58+
@Override
59+
public String getMapping(Method method) {
60+
return attemptToResolve(resolver, this.discoverer.getMapping(method));
61+
}
62+
63+
/**
64+
* Returns the mapping for the given {@link Method} invoked on the given type. This can be used to calculate the
65+
* mapping for a super type method being invoked on a sub-type with a type mapping.
66+
*
67+
* @param type must not be {@literal null}.
68+
* @param method must not be {@literal null}.
69+
* @return the method mapping including the type-level one or {@literal null} if neither of them present.
70+
*/
71+
@Override
72+
public String getMapping(Class<?> type, Method method) {
73+
return attemptToResolve(resolver, this.discoverer.getMapping(type, method));
74+
}
75+
76+
/**
77+
* Use the {@link PropertyResolver} to substitute values into the link.
78+
*
79+
* @param resolver
80+
* @param value
81+
* @return
82+
*/
83+
private String attemptToResolve(PropertyResolver resolver, String value) {
84+
return resolver.resolvePlaceholders(value);
85+
}
86+
}

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

+61-10
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import static org.springframework.util.StringUtils.*;
1919

2020
import lombok.RequiredArgsConstructor;
21-
import lombok.experimental.Delegate;
2221

2322
import java.lang.reflect.Method;
2423
import java.net.URI;
@@ -53,16 +52,19 @@
5352
* @author Kevin Conaway
5453
* @author Andrew Naydyonock
5554
* @author Oliver Trosien
55+
* @author Josh Ghiloni
5656
* @author Greg Turnquist
5757
*/
5858
public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuilder> {
5959

6060
private static final String REQUEST_ATTRIBUTES_MISSING = "Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?";
6161
private static final CachingAnnotationMappingDiscoverer DISCOVERER = new CachingAnnotationMappingDiscoverer(
62-
new AnnotationMappingDiscoverer(RequestMapping.class));
62+
new AnnotationMappingDiscoverer(RequestMapping.class));
6363
private static final ControllerLinkBuilderFactory FACTORY = new ControllerLinkBuilderFactory();
6464
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler();
6565

66+
private static MappingDiscoverer delegateDiscovererOverride = null;
67+
6668
private final TemplateVariables variables;
6769

6870
/**
@@ -78,19 +80,25 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
7880
}
7981

8082
/**
81-
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponents}.
83+
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponents}, {@link TemplateVariables}, and
84+
* {@link MappingDiscoverer}.
8285
*
8386
* @param uriComponents must not be {@literal null}.
8487
*/
85-
ControllerLinkBuilder(UriComponents uriComponents) {
86-
this(uriComponents, TemplateVariables.NONE);
87-
}
88-
89-
ControllerLinkBuilder(UriComponents uriComponents, TemplateVariables variables) {
88+
ControllerLinkBuilder(UriComponents uriComponents, TemplateVariables variables, MappingDiscoverer discoverer) {
9089

9190
super(uriComponents);
9291

9392
this.variables = variables;
93+
this.delegateDiscovererOverride = discoverer;
94+
}
95+
96+
public static void setDelegateDiscoverer(MappingDiscoverer discoverer) {
97+
delegateDiscovererOverride = discoverer;
98+
}
99+
100+
public static void clearDelegateDiscoverer() {
101+
delegateDiscovererOverride = null;
94102
}
95103

96104
/**
@@ -304,12 +312,20 @@ private static HttpServletRequest getCurrentRequest() {
304312
@RequiredArgsConstructor
305313
private static class CachingAnnotationMappingDiscoverer implements MappingDiscoverer {
306314

307-
private final @Delegate AnnotationMappingDiscoverer delegate;
315+
private final AnnotationMappingDiscoverer delegate;
308316
private final Map<String, UriTemplate> templates = new ConcurrentReferenceHashMap<String, UriTemplate>();
309317

318+
/**
319+
* If {@link ControllerLinkBuilder} has a static {@link MappingDiscoverer}, use it instead of the delegate.
320+
* @return
321+
*/
322+
private MappingDiscoverer getDelegate() {
323+
return (delegateDiscovererOverride != null) ? delegateDiscovererOverride : this.delegate;
324+
}
325+
310326
public UriTemplate getMappingAsUriTemplate(Class<?> type, Method method) {
311327

312-
String mapping = delegate.getMapping(type, method);
328+
String mapping = getDelegate().getMapping(type, method);
313329

314330
UriTemplate template = templates.get(mapping);
315331

@@ -320,6 +336,41 @@ public UriTemplate getMappingAsUriTemplate(Class<?> type, Method method) {
320336

321337
return template;
322338
}
339+
340+
/**
341+
* Returns the mapping associated with the given type.
342+
*
343+
* @param type must not be {@literal null}.
344+
* @return the type-level mapping or {@literal null} in case none is present.
345+
*/
346+
@Override
347+
public String getMapping(Class<?> type) {
348+
return getDelegate().getMapping(type);
349+
}
350+
351+
/**
352+
* Returns the mapping associated with the given {@link Method}. This will include the type-level mapping.
353+
*
354+
* @param method must not be {@literal null}.
355+
* @return the method mapping including the type-level one or {@literal null} if neither of them present.
356+
*/
357+
@Override
358+
public String getMapping(Method method) {
359+
return getDelegate().getMapping(method);
360+
}
361+
362+
/**
363+
* Returns the mapping for the given {@link Method} invoked on the given type. This can be used to calculate the
364+
* mapping for a super type method being invoked on a sub-type with a type mapping.
365+
*
366+
* @param type must not be {@literal null}.
367+
* @param method must not be {@literal null}.
368+
* @return the method mapping including the type-level one or {@literal null} if neither of them present.
369+
*/
370+
@Override
371+
public String getMapping(Class<?> type, Method method) {
372+
return getDelegate().getMapping(type, method);
373+
}
323374
}
324375

325376
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

+46-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
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+
import org.springframework.core.env.MapPropertySource;
27+
import org.springframework.core.env.StandardEnvironment;
2428
import org.springframework.web.bind.annotation.GetMapping;
2529
import org.springframework.web.bind.annotation.RequestMapping;
2630

@@ -30,10 +34,12 @@
3034
* @author Oliver Gierke
3135
* @author Kevin Conaway
3236
* @author Mark Paluch
37+
* @author Josh Ghiloni
38+
* @author Greg Turnquist
3339
*/
3440
public class AnnotationMappingDiscovererUnitTest {
3541

36-
MappingDiscoverer discoverer = new AnnotationMappingDiscoverer(RequestMapping.class);
42+
AnnotationMappingDiscoverer discoverer = new AnnotationMappingDiscoverer(RequestMapping.class);
3743

3844
@Test(expected = IllegalArgumentException.class)
3945
public void rejectsNullAnnotation() {
@@ -151,6 +157,35 @@ public void discoversMethodLevelMappingUsingComposedAnnotation() throws Exceptio
151157
assertThat(discoverer.getMapping(method), is("/type/otherMethod"));
152158
}
153159

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

@@ -230,4 +265,14 @@ interface MultipleMappingsController {
230265
@RequestMapping({ "/method", "/methodAlias" })
231266
void method();
232267
}
268+
269+
@RequestMapping("/${test.variable}")
270+
interface DynamicEndpointController {}
271+
272+
@RequestMapping("/${test.variable}")
273+
interface DynamicEndpointControllerWithMethod {
274+
275+
@RequestMapping("/${test.child}")
276+
void method();
277+
}
233278
}

0 commit comments

Comments
 (0)