24
24
import org .springframework .context .annotation .Configuration ;
25
25
import org .springframework .hateoas .client .LinkDiscoverer ;
26
26
import org .springframework .hateoas .config .HypermediaMappingInformation ;
27
+ import org .springframework .hateoas .mediatype .MediaTypeConfigurationCustomizer ;
28
+ import org .springframework .hateoas .mediatype .MediaTypeConfigurationFactory ;
27
29
import org .springframework .hateoas .mediatype .MessageResolver ;
28
30
import org .springframework .hateoas .mediatype .hal .CurieProvider ;
29
31
import org .springframework .hateoas .mediatype .hal .HalConfiguration ;
@@ -45,22 +47,32 @@ class HalFormsMediaTypeConfiguration implements HypermediaMappingInformation {
45
47
46
48
private final DelegatingLinkRelationProvider relProvider ;
47
49
private final ObjectProvider <CurieProvider > curieProvider ;
48
- private final ObjectProvider <HalFormsConfiguration > halFormsConfiguration ;
49
- private final ObjectProvider <HalConfiguration > halConfiguration ;
50
+ private final MediaTypeConfigurationFactory <HalFormsConfiguration , ? extends MediaTypeConfigurationCustomizer <HalFormsConfiguration >> configurationFactory ;
50
51
private final MessageResolver resolver ;
51
52
private final AbstractAutowireCapableBeanFactory beanFactory ;
52
53
53
- private HalFormsConfiguration resolvedConfiguration ;
54
-
55
54
public HalFormsMediaTypeConfiguration (DelegatingLinkRelationProvider relProvider ,
56
- ObjectProvider <CurieProvider > curieProvider , ObjectProvider <HalFormsConfiguration > halFormsConfiguration ,
57
- ObjectProvider <HalConfiguration > halConfiguration , MessageResolver resolver ,
58
- AbstractAutowireCapableBeanFactory beanFactory ) {
55
+ ObjectProvider <CurieProvider > curieProvider ,
56
+ ObjectProvider <HalConfiguration > halConfiguration ,
57
+ ObjectProvider <MediaTypeConfigurationCustomizer <HalConfiguration >> halCustomizers ,
58
+ ObjectProvider <HalFormsConfiguration > halFormsConfiguration ,
59
+ ObjectProvider <MediaTypeConfigurationCustomizer <HalFormsConfiguration >> halFormsCustomizers ,
60
+ MessageResolver resolver , AbstractAutowireCapableBeanFactory beanFactory ) {
59
61
60
62
this .relProvider = relProvider ;
61
63
this .curieProvider = curieProvider ;
62
- this .halFormsConfiguration = halFormsConfiguration ;
63
- this .halConfiguration = halConfiguration ;
64
+
65
+ Supplier <HalFormsConfiguration > defaultConfig = () -> {
66
+
67
+ MediaTypeConfigurationFactory <HalConfiguration , ?> customizedHalConfiguration = new MediaTypeConfigurationFactory <>(
68
+ () -> halConfiguration .getIfAvailable (HalConfiguration ::new ), halCustomizers );
69
+
70
+ return new HalFormsConfiguration (
71
+ customizedHalConfiguration .getConfiguration ());
72
+ };
73
+
74
+ this .configurationFactory = new MediaTypeConfigurationFactory <>(
75
+ () -> halFormsConfiguration .getIfAvailable (defaultConfig ), halFormsCustomizers );
64
76
this .resolver = resolver ;
65
77
this .beanFactory = beanFactory ;
66
78
}
@@ -73,7 +85,7 @@ LinkDiscoverer halFormsLinkDiscoverer() {
73
85
@ Bean
74
86
HalFormsTemplatePropertyWriter halFormsTemplatePropertyWriter () {
75
87
76
- HalFormsConfiguration configuration = getResolvedConfiguration ();
88
+ HalFormsConfiguration configuration = configurationFactory . getConfiguration ();
77
89
HalFormsTemplateBuilder builder = new HalFormsTemplateBuilder (configuration , resolver );
78
90
79
91
return new HalFormsTemplatePropertyWriter (builder );
@@ -86,7 +98,7 @@ HalFormsTemplatePropertyWriter halFormsTemplatePropertyWriter() {
86
98
@ Override
87
99
public ObjectMapper configureObjectMapper (ObjectMapper mapper ) {
88
100
89
- HalFormsConfiguration halFormsConfig = getResolvedConfiguration ();
101
+ HalFormsConfiguration halFormsConfig = configurationFactory . getConfiguration ();
90
102
CurieProvider provider = curieProvider .getIfAvailable (() -> CurieProvider .NONE );
91
103
92
104
mapper .disable (DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES );
@@ -105,18 +117,15 @@ public ObjectMapper configureObjectMapper(ObjectMapper mapper) {
105
117
*/
106
118
@ Override
107
119
public List <MediaType > getMediaTypes () {
108
- return getResolvedConfiguration ().getMediaTypes ();
120
+ return configurationFactory . getConfiguration ().getMediaTypes ();
109
121
}
110
122
123
+ /**
124
+ * For testing purposes.
125
+ *
126
+ * @return
127
+ */
111
128
HalFormsConfiguration getResolvedConfiguration () {
112
-
113
- Supplier <HalFormsConfiguration > defaultConfig = () -> new HalFormsConfiguration (
114
- halConfiguration .getIfAvailable (HalConfiguration ::new ));
115
-
116
- if (resolvedConfiguration == null ) {
117
- this .resolvedConfiguration = halFormsConfiguration .getIfAvailable (defaultConfig );
118
- }
119
-
120
- return resolvedConfiguration ;
129
+ return configurationFactory .getConfiguration ();
121
130
}
122
131
}
0 commit comments