Skip to content

Commit 53d2ef5

Browse files
Josh Ghilonigregturn
Josh Ghiloni
authored andcommitted
#361 - Add support for resolving values in request mappings
Original pull-request: #391
1 parent 31c8b21 commit 53d2ef5

8 files changed

+291
-31
lines changed

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*
3030
* @author Oliver Gierke
3131
* @author Mark Paluch
32+
* @author Josh Ghiloni
33+
* @author Greg Turnquist
3234
*/
3335
public class AnnotationMappingDiscoverer implements MappingDiscoverer {
3436

@@ -75,7 +77,7 @@ public String getMapping(Class<?> type) {
7577
return mapping.length == 0 ? null : mapping[0];
7678
}
7779

78-
/*
80+
/*
7981
* (non-Javadoc)
8082
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.reflect.Method)
8183
*/
@@ -86,7 +88,7 @@ public String getMapping(Method method) {
8688
return getMapping(method.getDeclaringClass(), method);
8789
}
8890

89-
/*
91+
/*
9092
* (non-Javadoc)
9193
* @see org.springframework.hateoas.core.MappingDiscoverer#getMapping(java.lang.Class, java.lang.reflect.Method)
9294
*/

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

+24-24
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,17 +50,21 @@
5350
* @author Kevin Conaway
5451
* @author Andrew Naydyonock
5552
* @author Oliver Trosien
53+
* @author Josh Ghiloni
54+
* @author Greg Turnquist
5655
*/
5756
public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuilder> {
5857

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

6564
private final TemplateVariables variables;
6665

66+
private static MappingDiscoverer discovererOverride = null;
67+
6768
/**
6869
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponentsBuilder}.
6970
*
@@ -92,6 +93,14 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
9293
this.variables = variables;
9394
}
9495

96+
public static void setDiscoverer(MappingDiscoverer discoverer) {
97+
discovererOverride = discoverer;
98+
}
99+
100+
private static MappingDiscoverer getDiscoverer() {
101+
return (discovererOverride != null ? discovererOverride : DISCOVERER);
102+
}
103+
95104
/**
96105
* Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class.
97106
*
@@ -116,7 +125,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Object... parame
116125
Assert.notNull(controller, "Controller must not be null!");
117126
Assert.notNull(parameters, "Parameters must not be null!");
118127

119-
String mapping = DISCOVERER.getMapping(controller);
128+
String mapping = getDiscoverer().getMapping(controller);
120129

121130
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
122131
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
@@ -138,7 +147,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Map<String, ?> p
138147
Assert.notNull(controller, "Controller must not be null!");
139148
Assert.notNull(parameters, "Parameters must not be null!");
140149

141-
String mapping = DISCOVERER.getMapping(controller);
150+
String mapping = getDiscoverer().getMapping(controller);
142151

143152
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping);
144153
UriComponents uriComponents = HANDLER.expandAndEncode(builder, parameters);
@@ -161,7 +170,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Method method, O
161170
Assert.notNull(controller, "Controller type must not be null!");
162171
Assert.notNull(method, "Method must not be null!");
163172

164-
UriTemplate template = DISCOVERER.getMappingAsUriTemplate(controller, method);
173+
UriTemplate template = getUriTemplate(getDiscoverer().getMapping(controller, method));
165174
URI uri = template.expand(parameters);
166175

167176
return new ControllerLinkBuilder(getBuilder()).slash(uri);
@@ -295,25 +304,16 @@ private static HttpServletRequest getCurrentRequest() {
295304
return servletRequest;
296305
}
297306

298-
@RequiredArgsConstructor
299-
private static class CachingAnnotationMappingDiscoverer implements MappingDiscoverer {
300-
301-
private final @Delegate AnnotationMappingDiscoverer delegate;
302-
private final Map<String, UriTemplate> templates = new ConcurrentReferenceHashMap<String, UriTemplate>();
303-
304-
public UriTemplate getMappingAsUriTemplate(Class<?> type, Method method) {
307+
private static UriTemplate getUriTemplate(String mapping) {
305308

306-
String mapping = delegate.getMapping(type, method);
309+
UriTemplate template = TEMPLATES.get(mapping);
307310

308-
UriTemplate template = templates.get(mapping);
309-
310-
if (template == null) {
311-
template = new UriTemplate(mapping);
312-
templates.put(mapping, template);
313-
}
314-
315-
return template;
311+
if (template == null) {
312+
template = new UriTemplate(mapping);
313+
TEMPLATES.put(mapping, template);
316314
}
315+
316+
return template;
317317
}
318318

319319
private static class CustomUriTemplateHandler extends DefaultUriTemplateHandler {

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

+12-2
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);

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)