merge conflict

This commit is contained in:
Konstantinos Spyrou 2023-01-13 18:11:41 +02:00
commit 87f610b06b
26 changed files with 286 additions and 295 deletions

View File

@ -5,6 +5,5 @@
## Install and run: ## Install and run:
- Run **git clone** and then **cd uoa-repository-manager-service**. - Run **git clone** and then **cd uoa-repository-manager-service**.
- Provide all not-set or redacted configurations, inside the **src/main/resources/application.properties** file. - Provide all not-set or redacted configurations, inside the **src/main/resources/application.properties** file.
- Execute the **installAndRun.sh** script which installs and runs the app. - Build the app with: `mvn clean install -s <settings-file>`
- If you want to just run the app, then run the script with the argument "1": **./installAndRun.sh 1** - Run the app with: `java -jar ./target/uoa-repository-manager-service.jar`
- If you want to just install the app, then run the script with the argument "2": **./installAndRun.sh 2**

View File

@ -1,8 +1,8 @@
package eu.dnetlib.repo.manager.config; package eu.dnetlib.repo.manager.config;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import org.slf4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@ -33,14 +33,16 @@ public class FrontEndLinkURIAuthenticationSuccessHandler implements Authenticati
@Value("${services.provide.aai.oidc.domain}") @Value("${services.provide.aai.oidc.domain}")
private String domain; private String domain;
private static final Pattern AUTH_REGEX = Pattern.compile("^([A-Za-z0-9-_=]+)\\.([A-Za-z0-9-_=]+)\\.?([A-Za-z0-9-_.+=]*)$");
@Override @Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication; OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication;
request.getSession().setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, authOIDC.getUserInfo().getSub()); request.getSession().setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, authOIDC.getUserInfo().getSub());
Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue()); Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue());
String regex = "^([A-Za-z0-9-_=]+)\\.([A-Za-z0-9-_=]+)\\.?([A-Za-z0-9-_.+=]*)$"; Matcher matcher = AUTH_REGEX.matcher(authOIDC.getAccessTokenValue());
Matcher matcher = Pattern.compile(regex).matcher(authOIDC.getAccessTokenValue());
if (matcher.find()) { if (matcher.find()) {
long exp = new JsonParser().parse(new String(Base64.getDecoder().decode(matcher.group(2)))).getAsJsonObject().get("exp").getAsLong(); long exp = new JsonParser().parse(new String(Base64.getDecoder().decode(matcher.group(2)))).getAsJsonObject().get("exp").getAsLong();
accessToken.setMaxAge((int) (exp - (new Date().getTime() / 1000))); accessToken.setMaxAge((int) (exp - (new Date().getTime() / 1000)));

View File

@ -3,9 +3,9 @@ package eu.dnetlib.repo.manager.config;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.nimbusds.jwt.JWT; import com.nimbusds.jwt.JWT;
import eu.dnetlib.repo.manager.service.security.AuthoritiesMapper; import eu.dnetlib.repo.manager.service.security.AuthoritiesMapper;
import org.slf4j.Logger;
import org.mitre.openid.connect.client.OIDCAuthoritiesMapper; import org.mitre.openid.connect.client.OIDCAuthoritiesMapper;
import org.mitre.openid.connect.model.UserInfo; import org.mitre.openid.connect.model.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;

View File

@ -94,5 +94,4 @@ public class DashboardController {
return new BrokerSummary(brokerService.getSimpleSubscriptionsOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail()), brokerService.getTopicsForDatasource(datasourceName)); return new BrokerSummary(brokerService.getSimpleSubscriptionsOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail()), brokerService.getTopicsForDatasource(datasourceName));
} }
} }

View File

@ -6,8 +6,8 @@ import eu.dnetlib.repo.manager.exception.BrokerException;
import eu.dnetlib.repo.manager.exception.EndPointException; import eu.dnetlib.repo.manager.exception.EndPointException;
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
import eu.dnetlib.repo.manager.exception.ServerError; import eu.dnetlib.repo.manager.exception.ServerError;
import org.slf4j.Logger;
import org.json.JSONException; import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;

View File

@ -6,9 +6,9 @@ import eu.dnetlib.repo.manager.domain.JobsOfUser;
import eu.dnetlib.repo.manager.service.MonitorServiceImpl; import eu.dnetlib.repo.manager.service.MonitorServiceImpl;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.json.JSONException; import org.json.JSONException;
import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;

View File

