18
18
import static org .springframework .util .StringUtils .*;
19
19
20
20
import lombok .RequiredArgsConstructor ;
21
- import lombok .experimental .Delegate ;
22
21
23
22
import java .lang .reflect .Method ;
24
23
import java .net .URI ;
53
52
* @author Kevin Conaway
54
53
* @author Andrew Naydyonock
55
54
* @author Oliver Trosien
55
+ * @author Josh Ghiloni
56
56
* @author Greg Turnquist
57
57
*/
58
58
public class ControllerLinkBuilder extends LinkBuilderSupport <ControllerLinkBuilder > {
59
59
60
60
private static final String REQUEST_ATTRIBUTES_MISSING = "Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?" ;
61
61
private static final CachingAnnotationMappingDiscoverer DISCOVERER = new CachingAnnotationMappingDiscoverer (
62
- new AnnotationMappingDiscoverer (RequestMapping .class ));
62
+ new AnnotationMappingDiscoverer (RequestMapping .class ));
63
63
private static final ControllerLinkBuilderFactory FACTORY = new ControllerLinkBuilderFactory ();
64
64
private static final CustomUriTemplateHandler HANDLER = new CustomUriTemplateHandler ();
65
65
66
+ private static MappingDiscoverer delegateDiscovererOverride = null ;
67
+
66
68
private final TemplateVariables variables ;
67
69
68
70
/**
@@ -78,19 +80,25 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
78
80
}
79
81
80
82
/**
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}.
82
85
*
83
86
* @param uriComponents must not be {@literal null}.
84
87
*/
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 ) {
90
89
91
90
super (uriComponents );
92
91
93
92
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 ;
94
102
}
95
103
96
104
/**
@@ -304,12 +312,20 @@ private static HttpServletRequest getCurrentRequest() {
304
312
@ RequiredArgsConstructor
305
313
private static class CachingAnnotationMappingDiscoverer implements MappingDiscoverer {
306
314
307
- private final @ Delegate AnnotationMappingDiscoverer delegate ;
315
+ private final AnnotationMappingDiscoverer delegate ;
308
316
private final Map <String , UriTemplate > templates = new ConcurrentReferenceHashMap <String , UriTemplate >();
309
317
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
+
310
326
public UriTemplate getMappingAsUriTemplate (Class <?> type , Method method ) {
311
327
312
- String mapping = delegate .getMapping (type , method );
328
+ String mapping = getDelegate () .getMapping (type , method );
313
329
314
330
UriTemplate template = templates .get (mapping );
315
331
@@ -320,6 +336,41 @@ public UriTemplate getMappingAsUriTemplate(Class<?> type, Method method) {
320
336
321
337
return template ;
322
338
}
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
+ }
323
374
}
324
375
325
376
private static class CustomUriTemplateHandler extends DefaultUriTemplateHandler {
0 commit comments