43
43
import org .springdoc .data .rest .core .DataRestRouterOperationService ;
44
44
45
45
import org .springframework .context .ApplicationContext ;
46
+ import org .springframework .core .annotation .AnnotatedElementUtils ;
46
47
import org .springframework .data .mapping .PersistentEntity ;
47
48
import org .springframework .data .mapping .PersistentProperty ;
48
49
import org .springframework .data .mapping .SimpleAssociationHandler ;
54
55
import org .springframework .data .rest .core .mapping .SearchResourceMappings ;
55
56
import org .springframework .data .rest .webmvc .BasePathAwareHandlerMapping ;
56
57
import org .springframework .data .rest .webmvc .ProfileController ;
58
+ import org .springframework .data .rest .webmvc .RepositoryRestController ;
57
59
import org .springframework .data .rest .webmvc .RepositoryRestHandlerMapping ;
58
60
import org .springframework .data .rest .webmvc .alps .AlpsController ;
59
61
import org .springframework .data .rest .webmvc .json .JacksonMetadata ;
60
62
import org .springframework .data .rest .webmvc .mapping .Associations ;
61
63
import org .springframework .web .method .HandlerMethod ;
62
64
import org .springframework .web .servlet .HandlerMapping ;
63
65
import org .springframework .web .servlet .mvc .method .RequestMappingInfo ;
66
+ import org .springframework .web .servlet .mvc .method .RequestMappingInfoHandlerMapping ;
64
67
65
68
/**
66
69
* The type Spring repository rest resource provider.
69
72
public class SpringRepositoryRestResourceProvider implements RepositoryRestResourceProvider {
70
73
71
74
/**
72
- * The constant REPOSITORY_ENTITY_CONTROLLER .
75
+ * The constant SPRING_DATA_REST_PACKAGE .
73
76
*/
74
- private static final String REPOSITORY_ENTITY_CONTROLLER = "org.springframework.data.rest.webmvc.RepositoryEntityController " ;
77
+ private static final String SPRING_DATA_REST_PACKAGE = "org.springframework.data.rest" ;
75
78
76
79
/**
77
- * The constant REPOSITORY_SERACH_CONTROLLER .
80
+ * The constant REPOSITORY_SCHEMA_CONTROLLER .
78
81
*/
79
- private static final String REPOSITORY_SERACH_CONTROLLER = "org.springframework.data.rest. webmvc.RepositorySearchController " ;
82
+ public static final String REPOSITORY_SCHEMA_CONTROLLER = SPRING_DATA_REST_PACKAGE + ". webmvc.RepositorySchemaController " ;
80
83
81
84
/**
82
- * The constant REPOSITORY_SCHEMA_CONTROLLER .
85
+ * The constant REPOSITORY_ENTITY_CONTROLLER .
83
86
*/
84
- public static final String REPOSITORY_SCHEMA_CONTROLLER = "org.springframework.data.rest.webmvc.RepositorySchemaController" ;
87
+ private static final String REPOSITORY_ENTITY_CONTROLLER = SPRING_DATA_REST_PACKAGE + ".webmvc.RepositoryEntityController" ;
88
+
89
+ /**
90
+ * The constant REPOSITORY_SEARCH_CONTROLLER.
91
+ */
92
+ private static final String REPOSITORY_SERACH_CONTROLLER = SPRING_DATA_REST_PACKAGE + ".webmvc.RepositorySearchController" ;
85
93
86
94
/**
87
95
* The constant REPOSITORY_PROPERTY_CONTROLLER.
88
96
*/
89
- private static final String REPOSITORY_PROPERTY_CONTROLLER = "org.springframework.data.rest .webmvc.RepositoryPropertyReferenceController" ;
97
+ private static final String REPOSITORY_PROPERTY_CONTROLLER = SPRING_DATA_REST_PACKAGE + " .webmvc.RepositoryPropertyReferenceController" ;
90
98
91
99
/**
92
100
* The Delegating handler mapping class.
93
101
*/
94
- private static final String DELEGATING_HANDLER_MAPPING_CLASS = "org.springframework.data.rest .webmvc.config.DelegatingHandlerMapping" ;
102
+ private static final String DELEGATING_HANDLER_MAPPING_CLASS = SPRING_DATA_REST_PACKAGE + " .webmvc.config.DelegatingHandlerMapping" ;
95
103
96
104
/**
97
105
* The Delegating handler mapping interface.
98
106
*/
99
- private static final String DELEGATING_HANDLER_MAPPING_INTERFACE = "org.springframework.data.rest .webmvc.support.DelegatingHandlerMapping" ;
107
+ private static final String DELEGATING_HANDLER_MAPPING_INTERFACE = SPRING_DATA_REST_PACKAGE + " .webmvc.support.DelegatingHandlerMapping" ;
100
108
101
109
/**
102
110
* The constant LOGGER.
@@ -138,6 +146,11 @@ public class SpringRepositoryRestResourceProvider implements RepositoryRestResou
138
146
*/
139
147
private ApplicationContext applicationContext ;
140
148
149
+ /**
150
+ * The Handler mapping list.
151
+ */
152
+ private List <HandlerMapping > handlerMappingList ;
153
+
141
154
/**
142
155
* Instantiates a new Spring repository rest resource provider.
143
156
*
@@ -159,9 +172,10 @@ public SpringRepositoryRestResourceProvider(ResourceMappings mappings, Repositor
159
172
this .mapper = mapper ;
160
173
}
161
174
175
+
162
176
public List <RouterOperation > getRouterOperations (OpenAPI openAPI ) {
163
177
List <RouterOperation > routerOperationList = new ArrayList <>();
164
- List < HandlerMapping > handlerMappingList = getHandlerMappingList ();
178
+ handlerMappingList = getHandlerMappingList ();
165
179
for (Class <?> domainType : repositories ) {
166
180
Class <?> repository = repositories .getRequiredRepositoryInformation (domainType ).getRepositoryInterface ();
167
181
DataRestRepository dataRestRepository = new DataRestRepository (domainType , repository );
@@ -223,34 +237,61 @@ else if (handlerMapping instanceof BasePathAwareHandlerMapping) {
223
237
return routerOperationList ;
224
238
}
225
239
240
+ /**
241
+ * Gets repository rest controller endpoints.
242
+ *
243
+ * @return the repository rest controller endpoints
244
+ */
245
+ @ Override
246
+ public Map <String , Object > getRepositoryRestControllerEndpoints () {
247
+ return applicationContext .getBeansWithAnnotation (RepositoryRestController .class );
248
+ }
249
+
250
+ /**
251
+ * Gets handler methods.
252
+ *
253
+ * @return the handler methods
254
+ */
255
+ @ Override
256
+ public Map getHandlerMethods () {
257
+ handlerMappingList = getHandlerMappingList ();
258
+ return handlerMappingList .stream ().filter (RequestMappingInfoHandlerMapping .class ::isInstance )
259
+ .flatMap (
260
+ handler -> ((RequestMappingInfoHandlerMapping ) handler ).getHandlerMethods ().entrySet ().stream ())
261
+ .filter (entry -> !entry .getValue ().getBeanType ().getName ().startsWith (SPRING_DATA_REST_PACKAGE ) && AnnotatedElementUtils .hasAnnotation (entry .getValue ().getBeanType (), RepositoryRestController .class ))
262
+ .collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue ));
263
+ }
226
264
/**
227
265
* Gets handler mapping list.
228
266
*
229
267
* @return the handler mapping list
230
268
*/
231
269
private List <HandlerMapping > getHandlerMappingList () {
232
- List <HandlerMapping > handlerMappingList = new ArrayList <>();
233
- Class delegatingHandlerMappingClass = null ;
234
- try {
235
- delegatingHandlerMappingClass = Class .forName (DELEGATING_HANDLER_MAPPING_CLASS );
236
- }
237
- catch (ClassNotFoundException e ) {
270
+ if (handlerMappingList == null ) {
271
+ handlerMappingList = new ArrayList <>();
272
+ Class delegatingHandlerMappingClass = null ;
238
273
try {
239
- delegatingHandlerMappingClass = Class .forName (DELEGATING_HANDLER_MAPPING_INTERFACE );
240
- }
241
- catch (ClassNotFoundException exception ) {
242
- LOGGER .warn (e .getMessage ());
274
+ delegatingHandlerMappingClass = Class .forName (DELEGATING_HANDLER_MAPPING_CLASS );
243
275
}
244
- }
245
- if (delegatingHandlerMappingClass != null ) {
246
- Object object = applicationContext .getBean (delegatingHandlerMappingClass );
247
- try {
248
- handlerMappingList = (List <HandlerMapping >) MethodUtils .invokeMethod (object , "getDelegates" );
276
+ catch (ClassNotFoundException e ) {
277
+ try {
278
+ delegatingHandlerMappingClass = Class .forName (DELEGATING_HANDLER_MAPPING_INTERFACE );
279
+ }
280
+ catch (ClassNotFoundException exception ) {
281
+ LOGGER .warn (e .getMessage ());
282
+ }
249
283
}
250
- catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
251
- LOGGER .warn (e .getMessage ());
284
+ if (delegatingHandlerMappingClass != null ) {
285
+ Object object = applicationContext .getBean (delegatingHandlerMappingClass );
286
+ try {
287
+ handlerMappingList = (List <HandlerMapping >) MethodUtils .invokeMethod (object , "getDelegates" );
288
+ }
289
+ catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
290
+ LOGGER .warn (e .getMessage ());
291
+ }
252
292
}
253
293
}
294
+
254
295
return handlerMappingList ;
255
296
}
256
297
@@ -312,8 +353,7 @@ private List<RouterOperation> findSearchControllers(List<RouterOperation> router
312
353
* @param openAPI the open api
313
354
* @return the list
314
355
*/
315
- private List <RouterOperation > findControllers
316
- (List <RouterOperation > routerOperationList ,
356
+ private List <RouterOperation > findControllers (List <RouterOperation > routerOperationList ,
317
357
Map <RequestMappingInfo , HandlerMethod > handlerMethodMap , ResourceMetadata resourceMetadata ,
318
358
DataRestRepository dataRestRepository , OpenAPI openAPI ) {
319
359
dataRestRouterOperationService .buildEntityRouterOperationList (routerOperationList , handlerMethodMap , resourceMetadata ,
0 commit comments