@ -76,6 +76,7 @@ public class PiWikController {
results.setResults(returning); results.setResults(returning);
return results; return results;
} }
@ApiImplicitParams({ @ApiImplicitParams({
@ApiImplicitParam(name = "from", dataType = "number", paramType = "query"), @ApiImplicitParam(name = "from", dataType = "number", paramType = "query"),
@ApiImplicitParam(name = "quantity", dataType = "number", paramType = "query"), @ApiImplicitParam(name = "quantity", dataType = "number", paramType = "query"),
@ -103,20 +104,17 @@ public class PiWikController {
sb.append(" Repository ID , Repository name, Country, Site ID, Authentication token, Creation date, Requestor full name, Requestor email, Validated, Validation date, Comment \n"); sb.append(" Repository ID , Repository name, Country, Site ID, Authentication token, Creation date, Requestor full name, Requestor email, Validated, Validation date, Comment \n");
for(PiwikInfo piwikInfo : returning){ for(PiwikInfo piwikInfo : returning){
sb.append( sb.append(piwikInfo.getRepositoryId() == null ? "," : piwikInfo.getRepositoryId() + ",")
(piwikInfo.getRepositoryId() == null ? "," : piwikInfo.getRepositoryId()+ ",")+ .append(piwikInfo.getRepositoryName() == null ? "," : piwikInfo.getRepositoryName() + ",")
(piwikInfo.getRepositoryName() == null ? "," : piwikInfo.getRepositoryName()+ ",")+ .append(piwikInfo.getCountry() == null ? "," : piwikInfo.getCountry() + ",")
(piwikInfo.getCountry() == null ? "," : piwikInfo.getCountry()+ ",")+ .append(piwikInfo.getSiteId() == null ? "," : piwikInfo.getSiteId() + ",")
(piwikInfo.getSiteId() == null ? "," : piwikInfo.getSiteId()+ ",") + .append(piwikInfo.getAuthenticationToken() == null ? "," : piwikInfo.getAuthenticationToken() + ",")
(piwikInfo.getAuthenticationToken() == null ? "," : piwikInfo.getAuthenticationToken()+ ",")+ .append(piwikInfo.getCreationDate() == null ? "," : piwikInfo.getCreationDate().toString() + ",")
(piwikInfo.getCreationDate() == null ? "," : piwikInfo.getCreationDate().toString()+ ",") + .append(piwikInfo.getRequestorName() == null ? "," : piwikInfo.getRequestorName() + ",")
(piwikInfo.getRequestorName() == null ? "," : piwikInfo.getRequestorName()+ ",") + .append(piwikInfo.getRequestorEmail() == null ? "," : piwikInfo.getRequestorEmail() + ",")
(piwikInfo.getRequestorEmail() == null ? "," : piwikInfo.getRequestorEmail()+ ",")+ .append(piwikInfo.isValidated()).append(",")
piwikInfo.isValidated() + "," + .append(piwikInfo.getValidationDate() == null ? "," : piwikInfo.getValidationDate().toString() + ",")
(piwikInfo.getValidationDate() == null ? "," : piwikInfo.getValidationDate().toString()+ ",") + .append(piwikInfo.getComment() == null ? "\n" : piwikInfo.getComment() + "\n");
(piwikInfo.getComment() == null ? "\n" : piwikInfo.getComment()+ "\n")
);
} }
writer.write(sb.toString()); writer.write(sb.toString());
@ -141,7 +139,6 @@ public class PiWikController {
strDate); strDate);
response.setHeader(headerKey, headerValue); response.setHeader(headerKey, headerValue);
return new FileSystemResource(p.toFile()); return new FileSystemResource(p.toFile());
} }

View File

@ -11,9 +11,9 @@ import eu.dnetlib.repo.manager.service.RepositoryService;
import eu.dnetlib.repo.manager.service.security.AuthorizationService; import eu.dnetlib.repo.manager.service.security.AuthorizationService;
import eu.dnetlib.repo.manager.utils.JsonUtils; import eu.dnetlib.repo.manager.utils.JsonUtils;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.json.JSONException; import org.json.JSONException;
import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;

View File

@ -1,13 +1,11 @@
package eu.dnetlib.repo.manager.service; package eu.dnetlib.repo.manager.service;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.enabling.datasources.common.AggregationInfo; import eu.dnetlib.enabling.datasources.common.AggregationInfo;
import eu.dnetlib.repo.manager.domain.AggregationHistoryResponse; import eu.dnetlib.repo.manager.domain.AggregationHistoryResponse;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;

View File

@ -28,7 +28,10 @@ import javax.annotation.PostConstruct;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service("brokerService") @Service("brokerService")
public class BrokerServiceImpl implements BrokerService { public class BrokerServiceImpl implements BrokerService {
@ -49,7 +52,7 @@ public class BrokerServiceImpl implements BrokerService {
private HttpHeaders httpHeaders; private HttpHeaders httpHeaders;
private HashMap<String, Term> topics = new HashMap<String, Term>(); private HashMap<String, Term> topics = new HashMap<>();
@PostConstruct @PostConstruct
private void initDnetTopicsMap() { private void initDnetTopicsMap() {
@ -122,7 +125,7 @@ public class BrokerServiceImpl implements BrokerService {
@Override @Override
public EventsPage advancedShowEvents(String page, public EventsPage advancedShowEvents(String page,
String size, String size,
AdvQueryObject advQueryObject) throws BrokerException, JSONException, IOException { AdvQueryObject advQueryObject) throws BrokerException {
final String service = "/events/{page}/{pageSize}"; final String service = "/events/{page}/{pageSize}";
@ -157,7 +160,7 @@ public class BrokerServiceImpl implements BrokerService {
for (RepositorySnippet repo : repositories) { for (RepositorySnippet repo : repositories) {
BrowseEntry temp = new BrowseEntry(); BrowseEntry temp = new BrowseEntry();
temp.setValue(repo.getOfficialname()); temp.setValue(repo.getOfficialname());
temp.setSize(new Long(0)); temp.setSize(0L);
for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialname())) { for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialname())) {
temp.setSize(temp.getSize() + e.getSize()); temp.setSize(temp.getSize() + e.getSize());
} }
@ -166,12 +169,7 @@ public class BrokerServiceImpl implements BrokerService {
} }
// sort the collection by the second field of the tuple which is size // sort the collection by the second field of the tuple which is size
Collections.sort(entries, new Comparator<Tuple<BrowseEntry, String>>() { entries.sort((e1, e2) -> (int) (e2.getFirst().getSize() - e1.getFirst().getSize()));
@Override
public int compare(Tuple<BrowseEntry, String> e1, Tuple<BrowseEntry, String> e2) {
return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue());
}
});
long stop = System.currentTimeMillis(); long stop = System.currentTimeMillis();
System.out.println("getDatasourcesOfUserType returned in " + (stop - start) + "ms "); System.out.println("getDatasourcesOfUserType returned in " + (stop - start) + "ms ");

View File

@ -6,8 +6,8 @@ import eu.dnetlib.repo.manager.domain.Repository;
import eu.dnetlib.repo.manager.domain.RepositoryInterface; import eu.dnetlib.repo.manager.domain.RepositoryInterface;
import eu.dnetlib.repo.manager.exception.ValidationServiceException; import eu.dnetlib.repo.manager.exception.ValidationServiceException;
import eu.dnetlib.utils.MailLibrary; import eu.dnetlib.utils.MailLibrary;
import org.slf4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -745,6 +745,6 @@ public class EmailUtilsImpl implements EmailUtils {
user = ((OIDCAuthenticationToken) authentication).getUserInfo().getName(); user = ((OIDCAuthenticationToken) authentication).getUserInfo().getName();
} catch (NullPointerException ex) {} } catch (NullPointerException ex) {}
} }
return user; return user; // It may be just "user". TODO - Wouldn't be better if it was null?
} }
} }

View File

@ -7,10 +7,10 @@ import eu.dnetlib.repo.manager.domain.Constants;
import eu.dnetlib.repo.manager.domain.JobsOfUser; import eu.dnetlib.repo.manager.domain.JobsOfUser;
import eu.dnetlib.repo.manager.utils.CrisValidatorUtils; import eu.dnetlib.repo.manager.utils.CrisValidatorUtils;
import gr.uoa.di.driver.util.ServiceLocator; import gr.uoa.di.driver.util.ServiceLocator;
import org.slf4j.Logger;
import org.eurocris.openaire.cris.validator.model.Job; import org.eurocris.openaire.cris.validator.model.Job;
import org.eurocris.openaire.cris.validator.service.MapJobDao; import org.eurocris.openaire.cris.validator.service.MapJobDao;
import org.json.JSONException; import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;

