1
1
/*
2
- * Copyright 2012-2018 the original author or authors.
2
+ * Copyright 2012-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
18
18
import static org .springframework .hateoas .mvc .ForwardedHeader .*;
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 ;
39
38
import org .springframework .hateoas .core .DummyInvocationUtils .MethodInvocation ;
40
39
import org .springframework .hateoas .core .LinkBuilderSupport ;
41
40
import org .springframework .hateoas .core .MappingDiscoverer ;
41
+ import org .springframework .http .HttpMethod ;
42
42
import org .springframework .http .MediaType ;
43
43
import org .springframework .plugin .core .OrderAwarePluginRegistry ;
44
44
import org .springframework .plugin .core .PluginRegistry ;
63
63
* @author Kevin Conaway
64
64
* @author Andrew Naydyonock
65
65
* @author Oliver Trosien
66
+ * @author Josh Ghiloni
66
67
* @author Greg Turnquist
67
68
*/
68
69
public class ControllerLinkBuilder extends LinkBuilderSupport <ControllerLinkBuilder > {
@@ -82,6 +83,8 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
82
83
.create (factories );
83
84
}
84
85
86
+ private static MappingDiscoverer delegateDiscovererOverride = null ;
87
+
85
88
private final TemplateVariables variables ;
86
89
87
90
/**
@@ -102,17 +105,26 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
102
105
* @param uriComponents must not be {@literal null}.
103
106
*/
104
107
ControllerLinkBuilder (UriComponents uriComponents ) {
105
- this (uriComponents , TemplateVariables .NONE , null );
108
+ this (uriComponents , TemplateVariables .NONE , null , null );
106
109
}
107
110
108
- ControllerLinkBuilder (UriComponents uriComponents , TemplateVariables variables , MethodInvocation invocation ) {
111
+ ControllerLinkBuilder (UriComponents uriComponents , TemplateVariables variables , MethodInvocation invocation , MappingDiscoverer discoverer ) {
109
112
110
113
super (uriComponents );
111
114
112
115
this .variables = variables ;
116
+ ControllerLinkBuilder .delegateDiscovererOverride = discoverer ;
113
117
this .addAffordances (findAffordances (invocation , uriComponents ));
114
118
}
115
119
120
+ public static void setDelegateDiscoverer (MappingDiscoverer delegateDiscovererOverride ) {
121
+ ControllerLinkBuilder .delegateDiscovererOverride = delegateDiscovererOverride ;
122
+ }
123
+
124
+ public static void clearDelegateDiscoverer () {
125
+ ControllerLinkBuilder .delegateDiscovererOverride = null ;
126
+ }
127
+
116
128
/**
117
129
* Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class.
118
130
*
@@ -368,14 +380,72 @@ private static Collection<Affordance> findAffordances(MethodInvocation invocatio
368
380
@ RequiredArgsConstructor
369
381
private static class CachingAnnotationMappingDiscoverer implements MappingDiscoverer {
370
382
371
- private final @ Delegate AnnotationMappingDiscoverer delegate ;
383
+ private final AnnotationMappingDiscoverer delegate ;
372
384
private final Map <String , UriTemplate > templates = new ConcurrentReferenceHashMap <>();
373
385
374
386
public UriTemplate getMappingAsUriTemplate (Class <?> type , Method method ) {
375
387
376
- String mapping = delegate .getMapping (type , method );
388
+ String mapping = getDelegate () .getMapping (type , method );
377
389
return templates .computeIfAbsent (mapping , UriTemplate ::new );
378
390
}
391
+
392
+ /**
393
+ * Returns the mapping associated with the given type.
394
+ *
395
+ * @param type must not be {@literal null}.
396
+ * @return the type-level mapping or {@literal null} in case none is present.
397
+ */
398
+ @ Override
399
+ public String getMapping (Class <?> type ) {
400
+ return getDelegate ().getMapping (type );
401
+ }
402
+
403
+ /**
404
+ * Returns the mapping associated with the given {@link Method}. This will include the type-level mapping.
405
+ *
406
+ * @param method must not be {@literal null}.
407
+ * @return the method mapping including the type-level one or {@literal null} if neither of them present.
408
+ */
409
+ @ Override
410
+ public String getMapping (Method method ) {
411
+ return getDelegate ().getMapping (method );
412
+ }
413
+
414
+ /**
415
+ * Returns the mapping for the given {@link Method} invoked on the given type. This can be used to calculate the
416
+ * mapping for a super type method being invoked on a sub-type with a type mapping.
417
+ *
418
+ * @param type must not be {@literal null}.
419
+ * @param method must not be {@literal null}.
420
+ * @return the method mapping including the type-level one or {@literal null} if neither of them present.
421
+ */
422
+ @ Override
423
+ public String getMapping (Class <?> type , Method method ) {
424
+ return getDelegate ().getMapping (type , method );
425
+ }
426
+
427
+ /**
428
+ * Returns the HTTP verbs for the given {@link Method} invoked on the given type. This can be used to build hypermedia
429
+ * templates.
430
+ *
431
+ * @param type
432
+ * @param method
433
+ * @return a collection of {@link HttpMethod}s.
434
+ */
435
+ @ Override
436
+ public Collection <HttpMethod > getRequestMethod (Class <?> type , Method method ) {
437
+ return getDelegate ().getRequestMethod (type , method );
438
+ }
439
+
440
+ /**
441
+ * If {@link ControllerLinkBuilder} has a static {@link MappingDiscoverer} override, use it instead of the delegate.
442
+ */
443
+ private MappingDiscoverer getDelegate () {
444
+
445
+ return (ControllerLinkBuilder .delegateDiscovererOverride != null )
446
+ ? ControllerLinkBuilder .delegateDiscovererOverride
447
+ : this .delegate ;
448
+ }
379
449
}
380
450
381
451
private static class CustomUriTemplateHandler extends DefaultUriTemplateHandler {
@@ -402,4 +472,4 @@ public UriComponents expandAndEncode(UriComponentsBuilder builder, Object[] uriV
402
472
return super .expandAndEncode (builder , uriVariables );
403
473
}
404
474
}
405
- }
475
+ }
0 commit comments