Revision 52781
Added by Panagiotis Kanakakis almost 6 years ago
RepositoryApiImpl.java | ||
---|---|---|
6 | 6 |
import eu.dnetlib.domain.data.Repository; |
7 | 7 |
import eu.dnetlib.domain.data.RepositoryInterface; |
8 | 8 |
import eu.dnetlib.domain.enabling.Vocabulary; |
9 |
import eu.dnetlib.repo.manager.service.domain.RequestFilter; |
|
10 |
import eu.dnetlib.repo.manager.service.exception.ResourceNotFoundException; |
|
9 | 11 |
import eu.dnetlib.repo.manager.service.utils.Converter; |
10 |
import eu.dnetlib.repo.manager.service.domain.RequestFilter; |
|
11 | 12 |
import eu.dnetlib.repo.manager.shared.*; |
12 | 13 |
import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader; |
13 | 14 |
import org.apache.commons.codec.digest.DigestUtils; |
... | ... | |
18 | 19 |
import org.springframework.beans.factory.annotation.Autowired; |
19 | 20 |
import org.springframework.beans.factory.annotation.Value; |
20 | 21 |
import org.springframework.core.ParameterizedTypeReference; |
21 |
import org.springframework.http.*; |
|
22 |
import org.springframework.http.HttpEntity; |
|
23 |
import org.springframework.http.HttpHeaders; |
|
24 |
import org.springframework.http.HttpMethod; |
|
25 |
import org.springframework.http.ResponseEntity; |
|
22 | 26 |
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
27 |
import org.springframework.security.access.annotation.Secured; |
|
28 |
import org.springframework.security.access.prepost.PostAuthorize; |
|
23 | 29 |
import org.springframework.security.access.prepost.PreAuthorize; |
24 | 30 |
import org.springframework.stereotype.Component; |
25 | 31 |
import org.springframework.web.bind.annotation.PathVariable; |
... | ... | |
32 | 38 |
|
33 | 39 |
import javax.annotation.PostConstruct; |
34 | 40 |
import java.sql.Timestamp; |
35 |
import java.text.Normalizer; |
|
36 | 41 |
import java.util.*; |
37 | 42 |
import java.util.concurrent.ConcurrentHashMap; |
43 |
import java.util.function.Function; |
|
44 |
import java.util.stream.Collectors; |
|
38 | 45 |
|
39 | 46 |
@Component |
40 | 47 |
public class RepositoryApiImpl implements RepositoryApi { |
... | ... | |
174 | 181 |
jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); |
175 | 182 |
} |
176 | 183 |
for (Repository r : resultSet) |
177 |
this.getRepositoryInfo(r);
|
|
184 |
this.updateRepositoryInfo(r);
|
|
178 | 185 |
|
179 | 186 |
return resultSet; |
180 | 187 |
}catch (Exception e){ |
... | ... | |
186 | 193 |
|
187 | 194 |
} |
188 | 195 |
|
189 |
private void getRepositoryInfo(Repository r) throws JSONException {
|
|
196 |
private Repository updateRepositoryInfo(Repository r) throws JSONException {
|
|
190 | 197 |
|
191 | 198 |
/* |
192 | 199 |
* from datasource class |
... | ... | |
196 | 203 |
r.setInterfaces(this.getRepositoryInterface(r.getId())); |
197 | 204 |
r.setPiwikInfo(piWikApi.getPiwikSiteForRepo(r.getId())); |
198 | 205 |
r.setCountryName(getCountryName(r.getCountryCode())); |
206 |
return r; |
|
199 | 207 |
} |
200 | 208 |
|
201 | 209 |
|
... | ... | |
226 | 234 |
|
227 | 235 |
List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs)); |
228 | 236 |
for (Repository r : repos) |
229 |
this.getRepositoryInfo(r);
|
|
237 |
this.updateRepositoryInfo(r);
|
|
230 | 238 |
|
231 | 239 |
return repos; |
232 | 240 |
}catch (Exception e){ |
... | ... | |
237 | 245 |
} |
238 | 246 |
|
239 | 247 |
@Override |
240 |
public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException { |
|
248 |
public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException {
|
|
241 | 249 |
|
242 | 250 |
LOGGER.debug("Retreiving repositories with id : " + id ); |
243 | 251 |
Repository repo = null; |
... | ... | |
248 | 256 |
try{ |
249 | 257 |
String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class); |
250 | 258 |
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); |
251 |
if(jsonArray.length() > 0) |
|
252 |
repo = Converter.jsonToRepositoryObject(jsonArray.getJSONObject(0)); |
|
253 |
if (repo != null) |
|
254 |
getRepositoryInfo(repo); |
|
255 |
return repo; |
|
256 |
}catch (Exception e){ |
|
259 |
|
|
260 |
if(jsonArray.length() == 0) |
|
261 |
throw new ResourceNotFoundException(); |
|
262 |
|
|
263 |
repo = Converter.jsonToRepositoryObject(jsonArray.getJSONObject(0)); |
|
264 |
return updateRepositoryInfo(repo); |
|
265 |
}catch (JSONException e){ |
|
257 | 266 |
LOGGER.debug("Exception on getRepositoryById" , e); |
258 | 267 |
emailUtils.reportException(e); |
259 | 268 |
throw e; |
... | ... | |
263 | 272 |
|
264 | 273 |
|
265 | 274 |
@Override |
266 |
public Aggregations getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
|
|
275 |
public List<AggregationDetails> getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
|
|
267 | 276 |
|
268 | 277 |
LOGGER.debug("Retreiving aggregations for repository with id : " + id ); |
269 | 278 |
UriComponents uriComponents = searchDatasource("0","100"); |
270 | 279 |
RequestFilter requestFilter = new RequestFilter(); |
271 | 280 |
requestFilter.setId(id); |
272 |
Aggregations aggregations = new Aggregations(); |
|
273 | 281 |
|
282 |
List<AggregationDetails> aggregationHistory = new ArrayList<>(); |
|
283 |
|
|
274 | 284 |
try { |
275 | 285 |
String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class); |
276 | 286 |
JSONObject repository = new JSONObject(rs); |
287 |
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0))); |
|
288 |
return aggregationHistory.size() == 0? aggregationHistory : aggregationHistory.stream() |
|
289 |
.sorted(Comparator.comparing(AggregationDetails::getDate).reversed()) |
|
290 |
.limit(20) |
|
291 |
.collect(Collectors.toList()); |
|
292 |
} catch (JSONException e) { |
|
293 |
LOGGER.debug("Exception on getRepositoryAggregations" , e); |
|
294 |
emailUtils.reportException(e); |
|
295 |
throw e; |
|
296 |
} |
|
277 | 297 |
|
278 |
aggregations.setAggregationHistory(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0))); |
|
279 |
aggregations.setLastCollection(Converter.getLastCollectionFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0))); |
|
280 |
aggregations.setLastTransformation(Converter.getLastTransformationFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0))); |
|
281 |
return aggregations; |
|
298 |
} |
|
299 |
|
|
300 |
@Override |
|
301 |
public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException { |
|
302 |
LOGGER.debug("Retreiving aggregations (by year) for repository with id : " + id ); |
|
303 |
UriComponents uriComponents = searchDatasource("0","100"); |
|
304 |
RequestFilter requestFilter = new RequestFilter(); |
|
305 |
requestFilter.setId(id); |
|
306 |
|
|
307 |
List<AggregationDetails> aggregationHistory = new ArrayList<>(); |
|
308 |
Map<String, List<AggregationDetails>> aggregationByYear = new HashMap<>(); |
|
309 |
try { |
|
310 |
String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class); |
|
311 |
JSONObject repository = new JSONObject(rs); |
|
312 |
aggregationHistory.addAll(Converter.getAggregationHistoryFromJson(repository.getJSONArray("datasourceInfo").getJSONObject(0))); |
|
313 |
return aggregationHistory.size() == 0? aggregationByYear:createYearMap(aggregationHistory); |
|
314 |
|
|
282 | 315 |
} catch (JSONException e) { |
283 | 316 |
LOGGER.debug("Exception on getRepositoryAggregations" , e); |
284 | 317 |
emailUtils.reportException(e); |
285 | 318 |
throw e; |
286 | 319 |
} |
320 |
} |
|
287 | 321 |
|
322 |
private Map<String,List<AggregationDetails>> createYearMap(List<AggregationDetails> aggregationHistory) { |
|
323 |
Map<String, List<AggregationDetails>> aggregationByYear; |
|
324 |
aggregationHistory = aggregationHistory.stream() |
|
325 |
.sorted(Comparator.comparing(AggregationDetails::getDate).reversed()) |
|
326 |
.collect(Collectors.toList()); |
|
327 |
|
|
328 |
return aggregationHistory.stream() |
|
329 |
.collect(Collectors.groupingBy(AggregationDetails::getYear)); |
|
288 | 330 |
} |
289 | 331 |
|
332 |
|
|
290 | 333 |
@Override |
291 | 334 |
public List<Repository> getRepositoriesByName(@PathVariable("name") String name, |
292 | 335 |
@PathVariable("page") String page, |
... | ... | |
301 | 344 |
String rs = restTemplate.postForObject(uriComponents.toUri(),requestFilter, String.class); |
302 | 345 |
List<Repository> repos = Converter.jsonToRepositoryList(new JSONObject(rs)); |
303 | 346 |
for (Repository r : repos) |
304 |
getRepositoryInfo(r);
|
|
347 |
updateRepositoryInfo(r);
|
|
305 | 348 |
return repos; |
306 | 349 |
}catch (Exception e){ |
307 | 350 |
LOGGER.debug("Exception on getRepositoriesByName" , e); |
... | ... | |
331 | 374 |
} |
332 | 375 |
|
333 | 376 |
@Override |
334 |
@PreAuthorize("hasRole('ROLE_USER')") |
|
377 |
@PreAuthorize("hasRole('ROLE_USER') and #repository.registeredBy == authentication.userInfo.email")
|
|
335 | 378 |
public Repository addRepository(@RequestParam("datatype") String datatype, |
336 | 379 |
@RequestBody Repository repository) throws Exception { |
337 | 380 |
|
... | ... | |
342 | 385 |
} |
343 | 386 |
|
344 | 387 |
@Override |
345 |
@PreAuthorize("hasRole('ROLE_USER')") |
|
388 |
@PreAuthorize("hasRole('ROLE_USER') and #repository.registeredBy == authentication.userInfo.email")
|
|
346 | 389 |
public Repository updateRepository(@RequestBody Repository repository) throws JSONException { |
347 | 390 |
UriComponents uriComponents = UriComponentsBuilder |
348 | 391 |
.fromHttpUrl(baseAddress + "/ds/update/") |
... | ... | |
432 | 475 |
} |
433 | 476 |
|
434 | 477 |
@Override |
435 |
@PreAuthorize("hasRole('ROLE_USER')") |
|
436 |
public void deleteRepositoryInterface(@RequestParam("id") String id){ |
|
478 |
@PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email") |
|
479 |
public void deleteRepositoryInterface(@RequestParam("id") String id , |
|
480 |
@RequestParam("registeredBy") String registeredBy){ |
|
437 | 481 |
UriComponents uriComponents = UriComponentsBuilder |
438 | 482 |
.fromHttpUrl(baseAddress + "/ds/api/") |
439 | 483 |
.path("/{id}") |
... | ... | |
443 | 487 |
} |
444 | 488 |
|
445 | 489 |
@Override |
446 |
@PreAuthorize("hasRole('ROLE_USER')") |
|
490 |
@PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email")
|
|
447 | 491 |
public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype, |
448 | 492 |
@RequestParam("repoId") String repoId, |
449 |
@RequestBody RepositoryInterface repositoryInterface) throws JSONException { |
|
493 |
@RequestParam("registeredBy") String registeredBy, |
|
494 |
@RequestBody RepositoryInterface repositoryInterface) throws JSONException,ResourceNotFoundException { |
|
450 | 495 |
try { |
451 | 496 |
Repository e = this.getRepositoryById(repoId); |
452 | 497 |
repositoryInterface = createRepositoryInterface(e,repositoryInterface,datatype); |
... | ... | |
670 | 715 |
} |
671 | 716 |
|
672 | 717 |
@Override |
673 |
@PreAuthorize("hasRole('ROLE_USER')") |
|
718 |
@PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email")
|
|
674 | 719 |
public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId, |
720 |
@RequestParam("registeredBy") String registeredBy, |
|
675 | 721 |
@RequestBody RepositoryInterface repositoryInterface) throws JSONException { |
676 | 722 |
|
677 | 723 |
this.updateBaseUrl(repoId,repositoryInterface.getId(),repositoryInterface.getBaseUrl()); |
Also available in: Unified diff
1. Dockerize service
2. Add authorization checks
3. Handle exceptions ( controller advice, exception package)
4. Login-logout operations bug fixes