View File

@ -5,8 +5,6 @@ import eu.dnetlib.repo.manager.exception.RepositoryServiceException;
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
import org.json.JSONException; import org.json.JSONException;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;

View File

@ -149,8 +149,6 @@ public class RepositoryServiceImpl implements RepositoryService {
countriesMap.put(c.getName(), c.getCode()); countriesMap.put(c.getName(), c.getCode());
inverseCountriesMap.put(c.getCode(), c.getName()); inverseCountriesMap.put(c.getCode(), c.getName());
} }
} }
@Override @Override
@ -181,11 +179,13 @@ public class RepositoryServiceImpl implements RepositoryService {
for (String repoId : ids) { for (String repoId : ids) {
requestFilter.setId(repoId); requestFilter.setId(repoId);
List<Repository> rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, List.class); List rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, List.class);
// repos.addAll(converter.toRepositoryList(new JSONObject(rs))); // repos.addAll(converter.toRepositoryList(new JSONObject(rs)));
} }
// TODO - "repos" is EMPTY!!
for (Repository r : repos) for (Repository r : repos)
r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId())); r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId()));
return repos; return repos;
@ -215,6 +215,10 @@ public class RepositoryServiceImpl implements RepositoryService {
requestFilter.setId(repoId); requestFilter.setId(repoId);
DatasourceResponse rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, DatasourceResponse.class); DatasourceResponse rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, DatasourceResponse.class);
if ( rs == null ) {
logger.error("The \"DatasourceResponse\" is null!");
return null;
}
resultSet.addAll(objectMapper.readValue(objectMapper.writeValueAsString(rs.getDatasourceInfo()), resultSet.addAll(objectMapper.readValue(objectMapper.writeValueAsString(rs.getDatasourceInfo()),
objectMapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); objectMapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class)));
@ -258,6 +262,11 @@ public class RepositoryServiceImpl implements RepositoryService {
requestFilter.setEoscDatasourceType(filterKey); requestFilter.setEoscDatasourceType(filterKey);
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
if ( rs == null ) {
logger.error("The result is null!");
return null;
}
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
while (jsonArray.length() > 0) { while (jsonArray.length() > 0) {
@ -266,6 +275,11 @@ public class RepositoryServiceImpl implements RepositoryService {
page += 1; page += 1;
uriComponents = searchSnipperDatasource(String.valueOf(page), String.valueOf(size)); uriComponents = searchSnipperDatasource(String.valueOf(page), String.valueOf(size));
rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
if ( rs == null ) {
logger.error("The result is null!");
break;
}
jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
} }
return resultSet; return resultSet;
@ -309,7 +323,7 @@ public class RepositoryServiceImpl implements RepositoryService {
} }
@Override @Override
public int getTotalRegisteredRepositories() { public int getTotalRegisteredRepositories() throws NullPointerException {
UriComponents uriComponents = UriComponentsBuilder UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(baseAddress + "/ds/countregistered") .fromHttpUrl(baseAddress + "/ds/countregistered")
.queryParam("fromDate", "1900-01-01") .queryParam("fromDate", "1900-01-01")
@ -373,6 +387,11 @@ public class RepositoryServiceImpl implements RepositoryService {
requestFilter.setId(id); requestFilter.setId(id);
String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class);
if ( rs == null ) {
logger.error("The result is null!");
return null;
}
JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
if (jsonArray.length() == 0) if (jsonArray.length() == 0)
@ -395,7 +414,12 @@ public class RepositoryServiceImpl implements RepositoryService {
// JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); // JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo");
DatasourceResponse response; DatasourceResponse response;
response = (DatasourceResponse) restTemplate.postForObject(uriComponents.toUri(), requestFilter, DatasourceResponse.class); response = restTemplate.postForObject(uriComponents.toUri(), requestFilter, DatasourceResponse.class);
if ( response == null ) {
logger.error("The response is null!");
return null;
}
List<DatasourceDetails> datasources = response.getDatasourceInfo(); List<DatasourceDetails> datasources = response.getDatasourceInfo();
if (datasources.size() == 0) if (datasources.size() == 0)
throw new ResourceNotFoundException(); throw new ResourceNotFoundException();
@ -530,7 +554,7 @@ public class RepositoryServiceImpl implements RepositoryService {
// logger.debug("JSON to add(update) -> " + json_repository); // logger.debug("JSON to add(update) -> " + json_repository);
HttpEntity<Repository> httpEntity = new HttpEntity<>(repository, httpHeaders); // TODO: check if it works (Repository contains extra fields) HttpEntity<Repository> httpEntity = new HttpEntity<>(repository, httpHeaders); // TODO: check if it works (Repository contains extra fields)
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); ResponseEntity<ResponseEntity> responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
emailUtils.sendUserRegistrationEmail(repository, authentication); emailUtils.sendUserRegistrationEmail(repository, authentication);
@ -556,7 +580,7 @@ public class RepositoryServiceImpl implements RepositoryService {
// logger.debug("JSON to update -> " + json_repository); // logger.debug("JSON to update -> " + json_repository);
HttpEntity<Repository> httpEntity = new HttpEntity<>(repository, httpHeaders); HttpEntity<Repository> httpEntity = new HttpEntity<>(repository, httpHeaders);
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); ResponseEntity<ResponseEntity> responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
emailUtils.sendUserUpdateRepositoryInfoEmail(repository, authentication); emailUtils.sendUserUpdateRepositoryInfoEmail(repository, authentication);
@ -584,7 +608,7 @@ public class RepositoryServiceImpl implements RepositoryService {
.encode(); .encode();
// String json_repository = converter.toJson(repository); // String json_repository = converter.toJson(repository);
HttpEntity<Repository> httpEntity = new HttpEntity<>(repository, httpHeaders); HttpEntity<Repository> httpEntity = new HttpEntity<>(repository, httpHeaders);
ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); ResponseEntity<ResponseEntity> responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class);
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
emailUtils.sendUserRegistrationEmail(repository, authentication); emailUtils.sendUserRegistrationEmail(repository, authentication);
@ -776,7 +800,7 @@ public class RepositoryServiceImpl implements RepositoryService {
if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_ALL)) if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_ALL))
return compatibilityClasses; return compatibilityClasses;
else if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA)) { else if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_RE3DATA)) {
if (entry.getKey().matches("^openaire[1-9].0_data$")) { if ( ValidatorServiceImpl.OPENAIRE_DATA_REGEX.matcher(entry.getKey()).matches() ) {
retMap.put(entry.getKey(), entry.getValue()); retMap.put(entry.getKey(), entry.getValue());
foundData = true; foundData = true;
} }
@ -787,7 +811,7 @@ public class RepositoryServiceImpl implements RepositoryService {
foundData = true; foundData = true;
} }
} else { } else {
if (entry.getKey().matches("^openaire[1-9].0$") || entry.getKey().equals("driver")) if ( ValidatorServiceImpl.OPENAIRE_OR_DRIVER_REGEX.matcher(entry.getKey()).matches() )
retMap.put(entry.getKey(), entry.getValue()); retMap.put(entry.getKey(), entry.getValue());
} }
} }

