-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathAffordanceModel.java
More file actions
584 lines (491 loc) · 13.5 KB
/
AffordanceModel.java
File metadata and controls
584 lines (491 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.hateoas;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.core.ResolvableType;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Collection of attributes needed to render any form of hypermedia.
*
* @author Greg Turnquist
* @author Oliver Drotbohm
*/
public abstract class AffordanceModel {
/**
* Name for the REST action of this resource.
*/
private String name;
/**
* {@link Link} for the URI of the resource.
*/
private Link link;
/**
* Request method verb for this resource. For multiple methods, add multiple {@link Affordance}s.
*/
private HttpMethod httpMethod;
/**
* Domain type used to create a new resource.
*/
private InputPayloadMetadata input;
/**
* Collection of {@link QueryParameter}s to interrogate a resource.
*/
private List<QueryParameter> queryMethodParameters;
/**
* Response body domain type.
*/
private PayloadMetadata output;
public AffordanceModel(String name, Link link, HttpMethod httpMethod, InputPayloadMetadata input,
List<QueryParameter> queryMethodParameters, PayloadMetadata output) {
this.name = name;
this.link = link;
this.httpMethod = httpMethod;
this.input = input;
this.queryMethodParameters = queryMethodParameters;
this.output = output;
}
/**
* Expand the {@link Link} into an {@literal href} with no parameters.
*
* @return
*/
public String getURI() {
return this.link.expand().getHref();
}
/**
* Returns whether the {@link Affordance} has the given {@link HttpMethod}.
*
* @param method must not be {@literal null}.
* @return
*/
public boolean hasHttpMethod(HttpMethod method) {
Assert.notNull(method, "HttpMethod must not be null!");
return this.httpMethod.equals(method);
}
/**
* Returns whether the {@link Affordance} points to the target of the given {@link Link}.
*
* @param link must not be {@literal null}.
* @return
*/
public boolean pointsToTargetOf(Link link) {
Assert.notNull(link, "Link must not be null!");
return getURI().equals(link.expand().getHref());
}
public String getName() {
return this.name;
}
public Link getLink() {
return this.link;
}
public HttpMethod getHttpMethod() {
return this.httpMethod;
}
public InputPayloadMetadata getInput() {
return this.input;
}
public List<QueryParameter> getQueryMethodParameters() {
return this.queryMethodParameters;
}
public PayloadMetadata getOutput() {
return this.output;
}
/**
* Creates a {@link List} of properties based on the given creator.
*
* @param <T> the property type
* @param creator a creator function that turns an {@link InputPayloadMetadata} and {@link PropertyMetadata} into a
* property instance.
* @return will never be {@literal null}.
* @since 1.3
*/
public <T> List<T> createProperties(PropertyCreator<T> creator) {
PropertyCreationContext context = new PropertyCreationContext(getLink());
return input.stream() //
.map(it -> creator.apply(input, it, context)) //
.collect(Collectors.toList());
}
@FunctionalInterface
public interface PropertyCreator<T> {
T apply(InputPayloadMetadata payload, PropertyMetadata metadata, PropertyCreationContext context);
}
public static final class PropertyCreationContext {
private final Link link;
private PropertyCreationContext(Link link) {
this.link = link;
}
/**
* {@link Link} for the URI of the resource.
*/
public Link link() {
return link;
}
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
AffordanceModel that = (AffordanceModel) o;
return Objects.equals(this.name, that.name) //
&& Objects.equals(this.link, that.link) //
&& this.httpMethod == that.httpMethod //
&& Objects.equals(this.input, that.input) //
&& Objects.equals(this.queryMethodParameters, that.queryMethodParameters) //
&& Objects.equals(this.output, that.output);
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
return Objects.hash(this.name, this.link, this.httpMethod, this.input, this.queryMethodParameters, this.output);
}
/**
* Metadata about payloads.
*
* @author Oliver Drotbohm
*/
public interface PayloadMetadata {
PayloadMetadata NONE = NoPayloadMetadata.INSTANCE;
/**
* Returns all properties contained in a payload.
*
* @return
*/
Stream<PropertyMetadata> stream();
/**
* @deprecated since 1.4, for removal in 1.5. Prefer {@link #stream()} and selecting individual
* {@code PropertyMetadata} instances yourself.
*/
@Deprecated
default Optional<PropertyMetadata> getPropertyMetadata(String name) {
return stream().filter(it -> it.hasName(name)).findFirst();
}
@Nullable
default Class<?> getType() {
return null;
}
}
/**
* Payload metadata for incoming requests.
*
* @author Oliver Drotbohm
*/
public interface InputPayloadMetadata extends PayloadMetadata {
InputPayloadMetadata NONE = from(PayloadMetadata.NONE);
static InputPayloadMetadata from(PayloadMetadata metadata) {
return InputPayloadMetadata.class.isInstance(metadata) //
? InputPayloadMetadata.class.cast(metadata)
: DelegatingInputPayloadMetadata.of(metadata);
}
/**
* Applies the {@link InputPayloadMetadata} to the given target.
*
* @param <T>
* @param target
* @return
* @deprecated since 1.3, prefer setting up the model types via
* {@link AffordanceModel#createProperties(BiFunction)}.
*/
@Deprecated
default <T extends PropertyMetadataConfigured<T> & Named> T applyTo(T target) {
return getPropertyMetadata(target.getName()) //
.map(it -> target.apply(it)) //
.orElse(target);
}
<T extends Named> T customize(T target, Function<PropertyMetadata, T> customizer);
/**
* Returns the I18n codes to be used to resolve a name for the payload metadata.
*
* @return
*/
List<String> getI18nCodes();
/**
* Creates a new {@link InputPayloadMetadata} with the given {@link MediaType} assigned.
*
* @param mediaType can be {@literal null}.
* @return will never be {@literal null}.
* @since 1.3
*/
InputPayloadMetadata withMediaTypes(List<MediaType> mediaType);
/**
* Returns the {@link MediaType} that the payload requires.
*
* @return will never be {@literal null}.
* @since 1.3
*/
List<MediaType> getMediaTypes();
/**
* Returns the primary {@link MediaType} expected for the input. That is, from {@link #getMediaTypes()} the first
* one, if available.
*
* @return can be {@literal null}.
*/
@Nullable
default MediaType getPrimaryMediaType() {
List<MediaType> mediaTypes = getMediaTypes();
return mediaTypes.isEmpty() ? null : mediaTypes.get(0);
}
}
/**
* {@link InputPayloadMetadata} to delegate to a target {@link PayloadMetadata} not applying any customizations.
*
* @author Oliver Drotbohm
*/
private static class DelegatingInputPayloadMetadata implements InputPayloadMetadata {
private final PayloadMetadata metadata;
private final List<MediaType> mediaTypes;
public static DelegatingInputPayloadMetadata of(PayloadMetadata metadata) {
return new DelegatingInputPayloadMetadata(metadata, Collections.emptyList());
}
private DelegatingInputPayloadMetadata(PayloadMetadata metadata, List<MediaType> mediaTypes) {
this.metadata = metadata;
this.mediaTypes = mediaTypes;
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.AffordanceModel.PayloadMetadata#stream()
*/
@Override
public Stream<PropertyMetadata> stream() {
return metadata.stream();
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.AffordanceModel.InputPayloadMetadata#customize(org.springframework.hateoas.AffordanceModel.PropertyMetadataConfigured)
*/
@Override
public <T extends PropertyMetadataConfigured<T> & Named> T applyTo(T target) {
return target;
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.AffordanceModel.InputPayloadMetadata#customize(org.springframework.hateoas.AffordanceModel.Named, java.util.function.Function)
*/
@Override
public <T extends Named> T customize(T target, Function<PropertyMetadata, T> customizer) {
return target;
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.AffordanceModel.InputPayloadMetadata#getI18nCodes()
*/
@Override
public List<String> getI18nCodes() {
return Collections.emptyList();
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.AffordanceModel.InputPayloadMetadata#withMediaTypes(java.util.List)
*/
@Override
public InputPayloadMetadata withMediaTypes(List<MediaType> mediaTypes) {
return new DelegatingInputPayloadMetadata(metadata, mediaTypes);
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.AffordanceModel.InputPayloadMetadata#getMediaTypes()
*/
@Override
public List<MediaType> getMediaTypes() {
return mediaTypes;
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.AffordanceModel.InputPayloadMetadata#getType()
*/
@Nullable
@Override
public Class<?> getType() {
return metadata.getType();
}
/*
* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(@Nullable Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DelegatingInputPayloadMetadata that = (DelegatingInputPayloadMetadata) o;
return Objects.equals(this.metadata, that.metadata);
}
@Override
public int hashCode() {
return Objects.hash(this.metadata);
}
@Override
public String toString() {
return "AffordanceModel.DelegatingInputPayloadMetadata(metadata=" + this.metadata + ")";
}
}
/**
* Metadata about the property model of a representation.
*
* @author Oliver Drotbohm
*/
public interface PropertyMetadata extends Named {
/**
* The name of the property.
*
* @return will never be {@literal null} or empty.
*/
String getName();
/**
* Whether the property has the given name.
*
* @param name must not be {@literal null} or empty.
* @return
*/
default boolean hasName(String name) {
Assert.hasText(name, "Name must not be null or empty!");
return getName().equals(name);
}
/**
* Whether the property is required to be submitted or always present in the representation returned.
*
* @return
*/
boolean isRequired();
/**
* Whether the property is read only, i.e. must not be manipulated in requests modifying state.
*
* @return
*/
boolean isReadOnly();
/**
* Returns the (regular expression) pattern the property has to adhere to.
*
* @return will never be {@literal null}.
*/
Optional<String> getPattern();
/**
* Return the type of the property. If no type can be determined, return {@link Object}.
*
* @return
*/
ResolvableType getType();
/**
* Return the minimum value allowed for a numeric type.
*
* @return can be {@literal null}.
* @since 1.3
*/
@Nullable
default Number getMin() {
return null;
}
/**
* Return the maximum value allowed for a numeric type.
*
* @return can be {@literal null}.
* @since 1.3
*/
@Nullable
default Number getMax() {
return null;
}
/**
* Return the minimum length allowed for a string type.
*
* @return can be {@literal null}.
* @since 1.3
*/
@Nullable
default Long getMinLength() {
return null;
}
/**
* Return the maximum length allowed for a string type.
*
* @return can be {@literal null}.
* @since 1.3
*/
@Nullable
default Long getMaxLength() {
return null;
}
/**
* Returns the input type of the property.
*
* @return
* @see InputType
*/
@Nullable
String getInputType();
}
/**
* SPI for a type that can get {@link PropertyMetadata} applied.
*
* @author Oliver Drotbohm
*/
public interface PropertyMetadataConfigured<T> {
/**
* Applies the given {@link PropertyMetadata}.
*
* @param metadata will never be {@literal null}.
* @return
*/
T apply(PropertyMetadata metadata);
}
/**
* A named component.
*
* @author Oliver Drotbohm
*/
public interface Named {
String getName();
}
/**
* Empty {@link PayloadMetadata}.
*
* @author Oliver Drotbohm
*/
private enum NoPayloadMetadata implements PayloadMetadata {
INSTANCE;
/*
* (non-Javadoc)
* @see org.springframework.hateoas.AffordanceModel.PayloadMetadata#stream()
*/
@Override
public Stream<PropertyMetadata> stream() {
return Stream.empty();
}
}
}