18
18
19
19
import java .util .ArrayList ;
20
20
import java .util .List ;
21
+ import java .util .Set ;
22
+ import java .util .stream .Collectors ;
23
+ import java .util .stream .Stream ;
21
24
22
25
import org .springframework .boot .autoconfigure .hateoas .HypermediaAutoConfiguration ;
23
26
import org .springframework .boot .autoconfigure .hateoas .HypermediaHttpMessageConverterConfiguration ;
44
47
import org .springframework .nativex .type .Type ;
45
48
import org .springframework .nativex .type .TypeProcessor ;
46
49
import org .springframework .nativex .type .TypeSystem ;
50
+ import org .springframework .util .StringUtils ;
47
51
48
52
49
53
@ NativeHint (trigger = WebStackImportSelector .class , types = {
@@ -94,23 +98,64 @@ public class HateoasHints implements NativeConfiguration {
94
98
95
99
private static final String ENTITY_LINKS = "org/springframework/hateoas/server/EntityLinks" ;
96
100
private static final String JACKSON_ANNOTATION = "Lcom/fasterxml/jackson/annotation/JacksonAnnotation;" ;
101
+ private static final String ENABLE_HYPERMEDIA_SUPPORT = "Lorg/springframework/hateoas/config/EnableHypermediaSupport;" ;
97
102
98
103
@ Override
99
104
public List <HintDeclaration > computeHints (TypeSystem typeSystem ) {
100
105
106
+ Set <String > hypermediaFormats = computeConfiguredHypermediaFormats (typeSystem );
107
+
101
108
List <HintDeclaration > hints = new ArrayList <>();
102
109
103
- hints .addAll (computeAtConfigurationClasses (typeSystem ));
110
+ hints .addAll (computeAtConfigurationClasses (typeSystem , hypermediaFormats ));
104
111
hints .addAll (computeRepresentationModels (typeSystem ));
105
112
hints .addAll (computeEntityLinks (typeSystem ));
106
- hints .addAll (computeJacksonMappings (typeSystem ));
113
+ hints .addAll (computeJacksonMappings (typeSystem , hypermediaFormats ));
107
114
108
115
return hints ;
109
116
}
110
117
111
- private List <HintDeclaration > computeAtConfigurationClasses (TypeSystem typeSystem ) {
118
+ private Set <String > computeConfiguredHypermediaFormats (TypeSystem typeSystem ) {
119
+
120
+ return typeSystem .scanUserCodeDirectoriesAndSpringJars (type -> {
121
+ try {
122
+ return type .isAnnotated (ENABLE_HYPERMEDIA_SUPPORT );
123
+ } catch (MissingTypeException e ) {
124
+ return false ;
125
+ }
126
+ })
127
+ .flatMap (type -> {
128
+ try {
129
+ String formats = type .getAnnotationValuesInHierarchy (ENABLE_HYPERMEDIA_SUPPORT ).getOrDefault ("type" , "" );
130
+ return StringUtils .hasText (formats ) ? Stream .of (formats .split (";" )) : Stream .empty ();
131
+ } catch (MissingTypeException ex ) {
132
+ return Stream .empty ();
133
+ }
134
+ })
135
+ .map (it -> {
136
+ String value = it .replaceAll (".*org\\ .springframework\\ .hateoas\\ .config\\ .EnableHypermediaSupport\\ $HypermediaType\\ ." , "" ).toLowerCase ();
137
+ switch (value ) {
138
+ case "hal_forms" :
139
+ return "hal" ;
140
+ case "collection_json" :
141
+ return "collectionjson" ;
142
+ default :
143
+ return value ;
144
+ }
145
+ })
146
+ .collect (Collectors .toSet ());
147
+ }
148
+
149
+
150
+ private List <HintDeclaration > computeAtConfigurationClasses (TypeSystem typeSystem , Set <String > hypermediaFormats ) {
112
151
113
152
return TypeProcessor .namedProcessor ("HateoasHints - Configuration Classes" )
153
+ .skipTypesMatching (type -> {
154
+ if (type .isPartOfDomain ("org.springframework.hateoas.mediatype" )) {
155
+ return !isSupportedHypermediaFormat (type , hypermediaFormats );
156
+ }
157
+ return false ;
158
+ })
114
159
.skipAnnotationInspection ()
115
160
.skipMethodInspection ()
116
161
.skipFieldInspection ()
@@ -159,7 +204,7 @@ List<HintDeclaration> computeRepresentationModels(TypeSystem typeSystem) {
159
204
});
160
205
}
161
206
162
- List <HintDeclaration > computeJacksonMappings (TypeSystem typeSystem ) {
207
+ List <HintDeclaration > computeJacksonMappings (TypeSystem typeSystem , Set < String > hypermediaFormats ) {
163
208
164
209
return TypeProcessor .namedProcessor ("HateoasHints - Jackson Mapping Candidates" )
165
210
.skipTypesMatching (type -> {
@@ -170,6 +215,7 @@ List<HintDeclaration> computeJacksonMappings(TypeSystem typeSystem) {
170
215
if (!type .isPartOfDomain ("org.springframework.hateoas." )) {
171
216
return true ;
172
217
}
218
+ return !isSupportedHypermediaFormat (type , hypermediaFormats );
173
219
}
174
220
return false ;
175
221
})
@@ -212,4 +258,26 @@ private boolean usesJackson(Type type) {
212
258
213
259
return false ;
214
260
}
261
+
262
+ private boolean isSupportedHypermediaFormat (Type type , Set <String > hypermediaFormats ) {
263
+
264
+ if (hypermediaFormats .isEmpty ()) {
265
+ return true ;
266
+ }
267
+
268
+ if (!type .isPartOfDomain ("org.springframework.hateoas.mediatype" )) {
269
+ return true ;
270
+ }
271
+
272
+ if (type .getPackageName ().equals ("org.springframework.hateoas.mediatype" )) {
273
+ return true ;
274
+ }
275
+
276
+ for (String hypermediaFormat : hypermediaFormats ) {
277
+ if (type .getPackageName ().contains (hypermediaFormat )) {
278
+ return true ;
279
+ }
280
+ }
281
+ return false ;
282
+ }
215
283
}
0 commit comments