View File

@ -84,28 +84,7 @@ public class StatsServiceImpl implements StatsService {
" oaftype exact datasource and " + " oaftype exact datasource and " +
" datasourcetypename exact Data Repository "; " datasourcetypename exact Data Repository ";
UriComponents uriComponents = UriComponentsBuilder return getMetadataTotals(url);
.fromHttpUrl(url)
.queryParam("page",0)
.queryParam("size",0)
.queryParam("format","json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("meta");
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
} }
@ -117,28 +96,7 @@ public class StatsServiceImpl implements StatsService {
" ( datasourcetypename exact Institutional Repository " + " ( datasourcetypename exact Institutional Repository " +
" or datasourcetypename exact Publication Repository )"; " or datasourcetypename exact Publication Repository )";
UriComponents uriComponents = UriComponentsBuilder return getMetadataTotals(url);
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("meta");
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
} }
@ -149,28 +107,7 @@ public class StatsServiceImpl implements StatsService {
" oaftype exact datasource and " + " oaftype exact datasource and " +
" datasourcetypename exact Journal"; " datasourcetypename exact Journal";
UriComponents uriComponents = UriComponentsBuilder return getMetadataTotals(url);
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("meta");
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
} }
@ -178,28 +115,7 @@ public class StatsServiceImpl implements StatsService {
{ {
String url = baseAddress + "/publications/count"; String url = baseAddress + "/publications/count";
UriComponents uriComponents = UriComponentsBuilder return getCommonStats(url);
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map<?, ?>) rs.getBody();
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
} }
@ -207,28 +123,7 @@ public class StatsServiceImpl implements StatsService {
{ {
String url = baseAddress + "/datasets/count"; String url = baseAddress + "/datasets/count";
UriComponents uriComponents = UriComponentsBuilder return getCommonStats(url);
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map<?, ?>) rs.getBody();
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
} }
@ -236,28 +131,7 @@ public class StatsServiceImpl implements StatsService {
{ {
String url = baseAddress + "/software/count"; String url = baseAddress + "/software/count";
UriComponents uriComponents = UriComponentsBuilder return getCommonStats(url);
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map<?, ?>) rs.getBody();
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
} }
@ -298,17 +172,70 @@ public class StatsServiceImpl implements StatsService {
try { try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("totals"); Map statsTotals = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("totals");
if ( metadata == null ) { if ( statsTotals == null ) {
logger.error("The metadata was null!"); logger.error("The statsTotals was null!");
return null; return null;
} }
// TODO - Will we ever need the following lines?
// String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); // String rs = restTemplate.getForObject(uriComponents.toUri(), String.class);
// JSONObject resultSet = new JSONObject(rs); // JSONObject resultSet = new JSONObject(rs);
// JSONObject totals = resultSet.getJSONObject("totals"); // JSONObject totals = resultSet.getJSONObject("totals");
return (Integer) metadata.get("events"); return (Integer) statsTotals.get("events");
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private String getMetadataTotals(String url) {
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map metadata = (Map) ((Map<?, ?>) Objects.requireNonNull(rs.getBody())).get("meta");
if ( metadata == null ) {
logger.error("The metadata was null!");
return null;
}
return String.valueOf(metadata.get("total"));
} catch ( RestClientException rce ) {
logger.error(rce.getMessage());
return null;
} catch ( Exception e ) {
logger.error("", e);
return null;
}
}
private String getCommonStats(String url) {
UriComponents uriComponents = UriComponentsBuilder
.fromHttpUrl(url)
.queryParam("page", 0)
.queryParam("size", 0)
.queryParam("format", "json")
.build().encode();
try {
ResponseEntity<Map> rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class);
Map stats = (Map<?, ?>) rs.getBody();
if ( stats == null ) {
logger.error("The stats was null!");
return null;
}
return String.valueOf(stats.get("total"));
} catch ( RestClientException rce ) { } catch ( RestClientException rce ) {
logger.error(rce.getMessage()); logger.error(rce.getMessage());
return null; return null;

View File

@ -1,5 +1,6 @@
package eu.dnetlib.repo.manager.service; package eu.dnetlib.repo.manager.service;
import eu.dnetlib.usagestats.sushilite.domain.Customer;
import eu.dnetlib.usagestats.sushilite.domain.ReportItem; import eu.dnetlib.usagestats.sushilite.domain.ReportItem;
import eu.dnetlib.usagestats.sushilite.domain.ReportResponseWrapper; import eu.dnetlib.usagestats.sushilite.domain.ReportResponseWrapper;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -7,10 +8,12 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference; import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder; import org.springframework.web.util.UriComponentsBuilder;
@ -42,62 +45,79 @@ public class SushiliteServiceImpl implements SushiliteService {
String Granularity, String Granularity,
String Pretty) { String Pretty) {
//build the uri params //build the uri params
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usagestatsSushiliteEndpoint + "GetReport/") UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usagestatsSushiliteEndpoint + "GetReport/")
.queryParam("Report", Report) .queryParam("Report", Report)
.queryParam("Release", Release) .queryParam("Release", Release)
.queryParam("RequestorID", RequestorID) .queryParam("RequestorID", RequestorID)
.queryParam("BeginDate", BeginDate) .queryParam("BeginDate", BeginDate)
.queryParam("EndDate", EndDate) .queryParam("EndDate", EndDate)
.queryParam("RepositoryIdentifier", RepositoryIdentifier) .queryParam("RepositoryIdentifier", RepositoryIdentifier)
.queryParam("ItemIdentifier", ItemIdentifier) .queryParam("ItemIdentifier", ItemIdentifier)
.queryParam("ItemDataType", ItemDataType) .queryParam("ItemDataType", ItemDataType)
.queryParam("Granularity", Granularity) .queryParam("Granularity", Granularity)
.queryParam("Pretty", Pretty); .queryParam("Pretty", Pretty);
//create new template engine //create new restTemplate engine
RestTemplate template = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
ResponseEntity<ReportResponseWrapper> resp; ResponseEntity<ReportResponseWrapper> resp;
try {
//communicate with endpoint //communicate with endpoint
resp = template.exchange( resp = restTemplate.exchange(
builder.build().encode().toUri(), builder.build().encode().toUri(),
HttpMethod.GET, HttpMethod.GET,
null, null,
new ParameterizedTypeReference<ReportResponseWrapper>() { new ParameterizedTypeReference<ReportResponseWrapper>() {});
}); } catch (RestClientException rce) {
logger.error("", rce);
return null;
}
// check remote api's response // check remote api's response
System.out.println("sushi responded status " + resp.getStatusCode()); HttpStatus httpStatus = resp.getStatusCode();
if ( httpStatus != HttpStatus.OK ) {
logger.warn("Sushi cannot give us data! Responded status: " + httpStatus);
return null;
}
// get the items corresponding to the requested page ReportResponseWrapper reportResponseWrapper = resp.getBody();
List<ReportItem> requestedItemList = new ArrayList<>(); if ( reportResponseWrapper == null ) {
if (resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems() != null) { logger.error("The \"reportResponseWrapper\" for sushi was null!");
try { return null;
int totalItems = resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems().size(); }
int size = Integer.parseInt(pageSize);
int offset = Integer.parseInt(page)*size;
if (offset < totalItems ) { // get the items corresponding to the requested page
int upperIndex = offset+size; List<ReportItem> requestedItemList = new ArrayList<>();
if (upperIndex > totalItems) {
upperIndex = totalItems; Customer customer = reportResponseWrapper.getReportResponse().getReportWrapper().getReport().getCustomer();
} List<ReportItem> allReportItems = customer.getReportItems();
requestedItemList = resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems().subList(offset,upperIndex);
if ( allReportItems != null) {
try {
int totalItems = allReportItems.size();
int size = Integer.parseInt(pageSize);
int offset = (Integer.parseInt(page) * size);
if (offset < totalItems ) {
int upperIndex = (offset + size);
if (upperIndex > totalItems) {
upperIndex = totalItems;
} }
} catch (NumberFormatException e) { requestedItemList = allReportItems.subList(offset, upperIndex);
logger.debug("Exception on getReportResults - trying to cast strings to integers", e);
//emailUtils.reportException(e);
throw e;
} }
} catch (NumberFormatException e) {
logger.debug("Exception on getReportResults - trying to cast strings to integers", e);
//emailUtils.reportException(e);
throw e;
} }
}
ReportResponseWrapper newReportResponse = resp.getBody(); customer.setReportItems(requestedItemList); // Setting the reportItems to the "customer"-reference, updates the "reportResponseWrapper" object.
newReportResponse.getReportResponse().getReportWrapper().getReport().getCustomer().setReportItems(requestedItemList);
return newReportResponse; return reportResponseWrapper;
} }
} }

