Revision 61372
Added by Konstantinos Spyrou almost 3 years ago
RepositoryServiceImpl.java | ||
---|---|---|
13 | 13 |
import eu.dnetlib.repo.manager.domain.dto.Role; |
14 | 14 |
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; |
15 | 15 |
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService; |
16 |
import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater; |
|
17 |
import eu.dnetlib.repo.manager.service.security.AuthorizationService; |
|
16 | 18 |
import eu.dnetlib.repo.manager.service.security.RoleMappingService; |
17 |
import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater; |
|
18 | 19 |
import eu.dnetlib.repo.manager.utils.Converter; |
19 | 20 |
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader; |
20 | 21 |
import org.apache.commons.codec.digest.DigestUtils; |
... | ... | |
50 | 51 |
|
51 | 52 |
private static final Logger LOGGER = Logger.getLogger(RepositoryServiceImpl.class); |
52 | 53 |
|
54 |
private final AuthorizationService authorizationService; |
|
53 | 55 |
private final RoleMappingService roleMappingService; |
54 | 56 |
private final AaiRegistryService registryCalls; |
55 | 57 |
private final AuthoritiesUpdater authoritiesUpdater; |
... | ... | |
84 | 86 |
private HttpHeaders httpHeaders; |
85 | 87 |
|
86 | 88 |
@Autowired |
87 |
public RepositoryServiceImpl(RoleMappingService roleMappingService, |
|
89 |
public RepositoryServiceImpl(AuthorizationService authorizationService, |
|
90 |
RoleMappingService roleMappingService, |
|
88 | 91 |
AaiRegistryService registryCalls, |
89 | 92 |
AuthoritiesUpdater authoritiesUpdater, |
90 | 93 |
VocabularyLoader vocabularyLoader, |
... | ... | |
92 | 95 |
@Lazy EmailUtils emailUtils, |
93 | 96 |
@Lazy ValidatorService validatorService, |
94 | 97 |
@Lazy PiWikService piWikService) { |
98 |
this.authorizationService = authorizationService; |
|
95 | 99 |
this.roleMappingService = roleMappingService; |
96 | 100 |
this.registryCalls = registryCalls; |
97 | 101 |
this.authoritiesUpdater = authoritiesUpdater; |
... | ... | |
102 | 106 |
this.restTemplate = restTemplate; |
103 | 107 |
} |
104 | 108 |
|
105 |
private String sendEmail() {
|
|
109 |
private String getAuthenticatedUserEmail() {
|
|
106 | 110 |
OIDCAuthenticationToken authenticationToken = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); |
107 | 111 |
return authenticationToken.getUserInfo().getEmail(); |
108 | 112 |
} |
... | ... | |
153 | 157 |
.build().encode(); |
154 | 158 |
return restTemplate.getForObject(uriComponents.toUri(), Country[].class); |
155 | 159 |
} |
160 |
|
|
161 |
// FIXME: with the new roles of the users the "requestFilter.setRegisteredby(userEmail)" can no longer be used |
|
162 |
// and the "requestFilter.setId(repoId)" should return only one result at a time, thus, |
|
163 |
// another way for paging must be implemented. |
|
164 |
@Override |
|
165 |
public List<Repository> getRepositories(List<String> ids) throws JSONException { |
|
166 |
return getRepositories(ids, 0, 10); |
|
167 |
} |
|
156 | 168 |
|
169 |
// FIXME: with the new roles of the users the "requestFilter.setRegisteredby(userEmail)" can no longer be used |
|
170 |
// and the "requestFilter.setId(repoId)" should return only one result at a time, thus, |
|
171 |
// another way for paging must be implemented. |
|
172 |
@Override |
|
173 |
public List<Repository> getRepositories(List<String> ids, int page, int size) throws JSONException { |
|
174 |
List<Repository> repos = new ArrayList<>(); |
|
175 |
LOGGER.debug("Retreiving repositories with ids : " + String.join(", ", ids)); |
|
176 |
UriComponents uriComponents = searchDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); |
|
177 |
RequestFilter requestFilter = new RequestFilter(); |
|
157 | 178 |
|
179 |
try { |
|
180 |
for (String repoId : ids) { |
|
181 |
requestFilter.setId(repoId); |
|
182 |
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); |
|
183 |
|
|
184 |
repos.addAll(Converter.jsonToRepositoryList(new JSONObject(rs))); |
|
185 |
} |
|
186 |
} catch (JSONException e) { |
|
187 |
LOGGER.debug("Exception on getRepositoriesOfUser", e); |
|
188 |
emailUtils.reportException(e); |
|
189 |
throw e; |
|
190 |
} |
|
191 |
|
|
192 |
for (Repository r : repos) |
|
193 |
r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId())); |
|
194 |
return repos; |
|
195 |
} |
|
196 |
|
|
197 |
// FIXME: with the new roles of the users the "requestFilter.setRegisteredby(userEmail)" can no longer be used |
|
198 |
// and the "requestFilter.setId(repoId)" should return only one result at a time, thus, |
|
199 |
// another way for paging must be implemented. |
|
158 | 200 |
@Override |
201 |
public List<RepositorySnippet> getRepositoriesSnippets(List<String> ids) throws Exception { |
|
202 |
return getRepositoriesSnippets(ids, 0, 10); |
|
203 |
} |
|
204 |
|
|
205 |
// FIXME: with the new roles of the users the "requestFilter.setRegisteredby(userEmail)" can no longer be used |
|
206 |
// and the "requestFilter.setId(repoId)" should return only one result at a time, thus, |
|
207 |
// another way for paging must be implemented. |
|
208 |
@Override |
|
209 |
public List<RepositorySnippet> getRepositoriesSnippets(List<String> ids, int page, int size) throws Exception { |
|
210 |
List<RepositorySnippet> resultSet = new ArrayList<>(); |
|
211 |
ObjectMapper mapper = new ObjectMapper(); |
|
212 |
|
|
213 |
// here page should be 0 |
|
214 |
UriComponents uriComponents = searchSnipperDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); |
|
215 |
RequestFilter requestFilter = new RequestFilter(); |
|
216 |
|
|
217 |
try { |
|
218 |
for (String repoId : ids) { |
|
219 |
requestFilter.setId(repoId); |
|
220 |
|
|
221 |
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); |
|
222 |
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); |
|
223 |
resultSet.addAll(mapper.readValue(String.valueOf(jsonArray), |
|
224 |
mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); |
|
225 |
} |
|
226 |
} catch (Exception e) { |
|
227 |
LOGGER.debug("Exception on getRepositoriesSnippetOfUser", e); |
|
228 |
throw e; |
|
229 |
} |
|
230 |
|
|
231 |
LOGGER.debug("resultSet:" + resultSet); |
|
232 |
resultSet.parallelStream().forEach(repositorySnippet -> { |
|
233 |
repositorySnippet.setPiwikInfo(piWikService.getPiwikSiteForRepo(repositorySnippet.getId())); |
|
234 |
}); |
|
235 |
return resultSet; |
|
236 |
} |
|
237 |
|
|
238 |
|
|
239 |
@Override |
|
159 | 240 |
public List<RepositorySnippet> getRepositoriesByCountry(String country, |
160 | 241 |
String mode, |
161 | 242 |
Boolean managed) throws JSONException, IOException { |
... | ... | |
255 | 336 |
} |
256 | 337 |
|
257 | 338 |
@Override |
258 |
public List<Repository> getRepositoriesOfUser(String userEmail, |
|
259 |
String page, |
|
260 |
String size) throws JSONException { |
|
339 |
public List<Repository> getRepositoriesOfUser(String page, String size) throws JSONException { |
|
340 |
String userEmail = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(); |
|
341 |
LOGGER.debug("Retreiving repositories of authenticated user : " + userEmail); |
|
342 |
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles()); |
|
343 |
return getRepositories(new ArrayList<>(repoIds)); |
|
344 |
} |
|
261 | 345 |
|
262 |
LOGGER.debug("Retreiving repositories of user : " + userEmail); |
|
263 |
UriComponents uriComponents = searchDatasource(page, size); |
|
264 |
RequestFilter requestFilter = new RequestFilter(); |
|
265 |
// requestFilter.setRegisteredby(userEmail); |
|
346 |
@Override |
|
347 |
public List<Repository> getRepositoriesOfUser(String userEmail, String page, String size) throws JSONException { |
|
348 |
LOGGER.debug("Retreiving repositories of authenticated user : " + userEmail); |
|
349 |
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles(userEmail)); |
|
350 |
return getRepositories(new ArrayList<>(repoIds)); |
|
351 |
} |
|
266 | 352 |
|
267 |
List<String> repoIds = getRepoIdsFromUserRoles(userEmail); |
|
268 |
List<Repository> repos = new ArrayList<>(); |
|
269 |
|
|
270 |
try { |
|
271 |
for (String repoId : repoIds) { |
|
272 |
requestFilter.setId(repoId); |
|
273 |
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); |
|
274 |
|
|
275 |
repos.addAll(Converter.jsonToRepositoryList(new JSONObject(rs))); |
|
276 |
} |
|
277 |
} catch (Exception e) { |
|
278 |
LOGGER.debug("Exception on getRepositoriesOfUser", e); |
|
279 |
emailUtils.reportException(e); |
|
280 |
throw e; |
|
281 |
} |
|
282 |
|
|
283 |
for (Repository r : repos) |
|
284 |
r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId())); |
|
285 |
return repos; |
|
353 |
@Override |
|
354 |
public List<RepositorySnippet> getRepositoriesSnippetOfUser(String page, String size) throws Exception { |
|
355 |
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles()); |
|
356 |
return getRepositoriesSnippets(new ArrayList<>(repoIds)); |
|
286 | 357 |
} |
287 | 358 |
|
288 | 359 |
@Override |
289 |
public List<RepositorySnippet> getRepositoriesSnippetOfUser(String userEmail, String page, String size) throws IOException, JSONException { |
|
290 |
|
|
291 |
// TODO: Step 3 - For each repo from the previous step call findByRepositoryId piwik_site to get the full info of repo |
|
292 |
List<String> repoIds = getRepoIdsFromUserRoles(userEmail); |
|
293 |
|
|
294 |
List<RepositorySnippet> resultSet = new ArrayList<>(); |
|
295 |
ObjectMapper mapper = new ObjectMapper(); |
|
296 |
|
|
297 |
// here page should be 0 |
|
298 |
UriComponents uriComponents = searchSnipperDatasource(page, size); |
|
299 |
RequestFilter requestFilter = new RequestFilter(); |
|
300 |
|
|
301 |
try { |
|
302 |
for (String repoId : repoIds) { |
|
303 |
requestFilter.setId(repoId); |
|
304 |
|
|
305 |
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); |
|
306 |
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); |
|
307 |
resultSet.addAll(mapper.readValue(String.valueOf(jsonArray), |
|
308 |
mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); |
|
309 |
} |
|
310 |
} catch (Exception e) { |
|
311 |
LOGGER.debug("Exception on getRepositoriesSnippetOfUser", e); |
|
312 |
throw e; |
|
313 |
} |
|
314 |
|
|
315 |
LOGGER.debug("resultSet:" + resultSet); |
|
316 |
resultSet.parallelStream().forEach(repositorySnippet -> { |
|
317 |
repositorySnippet.setPiwikInfo(piWikService.getPiwikSiteForRepo(repositorySnippet.getId())); |
|
318 |
}); |
|
319 |
return resultSet; |
|
360 |
public List<RepositorySnippet> getRepositoriesSnippetOfUser(String userEmail, String page, String size) throws Exception { |
|
361 |
Collection<String> repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles(userEmail)); |
|
362 |
return getRepositoriesSnippets(new ArrayList<>(repoIds)); |
|
320 | 363 |
} |
321 | 364 |
|
322 | 365 |
@Override |
Also available in: Unified diff
1. optimization in retrieval time of repositories/snippets of authenticated user
2. Marked with FIXME methods that need attention