17
17
18
18
import static org .springframework .util .StringUtils .*;
19
19
20
- import lombok .RequiredArgsConstructor ;
21
- import lombok .experimental .Delegate ;
22
-
23
20
import java .lang .reflect .Method ;
24
21
import java .net .URI ;
25
22
import java .util .Map ;
53
50
* @author Kevin Conaway
54
51
* @author Andrew Naydyonock
55
52
* @author Oliver Trosien
53
+ * @author Josh Ghiloni
54
+ * @author Greg Turnquist
56
55
*/
57
56
public class ControllerLinkBuilder extends LinkBuilderSupport <ControllerLinkBuilder > {
58
57
59
58
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 );
62
60
private static final ControllerLinkBuilderFactory FACTORY = new ControllerLinkBuilderFactory ();
63
61
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler ();
62
+ private static final Map <String , UriTemplate > TEMPLATES = new ConcurrentReferenceHashMap <String , UriTemplate >();
64
63
65
64
private final TemplateVariables variables ;
66
65
66
+ private static MappingDiscoverer discovererOverride = null ;
67
+
67
68
/**
68
69
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponentsBuilder}.
69
70
*
@@ -92,6 +93,14 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
92
93
this .variables = variables ;
93
94
}
94
95
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
+
95
104
/**
96
105
* Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class.
97
106
*
@@ -116,7 +125,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Object... parame
116
125
Assert .notNull (controller , "Controller must not be null!" );
117
126
Assert .notNull (parameters , "Parameters must not be null!" );
118
127
119
- String mapping = DISCOVERER .getMapping (controller );
128
+ String mapping = getDiscoverer () .getMapping (controller );
120
129
121
130
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString (mapping == null ? "/" : mapping );
122
131
UriComponents uriComponents = HANDLER .expandAndEncode (builder , parameters );
@@ -138,7 +147,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Map<String, ?> p
138
147
Assert .notNull (controller , "Controller must not be null!" );
139
148
Assert .notNull (parameters , "Parameters must not be null!" );
140
149
141
- String mapping = DISCOVERER .getMapping (controller );
150
+ String mapping = getDiscoverer () .getMapping (controller );
142
151
143
152
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString (mapping == null ? "/" : mapping );
144
153
UriComponents uriComponents = HANDLER .expandAndEncode (builder , parameters );
@@ -161,7 +170,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Method method, O
161
170
Assert .notNull (controller , "Controller type must not be null!" );
162
171
Assert .notNull (method , "Method must not be null!" );
163
172
164
- UriTemplate template = DISCOVERER . getMappingAsUriTemplate (controller , method );
173
+ UriTemplate template = getUriTemplate ( getDiscoverer (). getMapping (controller , method ) );
165
174
URI uri = template .expand (parameters );
166
175
167
176
return new ControllerLinkBuilder (getBuilder ()).slash (uri );
@@ -295,25 +304,16 @@ private static HttpServletRequest getCurrentRequest() {
295
304
return servletRequest ;
296
305
}
297
306
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 ) {
305
308
306
- String mapping = delegate . getMapping ( type , method );
309
+ UriTemplate template = TEMPLATES . get ( mapping );
307
310
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 );
316
314
}
315
+
316
+ return template ;
317
317
}
318
318
319
319
private static class CustomUriTemplateHandler extends DefaultUriTemplateHandler {
0 commit comments