View File

@ -12,11 +12,11 @@ import eu.dnetlib.repo.manager.exception.ValidationServiceException;
import eu.dnetlib.repo.manager.utils.CrisValidatorUtils; import eu.dnetlib.repo.manager.utils.CrisValidatorUtils;
import eu.dnetlib.repo.manager.utils.OaiTools; import eu.dnetlib.repo.manager.utils.OaiTools;
import gr.uoa.di.driver.util.ServiceLocator; import gr.uoa.di.driver.util.ServiceLocator;
import org.slf4j.Logger;
import org.eurocris.openaire.cris.validator.model.Job; import org.eurocris.openaire.cris.validator.model.Job;
import org.eurocris.openaire.cris.validator.service.JobExecutor; import org.eurocris.openaire.cris.validator.service.JobExecutor;
import org.eurocris.openaire.cris.validator.service.MapJobDao; import org.eurocris.openaire.cris.validator.service.MapJobDao;
import org.json.JSONException; import org.json.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -29,6 +29,7 @@ import javax.annotation.PostConstruct;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.*; import java.util.*;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -69,6 +70,12 @@ public class ValidatorServiceImpl implements ValidatorService {
@Autowired @Autowired
private MapJobDao crisJobs; private MapJobDao crisJobs;
public static final Pattern OPENAIRE_DATA_REGEX = Pattern.compile("^openaire[1-9].0_data$");
public static final Pattern OPENAIRE_OR_DRIVER_REGEX = Pattern.compile("^(?:openaire[1-9].0|driver)$");
public static final Pattern OPENAIRE_CRIS_REGEX = Pattern.compile("^openaire[1-9].0_cris$");
public static final Pattern FAIR_REGEX = Pattern.compile(".*fair$");
@PostConstruct @PostConstruct
private void loadRules(){ private void loadRules(){
logger.debug("PostConstruct method! Load rules!"); logger.debug("PostConstruct method! Load rules!");
@ -76,13 +83,14 @@ public class ValidatorServiceImpl implements ValidatorService {
for (RuleSet ruleSet : getValidationService().getRuleSets()) { for (RuleSet ruleSet : getValidationService().getRuleSets()) {
if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) { if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) {
String key = ""; String key = "";
if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_data$")) String guidelinesAcronym = ruleSet.getGuidelinesAcronym();
if ( OPENAIRE_DATA_REGEX.matcher(guidelinesAcronym).matches() )
key = Constants.VALIDATION_MODE_DATA; key = Constants.VALIDATION_MODE_DATA;
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0$") || ruleSet.getGuidelinesAcronym().equals("driver")) else if ( OPENAIRE_OR_DRIVER_REGEX.matcher(guidelinesAcronym).matches() )
key = Constants.VALIDATION_MODE_LITERATURE; key = Constants.VALIDATION_MODE_LITERATURE;
else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$")) else if ( OPENAIRE_CRIS_REGEX.matcher(guidelinesAcronym).matches() )
key = Constants.VALIDATION_MODE_CRIS; key = Constants.VALIDATION_MODE_CRIS;
else if (ruleSet.getGuidelinesAcronym().matches(".*fair$")) else if ( FAIR_REGEX.matcher(guidelinesAcronym).matches() )
key = Constants.VALIDATION_MODE_FAIR; key = Constants.VALIDATION_MODE_FAIR;
if (rulesetMap.containsKey(key)) if (rulesetMap.containsKey(key))
@ -140,7 +148,7 @@ public class ValidatorServiceImpl implements ValidatorService {
///////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////
if (jobForValidation.getSelectedContentRules()!=null && jobForValidation.getSelectedContentRules().size() == 1 && if (jobForValidation.getSelectedContentRules()!=null && jobForValidation.getSelectedContentRules().size() == 1 &&
jobForValidation.getSelectedContentRules().contains(-1000) jobForValidation.getSelectedContentRules().contains(-1000)
|| jobForValidation.getDesiredCompatibilityLevel().matches("openaire-cris_1.1")) { || jobForValidation.getDesiredCompatibilityLevel().equals("openaire-cris_1.1")) {
crisValidatorExecutor.submit(jobForValidation.getBaseUrl(), jobForValidation.getUserEmail()); crisValidatorExecutor.submit(jobForValidation.getBaseUrl(), jobForValidation.getUserEmail());
} else { } else {
this.getValidationService().submitValidationJob(jobForValidation); this.getValidationService().submitValidationJob(jobForValidation);
@ -194,7 +202,7 @@ public class ValidatorServiceImpl implements ValidatorService {
@Override @Override
public List<RuleSet> getRuleSets(String mode) { public List<RuleSet> getRuleSets(String mode) {
logger.info("Getting rulesets for mode: {}", mode); logger.info("Getting ruleSets for mode: {}", mode);
return rulesetMap.get(mode); return rulesetMap.get(mode);
} }

View File

@ -7,8 +7,8 @@ import eu.dnetlib.repo.manager.domain.dto.Role;
import eu.dnetlib.repo.manager.domain.dto.User; import eu.dnetlib.repo.manager.domain.dto.User;
import eu.dnetlib.repo.manager.service.aai.registry.utils.RegistryUtils; import eu.dnetlib.repo.manager.service.aai.registry.utils.RegistryUtils;
import eu.dnetlib.repo.manager.utils.HttpUtils; import eu.dnetlib.repo.manager.utils.HttpUtils;
import org.slf4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
@ -99,11 +99,7 @@ public class RegistryCalls implements AaiRegistryService {
try { try {
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
String sub = authentication.getUserInfo().getSub(); String sub = authentication.getUserInfo().getSub();
Map<String, String> params = new HashMap<>(); return getCoPersonIdByIdentifier(sub);
params.put("coid", coid);
params.put("search.identifier", sub);
JsonElement response = httpUtils.get("co_people.json", params);
return (response != null) ? response.getAsJsonObject().get("CoPeople").getAsJsonArray().get(0).getAsJsonObject().get("Id").getAsInt() : null;
} catch (Exception e) { } catch (Exception e) {
logger.error("Get User info: An error occurred ", e); logger.error("Get User info: An error occurred ", e);
return null; return null;
@ -297,9 +293,11 @@ public class RegistryCalls implements AaiRegistryService {
JsonArray infos = getUserEmailByCouId(couId, false); JsonArray infos = getUserEmailByCouId(couId, false);
infos.forEach(info -> { infos.forEach(info -> {
JsonObject jsonInfo = info.getAsJsonObject();
User user = new User(); User user = new User();
user.setEmail(jsonInfo.get("email").getAsString());
// TODO: should add firstname and lastname and sub of user // TODO: should add firstname and lastname and sub of user
user.setEmail(info.getAsJsonObject().get("email").getAsString());
users.add(user); users.add(user);
}); });
@ -317,9 +315,10 @@ public class RegistryCalls implements AaiRegistryService {
JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray(); JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray();
JsonArray names = new JsonArray(); JsonArray names = new JsonArray();
infos.forEach(info -> { infos.forEach(info -> {
JsonObject jsonInfo = info.getAsJsonObject();
JsonObject user = new JsonObject(); JsonObject user = new JsonObject();
user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString()); user.addProperty("name", jsonInfo.get("Given").getAsString() + " " + jsonInfo.get("Family").getAsString());
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString()); user.addProperty("memberSince", jsonInfo.get("Created").getAsString());
names.add(user); names.add(user);
}); });
return names; return names;
@ -336,9 +335,10 @@ public class RegistryCalls implements AaiRegistryService {
JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray(); JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray();
JsonArray emails = new JsonArray(); JsonArray emails = new JsonArray();
infos.forEach(info -> { infos.forEach(info -> {
JsonObject jsonInfo = info.getAsJsonObject();
JsonObject user = new JsonObject(); JsonObject user = new JsonObject();
user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString()); user.addProperty("id", jsonInfo.get("Identifier").getAsString());
user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString()); user.addProperty("memberSince", jsonInfo.get("Created").getAsString());
emails.add(user); emails.add(user);
}); });
return emails; return emails;
@ -381,7 +381,11 @@ public class RegistryCalls implements AaiRegistryService {
params.put("copersonid", coPersonId.toString()); params.put("copersonid", coPersonId.toString());
JsonElement response = httpUtils.get("names.json", params); JsonElement response = httpUtils.get("names.json", params);
JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null; JsonObject info = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray().get(0).getAsJsonObject() : null;
return (info != null) ? info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString() : null; if ( info != null ) {
JsonObject jsonInfo = info.getAsJsonObject();
return jsonInfo.get("Given").getAsString() + " " + jsonInfo.get("Family").getAsString();
} else
return null;
} }
@Override @Override
@ -428,8 +432,9 @@ public class RegistryCalls implements AaiRegistryService {
int total = couIds.size(); int total = couIds.size();
for (JsonElement cou : cous) { for (JsonElement cou : cous) {
if (count < total) { if (count < total) {
if (idNameMap.containsKey(cou.getAsJsonObject().get("Id").getAsInt())) { JsonObject jsonCou = cou.getAsJsonObject();
idNameMap.put(cou.getAsJsonObject().get("Id").getAsInt(), cou.getAsJsonObject().get("Name").getAsString()); if (idNameMap.containsKey(jsonCou.get("Id").getAsInt())) {
idNameMap.put(jsonCou.get("Id").getAsInt(), jsonCou.get("Name").getAsString());
count++; count++;
} }
} else { } else {

View File

@ -2,11 +2,13 @@ package eu.dnetlib.repo.manager.service.security;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.nimbusds.jose.util.StandardCharset;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.util.*; import java.util.*;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -15,7 +17,7 @@ import java.util.regex.Pattern;
public class AuthoritiesMapper { public class AuthoritiesMapper {
private static final Logger logger = LoggerFactory.getLogger(AuthoritiesMapper.class); private static final Logger logger = LoggerFactory.getLogger(AuthoritiesMapper.class);
private static final String ENTITLEMENT_REGEX = "urn:geant:openaire[.]eu:group:([^:]*):?(.*)?:role=member#aai[.]openaire[.]eu"; private static final Pattern ENTITLEMENT_REGEX = Pattern.compile("urn:geant:openaire[.]eu:group:([^:]*):?(.*)?:role=member#aai[.]openaire[.]eu");
private AuthoritiesMapper() { private AuthoritiesMapper() {
} }
@ -26,22 +28,24 @@ public class AuthoritiesMapper {
return authorities; return authorities;
} }
public static List<String> entitlementRoles(JsonArray entitlements) { public static List<String> entitlementRoles(JsonArray entitlements) throws UnsupportedEncodingException {
List<String> roles = new ArrayList<>(); List<String> roles = new ArrayList<>();
if (entitlements != null) { if (entitlements != null) {
for (JsonElement obj : entitlements) { for (JsonElement obj : entitlements) {
Matcher matcher = Pattern.compile(ENTITLEMENT_REGEX).matcher(obj.getAsString()); Matcher matcher = ENTITLEMENT_REGEX.matcher(obj.getAsString());
if (matcher.find()) { if (matcher.find()) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (matcher.group(1) != null && matcher.group(1).length() > 0) { String group1 = matcher.group(1);
sb.append(matcher.group(1)); if (group1 != null && group1.length() > 0) {
sb.append(group1);
} }
if (matcher.group(2).length() > 0) { String group2 = matcher.group(2);
if (group2.length() > 0) {
sb.append(":"); sb.append(":");
sb.append(matcher.group(2)); sb.append(group2);
} }
String role = sb.toString().replace("+", " "); String role = sb.toString().replace("+", " ");
roles.add(URLDecoder.decode(role)); roles.add(URLDecoder.decode(role, StandardCharset.UTF_8.name()));
} }
} }
} }
@ -51,18 +55,20 @@ public class AuthoritiesMapper {
private static void entityRoles(JsonArray entitlements, Set<GrantedAuthority> authorities) { private static void entityRoles(JsonArray entitlements, Set<GrantedAuthority> authorities) {
if (entitlements != null) { if (entitlements != null) {
for (JsonElement obj : entitlements) { for (JsonElement obj : entitlements) {
Matcher matcher = Pattern.compile(ENTITLEMENT_REGEX).matcher(obj.getAsString()); Matcher matcher = ENTITLEMENT_REGEX.matcher(obj.getAsString());
if (matcher.find()) { if (matcher.find()) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
if (matcher.group(1) != null && matcher.group(1).length() > 0) { String group1 = matcher.group(1);
sb.append(matcher.group(1).replace("+-+", "_").replaceAll("[+.]", "_").toUpperCase()); if (group1 != null && group1.length() > 0) {
sb.append(group1.replace("+-+", "_").replaceAll("[+.]", "_").toUpperCase());
} }
if (matcher.group(2).length() > 0) { String group2 = matcher.group(2);
if (group2.length() > 0) {
sb.append("_"); sb.append("_");
if (matcher.group(2).equals("admins")) { if (group2.equals("admins")) {
sb.append("MANAGER"); sb.append("MANAGER");
} else { } else {
sb.append(matcher.group(2).toUpperCase()); sb.append(group2.toUpperCase());
} }
} }
authorities.add(new SimpleGrantedAuthority(sb.toString())); authorities.add(new SimpleGrantedAuthority(sb.toString()));

View File

@ -1,7 +1,7 @@
package eu.dnetlib.repo.manager.service.security; package eu.dnetlib.repo.manager.service.security;
import org.slf4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
@ -29,10 +29,10 @@ public class AuthoritiesUpdater extends HttpSessionSecurityContextRepository {
public void update(String id, Update update) { public void update(String id, Update update) {
if (sessions != null) { if (sessions != null) {
Map<String, Session> map = sessions. Map map = sessions.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, id);
findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, id);
if (map != null) { if (map != null) {
for (Session session : map.values()) { for ( Object sessionObject : map.values()) {
Session session = (Session) sessionObject;
logger.debug(session.getId()); logger.debug(session.getId());
if (!session.isExpired()) { if (!session.isExpired()) {
SecurityContext securityContext = session.getAttribute(SPRING_SECURITY_CONTEXT_KEY); SecurityContext securityContext = session.getAttribute(SPRING_SECURITY_CONTEXT_KEY);

View File

@ -4,9 +4,9 @@ import com.google.gson.JsonElement;
import eu.dnetlib.repo.manager.domain.dto.User; import eu.dnetlib.repo.manager.domain.dto.User;
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService; import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService;
import org.slf4j.Logger;
import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.OIDCAuthenticationToken;
import org.mitre.openid.connect.model.UserInfo; import org.mitre.openid.connect.model.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;

View File

@ -1,15 +1,13 @@
package eu.dnetlib.repo.manager.utils; package eu.dnetlib.repo.manager.utils;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import eu.dnetlib.repo.manager.domain.*; import eu.dnetlib.repo.manager.domain.*;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -53,7 +51,7 @@ public class Converter {
return resultSet; return resultSet;
} }
public List<RepositoryInterface> toRepositoryInterfaceList(List<ApiDetails> apiDetailsList) throws JSONException { public List<RepositoryInterface> toRepositoryInterfaceList(List<ApiDetails> apiDetailsList) {
List<RepositoryInterface> resultSet = new ArrayList<>(); List<RepositoryInterface> resultSet = new ArrayList<>();

View File

@ -3,6 +3,7 @@ package eu.dnetlib.repo.manager.utils;
import eu.dnetlib.domain.functionality.validator.JobResultEntry; import eu.dnetlib.domain.functionality.validator.JobResultEntry;
import eu.dnetlib.domain.functionality.validator.StoredJob; import eu.dnetlib.domain.functionality.validator.StoredJob;
import org.eurocris.openaire.cris.validator.model.Job; import org.eurocris.openaire.cris.validator.model.Job;
import org.eurocris.openaire.cris.validator.model.Rule;
import org.eurocris.openaire.cris.validator.model.RuleResults; import org.eurocris.openaire.cris.validator.model.RuleResults;
import java.text.DateFormat; import java.text.DateFormat;
@ -20,14 +21,17 @@ public class CrisValidatorUtils {
StoredJob sj = new StoredJob(); StoredJob sj = new StoredJob();
sj.setId(job.getId().hashCode()); sj.setId(job.getId().hashCode());
sj.setValidationStatus(job.getStatus()); sj.setValidationStatus(job.getStatus());
if (job.getDateFinished() != null) {
sj.setEnded(DATE_FORMATTER.format(job.getDateFinished())); Date dateFinished = job.getDateFinished();
sj.setDuration(DURATION_FORMATTER.format(new Date(job.getDateFinished().getTime() - job.getDateStarted().getTime()))); Date dateStarted = job.getDateStarted();
if ( dateFinished != null ) {
sj.setEnded(DATE_FORMATTER.format(dateFinished));
sj.setDuration(DURATION_FORMATTER.format(new Date(dateFinished.getTime() - dateStarted.getTime())));
} else { } else {
sj.setEnded("-"); sj.setEnded("-");
sj.setDuration("-"); sj.setDuration("-");
} }
sj.setStarted(DATE_FORMATTER.format(job.getDateStarted())); sj.setStarted(DATE_FORMATTER.format(dateStarted));
sj.setUserEmail(job.getUser()); sj.setUserEmail(job.getUser());
sj.setCris(true); sj.setCris(true);
@ -56,21 +60,24 @@ public class CrisValidatorUtils {
public static List<JobResultEntry> crisResultsToJobEntries(Job crisJob) { public static List<JobResultEntry> crisResultsToJobEntries(Job crisJob) {
List<JobResultEntry> jobResultEntries = new ArrayList<>(); List<JobResultEntry> jobResultEntries = new ArrayList<>();
for (RuleResults ruleResults : crisJob.getRuleResults()) { for (RuleResults ruleResults : crisJob.getRuleResults()) {
Rule rule = ruleResults.getRule();
JobResultEntry jobResultEntry = new JobResultEntry(); JobResultEntry jobResultEntry = new JobResultEntry();
jobResultEntry.setName(ruleResults.getRule().getName()); jobResultEntry.setName(rule.getName());
jobResultEntry.setRuleId(ruleResults.getRule().getId()); jobResultEntry.setRuleId(rule.getId());
jobResultEntry.setDescription(ruleResults.getRule().getDescription()); jobResultEntry.setDescription(rule.getDescription());
jobResultEntry.setMandatory(true); jobResultEntry.setMandatory(true);
jobResultEntry.setWeight(Math.round(ruleResults.getRule().getWeight())); jobResultEntry.setWeight(Math.round(rule.getWeight()));
jobResultEntry.setType(ruleResults.getRule().getType()); jobResultEntry.setType(rule.getType());
jobResultEntry.setHasErrors(false); jobResultEntry.setHasErrors(false);
if (ruleResults.getCount() == 0) { long countRuleResults = ruleResults.getCount();
if (countRuleResults == 0) {
jobResultEntry.setSuccesses("-"); jobResultEntry.setSuccesses("-");
} else { } else {
jobResultEntry.setSuccesses(ruleResults.getCount() - ruleResults.getFailed() + "/" + ruleResults.getCount()); jobResultEntry.setSuccesses(countRuleResults - ruleResults.getFailed() + "/" + countRuleResults);
} }
if (ruleResults.getErrorMessages() != null && !ruleResults.getErrorMessages().isEmpty()) { List<String> errorMessages = ruleResults.getErrorMessages();
jobResultEntry.setErrors(ruleResults.getErrorMessages()); if (errorMessages != null && !errorMessages.isEmpty()) {
jobResultEntry.setErrors(errorMessages);
jobResultEntry.setHasErrors(true); jobResultEntry.setHasErrors(true);
} }
jobResultEntries.add(jobResultEntry); jobResultEntries.add(jobResultEntry);

View File

@ -86,12 +86,17 @@ public class HttpUtils {
} }
private JsonElement getResponseEntityAsJsonElement(ResponseEntity<String> responseEntity) { private JsonElement getResponseEntityAsJsonElement(ResponseEntity<String> responseEntity) {
if (responseEntity != null && responseEntity.getBody() != null) {
logger.debug(responseEntity.getBody()); if ( responseEntity == null )
return null;
String responseBody = responseEntity.getBody();
if ( responseBody != null ) {
logger.debug(responseBody);
try { try {
return new JsonParser().parse(responseEntity.getBody()); return new JsonParser().parse(responseBody);
} catch (Exception e) { } catch (Exception e) {
logger.warn("Could not parse response body", e); logger.warn("Could not parse response body", e); // Will return null.
} }
} }
return null; return null;

View File

@ -1,7 +1,7 @@
package eu.dnetlib.repo.manager.utils; package eu.dnetlib.repo.manager.utils;
import org.slf4j.Logger;
import org.dom4j.io.DOMWriter; import org.dom4j.io.DOMWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.w3c.dom.Document; import org.w3c.dom.Document;
import se.kb.oai.pmh.*; import se.kb.oai.pmh.*;
@ -29,7 +29,7 @@ public class OaiTools {
public static List<String> getSetsOfRepo(String baseUrl) throws Exception { public static List<String> getSetsOfRepo(String baseUrl) throws Exception {
try { try {
logger.debug("Getting sets of repository " + baseUrl); logger.debug("Getting sets of repository: {}", baseUrl);
OaiPmhServer harvester = new OaiPmhServer(baseUrl); OaiPmhServer harvester = new OaiPmhServer(baseUrl);
SetsList setList = harvester.listSets(); SetsList setList = harvester.listSets();
ResumptionToken token = setList.getResumptionToken(); ResumptionToken token = setList.getResumptionToken();
@ -56,7 +56,7 @@ public class OaiTools {
} }
public static boolean identifyRepository(String baseUrl) throws Exception { public static boolean identifyRepository(String baseUrl) throws Exception {
logger.debug("sending identify request to repo " + baseUrl); logger.debug("sending identify request to repo: {}", baseUrl);
OaiPmhServer harvester = new OaiPmhServer(baseUrl); OaiPmhServer harvester = new OaiPmhServer(baseUrl);

View File

@ -1,4 +1,4 @@
package eu.dnetlib.repo.manager.controllers; package eu.dnetlib.repo.manager.utils;
import eu.dnetlib.repo.manager.exception.EndPointException; import eu.dnetlib.repo.manager.exception.EndPointException;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;