26
26
27
27
import org .springframework .core .MethodParameter ;
28
28
import org .springframework .hateoas .Link ;
29
+ import org .springframework .hateoas .TemplateVariables ;
29
30
import org .springframework .hateoas .server .MethodLinkBuilderFactory ;
31
+ import org .springframework .hateoas .server .core .AdditionalUriHandler ;
30
32
import org .springframework .hateoas .server .core .LinkBuilderSupport ;
33
+ import org .springframework .hateoas .server .core .MethodInvocation ;
31
34
import org .springframework .hateoas .server .core .MethodParameters ;
32
35
import org .springframework .hateoas .server .core .WebHandler ;
36
+ import org .springframework .web .util .UriComponents ;
33
37
import org .springframework .web .util .UriComponentsBuilder ;
34
38
35
39
/**
44
48
* @author Kevin Conaway
45
49
* @author Andrew Naydyonock
46
50
* @author Greg Turnquist
51
+ * @author Réda Housni Alaoui
47
52
*/
48
53
public class WebMvcLinkBuilderFactory implements MethodLinkBuilderFactory <WebMvcLinkBuilder > {
49
54
@@ -106,8 +111,29 @@ public WebMvcLinkBuilder linkTo(Object invocationValue) {
106
111
Function <String , UriComponentsBuilder > builderFactory = mapping -> UriComponentsBuilderFactory .getBuilder ()
107
112
.path (mapping );
108
113
109
- return WebHandler .linkTo (invocationValue , WebMvcLinkBuilder ::new , (builder , invocation ) -> {
114
+ return WebHandler .linkTo (invocationValue , WebMvcLinkBuilder ::new ,
115
+ new UriComponentsContributorsAdditionalUriHandler (uriComponentsContributors ), builderFactory );
116
+ }
110
117
118
+ /*
119
+ * (non-Javadoc)
120
+ * @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(java.lang.reflect.Method, java.lang.Object[])
121
+ */
122
+ @ Override
123
+ public WebMvcLinkBuilder linkTo (Method method , Object ... parameters ) {
124
+ return WebMvcLinkBuilder .linkTo (method , parameters );
125
+ }
126
+
127
+ private static class UriComponentsContributorsAdditionalUriHandler implements AdditionalUriHandler {
128
+
129
+ private final List <UriComponentsContributor > uriComponentsContributors ;
130
+
131
+ private UriComponentsContributorsAdditionalUriHandler (List <UriComponentsContributor > uriComponentsContributors ) {
132
+ this .uriComponentsContributors = uriComponentsContributors ;
133
+ }
134
+
135
+ @ Override
136
+ public UriComponentsBuilder apply (UriComponentsBuilder builder , MethodInvocation invocation ) {
111
137
MethodParameters parameters = MethodParameters .of (invocation .getMethod ());
112
138
Iterator <Object > parameterValues = Arrays .asList (invocation .getArguments ()).iterator ();
113
139
@@ -123,16 +149,22 @@ public WebMvcLinkBuilder linkTo(Object invocationValue) {
123
149
}
124
150
125
151
return builder ;
152
+ }
126
153
127
- }, builderFactory );
128
- }
154
+ @ Override
155
+ public TemplateVariables apply (TemplateVariables templateVariables , UriComponents uriComponents , MethodInvocation invocation ) {
156
+ MethodParameters parameters = MethodParameters .of (invocation .getMethod ());
129
157
130
- /*
131
- * (non-Javadoc)
132
- * @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(java.lang.reflect.Method, java.lang.Object[])
133
- */
134
- @ Override
135
- public WebMvcLinkBuilder linkTo (Method method , Object ... parameters ) {
136
- return WebMvcLinkBuilder .linkTo (method , parameters );
158
+ for (MethodParameter parameter : parameters .getParameters ()) {
159
+
160
+ for (UriComponentsContributor contributor : uriComponentsContributors ) {
161
+ if (contributor .supportsParameter (parameter )) {
162
+ templateVariables = contributor .enhance (templateVariables , uriComponents , parameter );
163
+ }
164
+ }
165
+ }
166
+
167
+ return templateVariables ;
168
+ }
137
169
}
138
170
}
0 commit comments