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
56
54
* @author Greg Turnquist
57
55
*/
58
56
public class ControllerLinkBuilder extends LinkBuilderSupport <ControllerLinkBuilder > {
59
57
60
58
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 );
63
60
private static final ControllerLinkBuilderFactory FACTORY = new ControllerLinkBuilderFactory ();
64
61
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler ();
62
+ private static final Map <String , UriTemplate > TEMPLATES = new ConcurrentReferenceHashMap <String , UriTemplate >();
65
63
66
64
private final TemplateVariables variables ;
67
65
66
+ private static MappingDiscoverer discovererOverride = null ;
67
+
68
68
/**
69
69
* Creates a new {@link ControllerLinkBuilder} using the given {@link UriComponentsBuilder}.
70
70
*
@@ -78,19 +78,29 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
78
78
}
79
79
80
80
/**
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}.
82
83
*
83
84
* @param uriComponents must not be {@literal null}.
84
85
*/
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 ) {
90
87
91
88
super (uriComponents );
92
89
93
90
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 );
94
104
}
95
105
96
106
/**
@@ -117,7 +127,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Object... parame
117
127
Assert .notNull (controller , "Controller must not be null!" );
118
128
Assert .notNull (parameters , "Parameters must not be null!" );
119
129
120
- String mapping = DISCOVERER .getMapping (controller );
130
+ String mapping = getDiscoverer () .getMapping (controller );
121
131
122
132
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString (mapping == null ? "/" : mapping );
123
133
UriComponents uriComponents = HANDLER .expandAndEncode (builder , parameters );
@@ -139,7 +149,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Map<String, ?> p
139
149
Assert .notNull (controller , "Controller must not be null!" );
140
150
Assert .notNull (parameters , "Parameters must not be null!" );
141
151
142
- String mapping = DISCOVERER .getMapping (controller );
152
+ String mapping = getDiscoverer () .getMapping (controller );
143
153
144
154
UriComponentsBuilder builder = UriComponentsBuilder .fromUriString (mapping == null ? "/" : mapping );
145
155
UriComponents uriComponents = HANDLER .expandAndEncode (builder , parameters );
@@ -162,7 +172,7 @@ public static ControllerLinkBuilder linkTo(Class<?> controller, Method method, O
162
172
Assert .notNull (controller , "Controller type must not be null!" );
163
173
Assert .notNull (method , "Method must not be null!" );
164
174
165
- UriTemplate template = DISCOVERER . getMappingAsUriTemplate (controller , method );
175
+ UriTemplate template = getUriTemplate ( getDiscoverer (). getMapping (controller , method ) );
166
176
URI uri = template .expand (parameters );
167
177
168
178
return new ControllerLinkBuilder (getBuilder ()).slash (uri );
@@ -301,25 +311,16 @@ private static HttpServletRequest getCurrentRequest() {
301
311
return servletRequest ;
302
312
}
303
313
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 ) {
311
315
312
- String mapping = delegate . getMapping ( type , method );
316
+ UriTemplate template = TEMPLATES . get ( mapping );
313
317
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 );
322
321
}
322
+
323
+ return template ;
323
324
}
324
325
325
326
private static class CustomUriTemplateHandler extends DefaultUriTemplateHandler {
0 commit comments