23
23
24
24
package org .springdoc .data .rest ;
25
25
26
+ import java .lang .reflect .InvocationTargetException ;
26
27
import java .util .ArrayList ;
27
28
import java .util .List ;
28
29
import java .util .Map ;
31
32
32
33
import com .fasterxml .jackson .databind .ObjectMapper ;
33
34
import io .swagger .v3 .oas .models .OpenAPI ;
35
+ import org .apache .commons .lang3 .reflect .MethodUtils ;
36
+ import org .slf4j .Logger ;
37
+ import org .slf4j .LoggerFactory ;
34
38
import org .springdoc .api .AbstractOpenApiResource ;
35
39
import org .springdoc .core .RepositoryRestResourceProvider ;
36
40
import org .springdoc .core .fn .RouterOperation ;
37
41
import org .springdoc .data .rest .core .ControllerType ;
38
42
import org .springdoc .data .rest .core .DataRestRepository ;
39
43
import org .springdoc .data .rest .core .DataRestRouterOperationBuilder ;
40
44
45
+ import org .springframework .context .ApplicationContext ;
41
46
import org .springframework .data .mapping .PersistentEntity ;
42
47
import org .springframework .data .mapping .PersistentProperty ;
43
48
import org .springframework .data .mapping .SimpleAssociationHandler ;
53
58
import org .springframework .data .rest .webmvc .alps .AlpsController ;
54
59
import org .springframework .data .rest .webmvc .json .JacksonMetadata ;
55
60
import org .springframework .data .rest .webmvc .mapping .Associations ;
56
- import org .springframework .data .rest .webmvc .support .DelegatingHandlerMapping ;
57
61
import org .springframework .web .method .HandlerMethod ;
58
62
import org .springframework .web .servlet .HandlerMapping ;
59
63
import org .springframework .web .servlet .mvc .method .RequestMappingInfo ;
@@ -67,12 +71,12 @@ public class SpringRepositoryRestResourceProvider implements RepositoryRestResou
67
71
/**
68
72
* The constant REPOSITORY_ENTITY_CONTROLLER.
69
73
*/
70
- public static final String REPOSITORY_ENTITY_CONTROLLER = "org.springframework.data.rest.webmvc.RepositoryEntityController" ;
74
+ private static final String REPOSITORY_ENTITY_CONTROLLER = "org.springframework.data.rest.webmvc.RepositoryEntityController" ;
71
75
72
76
/**
73
77
* The constant REPOSITORY_SERACH_CONTROLLER.
74
78
*/
75
- public static final String REPOSITORY_SERACH_CONTROLLER = "org.springframework.data.rest.webmvc.RepositorySearchController" ;
79
+ private static final String REPOSITORY_SERACH_CONTROLLER = "org.springframework.data.rest.webmvc.RepositorySearchController" ;
76
80
77
81
/**
78
82
* The constant REPOSITORY_SCHEMA_CONTROLLER.
@@ -82,7 +86,22 @@ public class SpringRepositoryRestResourceProvider implements RepositoryRestResou
82
86
/**
83
87
* The constant REPOSITORY_PROPERTY_CONTROLLER.
84
88
*/
85
- public static final String REPOSITORY_PROPERTY_CONTROLLER = "org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController" ;
89
+ private static final String REPOSITORY_PROPERTY_CONTROLLER = "org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController" ;
90
+
91
+ /**
92
+ * The Delegating handler mapping class.
93
+ */
94
+ private static final String DELEGATING_HANDLER_MAPPING_CLASS = "org.springframework.data.rest.webmvc.config.DelegatingHandlerMapping" ;
95
+
96
+ /**
97
+ * The Delegating handler mapping interface.
98
+ */
99
+ private static final String DELEGATING_HANDLER_MAPPING_INTERFACE = "org.springframework.data.rest.webmvc.support.DelegatingHandlerMapping" ;
100
+
101
+ /**
102
+ * The constant LOGGER.
103
+ */
104
+ private static final Logger LOGGER = LoggerFactory .getLogger (SpringRepositoryRestResourceProvider .class );
86
105
87
106
/**
88
107
* The Mappings.
@@ -99,11 +118,6 @@ public class SpringRepositoryRestResourceProvider implements RepositoryRestResou
99
118
*/
100
119
private Associations associations ;
101
120
102
- /**
103
- * The Delegating handler mapping.
104
- */
105
- private DelegatingHandlerMapping delegatingHandlerMapping ;
106
-
107
121
/**
108
122
* The Data rest router operation builder.
109
123
*/
@@ -119,30 +133,35 @@ public class SpringRepositoryRestResourceProvider implements RepositoryRestResou
119
133
*/
120
134
private ObjectMapper mapper ;
121
135
136
+ /**
137
+ * The Application context.
138
+ */
139
+ private ApplicationContext applicationContext ;
140
+
122
141
/**
123
142
* Instantiates a new Spring repository rest resource provider.
124
143
*
125
144
* @param mappings the mappings
126
145
* @param repositories the repositories
127
146
* @param associations the associations
128
- * @param delegatingHandlerMapping the delegating handler mapping
147
+ * @param applicationContext the application context
129
148
* @param dataRestRouterOperationBuilder the data rest router operation builder
130
149
* @param persistentEntities the persistent entities
131
150
* @param mapper the mapper
132
151
*/
133
- public SpringRepositoryRestResourceProvider (ResourceMappings mappings , Repositories repositories , Associations associations , DelegatingHandlerMapping delegatingHandlerMapping , DataRestRouterOperationBuilder dataRestRouterOperationBuilder , PersistentEntities persistentEntities , ObjectMapper mapper ) {
152
+ public SpringRepositoryRestResourceProvider (ResourceMappings mappings , Repositories repositories , Associations associations , ApplicationContext applicationContext , DataRestRouterOperationBuilder dataRestRouterOperationBuilder , PersistentEntities persistentEntities , ObjectMapper mapper ) {
134
153
this .mappings = mappings ;
135
154
this .repositories = repositories ;
136
155
this .associations = associations ;
137
- this .delegatingHandlerMapping = delegatingHandlerMapping ;
156
+ this .applicationContext = applicationContext ;
138
157
this .dataRestRouterOperationBuilder = dataRestRouterOperationBuilder ;
139
158
this .persistentEntities = persistentEntities ;
140
159
this .mapper = mapper ;
141
160
}
142
161
143
162
public List <RouterOperation > getRouterOperations (OpenAPI openAPI ) {
144
163
List <RouterOperation > routerOperationList = new ArrayList <>();
145
- List <HandlerMapping > handlerMappingList = delegatingHandlerMapping . getDelegates ();
164
+ List <HandlerMapping > handlerMappingList = getHandlerMappingList ();
146
165
for (Class <?> domainType : repositories ) {
147
166
Class <?> repository = repositories .getRequiredRepositoryInformation (domainType ).getRepositoryInterface ();
148
167
DataRestRepository dataRestRepository = new DataRestRepository (domainType , repository );
@@ -165,7 +184,7 @@ public List<RouterOperation> getRouterOperations(OpenAPI openAPI) {
165
184
findControllers (routerOperationList , handlerMethodMapFiltered , resourceMetadata , dataRestRepository , openAPI );
166
185
167
186
Map <RequestMappingInfo , HandlerMethod > handlerMethodMapFilteredMethodMap = handlerMethodMap .entrySet ().stream ()
168
- .filter (requestMappingInfoHandlerMethodEntry -> REPOSITORY_PROPERTY_CONTROLLER .equals (requestMappingInfoHandlerMethodEntry
187
+ .filter (requestMappingInfoHandlerMethodEntry -> REPOSITORY_PROPERTY_CONTROLLER .equals (requestMappingInfoHandlerMethodEntry
169
188
.getValue ().getBeanType ().getName ()))
170
189
.filter (controller -> !AbstractOpenApiResource .isHiddenRestControllers (controller .getValue ().getBeanType ()))
171
190
.collect (Collectors .toMap (Map .Entry ::getKey , Map .Entry ::getValue , (a1 , a2 ) -> a1 ));
@@ -204,6 +223,37 @@ else if (handlerMapping instanceof BasePathAwareHandlerMapping) {
204
223
return routerOperationList ;
205
224
}
206
225
226
+ /**
227
+ * Gets handler mapping list.
228
+ *
229
+ * @return the handler mapping list
230
+ */
231
+ 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 ) {
238
+ try {
239
+ delegatingHandlerMappingClass = Class .forName (DELEGATING_HANDLER_MAPPING_INTERFACE );
240
+ }
241
+ catch (ClassNotFoundException exception ) {
242
+ LOGGER .warn (e .getMessage ());
243
+ }
244
+ }
245
+ if (delegatingHandlerMappingClass != null ) {
246
+ Object object = applicationContext .getBean (delegatingHandlerMappingClass );
247
+ try {
248
+ handlerMappingList = (List <HandlerMapping >) MethodUtils .invokeMethod (object , "getDelegates" );
249
+ }
250
+ catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e ) {
251
+ LOGGER .warn (e .getMessage ());
252
+ }
253
+ }
254
+ return handlerMappingList ;
255
+ }
256
+
207
257
/**
208
258
* Find search resource mappings.
209
259
*
0 commit comments