-
-
Notifications
You must be signed in to change notification settings - Fork 469
/
SpringRepositoryRestResourceProvider.java
275 lines (249 loc) · 13.1 KB
/
SpringRepositoryRestResourceProvider.java
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
/*
*
* *
* * *
* * * * Copyright 2019-2020 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.springdoc.data.rest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.v3.oas.models.OpenAPI;
import org.springdoc.api.AbstractOpenApiResource;
import org.springdoc.core.RepositoryRestResourceProvider;
import org.springdoc.core.fn.RouterOperation;
import org.springdoc.data.rest.core.ControllerType;
import org.springdoc.data.rest.core.DataRestRepository;
import org.springdoc.data.rest.core.DataRestRouterOperationBuilder;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.SimpleAssociationHandler;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.repository.support.Repositories;
import org.springframework.data.rest.core.mapping.MethodResourceMapping;
import org.springframework.data.rest.core.mapping.ResourceMappings;
import org.springframework.data.rest.core.mapping.ResourceMetadata;
import org.springframework.data.rest.core.mapping.SearchResourceMappings;
import org.springframework.data.rest.webmvc.BasePathAwareHandlerMapping;
import org.springframework.data.rest.webmvc.ProfileController;
import org.springframework.data.rest.webmvc.RepositoryRestHandlerMapping;
import org.springframework.data.rest.webmvc.alps.AlpsController;
import org.springframework.data.rest.webmvc.json.JacksonMetadata;
import org.springframework.data.rest.webmvc.mapping.Associations;
import org.springframework.data.rest.webmvc.support.DelegatingHandlerMapping;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
/**
* The type Spring repository rest resource provider.
* @author bnasslahsen
*/
public class SpringRepositoryRestResourceProvider implements RepositoryRestResourceProvider {
/**
* The constant REPOSITORY_ENTITY_CONTROLLER.
*/
public static final String REPOSITORY_ENTITY_CONTROLLER = "org.springframework.data.rest.webmvc.RepositoryEntityController";
/**
* The constant REPOSITORY_SERACH_CONTROLLER.
*/
public static final String REPOSITORY_SERACH_CONTROLLER = "org.springframework.data.rest.webmvc.RepositorySearchController";
/**
* The constant REPOSITORY_SCHEMA_CONTROLLER.
*/
public static final String REPOSITORY_SCHEMA_CONTROLLER = "org.springframework.data.rest.webmvc.RepositorySchemaController";
/**
* The constant REPOSITORY_PROPERTY_CONTROLLER.
*/
public static final String REPOSITORY_PROPERTY_CONTROLLER = "org.springframework.data.rest.webmvc.RepositoryPropertyReferenceController";
/**
* The Mappings.
*/
private ResourceMappings mappings;
/**
* The Repositories.
*/
private Repositories repositories;
/**
* The Associations.
*/
private Associations associations;
/**
* The Delegating handler mapping.
*/
private DelegatingHandlerMapping delegatingHandlerMapping;
/**
* The Data rest router operation builder.
*/
private DataRestRouterOperationBuilder dataRestRouterOperationBuilder;
/**
* The Persistent entities.
*/
private PersistentEntities persistentEntities;
/**
* The Mapper.
*/
private ObjectMapper mapper;
/**
* Instantiates a new Spring repository rest resource provider.
*
* @param mappings the mappings
* @param repositories the repositories
* @param associations the associations
* @param delegatingHandlerMapping the delegating handler mapping
* @param dataRestRouterOperationBuilder the data rest router operation builder
* @param persistentEntities the persistent entities
* @param mapper the mapper
*/
public SpringRepositoryRestResourceProvider(ResourceMappings mappings, Repositories repositories, Associations associations, DelegatingHandlerMapping delegatingHandlerMapping, DataRestRouterOperationBuilder dataRestRouterOperationBuilder, PersistentEntities persistentEntities, ObjectMapper mapper) {
this.mappings = mappings;
this.repositories = repositories;
this.associations = associations;
this.delegatingHandlerMapping = delegatingHandlerMapping;
this.dataRestRouterOperationBuilder = dataRestRouterOperationBuilder;
this.persistentEntities = persistentEntities;
this.mapper = mapper;
}
public List<RouterOperation> getRouterOperations(OpenAPI openAPI) {
List<RouterOperation> routerOperationList = new ArrayList<>();
List<HandlerMapping> handlerMappingList = delegatingHandlerMapping.getDelegates();
for (Class<?> domainType : repositories) {
Class<?> repository = repositories.getRequiredRepositoryInformation(domainType).getRepositoryInterface();
DataRestRepository dataRestRepository = new DataRestRepository(domainType, repository);
ResourceMetadata resourceMetadata = mappings.getMetadataFor(domainType);
final PersistentEntity<?, ?> entity = persistentEntities.getRequiredPersistentEntity(domainType);
final JacksonMetadata jackson = new JacksonMetadata(mapper, domainType);
if (resourceMetadata.isExported()) {
for (HandlerMapping handlerMapping : handlerMappingList) {
if (handlerMapping instanceof RepositoryRestHandlerMapping) {
RepositoryRestHandlerMapping repositoryRestHandlerMapping = (RepositoryRestHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = repositoryRestHandlerMapping.getHandlerMethods();
// Entity controllers lookup first
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_ENTITY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.filter(controller -> !AbstractOpenApiResource.isHiddenRestControllers(controller.getValue().getBeanType()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
dataRestRepository.setControllerType(ControllerType.ENTITY);
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFilteredMethodMap = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_PROPERTY_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.filter(controller -> !AbstractOpenApiResource.isHiddenRestControllers(controller.getValue().getBeanType()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
entity.doWithAssociations((SimpleAssociationHandler) association -> {
PersistentProperty<?> property = association.getInverse();
if (jackson.isExported(property) && associations.isLinkableAssociation(property)) {
ResourceMetadata targetTypeMetadata = associations.getMetadataFor(property.getActualType());
dataRestRepository.setRelationName(targetTypeMetadata.getItemResourceRel().toString());
dataRestRepository.setControllerType(ControllerType.PROPERTY);
findControllers(routerOperationList, handlerMethodMapFilteredMethodMap, resourceMetadata, dataRestRepository, openAPI);
}
});
}
else if (handlerMapping instanceof BasePathAwareHandlerMapping) {
BasePathAwareHandlerMapping beanBasePathAwareHandlerMapping = (BasePathAwareHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = beanBasePathAwareHandlerMapping.getHandlerMethods();
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_SCHEMA_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.filter(controller -> !AbstractOpenApiResource.isHiddenRestControllers(controller.getValue().getBeanType()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
dataRestRepository.setControllerType(ControllerType.SCHEMA);
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI);
handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> ProfileController.class.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType()) || AlpsController.class.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
findControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, null, openAPI);
}
}
}
// search
findSearchResourceMappings(openAPI, routerOperationList, handlerMappingList, dataRestRepository, resourceMetadata);
}
return routerOperationList;
}
/**
* Find search resource mappings.
*
* @param openAPI the open api
* @param routerOperationList the router operation list
* @param handlerMappingList the handler mapping list
* @param dataRestRepository the repository data rest
* @param resourceMetadata the resource metadata
*/
private void findSearchResourceMappings(OpenAPI openAPI, List<RouterOperation> routerOperationList, List<HandlerMapping> handlerMappingList, DataRestRepository dataRestRepository, ResourceMetadata resourceMetadata) {
for (HandlerMapping handlerMapping : handlerMappingList) {
if (handlerMapping instanceof RepositoryRestHandlerMapping) {
RepositoryRestHandlerMapping repositoryRestHandlerMapping = (RepositoryRestHandlerMapping) handlerMapping;
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap = repositoryRestHandlerMapping.getHandlerMethods();
Map<RequestMappingInfo, HandlerMethod> handlerMethodMapFiltered = handlerMethodMap.entrySet().stream()
.filter(requestMappingInfoHandlerMethodEntry -> REPOSITORY_SERACH_CONTROLLER.equals(requestMappingInfoHandlerMethodEntry
.getValue().getBeanType().getName()))
.filter(controller -> !AbstractOpenApiResource.isHiddenRestControllers(controller.getValue().getBeanType()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a1, a2) -> a1));
ResourceMetadata metadata = associations.getMetadataFor(dataRestRepository.getDomainType());
SearchResourceMappings searchResourceMappings = metadata.getSearchResourceMappings();
if (searchResourceMappings.isExported()) {
findSearchControllers(routerOperationList, handlerMethodMapFiltered, resourceMetadata, dataRestRepository, openAPI, searchResourceMappings);
}
}
}
}
/**
* Find search controllers list.
*
* @param routerOperationList the router operation list
* @param handlerMethodMap the handler method map
* @param resourceMetadata the resource metadata
* @param dataRestRepository the repository data rest
* @param openAPI the open api
* @param searchResourceMappings the search resource mappings
* @return the list
*/
private List<RouterOperation> findSearchControllers(List<RouterOperation> routerOperationList,
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap, ResourceMetadata resourceMetadata, DataRestRepository dataRestRepository, OpenAPI openAPI, SearchResourceMappings searchResourceMappings) {
Stream<MethodResourceMapping> methodResourceMappingStream = searchResourceMappings.getExportedMappings();
methodResourceMappingStream.forEach(methodResourceMapping -> dataRestRouterOperationBuilder.buildSearchRouterOperationList(
routerOperationList, handlerMethodMap, resourceMetadata, dataRestRepository, openAPI, methodResourceMapping));
return routerOperationList;
}
/**
* Find controllers list.
*
* @param routerOperationList the router operation list
* @param handlerMethodMap the handler method map
* @param resourceMetadata the resource metadata
* @param dataRestRepository the repository data rest
* @param openAPI the open api
* @return the list
*/
private List<RouterOperation> findControllers
(List<RouterOperation> routerOperationList,
Map<RequestMappingInfo, HandlerMethod> handlerMethodMap, ResourceMetadata resourceMetadata,
DataRestRepository dataRestRepository, OpenAPI openAPI) {
dataRestRouterOperationBuilder.buildEntityRouterOperationList(routerOperationList, handlerMethodMap, resourceMetadata,
dataRestRepository, openAPI);
return routerOperationList;
}
}