diff --git a/README.md b/README.md new file mode 100644 index 0000000..5ca50b9 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Provide + +[...] + +## Install and run: +- 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. +- Build the app with: `mvn clean install -s ` +- Run the app with: `java -jar ./target/uoa-repository-manager-service.jar` diff --git a/installAndRun.sh b/installAndRun.sh deleted file mode 100755 index b92bd10..0000000 --- a/installAndRun.sh +++ /dev/null @@ -1,43 +0,0 @@ -cd "${0%/*}" || (echo "Could not change-dir to this script's dir!" && exit) # Change the working directory to the script's directory, when running from other location. - -if [[ $# -gt 1 ]]; then - echo -e "Wrong number of arguments given: ${#}\nPlease execute it like: installAndRun.sh <1 | 0 (optional)>"; exit 1 -fi - -justRun=0 - -if [[ $# -eq 1 ]]; then # If we have just 1 argument. - numbers_re='^[0-9]+$' - if ! [[ $1 =~ $numbers_re ]]; then # If the first argument is not numeric. - echo -e "Invalid, non-numeric argument given: ${1}\nPlease execute it like: installAndRun.sh <1 | 0 (optional)>"; exit 2 - fi - - if [[ $1 -ne 0 && $1 -ne 1 ]]; then # If the 1st argument does NOT equal to < 0 > or < 1 >. - echo -e "Invalid argument given: ${1}\nPlease execute it like: installAndRun.sh <1 | 0 (optional)>"; exit 3 - fi - - justRun=$1 # Assign <1> or <0> -fi - -dnet_repo=~/.m2/repository-dnet45 -settings_file=settings-dnet45.xml - -if [[ justRun -eq 1 ]]; then - if [ ! -f ${dnet_repo}"/"${settings_file} ]; then - echo -e "The file \"~/.m2/repository-dnet45/settings-dnet45.xml\" does not exist! Exiting.." && exit 4 - fi -else - if [ ! -d ${dnet_repo} ]; then - mkdir ${dnet_repo} || (echo -e "The directory \"~/.m2/repository-dnet45/\" could not be created! Exiting.." && exit 5) - echo "Created the \"dnet_repo\": ${dnet_repo}" - fi - - if [ ! -f ${dnet_repo}"/"${settings_file} ]; then - cp ${settings_file} ${dnet_repo}"/"${settings_file} || (echo -e "The file \"settings-dnet45.xml\" could not be copied in directory \"~/.m2/repository-dnet45\"! Exiting.." && exit 6) - echo "Copied the \"settings_file\" to: ${dnet_repo}/${settings_file}" - fi - - mvn clean install -s ${dnet_repo}"/"${settings_file} -fi - -java -jar ./target/uoa-repository-manager-service.jar diff --git a/pom.xml b/pom.xml index 351b8d0..5b76779 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ 4.0.0 eu.dnetlib uoa-repository-manager-service - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT jar @@ -22,7 +22,7 @@ org.springframework.boot - spring-boot-starter + spring-boot-starter-web org.springframework.boot @@ -30,27 +30,21 @@ - org.springframework.boot spring-boot-starter-log4j2 - - org.springframework.boot - spring-boot-starter-web - org.springframework.boot spring-boot-starter-data-jdbc org.springframework.boot - spring-boot-actuator-autoconfigure + spring-boot-starter-data-jpa org.springframework.boot - spring-boot-starter-tomcat - + spring-boot-actuator-autoconfigure org.springframework.boot @@ -60,26 +54,23 @@ org.springframework.boot spring-boot-starter-validation - - - + + org.springframework.boot + spring-boot-starter-test + test + + + com.h2database + h2 + + + + + eu.dnetlib.dhp + dnet-exporter-api + [3.3.3-SNAPSHOT, ) + + eu.dnetlib uoa-clients @@ -122,36 +113,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - eu.dnetlib dnet-openaire-usage-stats-sushilite @@ -191,38 +152,6 @@ 2.7.2 - - - - - - - - - - - - - - - - - - - com.sun.jersey jersey-client @@ -264,13 +193,6 @@ 9.1-901.jdbc3 - - - - - - - commons-fileupload commons-fileupload @@ -292,14 +214,6 @@ org.springframework.session spring-session-data-redis - @@ -323,18 +237,7 @@ cors-filter 2.5 - - com.netflix.hystrix - hystrix-core - 1.5.18 - - + javax.xml.ws @@ -381,37 +284,6 @@ - - org.mockito - mockito-core - 2.26.0 - test - - - - - - - - - - - - - - - - - - - - io.micrometer - micrometer-registry-prometheus - 1.7.2 - compile - - - @@ -520,30 +392,6 @@ false - - dnet45-bootstrap-snapshot - D-Net 45 Bootstrap Snapshot - https://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-bootstrap-snapshot - - false - - - true - - default - - - dnet45-bootstrap-release - D-Net 45 Bootstrap Release - https://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-bootstrap-release - - true - - - false - - default - diff --git a/settings-dnet45.xml b/settings-dnet45.xml deleted file mode 100644 index 6b67467..0000000 --- a/settings-dnet45.xml +++ /dev/null @@ -1,43 +0,0 @@ - - - ${env.HOME}/.m2/repository-dnet45 - - org.apache.tomcat.maven - - - - dnet4.0 - - true - - - - dnet45-bootstrap-snapshot - D-Net 45 Bootstrap Snapshot - https://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-bootstrap-snapshot - - false - - - true - - default - - - dnet45-bootstrap-release - D-Net 45 Bootstrap Release - https://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-bootstrap-release - - true - - - false - - default - - - - - diff --git a/src/main/java/eu/dnetlib/repo/manager/config/AaiSecurityConfiguration.java b/src/main/java/eu/dnetlib/repo/manager/config/AaiSecurityConfiguration.java index 495ebcd..0a02ce5 100644 --- a/src/main/java/eu/dnetlib/repo/manager/config/AaiSecurityConfiguration.java +++ b/src/main/java/eu/dnetlib/repo/manager/config/AaiSecurityConfiguration.java @@ -15,7 +15,6 @@ import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; -import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint; import org.springframework.security.web.authentication.preauth.AbstractPreAuthenticatedProcessingFilter; import java.util.*; diff --git a/src/main/java/eu/dnetlib/repo/manager/config/AsyncConfiguration.java b/src/main/java/eu/dnetlib/repo/manager/config/AsyncConfiguration.java index fea620a..e2689eb 100644 --- a/src/main/java/eu/dnetlib/repo/manager/config/AsyncConfiguration.java +++ b/src/main/java/eu/dnetlib/repo/manager/config/AsyncConfiguration.java @@ -1,6 +1,7 @@ package eu.dnetlib.repo.manager.config; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.AsyncConfigurer; @@ -15,7 +16,7 @@ public class AsyncConfiguration implements AsyncConfigurer { @Override public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() { return new AsyncUncaughtExceptionHandler() { - private final Logger logger = Logger.getLogger(getClass()); + private final Logger logger = LoggerFactory.getLogger(getClass()); @Override public void handleUncaughtException(Throwable throwable, Method method, Object... objects) { diff --git a/src/main/java/eu/dnetlib/repo/manager/config/CustomRestTemplateConfig.java b/src/main/java/eu/dnetlib/repo/manager/config/CustomRestTemplateConfig.java new file mode 100644 index 0000000..1a018a2 --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/config/CustomRestTemplateConfig.java @@ -0,0 +1,40 @@ +package eu.dnetlib.repo.manager.config; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +@Configuration +public class CustomRestTemplateConfig { + + @Bean + public RestTemplate restTemplate() { + RestTemplate restTemplate = new RestTemplate(); + restTemplate.getMessageConverters().add(0, mappingJacksonHttpMessageConverter()); + return restTemplate; + } + + @Bean + public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() { + MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(); + converter.setObjectMapper(customObjectMapper()); + return converter; + } + + @Bean + public ObjectMapper customObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + + // Do not fail when reading unknown properties + objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + // Do not fail when reading properties of different case + objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true); + + return objectMapper; + } + +} diff --git a/src/main/java/eu/dnetlib/repo/manager/config/DatasourceConfiguration.java b/src/main/java/eu/dnetlib/repo/manager/config/DatasourceConfiguration.java deleted file mode 100644 index 192606f..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/config/DatasourceConfiguration.java +++ /dev/null @@ -1,59 +0,0 @@ -package eu.dnetlib.repo.manager.config; - -import org.apache.commons.dbcp.BasicDataSource; -import org.apache.log4j.Logger; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.jdbc.datasource.DataSourceTransactionManager; -import org.springframework.transaction.annotation.EnableTransactionManagement; - -@Configuration -@EnableTransactionManagement -public class DatasourceConfiguration { - - private static Logger LOGGER = Logger.getLogger(DatasourceConfiguration.class); - - @Value("${services.provide.db.driverClassName}") - private String driverClassname; - - @Value("${services.provide.db.url}") - private String URL; - - @Value("${services.provide.db.username}") - private String username; - - @Value("${services.provide.db.password}") - private String password; - - @Bean - public BasicDataSource dataSource(){ - BasicDataSource basicDataSource = new BasicDataSource(); - basicDataSource.setDriverClassName(driverClassname); - basicDataSource.setUrl(URL); - basicDataSource.setUsername(username); - basicDataSource.setPassword(password); - basicDataSource.setMaxIdle(10); - basicDataSource.setMaxActive(100); - basicDataSource.setMaxWait(1000); - basicDataSource.setValidationQuery("SELECT 1;"); - basicDataSource.setTestOnBorrow(true); - basicDataSource.setTestOnReturn(true); - basicDataSource.setTestWhileIdle(true); - basicDataSource.setTimeBetweenEvictionRunsMillis(1200000); - basicDataSource.setMinEvictableIdleTimeMillis(1800000); - basicDataSource.setMinEvictableIdleTimeMillis(5); - basicDataSource.setPoolPreparedStatements(true); - basicDataSource.setDefaultAutoCommit(true); - - return basicDataSource; - } - - @Bean - public DataSourceTransactionManager txManager(){ - DataSourceTransactionManager txManager = new DataSourceTransactionManager(); - txManager.setDataSource(dataSource()); - return txManager; - } - -} diff --git a/src/main/java/eu/dnetlib/repo/manager/config/FrontEndLinkURIAuthenticationSuccessHandler.java b/src/main/java/eu/dnetlib/repo/manager/config/FrontEndLinkURIAuthenticationSuccessHandler.java index 99fbf7e..0afa2b6 100644 --- a/src/main/java/eu/dnetlib/repo/manager/config/FrontEndLinkURIAuthenticationSuccessHandler.java +++ b/src/main/java/eu/dnetlib/repo/manager/config/FrontEndLinkURIAuthenticationSuccessHandler.java @@ -1,8 +1,9 @@ package eu.dnetlib.repo.manager.config; import com.google.gson.JsonParser; -import org.apache.log4j.Logger; import org.mitre.openid.connect.model.OIDCAuthenticationToken; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; @@ -22,25 +23,26 @@ public class FrontEndLinkURIAuthenticationSuccessHandler implements Authenticati private String frontEndURI; - private static final Logger LOGGER = Logger - .getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class); + private static final Logger logger = LoggerFactory.getLogger(FrontEndLinkURIAuthenticationSuccessHandler.class); public void init() { - LOGGER.debug("Front end uri : " + frontEndURI); + logger.debug("Front end uri : {}", frontEndURI); } @Value("${services.provide.aai.oidc.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 public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication; - request.getSession().setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, authOIDC.getUserInfo().getEmail()); + request.getSession().setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, authOIDC.getUserInfo().getSub()); Cookie accessToken = new Cookie("AccessToken", authOIDC.getAccessTokenValue()); - String regex = "^([A-Za-z0-9-_=]+)\\.([A-Za-z0-9-_=]+)\\.?([A-Za-z0-9-_.+=]*)$"; - Matcher matcher = Pattern.compile(regex).matcher(authOIDC.getAccessTokenValue()); + Matcher matcher = AUTH_REGEX.matcher(authOIDC.getAccessTokenValue()); if (matcher.find()) { 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))); diff --git a/src/main/java/eu/dnetlib/repo/manager/config/OpenAIREAuthoritiesMapper.java b/src/main/java/eu/dnetlib/repo/manager/config/OpenAIREAuthoritiesMapper.java index c992e17..5558de6 100644 --- a/src/main/java/eu/dnetlib/repo/manager/config/OpenAIREAuthoritiesMapper.java +++ b/src/main/java/eu/dnetlib/repo/manager/config/OpenAIREAuthoritiesMapper.java @@ -3,9 +3,10 @@ package eu.dnetlib.repo.manager.config; import com.google.gson.JsonArray; import com.nimbusds.jwt.JWT; import eu.dnetlib.repo.manager.service.security.AuthoritiesMapper; -import org.apache.log4j.Logger; import org.mitre.openid.connect.client.OIDCAuthoritiesMapper; import org.mitre.openid.connect.model.UserInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; @@ -18,7 +19,7 @@ import java.util.Set; @Component public class OpenAIREAuthoritiesMapper implements OIDCAuthoritiesMapper { - private static final Logger logger = Logger.getLogger(OpenAIREAuthoritiesMapper.class); + private static final Logger logger = LoggerFactory.getLogger(OpenAIREAuthoritiesMapper.class); @Value("${services.provide.adminEmail}") String adminEmail; @@ -33,7 +34,7 @@ public class OpenAIREAuthoritiesMapper implements OIDCAuthoritiesMapper { } else if (userInfo.getSource().getAsJsonArray("eduperson_entitlement") != null) { entitlements = userInfo.getSource().getAsJsonArray("eduperson_entitlement"); } - logger.debug("user info: " + userInfo + "\nentitlements: " + entitlements); + logger.debug("user info: {}\nentitlements: {}", userInfo, entitlements); // FIXME: delete this if statement when super administrators are set if (userInfo.getEmail() != null && userInfo.getEmail().equals(adminEmail)) { diff --git a/src/main/java/eu/dnetlib/repo/manager/config/RedisConfiguration.java b/src/main/java/eu/dnetlib/repo/manager/config/RedisConfiguration.java index 74a4aa7..12ecf55 100644 --- a/src/main/java/eu/dnetlib/repo/manager/config/RedisConfiguration.java +++ b/src/main/java/eu/dnetlib/repo/manager/config/RedisConfiguration.java @@ -1,16 +1,14 @@ package eu.dnetlib.repo.manager.config; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; -import org.springframework.http.HttpHeaders; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.session.web.http.CookieSerializer; import org.springframework.session.web.http.DefaultCookieSerializer; -import org.springframework.web.client.RestTemplate; import javax.annotation.PostConstruct; @@ -18,7 +16,7 @@ import javax.annotation.PostConstruct; @EnableRedisHttpSession public class RedisConfiguration { - private static Logger LOGGER = Logger.getLogger(RedisConfiguration.class); + private static final Logger logger = LoggerFactory.getLogger(RedisConfiguration.class); @Value("${services.provide.redis.host}") private String host; @@ -34,12 +32,12 @@ public class RedisConfiguration { @PostConstruct private void init() { - LOGGER.info(String.format("Redis : %s Port : %s Password : %s", host, port, password)); + logger.info(String.format("Redis : %s Port : %s Password : %s", host, port, password)); } @Bean public JedisConnectionFactory connectionFactory() { - LOGGER.info(String.format("Redis : %s Port : %s Password : %s", host, port, password)); + logger.info(String.format("Redis : %s Port : %s Password : %s", host, port, password)); JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(); jedisConnectionFactory.setHostName(host); jedisConnectionFactory.setPort(Integer.parseInt(port)); @@ -54,18 +52,8 @@ public class RedisConfiguration { serializer.setCookieName("openAIRESession"); serializer.setCookiePath("/"); serializer.setDomainName(domain); - LOGGER.info("Serializer : " + serializer); + logger.info("Cookie Serializer : {}", serializer); return serializer; } - @Bean - public RestTemplate restTemplate() { - RestTemplate restTemplate = new RestTemplate(); - restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.set("Content-Type", "application/json"); - return restTemplate; - } - - } diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/BrokerController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/BrokerController.java index 29392b8..187da9c 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/BrokerController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/BrokerController.java @@ -1,8 +1,8 @@ package eu.dnetlib.repo.manager.controllers; -import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.domain.Term; import eu.dnetlib.repo.manager.domain.broker.*; +import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.service.BrokerServiceImpl; import io.swagger.annotations.Api; import io.swagger.annotations.ApiParam; diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/DashboardController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/DashboardController.java index 2a0fd23..c04ad14 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/DashboardController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/DashboardController.java @@ -1,12 +1,13 @@ package eu.dnetlib.repo.manager.controllers; -import eu.dnetlib.repo.manager.domain.*; +import eu.dnetlib.enabling.datasources.common.AggregationInfo; +import eu.dnetlib.repo.manager.domain.BrokerSummary; +import eu.dnetlib.repo.manager.domain.CollectionMonitorSummary; +import eu.dnetlib.repo.manager.domain.RepositorySummaryInfo; +import eu.dnetlib.repo.manager.domain.UsageSummary; import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.exception.RepositoryServiceException; -import eu.dnetlib.repo.manager.service.BrokerService; -import eu.dnetlib.repo.manager.service.DashboardService; -import eu.dnetlib.repo.manager.service.PiWikService; -import eu.dnetlib.repo.manager.service.RepositoryService; +import eu.dnetlib.repo.manager.service.*; import io.swagger.annotations.Api; import org.json.JSONException; import org.mitre.openid.connect.model.OIDCAuthenticationToken; @@ -29,6 +30,9 @@ public class DashboardController { @Autowired private RepositoryService repositoryService; + @Autowired + private AggregationService aggregationService; + @Autowired private BrokerService brokerService; @@ -53,20 +57,20 @@ public class DashboardController { @PathVariable("repoId") String repoId, @RequestParam(name = "size", required = false, defaultValue = "20") int size) throws JSONException { - List aggregationDetails = repositoryService.getRepositoryAggregations(repoId, 0, size); + List aggregationInfo = aggregationService.getRepositoryAggregations(repoId, 0, size); CollectionMonitorSummary collectionMonitorSummary = new CollectionMonitorSummary(); - collectionMonitorSummary.setAggregationDetails(aggregationDetails); + collectionMonitorSummary.setAggregationInfo(aggregationInfo); size = 0; do { - aggregationDetails = repositoryService.getRepositoryAggregations(repoId, size, size + 50); - for (AggregationDetails aggregationDetail : aggregationDetails) { - if (aggregationDetail.getIndexedVersion()) { + aggregationInfo = aggregationService.getRepositoryAggregations(repoId, size, size + 50); + for (AggregationInfo aggregationDetail : aggregationInfo) { + if (aggregationDetail.isIndexedVersion()) { collectionMonitorSummary.setLastIndexedVersion(aggregationDetail); break; } } size += 30; - } while (aggregationDetails.size() != 0 && collectionMonitorSummary.getLastIndexedVersion() == null); + } while (aggregationInfo.size() != 0 && collectionMonitorSummary.getLastIndexedVersion() == null); return collectionMonitorSummary; } @@ -90,5 +94,4 @@ public class DashboardController { return new BrokerSummary(brokerService.getSimpleSubscriptionsOfUser(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail()), brokerService.getTopicsForDatasource(datasourceName)); } - } diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/GenericControllerAdvice.java b/src/main/java/eu/dnetlib/repo/manager/controllers/GenericControllerAdvice.java index d71797c..6f4c0fa 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/GenericControllerAdvice.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/GenericControllerAdvice.java @@ -6,9 +6,9 @@ import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.exception.EndPointException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.exception.ServerError; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; import org.json.JSONException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; @@ -26,7 +26,7 @@ import java.net.UnknownHostException; @Order(Ordered.HIGHEST_PRECEDENCE) public class GenericControllerAdvice { - private Logger logger = LogManager.getLogger(GenericControllerAdvice.class); + private Logger logger = LoggerFactory.getLogger(GenericControllerAdvice.class); @ResponseStatus(HttpStatus.NOT_FOUND) diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/InterfaceComplianceRequestController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/InterfaceComplianceRequestController.java new file mode 100644 index 0000000..676b8b5 --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/InterfaceComplianceRequestController.java @@ -0,0 +1,47 @@ +package eu.dnetlib.repo.manager.controllers; + +import eu.dnetlib.repo.manager.domain.InterfaceComplianceRequest; +import eu.dnetlib.repo.manager.domain.InterfaceComplianceRequestId; +import eu.dnetlib.repo.manager.domain.dto.InterfaceComplianceRequestDTO; +import eu.dnetlib.repo.manager.service.InterfaceComplianceService; +import org.springframework.web.bind.annotation.*; + +import java.util.Date; + +@RestController +@RequestMapping("compliance") +public class InterfaceComplianceRequestController { + + private final InterfaceComplianceService service; + + public InterfaceComplianceRequestController(InterfaceComplianceService service) { + this.service = service; + } + + @GetMapping("{repositoryId}/{interfaceId}") + public InterfaceComplianceRequest get(@PathVariable("repositoryId") String repositoryId, @PathVariable("interfaceId") String interfaceId) { + InterfaceComplianceRequestId id = new InterfaceComplianceRequestId(); + id.setRepositoryId(repositoryId); + id.setInterfaceId(interfaceId); + return service.getById(id).orElse(null); + } + + @GetMapping() + public Iterable get() { + return service.get(); + } + + @PostMapping() + public InterfaceComplianceRequest add(@RequestBody InterfaceComplianceRequestDTO requestDTO) { + requestDTO.setSubmissionDate(new Date()); + return service.create(InterfaceComplianceRequest.from(requestDTO)); + } + + @DeleteMapping("{repositoryId}/{interfaceId}") + public void delete(@PathVariable("repositoryId") String repositoryId, @PathVariable("interfaceId") String interfaceId) { + InterfaceComplianceRequestId id = new InterfaceComplianceRequestId(); + id.setRepositoryId(repositoryId); + id.setInterfaceId(interfaceId); + this.service.delete(id); + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/MonitorController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/MonitorController.java index f1c7b2d..69b04db 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/MonitorController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/MonitorController.java @@ -6,9 +6,10 @@ import eu.dnetlib.repo.manager.domain.JobsOfUser; import eu.dnetlib.repo.manager.service.MonitorServiceImpl; import io.swagger.annotations.Api; import io.swagger.annotations.ApiParam; -import org.apache.log4j.Logger; import org.json.JSONException; import org.mitre.openid.connect.model.OIDCAuthenticationToken; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.security.access.prepost.PreAuthorize; @@ -20,8 +21,7 @@ import org.springframework.web.bind.annotation.*; @Api(description = "Monitor API", tags = {"monitor"}) public class MonitorController { - private static final Logger LOGGER = Logger - .getLogger(MonitorController.class); + private static final Logger logger = LoggerFactory.getLogger(MonitorController.class); @Autowired private MonitorServiceImpl monitorService; diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/PiWikController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/PiWikController.java index d8a38d0..d7edf52 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/PiWikController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/PiWikController.java @@ -9,7 +9,8 @@ import eu.dnetlib.repo.manager.service.PiWikServiceImpl; import io.swagger.annotations.Api; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.http.MediaType; @@ -33,8 +34,7 @@ import java.util.List; @Api(description = "Piwik API", tags = {"piwik"}) public class PiWikController { - private static final Logger LOGGER = Logger - .getLogger(PiWikController.class); + private static final Logger logger = LoggerFactory.getLogger(PiWikController.class); @Autowired private PiWikServiceImpl piWikService; @@ -76,6 +76,7 @@ public class PiWikController { results.setResults(returning); return results; } + @ApiImplicitParams({ @ApiImplicitParam(name = "from", dataType = "number", paramType = "query"), @ApiImplicitParam(name = "quantity", dataType = "number", paramType = "query"), @@ -103,25 +104,22 @@ 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"); for(PiwikInfo piwikInfo : returning){ - sb.append( - (piwikInfo.getRepositoryId() == null ? "," : piwikInfo.getRepositoryId()+ ",")+ - (piwikInfo.getRepositoryName() == null ? "," : piwikInfo.getRepositoryName()+ ",")+ - (piwikInfo.getCountry() == null ? "," : piwikInfo.getCountry()+ ",")+ - (piwikInfo.getSiteId() == null ? "," : piwikInfo.getSiteId()+ ",") + - (piwikInfo.getAuthenticationToken() == null ? "," : piwikInfo.getAuthenticationToken()+ ",")+ - (piwikInfo.getCreationDate() == null ? "," : piwikInfo.getCreationDate().toString()+ ",") + - (piwikInfo.getRequestorName() == null ? "," : piwikInfo.getRequestorName()+ ",") + - (piwikInfo.getRequestorEmail() == null ? "," : piwikInfo.getRequestorEmail()+ ",")+ - piwikInfo.isValidated() + "," + - (piwikInfo.getValidationDate() == null ? "," : piwikInfo.getValidationDate().toString()+ ",") + - (piwikInfo.getComment() == null ? "\n" : piwikInfo.getComment()+ "\n") - - ); + sb.append(piwikInfo.getRepositoryId() == null ? "," : piwikInfo.getRepositoryId() + ",") + .append(piwikInfo.getRepositoryName() == null ? "," : piwikInfo.getRepositoryName() + ",") + .append(piwikInfo.getCountry() == null ? "," : piwikInfo.getCountry() + ",") + .append(piwikInfo.getSiteId() == null ? "," : piwikInfo.getSiteId() + ",") + .append(piwikInfo.getAuthenticationToken() == null ? "," : piwikInfo.getAuthenticationToken() + ",") + .append(piwikInfo.getCreationDate() == null ? "," : piwikInfo.getCreationDate().toString() + ",") + .append(piwikInfo.getRequestorName() == null ? "," : piwikInfo.getRequestorName() + ",") + .append(piwikInfo.getRequestorEmail() == null ? "," : piwikInfo.getRequestorEmail() + ",") + .append(piwikInfo.isValidated()).append(",") + .append(piwikInfo.getValidationDate() == null ? "," : piwikInfo.getValidationDate().toString() + ",") + .append(piwikInfo.getComment() == null ? "\n" : piwikInfo.getComment() + "\n"); } writer.write(sb.toString()); } catch (FileNotFoundException e) { - LOGGER.error(e.getMessage()); + logger.error(e.getMessage()); } @@ -141,7 +139,6 @@ public class PiWikController { strDate); response.setHeader(headerKey, headerValue); - return new FileSystemResource(p.toFile()); } diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/PrometheusController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/PrometheusController.java index 7f8a7e6..28102ea 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/PrometheusController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/PrometheusController.java @@ -11,7 +11,8 @@ import io.micrometer.core.instrument.binder.system.UptimeMetrics; import io.micrometer.prometheus.PrometheusConfig; import io.micrometer.prometheus.PrometheusMeterRegistry; import io.prometheus.client.exporter.common.TextFormat; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; @@ -24,7 +25,7 @@ import java.io.File; @RestController @RequestMapping("/actuator/prometheus") public class PrometheusController { // TODO: remove this with migration to Spring Boot 2 - private static final Logger LOGGER = Logger.getLogger(PrometheusController.class); + private static final Logger logger = LoggerFactory.getLogger(PrometheusController.class); private final PiWikService piWikService; private final RepositoryService repositoryService; @@ -52,7 +53,7 @@ public class PrometheusController { // TODO: remove this with migration to Sprin try (JvmGcMetrics jvmGcMetrics = new JvmGcMetrics() ) { jvmGcMetrics.bindTo(registry); } catch (Exception e) { - LOGGER.error("", e); + logger.error("", e); } new JvmMemoryMetrics().bindTo(registry); new DiskSpaceMetrics(new File("/")).bindTo(registry); diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/RepositoryController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/RepositoryController.java index 473109a..807d0af 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/RepositoryController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/RepositoryController.java @@ -1,19 +1,20 @@ package eu.dnetlib.repo.manager.controllers; -import eu.dnetlib.repo.manager.domain.Repository; -import eu.dnetlib.repo.manager.domain.RepositoryInterface; +import eu.dnetlib.enabling.datasources.common.AggregationInfo; import eu.dnetlib.repo.manager.domain.*; import eu.dnetlib.repo.manager.domain.dto.RepositoryTerms; import eu.dnetlib.repo.manager.domain.dto.User; import eu.dnetlib.repo.manager.exception.RepositoryServiceException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; +import eu.dnetlib.repo.manager.service.AggregationService; import eu.dnetlib.repo.manager.service.RepositoryService; import eu.dnetlib.repo.manager.service.security.AuthorizationService; import eu.dnetlib.repo.manager.utils.JsonUtils; import io.swagger.annotations.Api; -import org.apache.log4j.Logger; import org.json.JSONException; import org.mitre.openid.connect.model.OIDCAuthenticationToken; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -35,15 +36,19 @@ import java.util.Map; @Api(description = "Repository API", tags = {"repositories"}) public class RepositoryController { - private static final Logger logger = Logger.getLogger(RepositoryController.class); + private static final Logger logger = LoggerFactory.getLogger(RepositoryController.class); private final RepositoryService repositoryService; + + private final AggregationService aggregationService; + private final AuthorizationService authorizationService; @Autowired RepositoryController(RepositoryService repositoryService, - AuthorizationService authorizationService) { + AggregationService aggregationService, AuthorizationService authorizationService) { this.repositoryService = repositoryService; + this.aggregationService = aggregationService; this.authorizationService = authorizationService; } @@ -121,15 +126,15 @@ public class RepositoryController { @RequestMapping(value = "/getRepositoryAggregations/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody - public List getRepositoryAggregations(@PathVariable("id") String id) throws JSONException { - return repositoryService.getRepositoryAggregations(id, 0, 20); + public List getRepositoryAggregations(@PathVariable("id") String id) throws JSONException { + return aggregationService.getRepositoryAggregations(id, 0, 20); } @RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody - public Map> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException { - return repositoryService.getRepositoryAggregationsByYear(id); + public Map> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException { + return aggregationService.getRepositoryAggregationsByYear(id); } @RequestMapping(value = "/getRepositoriesByName/{name:.+}/{page}/{size}/", method = RequestMethod.GET, @@ -205,9 +210,10 @@ public class RepositoryController { @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id)") public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype, @RequestParam("repoId") String id, + @RequestParam(required = false, name = "desiredCompatibilityLevel") String desiredCompatibilityLevel, @RequestParam(value = "comment", required = false) String comment, @RequestBody RepositoryInterface repositoryInterface) throws Exception { - return repositoryService.addRepositoryInterface(datatype, id, comment, repositoryInterface); + return repositoryService.addRepositoryInterface(datatype, id, comment, repositoryInterface, desiredCompatibilityLevel); } @RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST, @@ -215,9 +221,10 @@ public class RepositoryController { @ResponseBody @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or @authorizationService.isMemberOf(#id)") public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String id, + @RequestParam(required = false, name = "desiredCompatibilityLevel") String desiredCompatibilityLevel, @RequestParam(value = "comment", required = false) String comment, @RequestBody RepositoryInterface repositoryInterface) throws Exception { - return repositoryService.updateRepositoryInterface(id, comment, repositoryInterface); + return repositoryService.updateRepositoryInterface(id, comment, repositoryInterface, desiredCompatibilityLevel); } @RequestMapping(value = "/getUrlsOfUserRepos/{page}/{size}/", method = RequestMethod.GET, diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/StatsController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/StatsController.java index b48ebab..1cfd6a2 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/StatsController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/StatsController.java @@ -19,8 +19,7 @@ public class StatsController { @Autowired private StatsServiceImpl statsService; - @RequestMapping(value = "/getStatistics" , method = RequestMethod.GET, - produces = MediaType.APPLICATION_JSON_VALUE) + @RequestMapping(value = "/getStatistics" , method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map getStatistics(){ return statsService.getStatistics(); diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/UserRoleController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/UserRoleController.java index eb8bfcf..255bac7 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/UserRoleController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/UserRoleController.java @@ -6,7 +6,6 @@ import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater; import eu.dnetlib.repo.manager.service.security.AuthorizationService; import eu.dnetlib.repo.manager.service.security.RoleMappingService; import eu.dnetlib.repo.manager.utils.JsonUtils; -import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -97,7 +96,7 @@ public class UserRoleController { @RequestMapping(method = RequestMethod.GET, path = "/users/{email}/roles") @PreAuthorize("hasAnyAuthority('SUPER_ADMINISTRATOR', 'CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or hasAuthority('REGISTERED_USER') and authentication.userInfo.email==#email") public ResponseEntity> getRolesByEmail(@PathVariable("email") String email) { - return ResponseEntity.ok(authorizationService.getUserRoles(email)); + return ResponseEntity.ok(authorizationService.getUserRolesByEmail(email)); } @@ -107,4 +106,4 @@ public class UserRoleController { return ResponseEntity.ok(authorizationService.getUserRoles()); } -} \ No newline at end of file +} diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/ValidatorController.java b/src/main/java/eu/dnetlib/repo/manager/controllers/ValidatorController.java index 822edc9..f94d860 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/ValidatorController.java +++ b/src/main/java/eu/dnetlib/repo/manager/controllers/ValidatorController.java @@ -5,8 +5,8 @@ import eu.dnetlib.domain.functionality.validator.JobForValidation; import eu.dnetlib.domain.functionality.validator.RuleSet; import eu.dnetlib.domain.functionality.validator.StoredJob; import eu.dnetlib.repo.manager.domain.InterfaceInformation; -import eu.dnetlib.repo.manager.exception.ValidationServiceException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; +import eu.dnetlib.repo.manager.exception.ValidationServiceException; import eu.dnetlib.repo.manager.service.EmailUtils; import eu.dnetlib.repo.manager.service.ValidatorServiceImpl; import io.swagger.annotations.Api; @@ -112,8 +112,8 @@ public class ValidatorController { @RequestMapping(value = "/complete" , method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public void validationCompleted( - @RequestParam(value = "interfaceId") String interfaceId, @RequestParam(value = "repoId") String repoId, + @RequestParam(value = "interfaceId") String interfaceId, @RequestParam(value = "jobId") String jobId, @RequestParam(value = "issuerEmail") String issuerEmail, @RequestParam(value = "isUpdate") boolean isUpdate, @@ -121,7 +121,7 @@ public class ValidatorController { @RequestParam(value = "scoreUsage") int scoreUsage, @RequestParam(value = "scoreContent") int scoreContent) throws Exception { - emailUtils.sendUponJobCompletion(repoId,interfaceId,scoreUsage,scoreContent,isSuccess,isUpdate,issuerEmail, jobId); + validatorService.onComplete(repoId, interfaceId, jobId, issuerEmail, isUpdate, isSuccess, scoreUsage, scoreContent); } diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/AggregationDetails.java b/src/main/java/eu/dnetlib/repo/manager/domain/AggregationDetails.java deleted file mode 100644 index b5b7df2..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/domain/AggregationDetails.java +++ /dev/null @@ -1,63 +0,0 @@ -package eu.dnetlib.repo.manager.domain; - -import java.util.Calendar; -import java.util.Date; - -public class AggregationDetails{ - - - private String aggregationStage; - private Date date; - private int numberOfRecords; - private String collectionMode; - private Boolean indexedVersion; - - public AggregationDetails() { - } - - public String getAggregationStage() { - return aggregationStage; - } - - public String getCollectionMode() { - return collectionMode; - } - - public void setCollectionMode(String collectionMode) { - this.collectionMode = collectionMode; - } - - public void setAggregationStage(String aggregationStage) { - this.aggregationStage = aggregationStage; - } - - public Date getDate() { - return date; - } - - public void setDate(Date date) { - this.date = date; - } - - public int getNumberOfRecords() { - return numberOfRecords; - } - - public void setNumberOfRecords(int numberOfRecords) { - this.numberOfRecords = numberOfRecords; - } - - public String getYear() { - Calendar calendar = Calendar.getInstance(); - calendar.setTime(getDate()); - return String.valueOf(calendar.get(Calendar.YEAR)); - } - - public Boolean getIndexedVersion() { - return indexedVersion; - } - - public void setIndexedVersion(Boolean indexedVersion) { - this.indexedVersion = indexedVersion; - } -} diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/AggregationHistoryResponse.java b/src/main/java/eu/dnetlib/repo/manager/domain/AggregationHistoryResponse.java new file mode 100644 index 0000000..65e932d --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/domain/AggregationHistoryResponse.java @@ -0,0 +1,29 @@ +package eu.dnetlib.repo.manager.domain; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; + +import java.util.List; + +@JsonAutoDetect +public class AggregationHistoryResponse extends Response { + + private List aggregationInfo; + + public AggregationHistoryResponse() { + // no-arg constructor + } + + public AggregationHistoryResponse(final List aggregationInfo) { + super(); + this.aggregationInfo = aggregationInfo; + } + + public List getAggregationInfo() { + return aggregationInfo; + } + + public void setAggregationInfo(final List aggregationInfo) { + this.aggregationInfo = aggregationInfo; + } +} + diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/AggregationInfo.java b/src/main/java/eu/dnetlib/repo/manager/domain/AggregationInfo.java new file mode 100644 index 0000000..2fa6a0e --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/domain/AggregationInfo.java @@ -0,0 +1,8 @@ +package eu.dnetlib.repo.manager.domain; + +// TODO: this class must be replaced by common dnet class (CollectionInfo / TransformationInfo / AggregationInfo) +public class AggregationInfo extends eu.dnetlib.enabling.datasources.common.AggregationInfo { + + public AggregationInfo() { + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/CollectionMonitorSummary.java b/src/main/java/eu/dnetlib/repo/manager/domain/CollectionMonitorSummary.java index a8a32b7..db279ea 100644 --- a/src/main/java/eu/dnetlib/repo/manager/domain/CollectionMonitorSummary.java +++ b/src/main/java/eu/dnetlib/repo/manager/domain/CollectionMonitorSummary.java @@ -1,32 +1,34 @@ package eu.dnetlib.repo.manager.domain; +import eu.dnetlib.enabling.datasources.common.AggregationInfo; + import java.util.List; public class CollectionMonitorSummary { - private List aggregationDetails; + private List aggregationInfo; - private AggregationDetails lastIndexedVersion; + private AggregationInfo lastIndexedVersion; public CollectionMonitorSummary(){} - public CollectionMonitorSummary(List aggregationDetails, AggregationDetails lastIndexedVersion) { - this.aggregationDetails = aggregationDetails; + public CollectionMonitorSummary(List aggregationInfo, AggregationInfo lastIndexedVersion) { + this.aggregationInfo = aggregationInfo; this.lastIndexedVersion = lastIndexedVersion; } - public List getAggregationDetails() { - return aggregationDetails; + public List getAggregationInfo() { + return aggregationInfo; } - public void setAggregationDetails(List aggregationDetails) { - this.aggregationDetails = aggregationDetails; + public void setAggregationInfo(List aggregationInfo) { + this.aggregationInfo = aggregationInfo; } - public AggregationDetails getLastIndexedVersion() { + public AggregationInfo getLastIndexedVersion() { return lastIndexedVersion; } - public void setLastIndexedVersion(AggregationDetails lastIndexedVersion) { + public void setLastIndexedVersion(AggregationInfo lastIndexedVersion) { this.lastIndexedVersion = lastIndexedVersion; } } diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/DatasourceDetails.java b/src/main/java/eu/dnetlib/repo/manager/domain/DatasourceDetails.java index 1b0af30..aeaf232 100644 --- a/src/main/java/eu/dnetlib/repo/manager/domain/DatasourceDetails.java +++ b/src/main/java/eu/dnetlib/repo/manager/domain/DatasourceDetails.java @@ -1,8 +1,8 @@ package eu.dnetlib.repo.manager.domain; +import javax.persistence.Transient; import java.util.Date; import java.util.Set; -import javax.persistence.Transient; public class DatasourceDetails { diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/DatasourcesCollection.java b/src/main/java/eu/dnetlib/repo/manager/domain/DatasourcesCollection.java index afe565d..2fffc48 100644 --- a/src/main/java/eu/dnetlib/repo/manager/domain/DatasourcesCollection.java +++ b/src/main/java/eu/dnetlib/repo/manager/domain/DatasourcesCollection.java @@ -1,7 +1,5 @@ package eu.dnetlib.repo.manager.domain; -import eu.dnetlib.repo.manager.domain.Repository; - import java.util.ArrayList; import java.util.List; diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/InterfaceComplianceRequest.java b/src/main/java/eu/dnetlib/repo/manager/domain/InterfaceComplianceRequest.java new file mode 100644 index 0000000..ecfffb3 --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/domain/InterfaceComplianceRequest.java @@ -0,0 +1,95 @@ +package eu.dnetlib.repo.manager.domain; + +import eu.dnetlib.repo.manager.domain.dto.InterfaceComplianceRequestDTO; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Transient; +import java.util.Date; + +@Entity +@IdClass(InterfaceComplianceRequestId.class) +public class InterfaceComplianceRequest { + + @Id + String repositoryId; + @Id + String interfaceId; + String desiredCompatibilityLevel; + Date submissionDate; + + public InterfaceComplianceRequest() { + this.submissionDate = new Date(); + } + + public InterfaceComplianceRequest(String repositoryId, String interfaceId, String desiredCompatibilityLevel) { + this.submissionDate = new Date(); + this.repositoryId = repositoryId; + this.interfaceId = interfaceId; + this.desiredCompatibilityLevel = desiredCompatibilityLevel; + } + + public static InterfaceComplianceRequest from(InterfaceComplianceRequestDTO dto) { + InterfaceComplianceRequest request = new InterfaceComplianceRequest(); + request.setRepositoryId(dto.getRepositoryId()); + request.setInterfaceId(dto.getInterfaceId()); + if (dto.getSubmissionDate() != null) { + request.setSubmissionDate(dto.getSubmissionDate()); + } else { + request.setSubmissionDate(new Date()); + } + request.setDesiredCompatibilityLevel(dto.getDesiredCompatibilityLevel()); + return request; + } + + @Transient + public InterfaceComplianceRequestId getId() { + InterfaceComplianceRequestId id = new InterfaceComplianceRequestId(); + id.repositoryId = this.repositoryId; + id.interfaceId = this.interfaceId; + return id; + } + + public String getRepositoryId() { + return repositoryId; + } + + public void setRepositoryId(String repositoryId) { + this.repositoryId = repositoryId; + } + + public String getInterfaceId() { + return interfaceId; + } + + public void setInterfaceId(String interfaceId) { + this.interfaceId = interfaceId; + } + + public String getDesiredCompatibilityLevel() { + return desiredCompatibilityLevel; + } + + public void setDesiredCompatibilityLevel(String desiredCompatibilityLevel) { + this.desiredCompatibilityLevel = desiredCompatibilityLevel; + } + + public Date getSubmissionDate() { + return submissionDate; + } + + public void setSubmissionDate(Date submissionDate) { + this.submissionDate = submissionDate; + } + + @Override + public String toString() { + return "InterfaceComplianceRequest{" + + "repositoryId='" + repositoryId + '\'' + + ", interfaceId='" + interfaceId + '\'' + + ", desiredCompatibilityLevel='" + desiredCompatibilityLevel + '\'' + + ", submissionDate=" + submissionDate + + '}'; + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/InterfaceComplianceRequestId.java b/src/main/java/eu/dnetlib/repo/manager/domain/InterfaceComplianceRequestId.java new file mode 100644 index 0000000..6eff74e --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/domain/InterfaceComplianceRequestId.java @@ -0,0 +1,50 @@ +package eu.dnetlib.repo.manager.domain; + +import java.io.Serializable; + +public class InterfaceComplianceRequestId implements Serializable { + String repositoryId; + String interfaceId; + + public String getRepositoryId() { + return repositoryId; + } + + public void setRepositoryId(String repositoryId) { + this.repositoryId = repositoryId; + } + + public String getInterfaceId() { + return interfaceId; + } + + public void setInterfaceId(String interfaceId) { + this.interfaceId = interfaceId; + } + + public static InterfaceComplianceRequestId of(String repositoryId, String interfaceId) { + InterfaceComplianceRequestId id = new InterfaceComplianceRequestId(); + id.setRepositoryId(repositoryId); + id.setInterfaceId(interfaceId); + return id; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof InterfaceComplianceRequestId)) return false; + + InterfaceComplianceRequestId that = (InterfaceComplianceRequestId) o; + + if (getRepositoryId() != null ? !getRepositoryId().equals(that.getRepositoryId()) : that.getRepositoryId() != null) + return false; + return getInterfaceId() != null ? getInterfaceId().equals(that.getInterfaceId()) : that.getInterfaceId() == null; + } + + @Override + public int hashCode() { + int result = getRepositoryId() != null ? getRepositoryId().hashCode() : 0; + result = 31 * result + (getInterfaceId() != null ? getInterfaceId().hashCode() : 0); + return result; + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/Repository.java b/src/main/java/eu/dnetlib/repo/manager/domain/Repository.java index c6fcbea..849f233 100644 --- a/src/main/java/eu/dnetlib/repo/manager/domain/Repository.java +++ b/src/main/java/eu/dnetlib/repo/manager/domain/Repository.java @@ -3,7 +3,8 @@ package eu.dnetlib.repo.manager.domain; import eu.dnetlib.domain.data.DataCollectionType; import eu.dnetlib.domain.data.PiwikInfo; -import java.util.*; +import java.util.ArrayList; +import java.util.List; /** * The domain object for the Repository resource data structure diff --git a/src/main/java/eu/dnetlib/repo/manager/domain/dto/InterfaceComplianceRequestDTO.java b/src/main/java/eu/dnetlib/repo/manager/domain/dto/InterfaceComplianceRequestDTO.java new file mode 100644 index 0000000..cfeedfe --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/domain/dto/InterfaceComplianceRequestDTO.java @@ -0,0 +1,47 @@ +package eu.dnetlib.repo.manager.domain.dto; + +import java.util.Date; + +public class InterfaceComplianceRequestDTO { + + String repositoryId; + String interfaceId; + String desiredCompatibilityLevel; + Date submissionDate; + + public InterfaceComplianceRequestDTO() { + // no-arg constructor + } + + public String getRepositoryId() { + return repositoryId; + } + + public void setRepositoryId(String repositoryId) { + this.repositoryId = repositoryId; + } + + public String getInterfaceId() { + return interfaceId; + } + + public void setInterfaceId(String interfaceId) { + this.interfaceId = interfaceId; + } + + public String getDesiredCompatibilityLevel() { + return desiredCompatibilityLevel; + } + + public void setDesiredCompatibilityLevel(String desiredCompatibilityLevel) { + this.desiredCompatibilityLevel = desiredCompatibilityLevel; + } + + public Date getSubmissionDate() { + return submissionDate; + } + + public void setSubmissionDate(Date submissionDate) { + this.submissionDate = submissionDate; + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/exception/ResourceConflictException.java b/src/main/java/eu/dnetlib/repo/manager/exception/ResourceConflictException.java new file mode 100644 index 0000000..40c01c8 --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/exception/ResourceConflictException.java @@ -0,0 +1,13 @@ +package eu.dnetlib.repo.manager.exception; + + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.CONFLICT) +public class ResourceConflictException extends RuntimeException { + + public ResourceConflictException(String message) { + super(message); + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/repository/InterfaceComplianceRequestsRepository.java b/src/main/java/eu/dnetlib/repo/manager/repository/InterfaceComplianceRequestsRepository.java new file mode 100644 index 0000000..bbe209b --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/repository/InterfaceComplianceRequestsRepository.java @@ -0,0 +1,15 @@ +package eu.dnetlib.repo.manager.repository; + +import eu.dnetlib.repo.manager.domain.InterfaceComplianceRequest; +import eu.dnetlib.repo.manager.domain.InterfaceComplianceRequestId; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.Date; +import java.util.Set; + +@Repository +public interface InterfaceComplianceRequestsRepository extends CrudRepository { + + Set findAllBySubmissionDateBefore(Date submittedBefore); +} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/AggregationService.java b/src/main/java/eu/dnetlib/repo/manager/service/AggregationService.java new file mode 100644 index 0000000..8741201 --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/service/AggregationService.java @@ -0,0 +1,17 @@ +package eu.dnetlib.repo.manager.service; + +import eu.dnetlib.enabling.datasources.common.AggregationInfo; +import org.json.JSONException; + +import java.util.List; +import java.util.Map; + +public interface AggregationService { + + List getRepositoryAggregations(String id) throws JSONException; + + List getRepositoryAggregations(String id, int from, int size) throws JSONException; + + Map> getRepositoryAggregationsByYear(String id) throws JSONException; + +} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/AggregationServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/AggregationServiceImpl.java new file mode 100644 index 0000000..b6080d1 --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/service/AggregationServiceImpl.java @@ -0,0 +1,81 @@ +package eu.dnetlib.repo.manager.service; + +import eu.dnetlib.enabling.datasources.common.AggregationInfo; +import eu.dnetlib.repo.manager.domain.AggregationHistoryResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponents; +import org.springframework.web.util.UriComponentsBuilder; + +import java.util.Comparator; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static eu.dnetlib.repo.manager.utils.DateUtils.getYear; + +@Service("aggregationService") +public class AggregationServiceImpl implements AggregationService { + + private static final Logger logger = LoggerFactory.getLogger(AggregationServiceImpl.class); + + + @Value("${services.provide.clients.dsm}") + private String baseAddress; + + private final RestTemplate restTemplate; + + + public AggregationServiceImpl(RestTemplate restTemplate) { + this.restTemplate = restTemplate; + } + + + @Override + public List getRepositoryAggregations(String id) { + + logger.debug("Retrieving aggregations for repository with id : " + id); + UriComponents uriComponents = getAggregationHistory(id); + + AggregationHistoryResponse rs = restTemplate.getForObject(uriComponents.toUri(), AggregationHistoryResponse.class); + + return rs != null ? (List) rs.getAggregationInfo() : null; + } + + @Override + public List getRepositoryAggregations(String id, int from, int size) { + + List res = getRepositoryAggregations(id); + + return res.subList(from, Math.min(from + size, res.size())); + } + + @Override + public Map> getRepositoryAggregationsByYear(String id) { + logger.debug("Retrieving aggregations (by year) for repository with id : " + id); + + List aggregationHistory = getRepositoryAggregations(id); + + return aggregationHistory.isEmpty() ? new HashMap<>() : createYearMap(aggregationHistory); + } + + private Map> createYearMap(List aggregationHistory) { + + return aggregationHistory.stream() + .sorted(Comparator.comparing(AggregationInfo::getDate).reversed()) + .collect(Collectors.groupingBy(item -> getYear(item.getDate()))); + } + + + private UriComponents getAggregationHistory(String repoId) { + return UriComponentsBuilder + .fromHttpUrl(baseAddress + "/ds/aggregationhistory/") + .path(repoId) + .build().expand(repoId).encode(); + } + +} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/BrokerService.java b/src/main/java/eu/dnetlib/repo/manager/service/BrokerService.java index 58eef11..3a87bc8 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/BrokerService.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/BrokerService.java @@ -1,8 +1,8 @@ package eu.dnetlib.repo.manager.service; -import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.domain.Term; import eu.dnetlib.repo.manager.domain.broker.*; +import eu.dnetlib.repo.manager.exception.BrokerException; import org.json.JSONException; import org.springframework.http.ResponseEntity; diff --git a/src/main/java/eu/dnetlib/repo/manager/service/BrokerServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/BrokerServiceImpl.java index e25273a..48fb2f8 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/BrokerServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/BrokerServiceImpl.java @@ -2,13 +2,15 @@ package eu.dnetlib.repo.manager.service; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.domain.RepositorySnippet; import eu.dnetlib.repo.manager.domain.Term; import eu.dnetlib.repo.manager.domain.Tuple; import eu.dnetlib.repo.manager.domain.broker.*; +import eu.dnetlib.repo.manager.exception.BrokerException; import org.apache.commons.lang.NotImplementedException; import org.json.JSONException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; @@ -26,7 +28,10 @@ import javax.annotation.PostConstruct; import java.io.IOException; import java.io.InputStream; 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") public class BrokerServiceImpl implements BrokerService { @@ -40,15 +45,14 @@ public class BrokerServiceImpl implements BrokerService { @Value("${services.provide.topic_types.url}") private String topicsURL; - private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger - .getLogger(BrokerServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(BrokerServiceImpl.class); @Autowired RestTemplate restTemplate; private HttpHeaders httpHeaders; - private HashMap topics = new HashMap(); + private HashMap topics = new HashMap<>(); @PostConstruct private void initDnetTopicsMap() { @@ -56,14 +60,14 @@ public class BrokerServiceImpl implements BrokerService { httpHeaders = new HttpHeaders(); httpHeaders.set("Content-Type", "application/json"); - LOGGER.debug("Init dnet topics!"); + logger.debug("Init dnet topics!"); try (InputStream is = new URL(topicsURL).openStream()) { ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(is); for (JsonNode term : root.path("terms")) topics.put(term.path("code").textValue(), parseTerm(term)); } catch (IOException e) { - LOGGER.error("Exception on initDnetTopicsMap", e); + logger.error("Exception on initDnetTopicsMap", e); } } @@ -89,7 +93,7 @@ public class BrokerServiceImpl implements BrokerService { // ret.setDatasourcesOfOthers(getDatasourcesOfUserType(getRepositoriesOfUser(user))); // } } catch (Exception e) { - LOGGER.error("Exception on getDatasourcesOfUser", e); + logger.error("Exception on getDatasourcesOfUser", e); } long end = System.currentTimeMillis(); System.out.println("Getting datasources of user in " + (end - start) + "ms"); @@ -121,7 +125,7 @@ public class BrokerServiceImpl implements BrokerService { @Override public EventsPage advancedShowEvents(String page, String size, - AdvQueryObject advQueryObject) throws BrokerException, JSONException, IOException { + AdvQueryObject advQueryObject) throws BrokerException { final String service = "/events/{page}/{pageSize}"; @@ -156,7 +160,7 @@ public class BrokerServiceImpl implements BrokerService { for (RepositorySnippet repo : repositories) { BrowseEntry temp = new BrowseEntry(); temp.setValue(repo.getOfficialname()); - temp.setSize(new Long(0)); + temp.setSize(0L); for (BrowseEntry e : getTopicsForDatasource(repo.getOfficialname())) { temp.setSize(temp.getSize() + e.getSize()); } @@ -165,12 +169,7 @@ public class BrokerServiceImpl implements BrokerService { } // sort the collection by the second field of the tuple which is size - Collections.sort(entries, new Comparator>() { - @Override - public int compare(Tuple e1, Tuple e2) { - return (int) (e2.getFirst().getSize().longValue() - e1.getFirst().getSize().longValue()); - } - }); + entries.sort((e1, e2) -> (int) (e2.getFirst().getSize() - e1.getFirst().getSize())); long stop = System.currentTimeMillis(); System.out.println("getDatasourcesOfUserType returned in " + (stop - start) + "ms "); @@ -213,7 +212,7 @@ public class BrokerServiceImpl implements BrokerService { UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(openairePath + service) .queryParam("email", userEmail); - LOGGER.debug(builder.build().encode().toUri()); + logger.debug("{}", builder.build().encode().toUri()); ResponseEntity>> resp; try { resp = restTemplate.exchange( diff --git a/src/main/java/eu/dnetlib/repo/manager/service/DashboardServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/DashboardServiceImpl.java index 6e75312..a344fcc 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/DashboardServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/DashboardServiceImpl.java @@ -1,10 +1,15 @@ package eu.dnetlib.repo.manager.service; -import eu.dnetlib.repo.manager.domain.*; +import eu.dnetlib.enabling.datasources.common.AggregationInfo; +import eu.dnetlib.repo.manager.domain.MetricsInfo; +import eu.dnetlib.repo.manager.domain.RepositorySnippet; +import eu.dnetlib.repo.manager.domain.RepositorySummaryInfo; import eu.dnetlib.repo.manager.domain.broker.BrowseEntry; import eu.dnetlib.repo.manager.exception.BrokerException; import eu.dnetlib.repo.manager.exception.RepositoryServiceException; -import org.apache.log4j.Logger; +import eu.dnetlib.repo.manager.utils.DateUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -14,11 +19,14 @@ import java.util.List; @Service("dashboardService") public class DashboardServiceImpl implements DashboardService { - private static final Logger logger = Logger.getLogger(DashboardServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(DashboardServiceImpl.class); @Autowired private RepositoryService repositoryService; + @Autowired + private AggregationService aggregationService; + @Autowired private BrokerService brokerService; @@ -30,7 +38,6 @@ public class DashboardServiceImpl implements DashboardService { List repositorySummaryInfoList = new ArrayList<>(); try { - List repositoriesOfUser = repositoryService.getRepositoriesSnippetsOfUser(userEmail, page, size); for (RepositorySnippet repository : repositoriesOfUser) { RepositorySummaryInfo repositorySummaryInfo = new RepositorySummaryInfo(); @@ -40,44 +47,37 @@ public class DashboardServiceImpl implements DashboardService { //TODO getRepositoryAggregations returns only the 20 more recent items. Is it positive that we will find an indexed version there? long start = System.currentTimeMillis(); - List aggregationDetailsList = repositoryService.getRepositoryAggregations(repository.getId(), 0, 20); - for (AggregationDetails aggregationDetails : aggregationDetailsList) { - if (aggregationDetails.getIndexedVersion()) { - repositorySummaryInfo.setRecordsCollected(aggregationDetails.getNumberOfRecords()); - repositorySummaryInfo.setLastIndexedVersion(aggregationDetails.getDate()); + List aggregationInfoList = aggregationService.getRepositoryAggregations(repository.getId(), 0, 20); + for (AggregationInfo aggregationInfo : aggregationInfoList) { + if (aggregationInfo.isIndexedVersion()) { + repositorySummaryInfo.setRecordsCollected(aggregationInfo.getNumberOfRecords()); + repositorySummaryInfo.setLastIndexedVersion(DateUtils.toDate(aggregationInfo.getDate())); break; } } long end = System.currentTimeMillis(); - System.out.println("Got repo aggregations in " + (end - start) + "ms"); try { - MetricsInfo metricsInfo = repositoryService.getMetricsInfoForRepository(repository.getId()); repositorySummaryInfo.setTotalDownloads(metricsInfo.getMetricsNumbers().getTotalDownloads()); repositorySummaryInfo.setTotalViews(metricsInfo.getMetricsNumbers().getTotalViews()); - } catch (RepositoryServiceException e) { logger.error("Exception getting metrics info for repository: " + repository.getId(), e); } try { - List events = brokerService.getTopicsForDatasource(repository.getOfficialname()); Long totalEvents = 0L; for (BrowseEntry browseEntry : events) totalEvents += browseEntry.getSize(); repositorySummaryInfo.setEnrichmentEvents(totalEvents); - - } catch (BrokerException e) { logger.error("Exception getting broker events for repository: " + repository.getId(), e); } repositorySummaryInfoList.add(repositorySummaryInfo); } - } catch (Exception e) { logger.error("Something baad happened!", e); } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/EmailUtils.java b/src/main/java/eu/dnetlib/repo/manager/service/EmailUtils.java index 0565387..fed2c64 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/EmailUtils.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/EmailUtils.java @@ -1,93 +1,103 @@ package eu.dnetlib.repo.manager.service; import eu.dnetlib.domain.data.PiwikInfo; +import eu.dnetlib.domain.functionality.validator.JobForValidation; +import eu.dnetlib.repo.manager.domain.InterfaceComplianceRequest; import eu.dnetlib.repo.manager.domain.Repository; import eu.dnetlib.repo.manager.domain.RepositoryInterface; -import eu.dnetlib.domain.functionality.validator.JobForValidation; import org.springframework.scheduling.annotation.Async; import org.springframework.security.core.Authentication; +import java.util.List; + public interface EmailUtils { @Async - void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception; + void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo); @Async - void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception; + void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo); @Async - void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception; + void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo); @Async - void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception; + void sendUserMetricsEnabled(PiwikInfo piwikInfo); /****USER REGISTRATION REQUEST EMAILS****/ @Async - void sendAdminRegistrationEmail(Repository repository, Authentication authentication) throws Exception; + void sendAdminRegistrationEmail(Repository repository, Authentication authentication); @Async - void sendUserRegistrationEmail(Repository repository, Authentication authentication) throws Exception; + void sendUserRegistrationEmail(Repository repository, Authentication authentication); @Async - void sendAdminRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception; + void sendAdminRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication); @Async - void sendUserRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception; + void sendUserRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication); /****SUCCESSFUL REGISTRATION RESULTS EMAILS****/ @Async - void sendUserRegistrationResultsSuccessEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendUserRegistrationResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); @Async - void sendAdminRegistrationResultsSuccessEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendAdminRegistrationResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); /****FAILURE REGISTRATION RESULTS EMAILS****/ @Async - void sendUserRegistrationResultsFailureEmail(String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendUserRegistrationResultsFailureEmail(String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); @Async - void sendAdminRegistrationResultsFailureEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendAdminRegistrationResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); /****SUCCESSFUL UPDATE RESULTS EMAILS****/ @Async - void sendUserUpdateResultsSuccessEmail(String issuer, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendUserUpdateResultsSuccessEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); @Async - void sendAdminUpdateResultsSuccessEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendAdminUpdateResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); /****FAILURE UPDATE RESULTS EMAILS****/ @Async - void sendUserUpdateResultsFailureEmail(String issuer, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendUserUpdateResultsFailureEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); @Async - void sendAdminUpdateResultsFailureEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendAdminUpdateResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); + + /****FAILURE UPDATE INTERFACE COMPLIANCE****/ + @Async + void sendUserUpdateInterfaceComplianceFailure(List emails, Repository repository, RepositoryInterface repositoryInterface, InterfaceComplianceRequest request); + + @Async + void sendAdminUpdateInterfaceComplianceFailure(Repository repository, RepositoryInterface repositoryInterface, InterfaceComplianceRequest request); /****VALIDATION OF CONTENT PROVIDER EMAILS****/ @Async - void sendUserValidationResults(String issuer,String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendUserValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); @Async - void sendAdminValidationResults(String issuer,String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendAdminValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); /****GENERAL FAILURE OF VALIDATOR****/ @Async - void sendAdminGeneralFailure(String issuer,String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception; + void sendAdminGeneralFailure(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication); @Async - void sendAdminUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception; + void sendAdminUpdateRepositoryInfoEmail(Repository repository, Authentication authentication); @Async - void sendUserUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception; + void sendUserUpdateRepositoryInfoEmail(Repository repository, Authentication authentication); @Async - void sendAdminUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception; + void sendAdminUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication); @Async - void sendUserUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception; + void sendUserUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication); @Async - void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) throws Exception; + void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation); @Async void sendUponJobCompletion(String repoId, diff --git a/src/main/java/eu/dnetlib/repo/manager/service/EmailUtilsImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/EmailUtilsImpl.java index 03dfc5d..6fc7ce8 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/EmailUtilsImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/EmailUtilsImpl.java @@ -1,13 +1,15 @@ package eu.dnetlib.repo.manager.service; import eu.dnetlib.domain.data.PiwikInfo; +import eu.dnetlib.domain.functionality.validator.JobForValidation; +import eu.dnetlib.repo.manager.domain.InterfaceComplianceRequest; import eu.dnetlib.repo.manager.domain.Repository; import eu.dnetlib.repo.manager.domain.RepositoryInterface; -import eu.dnetlib.domain.functionality.validator.JobForValidation; import eu.dnetlib.repo.manager.exception.ValidationServiceException; import eu.dnetlib.utils.MailLibrary; -import org.apache.log4j.Logger; import org.mitre.openid.connect.model.OIDCAuthenticationToken; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.Authentication; @@ -21,7 +23,7 @@ import java.util.stream.Collectors; @Component("emailUtils") public class EmailUtilsImpl implements EmailUtils { - private final static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class); + private final static Logger logger = LoggerFactory.getLogger(EmailUtilsImpl.class); private final MailLibrary mailLibrary; private final RepositoryService repositoryService; @@ -45,33 +47,32 @@ public class EmailUtilsImpl implements EmailUtils { } @Override - public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception { + public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) { try { String subject = "[OpenAIRE-Usage Statistics] New request to enable usage statistics"; String message = "we have received a request to enable the OpenAIRE usage statistics for the following repository \n" + - "\n" + - "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" + - "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" + - "Matomo ID - " + piwikInfo.getSiteId() + "\n" + - "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" + - "\n" + - "For more information about this request, go here: \n" + - this.baseUrl + "/admin/metrics"; + "\n" + + "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" + + "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" + + "Matomo ID - " + piwikInfo.getSiteId() + "\n" + + "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" + + "\n" + + "For more information about this request, go here: \n" + + this.baseUrl + "/admin/metrics"; message = createAdminMail(message); this.sendMail(this.usageStatsAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e); - throw e; + logger.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e); } } @Override - public void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception { + public void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) { try { String subject = "[OpenAIRE-Usage Statistics] Your request to enable usage statistics"; @@ -102,37 +103,35 @@ public class EmailUtilsImpl implements EmailUtils { this.sendMail(piwikInfo.getRequestorEmail(), subject, message); } catch (Exception e) { - LOGGER.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e); - throw e; + logger.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e); } } @Override - public void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception { + public void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) { try { String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled"; String message = "The installation and configuration of OpenAIRE's tracking code for the following repository " + - "has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" + - "\n" + - "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" + - "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" + - "Piwik ID - " + piwikInfo.getSiteId() + "\n" + - "Authentication token - " + piwikInfo.getAuthenticationToken(); + "has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" + + "\n" + + "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" + + "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" + + "Piwik ID - " + piwikInfo.getSiteId() + "\n" + + "Authentication token - " + piwikInfo.getAuthenticationToken(); message = createAdminMail(message); this.sendMail(this.usageStatsAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e); - throw e; + logger.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e); } } @Override - public void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception { + public void sendUserMetricsEnabled(PiwikInfo piwikInfo) { try { String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled"; @@ -154,70 +153,67 @@ public class EmailUtilsImpl implements EmailUtils { this.sendMail(piwikInfo.getRequestorEmail(), subject, message); } catch (Exception e) { - LOGGER.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e); - throw e; + logger.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e); } } @Override - public void sendAdminRegistrationEmail(Repository repository, Authentication authentication) throws Exception { + public void sendAdminRegistrationEmail(Repository repository, Authentication authentication) { try { String subject = "OpenAIRE content provider registration for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "We received a request to register the " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]" + - " to the OpenAIRE compliant list of content providers. " + - "\n\n" + - "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" + - "\n\n" + - "Please do not reply to this message\n" + - "This message has been generated automatically."; + " to the OpenAIRE compliant list of content providers. " + + "\n\n" + + "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" + + "\n\n" + + "Please do not reply to this message\n" + + "This message has been generated automatically."; message = createAdminMail(message); this.sendMail(this.provideAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to the administrator", e); - throw e; + logger.error("Error while sending registration notification email to the administrator", e); } } @Override - public void sendUserRegistrationEmail(Repository repository, Authentication authentication) throws Exception { + public void sendUserRegistrationEmail(Repository repository, Authentication authentication) { try { String subject = "OpenAIRE content provider registration for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "We received a request to register the " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]" + - " to the OpenAIRE compliant list of content providers. " + - "\n\n" + - "Please do not reply to this message\n" + - "This message has been generated automatically.\n\n" + - "If you have any questions, write to 'helpdesk@openaire.eu'."; + " to the OpenAIRE compliant list of content providers. " + + "\n\n" + + "Please do not reply to this message\n" + + "This message has been generated automatically.\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createUserMail(message, authentication); this.sendMail(repository.getRegisteredby(), subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendAdminRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception { + public void sendAdminRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) { try { String subject = "OpenAIRE new interface registration request started for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "We received a request to add the following interface: \n\n" + - "Base URL: " + repositoryInterface.getBaseurl() + "\n" + - "Set: " + repositoryInterface.getAccessSet() + "\n" + - "Guidelines: " + repositoryInterface.getCompatibilityOverride() + "\n\n" + - "to " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n"; + "Base URL: " + repositoryInterface.getBaseurl() + "\n" + + "Set: " + repositoryInterface.getAccessSet() + "\n" + + "Guidelines: " + repositoryInterface.getCompatibilityOverride() + "\n\n" + + "to " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n"; if (comment != null) message += "\nThe users comment was '" + comment + "'\n"; @@ -234,22 +230,21 @@ public class EmailUtilsImpl implements EmailUtils { this.sendMail(this.provideAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration of interface notification email to the administrator", e); - throw e; + logger.error("Error while sending registration of interface notification email to the administrator", e); } } @Override - public void sendUserRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception { + public void sendUserRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) { try { String subject = "OpenAIRE new interface registration request started for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "We received a request to add the following interface: \n\n" + - "Base URL: " + repositoryInterface.getBaseurl() + "\n" + - "Set: " + repositoryInterface.getAccessSet() + "\n" + - "Guidelines: " + repositoryInterface.getCompatibilityOverride() + "\n\n" + - "to " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n"; + "Base URL: " + repositoryInterface.getBaseurl() + "\n" + + "Set: " + repositoryInterface.getAccessSet() + "\n" + + "Guidelines: " + repositoryInterface.getCompatibilityOverride() + "\n\n" + + "to " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n"; if (comment != null) { message += "\n Your comment was '" + comment + "'\n"; @@ -265,362 +260,404 @@ public class EmailUtilsImpl implements EmailUtils { this.sendMail(repository.getRegisteredby(), subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration of interface notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration of interface notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendUserRegistrationResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendUserRegistrationResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE new interface registration request - results (success) for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "the compatibility test on " + "[" + repository.getOfficialname() + "]" + - " was successful and the datasource type \""+ repository.getEoscDatasourceType() + "\" will be prepared for aggregation in OpenAIRE."+ - "\n\n" + - "Please note that it usually takes about 3-4 weeks until a data source is indexed and it’s metadata visible on openaire.eu.\n\n" + - "Registration identifier in OpenAIRE: "+ repository.getNamespaceprefix()+ - "\nOfficial Name:" + repository.getOfficialname() + - "\n\nBase URL: "+ repositoryInterface.getBaseurl() + - "\n\nValidation Set: " + repositoryInterface.getAccessSet() + - "\n\nGuidelines: "+ repositoryInterface.getCompatibilityOverride() + - "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + - "\n\n\nPlease do not reply to this email\n"+ - "This message has been generated manually\n\n"+ - "If you have any questions, write to 'helpdesk@openaire.eu'."; + " was successful and the datasource type \"" + repository.getEoscDatasourceType() + "\" will be prepared for aggregation in OpenAIRE." + + "\n\n" + + "Please note that it usually takes about 3-4 weeks until a data source is indexed and it's metadata visible on openaire.eu.\n\n" + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + repositoryInterface.getCompatibilityOverride() + + "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated manually\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createUserMail(message, authentication); this.sendMail(repository.getRegisteredby(), subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendAdminRegistrationResultsSuccessEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendAdminRegistrationResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE new interface registration request - results (success) for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "the compatibility test on " + "[" + repository.getOfficialname() + "]" + - " was successful and the datasource type \""+ repository.getEoscDatasourceType() + "\" will be prepared for aggregation in OpenAIRE."+ - "\n\n" + - "Please note that it usually takes about 3-4 weeks until a data source is indexed and it’s metadata visible on openaire.eu.\n\n" + - "Registration identifier in OpenAIRE: "+ repository.getNamespaceprefix()+ - "\nOfficial Name:" + repository.getOfficialname() + - "\n\nBase URL: "+ repositoryInterface.getBaseurl() + - "\n\nValidation Set: " + repositoryInterface.getAccessSet() + - "\n\nGuidelines: "+ repositoryInterface.getCompatibilityOverride() + - "\n\nUser Contact:"+ issuerEmail +""+ - "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + - "\n\n\nPlease do not reply to this email\n"+ - "This message has been generated manually\n\n"+ - "If you have any questions, write to 'helpdesk@openaire.eu'."; + " was successful and the datasource type \"" + repository.getEoscDatasourceType() + "\" will be prepared for aggregation in OpenAIRE." + + "\n\n" + + "Please note that it usually takes about 3-4 weeks until a data source is indexed and it's metadata visible on openaire.eu.\n\n" + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + repositoryInterface.getCompatibilityOverride() + + "\n\nUser Contact:" + issuerEmail + "" + + "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated manually\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createAdminMail(message); this.sendMail(this.provideAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendUserRegistrationResultsFailureEmail(String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendUserRegistrationResultsFailureEmail(String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE new interface registration request - results (failure) for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "the compatibility test on " + "[" + repository.getOfficialname() + "]" + - " was not successful and the registration process was interrupted."+ - "\n\n" + - "We will check what caused the problem and get back to you within a couple of days.\n\n" + - "Registration identifier in OpenAIRE: "+ repository.getNamespaceprefix()+ - "\nOfficial Name:" + repository.getOfficialname() + - "\n\nBase URL: "+ repositoryInterface.getBaseurl() + - "\n\nValidation Set: " + repositoryInterface.getAccessSet() + - "\n\nGuidelines: "+ repositoryInterface.getCompatibilityOverride() + - "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + - "\n\n\nPlease do not reply to this email\n"+ - "This message has been generated manually\n\n"+ - "If you have any questions, write to 'helpdesk@openaire.eu'."; + " was not successful and the registration process was interrupted." + + "\n\n" + + "We will check what caused the problem and get back to you within a couple of days.\n\n" + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + repositoryInterface.getCompatibilityOverride() + + "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated manually\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createUserMail(message, authentication); this.sendMail(repository.getRegisteredby(), subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendAdminRegistrationResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendAdminRegistrationResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE new interface registration request - results (failure) for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "the compatibility test on " + "[" + repository.getOfficialname() + "]" + - " was not successful and the registration process was interrupted."+ - "\n\n" + - "We will check what caused the problem and get back to you within a couple of days.\n\n" + - "Registration identifier in OpenAIRE: "+ repository.getNamespaceprefix()+ - "\nOfficial Name:" + repository.getOfficialname() + - "\n\nBase URL: "+ repositoryInterface.getBaseurl() + - "\n\nValidation Set: " + repositoryInterface.getAccessSet() + - "\n\nGuidelines: "+ repositoryInterface.getCompatibilityOverride() + - "\n\nUser Contact:"+ issuerEmail +""+ - "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + - "\n\n\nPlease do not reply to this email\n"+ - "This message has been generated manually\n\n"+ - "If you have any questions, write to 'helpdesk@openaire.eu'."; + " was not successful and the registration process was interrupted." + + "\n\n" + + "We will check what caused the problem and get back to you within a couple of days.\n\n" + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + repositoryInterface.getCompatibilityOverride() + + "\n\nUser Contact:" + issuerEmail + "" + + "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated manually\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createAdminMail(message); this.sendMail(this.provideAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendUserUpdateResultsSuccessEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendUserUpdateResultsSuccessEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE interface update request - results (success) for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = - "the compatibility test on [" + repository.getOfficialname()+"] has been successful\n\n" + - "We will check your transmitted information and adjust the aggregation settings accordingly. Please note that it usually takes about 3-4 weeks until the changes are visible on openaire.eu."+"\n\n" + - "Registration identifier in OpenAIRE: "+ repository.getNamespaceprefix()+ - "\nOfficial Name:" + repository.getOfficialname() + - "\n\nBase URL: "+ repositoryInterface.getBaseurl() + - "\n\nValidation Set: " + repositoryInterface.getAccessSet() + - "\n\nGuidelines: "+ repositoryInterface.getCompatibilityOverride() + - "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + - "\n\n\nPlease do not reply to this email\n"+ - "This message has been generated manually\n\n"+ - "If you have any questions, write to 'helpdesk@openaire.eu'."; + "the compatibility test on [" + repository.getOfficialname() + "] has been successful\n\n" + + "We will check your transmitted information and adjust the aggregation settings accordingly. Please note that it usually takes about 3-4 weeks until the changes are visible on openaire.eu." + "\n\n" + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + repositoryInterface.getCompatibilityOverride() + + "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated manually\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createUserMail(message, authentication); this.sendMail(issuer, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to the administrator", e); - throw e; + logger.error("Error while sending registration notification email to the administrator", e); } } @Override - public void sendAdminUpdateResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { - try { - String subject = "OpenAIRE interface update request - results (success) for " + - repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; + public void sendAdminUpdateResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { + try { + String subject = "OpenAIRE interface update request - results (success) for " + + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; - String message = - "the compatibility test on [" + repository.getOfficialname()+"] has been successful\n\n" + - "We will check your transmitted information and adjust the aggregation settings accordingly. Please note that it usually takes about 3-4 weeks until the changes are visible on openaire.eu."+"\n\n" + - "Registration identifier in OpenAIRE: "+ repository.getNamespaceprefix()+ - "\nOfficial Name:" + repository.getOfficialname() + - "\n\nBase URL: "+ repositoryInterface.getBaseurl() + - "\n\nValidation Set: " + repositoryInterface.getAccessSet() + - "\n\nGuidelines: "+ repositoryInterface.getCompatibilityOverride() + - "\n\nUser Contact:"+ issuerEmail +""+ - "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + - "\n\n\nPlease do not reply to this email\n"+ - "This message has been generated manually\n\n"+ - "If you have any questions, write to 'helpdesk@openaire.eu'."; - message = createAdminMail(message); + String message = + "the compatibility test on [" + repository.getOfficialname() + "] has been successful\n\n" + + "We will check your transmitted information and adjust the aggregation settings accordingly. Please note that it usually takes about 3-4 weeks until the changes are visible on openaire.eu." + "\n\n" + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + repositoryInterface.getCompatibilityOverride() + + "\n\nUser Contact:" + issuerEmail + "" + + "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated manually\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; + message = createAdminMail(message); - this.sendMail(this.provideAdminEmail, subject, message); + this.sendMail(this.provideAdminEmail, subject, message); - } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to the administrator", e); - throw e; - } + } catch (Exception e) { + logger.error("Error while sending registration notification email to the administrator", e); + } } @Override - public void sendUserUpdateResultsFailureEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendUserUpdateResultsFailureEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE interface update request - results (failure) for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "the compatibility test on " + "[" + repository.getOfficialname() + "]" + - " was not successful."+ - "\n\n" + - "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" + - "Registration identifier in OpenAIRE: "+ repository.getNamespaceprefix()+ - "\nOfficial Name:" + repository.getOfficialname() + - "\n\nBase URL: "+ repositoryInterface.getBaseurl() + - "\n\nValidation Set: " + repositoryInterface.getAccessSet() + - "\n\nGuidelines: "+ repositoryInterface.getCompatibilityOverride() + - "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + - "\n\n\nPlease do not reply to this email\n"+ - "This message has been generated manually\n\n"+ - "If you have any questions, write to 'helpdesk@openaire.eu'."; + " was not successful." + + "\n\n" + + "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + repositoryInterface.getCompatibilityOverride() + + "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated manually\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createUserMail(message, authentication); this.sendMail(issuer, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendAdminUpdateResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendAdminUpdateResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE interface update request - results (failure) for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "the compatibility test on " + "[" + repository.getOfficialname() + "]" + - " was not successful."+ - "\n\n" + - "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" + - "Registration identifier in OpenAIRE: "+ repository.getNamespaceprefix()+ - "\nOfficial Name:" + repository.getOfficialname() + - "\n\nBase URL: "+ repositoryInterface.getBaseurl() + - "\n\nValidation Set: " + repositoryInterface.getAccessSet() + - "\n\nGuidelines: "+ repositoryInterface.getCompatibilityOverride() + - "\n\nUser Contact:"+ issuerEmail +""+ - "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + - "\n\n\nPlease do not reply to this email\n"+ - "This message has been generated manually\n\n"+ - "If you have any questions, write to 'helpdesk@openaire.eu'."; + " was not successful." + + "\n\n" + + "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + repositoryInterface.getCompatibilityOverride() + + "\n\nUser Contact:" + issuerEmail + "" + + "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated manually\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createAdminMail(message); this.sendMail(this.provideAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendUserValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendUserUpdateInterfaceComplianceFailure(List emails, Repository repository, RepositoryInterface repositoryInterface, InterfaceComplianceRequest request) { + try { + String subject = "OpenAIRE interface update compliance request - results (failure) for " + + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; + + String message = + "The request for changing the compatibility of " + "[" + repository.getOfficialname() + "]" + + " was not successful." + + "\n\n" + + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + request.getDesiredCompatibilityLevel() + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated automatically\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; + message = createRepoAdminsMail(message); + + this.sendMail(emails, subject, message); + + } catch (Exception e) { + logger.error("Notification email to repository admins failed.\nRepository: {}\nRequest: {}", repository, request, e); + } + } + + @Override + public void sendAdminUpdateInterfaceComplianceFailure(Repository repository, RepositoryInterface repositoryInterface, InterfaceComplianceRequest request) { + try { + String subject = "OpenAIRE interface update compliance request - results (failure) for " + + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; + + String message = + "The request for changing the compatibility of " + "[" + repository.getOfficialname() + "]" + + " was not successful." + + "\n\n" + + + "Registration identifier in OpenAIRE: " + repository.getNamespaceprefix() + + "\nOfficial Name:" + repository.getOfficialname() + + "\n\nBase URL: " + repositoryInterface.getBaseurl() + + "\n\nValidation Set: " + repositoryInterface.getAccessSet() + + "\n\nGuidelines: " + request.getDesiredCompatibilityLevel() + + "\n\n\nPlease do not reply to this email\n" + + "This message has been generated automatically\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; + message = createAdminMail(message); + + this.sendMail(this.provideAdminEmail, subject, message); + + } catch (Exception e) { + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); + } + } + + @Override + public void sendUserValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE validator - Test results "; String message = - "the validation request you have submitted has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" + - "Please do not reply to this message.\n" + - "This message has been generated automatically.\n" + - "If you have any questions, write to 'helpdesk@openaire.eu'."; + "the validation request you have submitted has finished. You can retrieve the results by following this url: " + valBaseUrl + "" + jobId + " .\n\n" + + "Please do not reply to this message.\n" + + "This message has been generated automatically.\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createUserMail(message, authentication); this.sendMail(issuer, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e); - throw e; + logger.error("Error while sending validation submission notification email to user: " + issuer, e); } } @Override - public void sendAdminValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendAdminValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE validator - Test results "; String message = - "a validation request has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" + - "\n\nUser Contact:"+ issuer +""+ - "Please do not reply to this message.\n" + - "This message has been generated automatically.\n" + - "If you have any questions, write to 'helpdesk@openaire.eu'."; + "a validation request has finished. You can retrieve the results by following this url: " + valBaseUrl + "" + jobId + " .\n\n" + + "\n\nUser Contact:" + issuer + "" + + "Please do not reply to this message.\n" + + "This message has been generated automatically.\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createAdminMail(message); this.sendMail(this.provideAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e); - throw e; + logger.error("Error while sending validation submission notification email to user: " + issuer, e); } } @Override - public void sendAdminGeneralFailure(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception { + public void sendAdminGeneralFailure(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) { try { String subject = "OpenAIRE validator - job failure"; String message = - "the validation job that was automatically submitted for the update/registration of the interface "+repositoryInterface.getId()+" ("+repositoryInterface.getBaseurl()+", "+repositoryInterface.getAccessSet()+") of the repository "+repository.getId()+" ("+repository.getOfficialname()+") failed to complete." + - "This message has been generated automatically."; + "the validation job that was automatically submitted for the update/registration of the interface " + repositoryInterface.getId() + " (" + repositoryInterface.getBaseurl() + ", " + repositoryInterface.getAccessSet() + ") of the repository " + repository.getId() + " (" + repository.getOfficialname() + ") failed to complete." + + "This message has been generated automatically."; message = createAdminMail(message); this.sendMail(this.provideAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e); - throw e; + logger.error("Error while sending validation submission notification email to user: " + issuer, e); } } @Override - public void sendAdminUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception { + public void sendAdminUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) { try { String subject = "OpenAIRE content provider update information for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "We received a request to update the basic information for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n\n" + - "Please do not reply to this message\n" + - "This message has been generated automatically."; + "Please do not reply to this message\n" + + "This message has been generated automatically."; message = createAdminMail(message); this.sendMail(this.provideAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to the administrator", e); - throw e; + logger.error("Error while sending registration notification email to the administrator", e); } } @Override - public void sendUserUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception { + public void sendUserUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) { try { String subject = "OpenAIRE content provider update information for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "We received a request to update the basic information for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n\n" + - "Please do not reply to this message\n" + - "This message has been generated automatically.\n\n" + - "If you have any questions, write to 'helpdesk@openaire.eu'."; + "Please do not reply to this message\n" + + "This message has been generated automatically.\n\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createUserMail(message, authentication); this.sendMail(repository.getRegisteredby(), subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendAdminUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception { + public void sendAdminUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) { try { String subject = "OpenAIRE interface update request started for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "We received a request to update the following interface: \n\n" + - "Base URL: " + repositoryInterface.getBaseurl() + "\n" + - "Set: " + repositoryInterface.getAccessSet() + "\n" + - "Guidelines: " + repositoryInterface.getCompatibilityOverride() + "\n\n" + - "for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n"; + "Base URL: " + repositoryInterface.getBaseurl() + "\n" + + "Set: " + repositoryInterface.getAccessSet() + "\n" + + "Guidelines: " + repositoryInterface.getCompatibilityOverride() + "\n\n" + + "for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n"; if (comment != null) message += "\nThe users comment was '" + comment + "'\n"; @@ -635,23 +672,22 @@ public class EmailUtilsImpl implements EmailUtils { this.sendMail(this.provideAdminEmail, subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to the administrator", e); - throw e; + logger.error("Error while sending registration notification email to the administrator", e); } } @Override - public void sendUserUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception { + public void sendUserUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) { try { String subject = "OpenAIRE interface update request started for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "]"; String message = "We received a request to update the following interface: \n\n" + - "Base URL: " + repositoryInterface.getBaseurl() + "\n" + - "Set: " + repositoryInterface.getAccessSet() + "\n" + - "Guidelines: " + repositoryInterface.getCompatibilityOverride() + "\n\n" + - "for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n"; + "Base URL: " + repositoryInterface.getBaseurl() + "\n" + + "Set: " + repositoryInterface.getAccessSet() + "\n" + + "Guidelines: " + repositoryInterface.getCompatibilityOverride() + "\n\n" + + "for " + repository.getEoscDatasourceType() + "[" + repository.getOfficialname() + "].\n"; if (comment != null) { message += "\n Your comment was '" + comment + "'\n"; @@ -666,28 +702,26 @@ public class EmailUtilsImpl implements EmailUtils { this.sendMail(repository.getRegisteredby(), subject, message); } catch (Exception e) { - LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); - throw e; + logger.error("Error while sending registration notification email to user: " + repository.getRegisteredby(), e); } } @Override - public void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) throws Exception { + public void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) { try { String subject = "OpenAIRE validator - Test submission "; String message = "The validation request you have submitted has started.\n" + - "Please do not reply to this message.\n" + - "This message has been generated automatically.\n" + - "If you have any questions, write to 'helpdesk@openaire.eu'."; + "Please do not reply to this message.\n" + + "This message has been generated automatically.\n" + + "If you have any questions, write to 'helpdesk@openaire.eu'."; message = createUserMail(message, authentication); this.sendMail(jobForValidation.getUserEmail(), subject, message); } catch (Exception e) { - LOGGER.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e); - throw e; + logger.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e); } } @@ -702,51 +736,50 @@ public class EmailUtilsImpl implements EmailUtils { String issuerEmail, String jobId) throws Exception { List repositoryInterfaces = repositoryService.getRepositoryInterface(repoId); - if(repositoryInterfaces.size()==0) - throw new ValidationServiceException("Repository interface with id \""+repoInterfaceId+"\" not found",ValidationServiceException.ErrorCode.GENERAL_ERROR); + if (repositoryInterfaces.isEmpty()) + throw new ValidationServiceException("Repository interface with id \"" + repoInterfaceId + "\" not found", ValidationServiceException.ErrorCode.GENERAL_ERROR); - RepositoryInterface repositoryInterface = repositoryInterfaces.stream().filter( repoInterface -> repoInterface.getId().equals(repoInterfaceId)).collect(Collectors.toList()).get(0); + RepositoryInterface repositoryInterface = repositoryInterfaces.stream().filter(repoInterface -> repoInterface.getId().equals(repoInterfaceId)).collect(Collectors.toList()).get(0); Repository repository = repositoryService.getRepositoryById(repoId); - if(!isUpdate){ - if(isSuccess){ - if(scoreContent>=50 && scoreUsage >= 50){ - this.sendUserRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication()); - this.sendAdminRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication()); - }else{ - this.sendUserRegistrationResultsFailureEmail(jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication()); - this.sendAdminRegistrationResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication()); + if (!isUpdate) { + if (isSuccess) { + if (scoreContent >= 50 && scoreUsage >= 50) { + this.sendUserRegistrationResultsSuccessEmail(issuerEmail, jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); + this.sendAdminRegistrationResultsSuccessEmail(issuerEmail, jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); + } else { + this.sendUserRegistrationResultsFailureEmail(jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); + this.sendAdminRegistrationResultsFailureEmail(issuerEmail, jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); } - }else{ - this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication()); + } else { + this.sendAdminGeneralFailure(issuerEmail, jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); } - }else{ - if(isSuccess){ - if(scoreContent>=50 && scoreUsage >= 50){ - this.sendUserUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication()); - this.sendAdminUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication()); - }else{ - this.sendUserUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication()); - this.sendAdminUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication()); + } else { + if (isSuccess) { + if (scoreContent >= 50 && scoreUsage >= 50) { + this.sendUserUpdateResultsSuccessEmail(issuerEmail, jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); + this.sendAdminUpdateResultsSuccessEmail(issuerEmail, jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); + } else { + this.sendUserUpdateResultsFailureEmail(issuerEmail, jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); + this.sendAdminUpdateResultsFailureEmail(issuerEmail, jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); } - }else{ - this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication()); + } else { + this.sendAdminGeneralFailure(issuerEmail, jobId, repositoryInterface, repository, SecurityContextHolder.getContext().getAuthentication()); } } } - private void sendMail(String email, String subject, String message) throws Exception { + private void sendMail(String email, String subject, String message) { this.sendMail(Collections.singletonList(email), subject, message); } - public void sendMail(List recipients, String subject, String message) throws Exception { + public void sendMail(List recipients, String subject, String message) { try { - LOGGER.debug("Sending e-mail\nRecipients: " + recipients + "\nSubject: " + subject + "\nMessage: " + message); + logger.debug("Sending e-mail\nRecipients: {}\nSubject: {}\nMessage: {}", recipients, subject, message); mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message); } catch (Exception e) { - LOGGER.error("Error sending e-mail\nRecipients: " + recipients + "\nSubject: " + subject + "\nMessage: " + message, e); - throw e; + logger.error("Error sending e-mail\nRecipients: " + recipients + "\nSubject: " + subject + "\nMessage: " + message, e); } } @@ -762,13 +795,20 @@ public class EmailUtilsImpl implements EmailUtils { return body; } + private String createRepoAdminsMail(String body) { + body = "Dear repository admins,\n\n" + body; + body += "\n\nBest Regards,\nthe OpenAIRE technical team\n"; + return body; + } + private String getUserName(Authentication authentication) { String user = "user"; if (authentication != null) { try { 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? } } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/InterfaceComplianceService.java b/src/main/java/eu/dnetlib/repo/manager/service/InterfaceComplianceService.java new file mode 100644 index 0000000..db72fea --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/service/InterfaceComplianceService.java @@ -0,0 +1,87 @@ +package eu.dnetlib.repo.manager.service; + +import eu.dnetlib.repo.manager.domain.*; +import eu.dnetlib.repo.manager.domain.dto.User; +import eu.dnetlib.repo.manager.exception.ResourceConflictException; +import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; +import eu.dnetlib.repo.manager.repository.InterfaceComplianceRequestsRepository; +import eu.dnetlib.repo.manager.service.security.AuthorizationService; +import org.json.JSONException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +import java.util.*; +import java.util.stream.Collectors; + +@Service +public class InterfaceComplianceService { + + private static final Logger logger = LoggerFactory.getLogger(InterfaceComplianceService.class); + + + private final InterfaceComplianceRequestsRepository repository; + private final EmailUtils emailUtils; + private final AuthorizationService authorizationService; + private final RepositoryService repositoryService; + + public InterfaceComplianceService(InterfaceComplianceRequestsRepository repository, + EmailUtils emailUtils, + AuthorizationService authorizationService, + RepositoryService repositoryService) { + this.repository = repository; + this.emailUtils = emailUtils; + this.authorizationService = authorizationService; + this.repositoryService = repositoryService; + } + + + @Scheduled(cron = "0 0 0 * * *") // every day at 00:00 + public void cleanUp() { + Set requests = getOutdated(); + for (InterfaceComplianceRequest request : requests) { + try { + Map repositoryInterfaceMap = repositoryService.getRepositoryInterface(request.getRepositoryId()) + .stream() + .collect(Collectors.toMap(ApiDetails::getId, i -> i)); + Repository repo = repositoryService.getRepositoryById(request.getRepositoryId()); + RepositoryInterface iFace = repositoryInterfaceMap.get(request.getInterfaceId()); + List repositoryAdmins = authorizationService.getAdminsOfRepo(request.getRepositoryId()); + emailUtils.sendUserUpdateInterfaceComplianceFailure(repositoryAdmins.stream().map(User::getEmail).collect(Collectors.toList()), repo, iFace, request); + emailUtils.sendAdminUpdateInterfaceComplianceFailure(repo, iFace, request); + } catch (JSONException | ResourceNotFoundException e) { + logger.error("Error", e); + } + } + repository.deleteAll(requests); + } + + private Set getOutdated() { + Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.DATE, -7); + return this.repository.findAllBySubmissionDateBefore(calendar.getTime()); + } + + public Optional getById(InterfaceComplianceRequestId id) { + return this.repository.findById(id); + } + + public Iterable get() { + return this.repository.findAll(); + } + + public InterfaceComplianceRequest create(InterfaceComplianceRequest request) { + Optional existing = getById(request.getId()); + if (existing.isPresent()) { + logger.warn("New Request: {}\nExisting request: {}", request, existing.get()); + throw new ResourceConflictException("A request for altering compliance already exists. Desired Compatibility value: " + existing.get().getDesiredCompatibilityLevel()); + } + + return this.repository.save(request); + } + + public void delete(InterfaceComplianceRequestId id) { + this.repository.deleteById(id); + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/MonitorServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/MonitorServiceImpl.java index 0670eea..3f0de75 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/MonitorServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/MonitorServiceImpl.java @@ -7,10 +7,11 @@ import eu.dnetlib.repo.manager.domain.Constants; import eu.dnetlib.repo.manager.domain.JobsOfUser; import eu.dnetlib.repo.manager.utils.CrisValidatorUtils; import gr.uoa.di.driver.util.ServiceLocator; -import org.apache.log4j.Logger; import org.eurocris.openaire.cris.validator.model.Job; import org.eurocris.openaire.cris.validator.service.MapJobDao; import org.json.JSONException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -22,6 +23,8 @@ import java.util.stream.Collectors; @Service("monitorService") public class MonitorServiceImpl implements MonitorService { + private static final Logger logger = LoggerFactory.getLogger(MonitorServiceImpl.class); + @Autowired private MapJobDao crisJobs; @@ -41,9 +44,6 @@ public class MonitorServiceImpl implements MonitorService { } - private static final Logger LOGGER = Logger - .getLogger(MonitorServiceImpl.class); - @Override public JobsOfUser getJobsOfUser(String user, String jobType, @@ -54,9 +54,6 @@ public class MonitorServiceImpl implements MonitorService { String validationStatus, String includeJobsTotal) throws JSONException, ValidatorServiceException { - LOGGER.debug("Getting jobs of user : " + user); - LOGGER.debug(user + "/" + jobType + "/" + offset + "/" + dateFrom + "/" + dateTo + "/" + validationStatus + "/" + includeJobsTotal); - ///////////////////////////////////////////////////////////////////////////////////////// // FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly // ///////////////////////////////////////////////////////////////////////////////////////// @@ -154,7 +151,7 @@ public class MonitorServiceImpl implements MonitorService { public int getJobsOfUserPerValidationStatus(String user, String jobType, String validationStatus) throws JSONException { - LOGGER.debug("Getting job with validation status : " + validationStatus); + logger.debug("Getting job with validation status : " + validationStatus); if (jobType.equalsIgnoreCase("cris")) { return crisJobs.getJobs(user, validationStatus).size(); @@ -163,7 +160,7 @@ public class MonitorServiceImpl implements MonitorService { try { return getValidationService().getStoredJobsTotalNumberNew(user, jobType, validationStatus); } catch (ValidatorServiceException e) { - LOGGER.error(e); + logger.error(e.getMessage(), e); } return 0; } @@ -171,12 +168,12 @@ public class MonitorServiceImpl implements MonitorService { @Override public StoredJob getJobSummary(String jobId, String groupBy) throws JSONException { - LOGGER.debug("Getting job summary with id : " + jobId); + logger.debug("Getting job summary with id : " + jobId); StoredJob job = null; try { job = getValidationService().getStoredJob(Integer.parseInt(jobId), groupBy); } catch (ValidatorServiceException e) { - LOGGER.error(e); + logger.error(e.getMessage(), e); } ///////////////////////////////////////////////////////////////////////////////////////// // FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly // diff --git a/src/main/java/eu/dnetlib/repo/manager/service/PiWikServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/PiWikServiceImpl.java index 5cca147..f5c9d56 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/PiWikServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/PiWikServiceImpl.java @@ -2,12 +2,13 @@ package eu.dnetlib.repo.manager.service; import com.fasterxml.jackson.databind.ObjectMapper; import eu.dnetlib.domain.data.PiwikInfo; -import eu.dnetlib.repo.manager.domain.Repository; import eu.dnetlib.repo.manager.domain.OrderByField; import eu.dnetlib.repo.manager.domain.OrderByType; +import eu.dnetlib.repo.manager.domain.Repository; import eu.dnetlib.repo.manager.exception.RepositoryServiceException; import org.apache.commons.codec.digest.DigestUtils; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; @@ -32,6 +33,9 @@ import java.util.Map; @Service("piwikService") public class PiWikServiceImpl implements PiWikService { + private static final Logger logger = LoggerFactory.getLogger(PiWikServiceImpl.class); + + @Autowired private DataSource dataSource; @@ -47,20 +51,20 @@ public class PiWikServiceImpl implements PiWikService { @Qualifier("emailUtils") private EmailUtils emailUtils; - private static final Logger LOGGER = Logger - .getLogger(PiWikServiceImpl.class); - private final static String GET_PIWIK_SITE = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site where repositoryid = ?;"; + private static final String GET_PIWIK_SITE = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site where repositoryid = ? LIMIT 1;"; - private final static String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)"; + private static final String INSERT_PIWIK_INFO = "insert into piwik_site (repositoryid, siteid, creationdate, requestorname, requestoremail, validated, repositoryname, country, authenticationtoken) values (?, ?, now(), ?, ?, ?, ?, ?, ?)"; - private final static String GET_PIWIK_SITES = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site "; + private static final String UPDATE_PIWIK_INFO = "update piwik_site set siteid = ?, creationdate = now(), requestorname = ?, requestoremail = ?, validated = ?, repositoryname = ?, country = ?, authenticationtoken = ? where repositoryid = ?"; - private final static String GET_PIWIK_SITES_TOTAL = "select count(*) as totals from piwik_site "; + private static final String GET_PIWIK_SITES = "select repositoryid, siteid, authenticationtoken, creationdate, requestorname, requestoremail, validated, validationdate, comment, repositoryname, country from piwik_site "; - private final static String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;"; + private static final String GET_PIWIK_SITES_TOTAL = "select count(*) as totals from piwik_site "; - private RowMapper piwikRowMapper = (rs, i) -> new PiwikInfo(rs.getString("repositoryid"), getOpenaireId(rs.getString("repositoryid")), rs.getString("repositoryname"), rs.getString("country"), + private static final String APPROVE_PIWIK_SITE = "update piwik_site set validated=true, validationdate=now() where repositoryid = ?;"; + + private final RowMapper piwikRowMapper = (rs, i) -> new PiwikInfo(rs.getString("repositoryid"), getOpenaireId(rs.getString("repositoryid")), rs.getString("repositoryname"), rs.getString("country"), rs.getString("siteid"), rs.getString("authenticationtoken"), rs.getTimestamp("creationdate"), rs.getString("requestorname"), rs.getString("requestoremail"), rs.getBoolean("validated"), rs.getTimestamp("validationdate"), rs.getString("comment")); @@ -78,9 +82,15 @@ public class PiWikServiceImpl implements PiWikService { @PreAuthorize("hasAuthority('SUPER_ADMINISTRATOR') or hasAuthority('CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR') or (hasAuthority('REGISTERED_USER') and #piwikInfo.requestorEmail == authentication.userInfo.email)") public PiwikInfo savePiwikInfo(PiwikInfo piwikInfo) { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); - jdbcTemplate.update(INSERT_PIWIK_INFO, new Object[]{piwikInfo.getRepositoryId(), piwikInfo.getSiteId(), piwikInfo.getRequestorName(), - piwikInfo.getRequestorEmail(), piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()}, - new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}); + if (getPiwikSiteForRepo(piwikInfo.getRepositoryId()) == null) { + jdbcTemplate.update(INSERT_PIWIK_INFO, new Object[]{piwikInfo.getRepositoryId(), piwikInfo.getSiteId(), piwikInfo.getRequestorName(), + piwikInfo.getRequestorEmail(), piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken()}, + new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}); + } else { + jdbcTemplate.update(UPDATE_PIWIK_INFO, new Object[]{piwikInfo.getSiteId(), piwikInfo.getRequestorName(), piwikInfo.getRequestorEmail(), + piwikInfo.isValidated(), piwikInfo.getRepositoryName(), piwikInfo.getCountry(), piwikInfo.getAuthenticationToken(), piwikInfo.getRepositoryId()}, + new int[]{Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.BOOLEAN, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR}); + } return piwikInfo; } @@ -168,10 +178,10 @@ public class PiWikServiceImpl implements PiWikService { emailUtils.sendUserMetricsEnabled(piwikInfo); } catch (EmptyResultDataAccessException e) { - LOGGER.error("Error while approving piwik site: ", e); + logger.error("Error while approving piwik site: ", e); throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR); } catch (Exception e) { - LOGGER.error("Error while sending email to administrator or user about the enabling of metrics", e); + logger.error("Error while sending email to administrator or user about the enabling of metrics", e); throw new RepositoryServiceException(e, RepositoryServiceException.ErrorCode.GENERAL_ERROR); } return new ResponseEntity<>("OK", HttpStatus.OK); @@ -183,9 +193,9 @@ public class PiWikServiceImpl implements PiWikService { String repoWebsite, PiwikInfo piwikInfo) throws RepositoryServiceException { try { - String URL = analyticsURL + "siteName=" + URLEncoder.encode(officialName, "UTF-8") + "&url=" + String url = analyticsURL + "siteName=" + URLEncoder.encode(officialName, "UTF-8") + "&url=" + URLEncoder.encode(repoWebsite, "UTF-8"); - Map map = new ObjectMapper().readValue(new URL(URL), Map.class); + Map map = new ObjectMapper().readValue(new URL(url), Map.class); String siteId = null; if (map.get("value") != null) { siteId = map.get("value").toString(); @@ -202,13 +212,13 @@ public class PiWikServiceImpl implements PiWikService { emailUtils.sendAdministratorRequestToEnableMetrics(piwikInfo); emailUtils.sendUserRequestToEnableMetrics(piwikInfo); } catch (UnsupportedEncodingException uee) { - LOGGER.error("Error while creating piwikScript URL", uee); + logger.error("Error while creating piwikScript URL", uee); throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR); } catch (IOException ioe) { - LOGGER.error("Error while creating piwik site", ioe); + logger.error("Error while creating piwik site", ioe); throw new RepositoryServiceException("login.generalError", RepositoryServiceException.ErrorCode.GENERAL_ERROR); } catch (Exception e) { - LOGGER.error("Error while sending email to administrator or user about the request to enable metrics", e); + logger.error("Error while sending email to administrator or user about the request to enable metrics", e); throw new RepositoryServiceException(e, RepositoryServiceException.ErrorCode.GENERAL_ERROR); } return piwikInfo; diff --git a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java index 3febfbd..ff760f9 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryService.java @@ -1,7 +1,5 @@ package eu.dnetlib.repo.manager.service; -import eu.dnetlib.repo.manager.domain.Repository; -import eu.dnetlib.repo.manager.domain.RepositoryInterface; import eu.dnetlib.repo.manager.domain.*; import eu.dnetlib.repo.manager.exception.RepositoryServiceException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; @@ -44,12 +42,6 @@ public interface RepositoryService { Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException; - List getRepositoryAggregations(String id) throws JSONException; - - List getRepositoryAggregations(String id, int from, int size) throws JSONException; - - Map> getRepositoryAggregationsByYear(String id) throws JSONException; - List getRepositoriesByName(String name, String page, String size) throws JSONException; @@ -68,7 +60,9 @@ public interface RepositoryService { RepositoryInterface addRepositoryInterface(String datatype, String repoId, - String comment, RepositoryInterface repositoryInterface) throws Exception; + String comment, + RepositoryInterface repositoryInterface, + String desiredCompatibilityLevel) throws Exception; List getDnetCountries(); @@ -92,6 +86,7 @@ public interface RepositoryService { Map getListLatestUpdate(String mode) throws JSONException; - RepositoryInterface updateRepositoryInterface(String repoId, String comment, RepositoryInterface repositoryInterface) throws Exception; + RepositoryInterface updateRepositoryInterface(String repoId, String comment, RepositoryInterface repositoryInterface, String desiredCompatibilityLevel) throws Exception; + void updateInterfaceCompliance(String repositoryId, String repositoryInterfaceId, String compliance); } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java index 7fcec21..04ad975 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/RepositoryServiceImpl.java @@ -5,8 +5,6 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import eu.dnetlib.api.functionality.ValidatorServiceException; -import eu.dnetlib.repo.manager.domain.Repository; -import eu.dnetlib.repo.manager.domain.RepositoryInterface; import eu.dnetlib.domain.enabling.Vocabulary; import eu.dnetlib.domain.functionality.validator.JobForValidation; import eu.dnetlib.repo.manager.domain.*; @@ -20,13 +18,15 @@ import eu.dnetlib.repo.manager.service.security.AuthoritiesUpdater; import eu.dnetlib.repo.manager.service.security.AuthorizationService; import eu.dnetlib.repo.manager.service.security.RoleMappingService; import eu.dnetlib.repo.manager.utils.Converter; +import eu.dnetlib.repo.manager.utils.DateUtils; import gr.uoa.di.driver.enabling.vocabulary.VocabularyLoader; import org.apache.commons.codec.digest.DigestUtils; -import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.mitre.openid.connect.model.OIDCAuthenticationToken; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; @@ -46,22 +46,23 @@ import java.io.IOException; import java.sql.Timestamp; import java.util.*; import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; @Service("repositoryService") public class RepositoryServiceImpl implements RepositoryService { - private static final Logger LOGGER = Logger.getLogger(RepositoryServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(RepositoryServiceImpl.class); private final AuthorizationService authorizationService; private final RoleMappingService roleMappingService; private final AaiRegistryService registryCalls; private final AuthoritiesUpdater authoritiesUpdater; private final RestTemplate restTemplate; + private final ObjectMapper objectMapper; private final VocabularyLoader vocabularyLoader; private final PiWikService piWikService; private final EmailUtils emailUtils; private final ValidatorService validatorService; + private final InterfaceComplianceService interfaceComplianceService; @Value("${services.provide.clients.dsm}") private String baseAddress; @@ -94,10 +95,12 @@ public class RepositoryServiceImpl implements RepositoryService { AuthoritiesUpdater authoritiesUpdater, VocabularyLoader vocabularyLoader, RestTemplate restTemplate, + ObjectMapper objectMapper, Converter converter, @Lazy EmailUtils emailUtils, @Lazy ValidatorService validatorService, - @Lazy PiWikService piWikService) { + @Lazy PiWikService piWikService, + @Lazy InterfaceComplianceService interfaceComplianceService) { this.authorizationService = authorizationService; this.roleMappingService = roleMappingService; this.registryCalls = registryCalls; @@ -108,12 +111,13 @@ public class RepositoryServiceImpl implements RepositoryService { this.emailUtils = emailUtils; this.validatorService = validatorService; this.restTemplate = restTemplate; + this.objectMapper = objectMapper; + this.interfaceComplianceService = interfaceComplianceService; } @PostConstruct private void init() { - LOGGER.debug("Initialization method of repository api!"); - LOGGER.debug("Updated version!"); + logger.debug("Initialization method of repository api! Updated version!"); for (String key : this.getVocabulary("dnet:datasource_typologies").getAsMap().keySet()) { if (key.contains("aggregator")) { @@ -137,7 +141,7 @@ public class RepositoryServiceImpl implements RepositoryService { } httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + httpHeaders.setContentType(MediaType.valueOf(MediaType.APPLICATION_JSON_VALUE)); for (String vocName : vocabularyNames) { vocabularyMap.put(vocName, vocabularyLoader.getVocabulary(vocName, Locale.ENGLISH, Locale.ROOT)); @@ -148,8 +152,6 @@ public class RepositoryServiceImpl implements RepositoryService { countriesMap.put(c.getName(), c.getCode()); inverseCountriesMap.put(c.getCode(), c.getName()); } - - } @Override @@ -159,7 +161,7 @@ public class RepositoryServiceImpl implements RepositoryService { .build().encode(); return restTemplate.getForObject(uriComponents.toUri(), Country[].class); } - + // FIXME: with the new roles of the users the "requestFilter.setRegisteredby(userEmail)" can no longer be used // and the "requestFilter.setId(repoId)" should return only one result at a time, thus, // another way for paging must be implemented. @@ -174,17 +176,19 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public List getRepositories(List ids, int page, int size) throws JSONException { List repos = new ArrayList<>(); - LOGGER.debug("Retrieving repositories with ids : " + String.join(", ", ids)); + logger.debug("Retrieving repositories with ids : {}", String.join(", ", ids)); UriComponents uriComponents = searchDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); RequestFilter requestFilter = new RequestFilter(); for (String repoId : ids) { requestFilter.setId(repoId); - List rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, List.class); + List rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, List.class); // repos.addAll(converter.toRepositoryList(new JSONObject(rs))); } + // TODO - "repos" is EMPTY!! + for (Repository r : repos) r.setPiwikInfo(piWikService.getPiwikSiteForRepo(r.getId())); return repos; @@ -204,27 +208,30 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public List getRepositoriesSnippets(List ids, int page, int size) throws Exception { List resultSet = new ArrayList<>(); - ObjectMapper mapper = new ObjectMapper(); // here page should be 0 - UriComponents uriComponents = searchSnipperDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); + UriComponents uriComponents = searchSnippetDatasource(Integer.toString(Math.abs(page)), Integer.toString(Math.abs(size))); RequestFilter requestFilter = new RequestFilter(); try { for (String repoId : ids) { requestFilter.setId(repoId); - String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); - JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); - resultSet.addAll(mapper.readValue(String.valueOf(jsonArray), - mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.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()), + objectMapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); } } catch (Exception e) { - LOGGER.debug("Exception on getRepositoriesSnippetOfUser", e); + logger.debug("Exception on getRepositoriesSnippetOfUser", e); throw e; } - LOGGER.debug("resultSet:" + resultSet); + logger.debug("resultSet: {}", resultSet); resultSet.parallelStream().forEach(repositorySnippet -> { repositorySnippet.setPiwikInfo(piWikService.getPiwikSiteForRepo(repositorySnippet.getId())); }); @@ -237,7 +244,7 @@ public class RepositoryServiceImpl implements RepositoryService { String mode, Boolean managed) throws JSONException, IOException { - LOGGER.debug("Getting repositories by country!"); + logger.debug("Getting repositories by country!"); int page = 0; int size = 100; List resultSet = new ArrayList<>(); @@ -250,24 +257,17 @@ public class RepositoryServiceImpl implements RepositoryService { filterKey = "CRIS system"; - LOGGER.debug("Country code equals : " + country); - LOGGER.debug("Filter mode equals : " + filterKey); + logger.debug("Country code equals : {} | Filter mode equals : {}", country, filterKey); - UriComponents uriComponents = searchSnipperDatasource(String.valueOf(page), String.valueOf(size)); + UriComponents uriComponents = searchSnippetDatasource(String.valueOf(page), String.valueOf(size)); RequestFilter requestFilter = new RequestFilter(); requestFilter.setCountry(country); requestFilter.setEoscDatasourceType(filterKey); - String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); - JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); - while (jsonArray.length() > 0) { - - resultSet.addAll(mapper.readValue(String.valueOf(jsonArray), + Map rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, Map.class); + if (rs != null) { + resultSet.addAll(mapper.readValue(mapper.writeValueAsString(rs.get("datasourceInfo")), mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); - page += 1; - uriComponents = searchSnipperDatasource(String.valueOf(page), String.valueOf(size)); - rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); - jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); } return resultSet; } @@ -275,7 +275,7 @@ public class RepositoryServiceImpl implements RepositoryService { public List searchRegisteredRepositories(String country, String typology, String englishName, String officialName, String requestSortBy, String order, int page, int pageSize) throws Exception { - LOGGER.debug("Searching registered repositories"); + logger.debug("Searching registered repositories"); Paging snippets = null; ObjectMapper mapper = new ObjectMapper(); @@ -289,28 +289,26 @@ public class RepositoryServiceImpl implements RepositoryService { requestFilter.setEnglishname(englishName); try { - String rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String.class); + Map rs = restTemplate.postForObject(uriComponents.toUri(), requestFilter, Map.class); if (rs == null) { - LOGGER.error(String.format("DSM response is null : [url=%s]", uriComponents.toUri())); + logger.error("DSM response is null : [url={}]", uriComponents.toUri()); } else { - JSONObject response = new JSONObject(rs); - JSONArray jsonArray = (JSONArray) response.get("datasourceInfo"); - Header header = mapper.readValue(response.get("header").toString(), Header.class); + Header header = mapper.readValue(mapper.writeValueAsString(rs.get("header")), Header.class); snippets = Paging.of(header, mapper.readValue( - String.valueOf(jsonArray), + mapper.writeValueAsString(rs.get("datasourceInfo")), mapper.getTypeFactory().constructCollectionType(List.class, RepositorySnippet.class))); } } catch (Exception e) { - LOGGER.error("Error searching registered datasources", e); + logger.error("Error searching registered datasources", e); throw e; } return snippets != null ? snippets.getResults() : null; // TODO: return paging when ui is compatible } @Override - public int getTotalRegisteredRepositories() { + public int getTotalRegisteredRepositories() throws NullPointerException { UriComponents uriComponents = UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/countregistered") .queryParam("fromDate", "1900-01-01") @@ -327,16 +325,16 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public List getRepositoriesOfUser(String page, String size) throws JSONException { - String userEmail = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail(); - LOGGER.debug("Retrieving repositories of authenticated user : " + userEmail); + logger.debug("Retrieving repositories of authenticated user : {}", + ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo().getEmail()); Collection repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles()); return getRepositories(new ArrayList<>(repoIds)); } @Override public List getRepositoriesOfUser(String userEmail, String page, String size) throws JSONException { - LOGGER.debug("Retrieving repositories of authenticated user : " + userEmail); - Collection repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles(userEmail)); + logger.debug("Retrieving repositories of authenticated user : {}", userEmail); + Collection repoIds = roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRolesByEmail(userEmail)); return getRepositories(new ArrayList<>(repoIds)); } @@ -351,7 +349,7 @@ public class RepositoryServiceImpl implements RepositoryService { int to = from + Integer.parseInt(size); List repoIds = new ArrayList<>(); if (userEmail != null && !"".equals(userEmail)) { - repoIds.addAll(roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles(userEmail))); + repoIds.addAll(roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRolesByEmail(userEmail))); } else { repoIds.addAll(roleMappingService.getRepoIdsByRoleIds(authorizationService.getUserRoles())); } @@ -367,13 +365,18 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public RepositorySnippet getRepositorySnippetById(String id) throws JSONException, ResourceNotFoundException { - LOGGER.debug("Retrieving repositories with id : " + id); + logger.debug("Retrieving repositories with id : {}", id); RepositorySnippet repo; - UriComponents uriComponents = searchSnipperDatasource("0", "100"); + UriComponents uriComponents = searchSnippetDatasource("0", "100"); RequestFilter requestFilter = new RequestFilter(); requestFilter.setId(id); 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"); if (jsonArray.length() == 0) @@ -386,7 +389,7 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public Repository getRepositoryById(String id) throws JSONException, ResourceNotFoundException { - LOGGER.debug("Retrieving repositories with id : " + id); + logger.debug("Retrieving repositories with id : {}", id); Repository repo; UriComponents uriComponents = searchDatasource("0", "100"); RequestFilter requestFilter = new RequestFilter(); @@ -396,7 +399,12 @@ public class RepositoryServiceImpl implements RepositoryService { // JSONArray jsonArray = (JSONArray) new JSONObject(rs).get("datasourceInfo"); 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 datasources = response.getDatasourceInfo(); if (datasources.size() == 0) throw new ResourceNotFoundException(); @@ -409,55 +417,12 @@ public class RepositoryServiceImpl implements RepositoryService { } - @Override - public List getRepositoryAggregations(String id) throws JSONException { - - LOGGER.debug("Retrieving aggregations for repository with id : " + id); - UriComponents uriComponents = getAggregationHistory(id); - - String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); - JSONArray aggregationInfo = new JSONObject(rs).getJSONArray("aggregationInfo"); - - List aggregationHistory = new ArrayList<>(converter.toAggregationHistory(aggregationInfo)); - - return aggregationHistory; - } - - @Override - public List getRepositoryAggregations(String id, int from, int size) throws JSONException { - - List res = getRepositoryAggregations(id); - - return res.subList(from, Math.min(from + size, res.size())); - } - - @Override - public Map> getRepositoryAggregationsByYear(String id) throws JSONException { - LOGGER.debug("Retrieving aggregations (by year) for repository with id : " + id); - - List aggregationHistory = getRepositoryAggregations(id); - Map> aggregationByYear = new HashMap<>(); - - return aggregationHistory.size() == 0 ? aggregationByYear : createYearMap(aggregationHistory); - } - - private Map> createYearMap(List aggregationHistory) { - - aggregationHistory = aggregationHistory.stream() - .sorted(Comparator.comparing(AggregationDetails::getDate).reversed()) - .collect(Collectors.toList()); - - return aggregationHistory.stream() - .collect(Collectors.groupingBy(AggregationDetails::getYear)); - } - - @Override public List getRepositoriesByName(String name, String page, String size) throws JSONException { - LOGGER.debug("Retrieving repositories with official name : " + name); + logger.debug("Retrieving repositories with official name : {}", name); UriComponents uriComponents = searchDatasource("0", "100"); RequestFilter requestFilter = new RequestFilter(); requestFilter.setOfficialname(name); @@ -480,15 +445,19 @@ public class RepositoryServiceImpl implements RepositoryService { // String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); ApiDetailsResponse rs = restTemplate.getForObject(uriComponents.toUri(), ApiDetailsResponse.class); - + if (rs == null) { + logger.error("The ApiDetailsResponse was null!"); + return null; + } // TODO STOP FILTERING OUT "sword", "rest" AND FIX UI! List res = new ArrayList<>(); - for (ApiDetails det: rs.getApi()) { - if (!det.getProtocol().equals("sword") && - !det.getProtocol().equals("rest") && - !det.getProtocol().equals("ftp")) { + for (ApiDetails det : rs.getApi()) { + String protocol = det.getProtocol(); + if (!protocol.equals("sword") && + !protocol.equals("rest") && + !protocol.equals("ftp")) { res.add(det); } } @@ -499,7 +468,7 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public Repository addRepository(String datatype, Repository repository) throws Exception { - LOGGER.debug("storing " + datatype + " repository with id: " + repository.getId()); + logger.debug("storing '{}' repository with id: {}", datatype, repository.getId()); repository.setActivationId(UUID.randomUUID().toString()); repository.setCollectedfrom("infrastruct_::openaire"); @@ -538,10 +507,10 @@ public class RepositoryServiceImpl implements RepositoryService { } catch (HttpClientErrorException e) { couId = registryCalls.getCouId(newRoleName); if (couId == null) { - LOGGER.error(String.format("Could not create role '%s'", newRoleName), e); + logger.error(String.format("Could not create role '%s'", newRoleName), e); } } catch (Exception e) { - LOGGER.error(String.format("Could not create role '%s'", newRoleName), e); + logger.error(String.format("Could not create role '%s'", newRoleName), e); throw e; } @@ -549,19 +518,12 @@ public class RepositoryServiceImpl implements RepositoryService { Integer coPersonId = registryCalls.getCoPersonIdByIdentifier(); if (couId != null) { Integer role = registryCalls.getRoleId(coPersonId, couId); - try { - registryCalls.assignMemberRole(coPersonId, couId, role); - - // Add role to current user authorities - authoritiesUpdater.addRole(roleMappingService.convertRepoIdToAuthority(repository.getId())); - } catch (Exception e) { - LOGGER.debug("Exception on assign role to user during add repository", e); - throw e; - } + registryCalls.assignMemberRole(coPersonId, couId, role); + // Add role to current user authorities + authoritiesUpdater.addRole(roleMappingService.convertRepoIdToAuthority(repository.getId())); } - return repository; } @@ -574,20 +536,19 @@ public class RepositoryServiceImpl implements RepositoryService { // FIXME: problematic // String json_repository = converter.toJson(repository); -// LOGGER.debug("JSON to add(update) -> " + json_repository); +// logger.debug("JSON to add(update) -> " + json_repository); HttpEntity 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 = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { - try { - emailUtils.sendUserRegistrationEmail(repository, authentication); - emailUtils.sendAdminRegistrationEmail(repository, authentication); - } catch (Exception e) { - LOGGER.error("Error sending email", e); - } - } else - LOGGER.error("Error storing repository: " + responseEntity.getBody().toString()); + emailUtils.sendUserRegistrationEmail(repository, authentication); + emailUtils.sendAdminRegistrationEmail(repository, authentication); + } else { + Object responseBody = responseEntity.getBody(); + if (responseBody != null) + logger.error("Error updating repository: {}", responseBody); + } return repository; } @@ -601,21 +562,19 @@ public class RepositoryServiceImpl implements RepositoryService { // FIXME: problematic // String json_repository = converter.toJson(repository); -// LOGGER.debug("JSON to update -> " + json_repository); +// logger.debug("JSON to update -> " + json_repository); HttpEntity httpEntity = new HttpEntity<>(repository, httpHeaders); - ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity - , ResponseEntity.class); + ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { - try { - emailUtils.sendUserUpdateRepositoryInfoEmail(repository, authentication); - emailUtils.sendAdminUpdateRepositoryInfoEmail(repository, authentication); - } catch (Exception e) { - LOGGER.error("Error sending emails: " + e); - } - } else - LOGGER.debug(responseEntity.getBody().toString()); + emailUtils.sendUserUpdateRepositoryInfoEmail(repository, authentication); + emailUtils.sendAdminUpdateRepositoryInfoEmail(repository, authentication); + } else { + Object responseBody = responseEntity.getBody(); + if (responseBody != null) + logger.error("Error updating repository: {}", responseBody); + } return repository; } @@ -634,17 +593,15 @@ public class RepositoryServiceImpl implements RepositoryService { .encode(); // String json_repository = converter.toJson(repository); HttpEntity httpEntity = new HttpEntity<>(repository, httpHeaders); - ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); + ResponseEntity responseEntity = restTemplate.exchange(uriComponents.toUri(), HttpMethod.POST, httpEntity, ResponseEntity.class); if (responseEntity.getStatusCode().equals(HttpStatus.OK)) { - try { - emailUtils.sendUserRegistrationEmail(repository, authentication); - emailUtils.sendAdminRegistrationEmail(repository, authentication); - } catch (Exception e) { - LOGGER.error("Error sending emails: " + e); - } + emailUtils.sendUserRegistrationEmail(repository, authentication); + emailUtils.sendAdminRegistrationEmail(repository, authentication); } else { - LOGGER.debug(responseEntity.getBody().toString()); + Object responseBody = responseEntity.getBody(); + if (responseBody != null) + logger.error("Error storing repository: {}", responseBody); } } @@ -655,14 +612,16 @@ public class RepositoryServiceImpl implements RepositoryService { .fromHttpUrl(baseAddress + "/ds/api/") .path("/{id}") .build().expand(id).encode(); - LOGGER.debug(uriComponents.toUri()); + logger.debug("{}", uriComponents.toUri()); restTemplate.delete(uriComponents.toUri()); } @Override public RepositoryInterface addRepositoryInterface(String datatype, String repoId, - String comment, RepositoryInterface repositoryInterface) throws Exception { + String comment, + RepositoryInterface repositoryInterface, + String desiredCompatibilityLevel) throws Exception { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); Repository e = this.getRepositoryById(repoId); repositoryInterface = createRepositoryInterface(e, repositoryInterface, datatype); @@ -677,14 +636,15 @@ public class RepositoryServiceImpl implements RepositoryService { restTemplate.postForObject(uriComponents.toUri(), httpEntity, String.class); - try { - emailUtils.sendAdminRegisterInterfaceEmail(e, comment, repositoryInterface, authentication); - emailUtils.sendUserRegisterInterfaceEmail(e, comment, repositoryInterface, authentication); - } catch (Exception ex) { - LOGGER.error("Error sending emails: " + ex); + emailUtils.sendAdminRegisterInterfaceEmail(e, comment, repositoryInterface, authentication); + emailUtils.sendUserRegisterInterfaceEmail(e, comment, repositoryInterface, authentication); + + if (desiredCompatibilityLevel != null && (repositoryInterface.getCompatibility() == null || !repositoryInterface.getCompatibility().equals(desiredCompatibilityLevel))) { + InterfaceComplianceRequest request = new InterfaceComplianceRequest(repoId, repositoryInterface.getId(), desiredCompatibilityLevel); + interfaceComplianceService.create(request); } - submitInterfaceValidation(e, getAuthenticatedUser().getEmail(), repositoryInterface, false); + submitInterfaceValidation(e, getAuthenticatedUser().getEmail(), repositoryInterface, false, repositoryInterface.getCompatibility()); return repositoryInterface; } @@ -692,41 +652,43 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public RepositoryInterface updateRepositoryInterface(String repoId, String comment, - RepositoryInterface repositoryInterface) throws Exception { + RepositoryInterface repositoryInterface, + String desiredCompatibilityLevel) throws Exception { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - this.updateBaseUrl(repoId, repositoryInterface.getId(), repositoryInterface.getBaseurl()); - this.updateCompliance(repoId, repositoryInterface.getId(), repositoryInterface.getCompatibility()); + Repository repository = this.getRepositoryById(repoId); + this.updateValidationSet(repoId, repositoryInterface.getId(), repositoryInterface.getAccessSet()); - Repository repository = this.getRepositoryById(repoId); - try { - try { - emailUtils.sendAdminUpdateInterfaceEmail(repository, comment, repositoryInterface, authentication); - emailUtils.sendUserUpdateInterfaceEmail(repository, comment, repositoryInterface, authentication); - } catch (Exception e) { - LOGGER.error("Error sending emails: " + e); - } - } catch (Exception e) { - LOGGER.warn("Could not send emails", e); + emailUtils.sendAdminUpdateInterfaceEmail(repository, comment, repositoryInterface, authentication); + emailUtils.sendUserUpdateInterfaceEmail(repository, comment, repositoryInterface, authentication); + + if (desiredCompatibilityLevel != null && (repositoryInterface.getCompatibility() == null || !repositoryInterface.getCompatibility().equals(desiredCompatibilityLevel))) { + InterfaceComplianceRequest request = new InterfaceComplianceRequest(repoId, repositoryInterface.getId(), desiredCompatibilityLevel); + interfaceComplianceService.create(request); } - submitInterfaceValidation(getRepositoryById(repoId), getAuthenticatedUser().getEmail(), repositoryInterface, true); + submitInterfaceValidation(getRepositoryById(repoId), getAuthenticatedUser().getEmail(), repositoryInterface, true, desiredCompatibilityLevel); return repositoryInterface; } + @Override + public void updateInterfaceCompliance(String repositoryId, String repositoryInterfaceId, String compliance) { + this.updateCompliance(repositoryId, repositoryInterfaceId, compliance); + } + private User getAuthenticatedUser() { return User.from(((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo()); } - private void submitInterfaceValidation(Repository repo, String userEmail, RepositoryInterface iFace, boolean updateExisting) throws ValidatorServiceException { + private void submitInterfaceValidation(Repository repo, String userEmail, RepositoryInterface iFace, boolean updateExisting, String desiredCompatibilityLevel) throws ValidatorServiceException { JobForValidation job = new JobForValidation(); job.setActivationId(UUID.randomUUID().toString()); job.setAdminEmails(Collections.singletonList(this.adminEmail)); job.setBaseUrl(iFace.getBaseurl()); job.setDatasourceId(repo.getId()); - job.setDesiredCompatibilityLevel(iFace.getCompatibilityOverride()); + job.setDesiredCompatibilityLevel(desiredCompatibilityLevel); job.setInterfaceId(iFace.getId()); job.setOfficialName(repo.getOfficialname()); job.setRepoType(repo.getEoscDatasourceType()); @@ -745,7 +707,12 @@ public class RepositoryServiceImpl implements RepositoryService { iFace.setDatasource(repo.getId()); iFace.setContentdescription("metadata"); - iFace.setCompatibility("UNKNOWN"); + + // TODO: double check me + logger.warn("Compatibility level: {}", iFace.getCompatibility()); + if (iFace.getCompatibility() == null || iFace.getCompatibility().equals("")) { + iFace.setCompatibility("UNKNOWN"); + } if (datatype.equals("re3data")) iFace.setAccessFormat("oai_datacite"); @@ -770,8 +737,7 @@ public class RepositoryServiceImpl implements RepositoryService { iFace.setMetadataIdentifierPath("//*[local-name()='header']/*[local-name()='identifier']"); iFace.setId("api_________::" + repo.getId() + "::" + UUID.randomUUID().toString().substring(0, 8)); if (iFace.getAccessSet() == null || iFace.getAccessSet().isEmpty()) { - LOGGER.debug("set is empty: " + iFace.getAccessSet()); -// iFace.removeAccessSet(); + logger.debug("set is empty: {}", iFace.getAccessSet()); iFace.setAccessSet("none"); } return iFace; @@ -779,7 +745,7 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public List getDnetCountries() { - LOGGER.debug("Getting dnet-countries!"); + logger.debug("Getting dnet-countries!"); return converter.readFile("countries.txt"); } @@ -805,7 +771,8 @@ public class RepositoryServiceImpl implements RepositoryService { RequestFilter requestFilter = new RequestFilter(); requestFilter.setRegisteredby(userEmail); - return Arrays.asList(restTemplate.postForObject(uriComponents.toUri(), requestFilter, String[].class)); + Object result = restTemplate.postForObject(uriComponents.toUri(), requestFilter, String[].class); + return (result != null) ? Collections.singletonList(result.toString()) : null; } private Vocabulary getVocabulary(String vocName) { @@ -820,7 +787,7 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public Map getCompatibilityClasses(String mode) { - LOGGER.debug("Getting compatibility classes for mode: " + mode); + logger.debug("Getting compatibility classes for mode: {}", mode); Map retMap = new HashMap(); Map compatibilityClasses = this.getVocabulary("dnet:compatibilityLevel").getAsMap(); @@ -829,18 +796,17 @@ public class RepositoryServiceImpl implements RepositoryService { if (mode.equalsIgnoreCase(Constants.REPOSITORY_MODE_ALL)) return compatibilityClasses; 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()); foundData = true; } - } - else if (mode.equalsIgnoreCase("cris")) { + } else if (mode.equalsIgnoreCase("cris")) { if (entry.getKey().contains("openaire-cris")) { retMap.put(entry.getKey(), entry.getValue()); foundData = true; } } 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()); } } @@ -855,7 +821,7 @@ public class RepositoryServiceImpl implements RepositoryService { @Override public Map getDatasourceClasses(String mode) { - LOGGER.debug("Getting datasource classes for mode: " + mode); + logger.debug("Getting datasource classes for mode: {}", mode); Map retMap = new HashMap(); @@ -916,7 +882,7 @@ public class RepositoryServiceImpl implements RepositoryService { return metricsInfo; } catch (Exception e) { - LOGGER.error("Error while getting metrics info for repository: ", e); + logger.error("Error while getting metrics info for repository: ", e); throw new RepositoryServiceException("General error", RepositoryServiceException.ErrorCode.GENERAL_ERROR); } } @@ -925,31 +891,30 @@ public class RepositoryServiceImpl implements RepositoryService { public Map getListLatestUpdate(String mode) throws JSONException { Map dates = new HashMap<>(); if (mode.equals("repository")) { - dates.put("opendoar", converter.toString(getRepositoryInterface("openaire____::opendoar").get(0).getLastCollectionDate())); - dates.put("fairsharing", converter.toString(getRepositoryInterface("openaire____::fairsharing").get(0).getLastCollectionDate())); + dates.put("opendoar", DateUtils.toString(getRepositoryInterface("openaire____::opendoar").get(0).getLastCollectionDate())); + dates.put("fairsharing", DateUtils.toString(getRepositoryInterface("openaire____::fairsharing").get(0).getLastCollectionDate())); // create re3data last collection date // dates.put("re3data", converter.toString(getRepositoryInterface("openaire____::re3data").get(1).getLastCollectionDate())); List re3interfaces = getRepositoryInterface("openaire____::re3data"); String re3Date = null; for (RepositoryInterface interf : re3interfaces) { if (interf.getLastCollectionDate() != null) { - re3Date = converter.toString(interf.getLastCollectionDate()); + re3Date = DateUtils.toString(interf.getLastCollectionDate()); break; } } dates.put("re3data", re3Date); return dates; - } - else if (mode.equals("cris")) - return Collections.singletonMap("lastCollectionDate", converter.toString(getRepositoryInterface("eurocrisdris::dris").get(0).getLastCollectionDate())); + } else if (mode.equals("cris")) + return Collections.singletonMap("lastCollectionDate", DateUtils.toString(getRepositoryInterface("eurocrisdris::dris").get(0).getLastCollectionDate())); else if (mode.equals("opendoar")) // TODO: remove this and else clause - return Collections.singletonMap("lastCollectionDate", converter.toString(getRepositoryInterface("openaire____::" + mode).get(0).getLastCollectionDate())); + return Collections.singletonMap("lastCollectionDate", DateUtils.toString(getRepositoryInterface("openaire____::" + mode).get(0).getLastCollectionDate())); else /* * first api of re3data has null value on collection date * */ - return Collections.singletonMap("lastCollectionDate", converter.toString(getRepositoryInterface("openaire____::" + mode).get(0).getLastCollectionDate())); + return Collections.singletonMap("lastCollectionDate", DateUtils.toString(getRepositoryInterface("openaire____::" + mode).get(0).getLastCollectionDate())); } private void updateValidationSet(String repositoryId, String repositoryInterfaceId, String validationSet) throws Exception { @@ -1014,12 +979,6 @@ public class RepositoryServiceImpl implements RepositoryService { return null; } - private UriComponents getAggregationHistory(String repoId) { - return UriComponentsBuilder - .fromHttpUrl(baseAddress + "/ds/aggregationhistory/") - .path(repoId) - .build().expand(repoId).encode(); - } private UriComponents searchDatasource(String page, String size) { @@ -1031,7 +990,7 @@ public class RepositoryServiceImpl implements RepositoryService { .build().expand(page, size).encode(); } - private UriComponents searchSnipperDatasource(String page, String size) { + private UriComponents searchSnippetDatasource(String page, String size) { return UriComponentsBuilder .fromHttpUrl(baseAddress + "/ds/searchsnippet/") diff --git a/src/main/java/eu/dnetlib/repo/manager/service/StatsServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/StatsServiceImpl.java index 235e9e3..359fb42 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/StatsServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/StatsServiceImpl.java @@ -1,17 +1,27 @@ package eu.dnetlib.repo.manager.service; -import eu.dnetlib.repo.manager.service.customHystrixCommands.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponents; +import org.springframework.web.util.UriComponentsBuilder; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.Objects; @Service("statsService") public class StatsServiceImpl implements StatsService { + private static final Logger logger = LoggerFactory.getLogger(StatsServiceImpl.class); + @Autowired RestTemplate restTemplate; @@ -24,27 +34,215 @@ public class StatsServiceImpl implements StatsService { @Override public Map getStatistics() { - - UsageStatsTotalHystrixCommand usageStatsTotalHystrixCommand = new UsageStatsTotalHystrixCommand(usagestatsEvents,restTemplate); - DataRepositoriesHystrixCommand dataRepositoriesHystrixCommand = new DataRepositoriesHystrixCommand(baseAddress,restTemplate); - AggregatorsHystrixCommand aggregatorsHystrixCommand = new AggregatorsHystrixCommand(baseAddress,restTemplate); - LiteratureHystrixCommand literatureHystrixCommand = new LiteratureHystrixCommand(baseAddress,restTemplate); - JournalHystrixCommand journalHystrixCommand = new JournalHystrixCommand(baseAddress,restTemplate); - PublicationHystrixCommand publicationHystrixCommand = new PublicationHystrixCommand(baseAddress,restTemplate); - DatasetsHystrixCommand datasetsHystrixCommand = new DatasetsHystrixCommand(baseAddress,restTemplate); - SoftwareHystrixCommand softwareHystrixCommand = new SoftwareHystrixCommand(baseAddress,restTemplate); - LastYearUsageStatsHystrixCommand lastYearUsageStatsHystrixCommand = new LastYearUsageStatsHystrixCommand(usagestatsBaseAddress,restTemplate); - Map stats = new HashMap<>(); - stats.put("aggregators",aggregatorsHystrixCommand.execute()); - stats.put("dataRepositories",dataRepositoriesHystrixCommand.execute()); - stats.put("literature",literatureHystrixCommand.execute()); - stats.put("journal",journalHystrixCommand.execute()); - stats.put("publications",publicationHystrixCommand.execute()); - stats.put("datasets",datasetsHystrixCommand.execute()); - stats.put("software",softwareHystrixCommand.execute()); - stats.put("lastYearUsagestats", lastYearUsageStatsHystrixCommand.execute()); - stats.put("usagestats",usageStatsTotalHystrixCommand.execute()); + stats.put("aggregators", this.getAggregatorsStats()); + stats.put("dataRepositories", this.getDataRepositoriesMetadata()); + stats.put("literature", this.getLiteratureStats()); + stats.put("journal", this.getJournalsStats()); + stats.put("publications", this.getPublicationStats()); + stats.put("datasets", this.getDatasetsStats()); + stats.put("software", this.getSoftwareStats()); + stats.put("lastYearUsagestats", this.getLastYearUsageStats()); + stats.put("usagestats", this.getUsageStatsTotal()); return stats; } + + + private String getAggregatorsStats() + { + String url = baseAddress + "/resources" + + "?query= " + + " oaftype exact datasource and " + + " ( datasourcetypename exact Institutional Repository Aggregator " + + " or datasourcetypename exact Publication Repository Aggregator )"; + + UriComponents uriComponents = UriComponentsBuilder + .fromHttpUrl(url) + .queryParam("page", 0) + .queryParam("size", 0) + .queryParam("format", "json") + .build().encode(); + + try { + ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); + Map metadata = (Map) ((Map) Objects.requireNonNull(rs.getBody())).get("meta"); + 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 getDataRepositoriesMetadata() + { + String url = baseAddress + "/resources" + + "?query= " + + " oaftype exact datasource and " + + " datasourcetypename exact Data Repository "; + + return getMetadataTotals(url); + } + + + private String getLiteratureStats() + { + String url = baseAddress + "/resources" + + "?query= " + + " oaftype exact datasource and " + + " ( datasourcetypename exact Institutional Repository " + + " or datasourcetypename exact Publication Repository )"; + + return getMetadataTotals(url); + } + + + private String getJournalsStats() + { + String url = baseAddress + "/resources" + + "?query= " + + " oaftype exact datasource and " + + " datasourcetypename exact Journal"; + + return getMetadataTotals(url); + } + + + private String getPublicationStats() + { + String url = baseAddress + "/publications/count"; + + return getCommonStats(url); + } + + + private String getDatasetsStats() + { + String url = baseAddress + "/datasets/count"; + + return getCommonStats(url); + } + + + private String getSoftwareStats() + { + String url = baseAddress + "/software/count"; + + return getCommonStats(url); + } + + + private Map getLastYearUsageStats() + { + UriComponents uriComponents = UriComponentsBuilder + .fromHttpUrl(usagestatsBaseAddress + "/totals") + .build().encode(); + + try { + ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); + + List yearly_stats = (List) ((Map) Objects.requireNonNull(rs.getBody())).get("yearly_stats"); + Map lastYear = (Map) yearly_stats.get(yearly_stats.size() - 2); + Integer downloads = (Integer) lastYear.get("downloads"); + Integer views = (Integer) lastYear.get("views"); + Integer year = (Integer) lastYear.get("year"); + + Map usagestats = new HashMap<>(); + usagestats.put("number", String.valueOf(downloads + views)); + usagestats.put("year", year); + return usagestats; + } catch ( RestClientException rce ) { + logger.error(rce.getMessage()); + return null; + } catch ( Exception e ) { + logger.error("", e); + return null; + } + } + + + private Integer getUsageStatsTotal() + { + UriComponents uriComponents = UriComponentsBuilder + .fromHttpUrl(usagestatsEvents) + .build().encode(); + + try { + ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); + Map statsTotals = (Map) ((Map) Objects.requireNonNull(rs.getBody())).get("totals"); + if ( statsTotals == null ) { + logger.error("The statsTotals was null!"); + return null; + } + + // TODO - Will we ever need the following lines? + // String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); + // JSONObject resultSet = new JSONObject(rs); + // JSONObject totals = resultSet.getJSONObject("totals"); + + 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 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 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 ) { + logger.error(rce.getMessage()); + return null; + } catch ( Exception e ) { + logger.error("", e); + return null; + } + } + } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/SushiliteServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/SushiliteServiceImpl.java index 90bb074..002c867 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/SushiliteServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/SushiliteServiceImpl.java @@ -1,14 +1,19 @@ 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.ReportResponseWrapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.ParameterizedTypeReference; import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; @@ -22,7 +27,7 @@ public class SushiliteServiceImpl implements SushiliteService { @Value("${services.provide.usagestats.sushiliteEndpoint}") private String usagestatsSushiliteEndpoint; - private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(SushiliteServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(SushiliteServiceImpl.class); @Override @@ -40,65 +45,79 @@ public class SushiliteServiceImpl implements SushiliteService { String Granularity, String Pretty) { - //build the uri params - UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usagestatsSushiliteEndpoint + "GetReport/") - .queryParam("Report", Report) - .queryParam("Release", Release) - .queryParam("RequestorID", RequestorID) - .queryParam("BeginDate", BeginDate) - .queryParam("EndDate", EndDate) - .queryParam("RepositoryIdentifier", RepositoryIdentifier) - .queryParam("ItemIdentifier", ItemIdentifier) - .queryParam("ItemDataType", ItemDataType) - .queryParam("Granularity", Granularity) - .queryParam("Pretty", Pretty); + //build the uri params + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(this.usagestatsSushiliteEndpoint + "GetReport/") + .queryParam("Report", Report) + .queryParam("Release", Release) + .queryParam("RequestorID", RequestorID) + .queryParam("BeginDate", BeginDate) + .queryParam("EndDate", EndDate) + .queryParam("RepositoryIdentifier", RepositoryIdentifier) + .queryParam("ItemIdentifier", ItemIdentifier) + .queryParam("ItemDataType", ItemDataType) + .queryParam("Granularity", Granularity) + .queryParam("Pretty", Pretty); - //create new template engine - RestTemplate template = new RestTemplate(); - template.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); + //create new restTemplate engine + RestTemplate restTemplate = new RestTemplate(); + restTemplate.getMessageConverters().add(new MappingJackson2HttpMessageConverter()); - ResponseEntity resp; + ResponseEntity resp; + try { //communicate with endpoint - resp = template.exchange( + resp = restTemplate.exchange( builder.build().encode().toUri(), HttpMethod.GET, null, - new ParameterizedTypeReference() { - }); + new ParameterizedTypeReference() {}); + } catch (RestClientException rce) { + logger.error("", rce); + return null; + } - // check remote api's response - System.out.println("sushi responded status " + resp.getStatusCode()); + // check remote api's response + 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 - List requestedItemList = new ArrayList<>(); - if (resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems() != null) { - try { - int totalItems = resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems().size(); - int size = Integer.parseInt(pageSize); - int offset = Integer.parseInt(page)*size; + ReportResponseWrapper reportResponseWrapper = resp.getBody(); + if ( reportResponseWrapper == null ) { + logger.error("The \"reportResponseWrapper\" for sushi was null!"); + return null; + } - if (offset < totalItems ) { - int upperIndex = offset+size; - if (upperIndex > totalItems) { - upperIndex = totalItems; - } - requestedItemList = resp.getBody().getReportResponse().getReportWrapper().getReport().getCustomer().getReportItems().subList(offset,upperIndex); + // get the items corresponding to the requested page + List requestedItemList = new ArrayList<>(); + + Customer customer = reportResponseWrapper.getReportResponse().getReportWrapper().getReport().getCustomer(); + List allReportItems = customer.getReportItems(); + + 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) { - LOGGER.debug("Exception on getReportResults - trying to cast strings to integers", e); - //emailUtils.reportException(e); - throw e; + requestedItemList = allReportItems.subList(offset, upperIndex); } - + } catch (NumberFormatException e) { + logger.debug("Exception on getReportResults - trying to cast strings to integers", e); + //emailUtils.reportException(e); + throw e; } + } - ReportResponseWrapper newReportResponse = resp.getBody(); - newReportResponse.getReportResponse().getReportWrapper().getReport().getCustomer().setReportItems(requestedItemList); + customer.setReportItems(requestedItemList); // Setting the reportItems to the "customer"-reference, updates the "reportResponseWrapper" object. - - return newReportResponse; + return reportResponseWrapper; } } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/UserServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/UserServiceImpl.java index 0d3a6fd..e97c64a 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/UserServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/UserServiceImpl.java @@ -1,7 +1,9 @@ package eu.dnetlib.repo.manager.service; import org.mitre.openid.connect.model.OIDCAuthenticationToken; -import org.springframework.beans.factory.annotation.Value; +import org.mitre.openid.connect.model.UserInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.GrantedAuthority; @@ -16,21 +18,24 @@ import java.util.stream.Collectors; @Service("userService") public class UserServiceImpl implements UserService { - private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger - .getLogger(UserServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class); @Override public ResponseEntity login() { OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); - LOGGER.debug("User authentication : " + authentication); + logger.debug("User authentication : " + authentication); Map body = new HashMap<>(); body.put("sub",authentication.getSub()); - if(authentication.getUserInfo().getName() == null || authentication.getUserInfo().getName().equals("")) - body.put("name",authentication.getUserInfo().getGivenName() + " " + authentication.getUserInfo().getFamilyName()); - else - body.put("name",authentication.getUserInfo().getName()); - body.put("email",authentication.getUserInfo().getEmail()); + UserInfo userInfo = authentication.getUserInfo(); + String userName = userInfo.getName(); + + if ( userName == null || userName.isEmpty() ) + body.put("name", userInfo.getGivenName() + " " + userInfo.getFamilyName()); + else + body.put("name", userName); + + body.put("email",userInfo.getEmail()); List roles = authentication.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()); body.put("role",roles); diff --git a/src/main/java/eu/dnetlib/repo/manager/service/ValidatorService.java b/src/main/java/eu/dnetlib/repo/manager/service/ValidatorService.java index 5541e1e..f8e656f 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/ValidatorService.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/ValidatorService.java @@ -5,8 +5,8 @@ import eu.dnetlib.domain.functionality.validator.JobForValidation; import eu.dnetlib.domain.functionality.validator.RuleSet; import eu.dnetlib.domain.functionality.validator.StoredJob; import eu.dnetlib.repo.manager.domain.InterfaceInformation; -import eu.dnetlib.repo.manager.exception.ValidationServiceException; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; +import eu.dnetlib.repo.manager.exception.ValidationServiceException; import org.json.JSONException; import org.springframework.http.ResponseEntity; @@ -42,4 +42,6 @@ public interface ValidatorService { InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException; List getJobsSummary(String repoId, int limit) throws ValidatorServiceException, ResourceNotFoundException, JSONException; + + void onComplete(String repoId, String interfaceId, String jobId, String issuerEmail, boolean isUpdate, boolean isSuccess, int scoreUsage, int scoreContent) throws Exception; } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/ValidatorServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/ValidatorServiceImpl.java index 699a4ad..908090c 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/ValidatorServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/ValidatorServiceImpl.java @@ -1,19 +1,21 @@ package eu.dnetlib.repo.manager.service; import eu.dnetlib.api.functionality.ValidatorServiceException; -import eu.dnetlib.repo.manager.domain.RepositoryInterface; -import eu.dnetlib.domain.functionality.validator.*; -import eu.dnetlib.repo.manager.domain.Constants; -import eu.dnetlib.repo.manager.domain.InterfaceInformation; +import eu.dnetlib.domain.functionality.validator.JobForValidation; +import eu.dnetlib.domain.functionality.validator.Rule; +import eu.dnetlib.domain.functionality.validator.RuleSet; +import eu.dnetlib.domain.functionality.validator.StoredJob; +import eu.dnetlib.repo.manager.domain.*; import eu.dnetlib.repo.manager.exception.ValidationServiceException; import eu.dnetlib.repo.manager.utils.CrisValidatorUtils; import eu.dnetlib.repo.manager.utils.OaiTools; import gr.uoa.di.driver.util.ServiceLocator; -import org.apache.log4j.Logger; import org.eurocris.openaire.cris.validator.model.Job; import org.eurocris.openaire.cris.validator.service.JobExecutor; import org.eurocris.openaire.cris.validator.service.MapJobDao; import org.json.JSONException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -25,6 +27,7 @@ import javax.annotation.PostConstruct; import javax.annotation.Resource; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Pattern; import java.util.stream.Collectors; @@ -52,10 +55,9 @@ public class ValidatorServiceImpl implements ValidatorService { this.validatorServiceLocator = validatorServiceLocator; } - private Map> rulesetMap = new ConcurrentHashMap>(); + private final Map> rulesetMap = new ConcurrentHashMap>(); - private static final Logger LOGGER = Logger - .getLogger(ValidatorServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(ValidatorServiceImpl.class); @Autowired private EmailUtils emailUtils; @@ -66,20 +68,30 @@ public class ValidatorServiceImpl implements ValidatorService { @Autowired private MapJobDao crisJobs; + @Autowired + InterfaceComplianceService interfaceComplianceService; + + + 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 private void loadRules(){ - LOGGER.debug("PostConstruct method! Load rules!"); + logger.debug("PostConstruct method! Load rules!"); try { for (RuleSet ruleSet : getValidationService().getRuleSets()) { if (ruleSet.getVisibility() != null && ruleSet.getVisibility().contains("development")) { 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; - 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; - else if (ruleSet.getGuidelinesAcronym().matches("^openaire[1-9].0_cris$")) + else if ( OPENAIRE_CRIS_REGEX.matcher(guidelinesAcronym).matches() ) key = Constants.VALIDATION_MODE_CRIS; - else if (ruleSet.getGuidelinesAcronym().matches(".*fair$")) + else if ( FAIR_REGEX.matcher(guidelinesAcronym).matches() ) key = Constants.VALIDATION_MODE_FAIR; if (rulesetMap.containsKey(key)) @@ -121,7 +133,7 @@ public class ValidatorServiceImpl implements ValidatorService { ///////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////// } catch (ValidatorServiceException e) { - LOGGER.error(e); + logger.error(e.getMessage(), e); } } @@ -129,19 +141,15 @@ public class ValidatorServiceImpl implements ValidatorService { @Override @PreAuthorize("hasAuthority('REGISTERED_USER')") public JobForValidation submitJobForValidation(JobForValidation jobForValidation) throws ValidatorServiceException { - LOGGER.debug("Submit job for validation with id : " + jobForValidation.getDatasourceId()); + logger.debug("Submit job for validation with id : {}", jobForValidation.getDatasourceId()); try { - try { - emailUtils.sendSubmitJobForValidationEmail(SecurityContextHolder.getContext().getAuthentication(), jobForValidation); - } catch (Exception e) { - LOGGER.error("Error sending email ", e); - } + emailUtils.sendSubmitJobForValidationEmail(SecurityContextHolder.getContext().getAuthentication(), jobForValidation); ///////////////////////////////////////////////////////////////////////////////////////// // FIXME: this is a hack for CRIS Jan Dvorak Validator, should be implemented properly // ///////////////////////////////////////////////////////////////////////////////////////// if (jobForValidation.getSelectedContentRules()!=null && jobForValidation.getSelectedContentRules().size() == 1 && jobForValidation.getSelectedContentRules().contains(-1000) - || jobForValidation.getDesiredCompatibilityLevel().matches("openaire-cris_1.1")) { + || jobForValidation.getDesiredCompatibilityLevel().equals("openaire-cris_1.1")) { crisValidatorExecutor.submit(jobForValidation.getBaseUrl(), jobForValidation.getUserEmail()); } else { this.getValidationService().submitValidationJob(jobForValidation); @@ -151,7 +159,7 @@ public class ValidatorServiceImpl implements ValidatorService { // this.getValidationService().submitValidationJob(jobForValidation); } catch (Exception e) { // FIXME: replaced exception with log // throw new ValidatorServiceException(e); - LOGGER.error(e); + logger.error(e.getMessage(), e); } return jobForValidation; @@ -160,7 +168,7 @@ public class ValidatorServiceImpl implements ValidatorService { @Override public ResponseEntity reSubmitJobForValidation(String email, String jobId) throws JSONException, ValidatorServiceException { - LOGGER.debug("Resubmit validation job with id : " + jobId); + logger.debug("Resubmit validation job with id : {}", jobId); StoredJob job = monitorApi.getJobSummary(jobId, "all"); Set contentRules = new HashSet(); Set usageRules = new HashSet(); @@ -195,20 +203,20 @@ public class ValidatorServiceImpl implements ValidatorService { @Override public List getRuleSets(String mode) { - LOGGER.info("Getting rulesets for mode: " + mode); + logger.info("Getting ruleSets for mode: {}", mode); return rulesetMap.get(mode); } @Override public List getSetsOfRepository(String url) { - LOGGER.debug("Getting sets of repository with url : " + url); + logger.debug("Getting sets of repository with url : {}", url); List sets = null; try { sets = OaiTools.getSetsOfRepo(url); } catch (Exception e) { - LOGGER.error("Exception on getSetsOfRepository" , e); + logger.error("Exception on getSetsOfRepository", e); } return sets; @@ -216,18 +224,18 @@ public class ValidatorServiceImpl implements ValidatorService { @Override public boolean identifyRepo(String url) { - LOGGER.debug("Identify repository with url : " + url); + logger.debug("Identify repository with url : {}", url); try { return OaiTools.identifyRepository(url); } catch (Exception e) { - LOGGER.error("Error while identifying repository with url: " + url, e); + logger.error("Error while identifying repository with url: " + url, e); return false; } } @Override public RuleSet getRuleSet(String acronym) { - LOGGER.debug("Getting ruleset with acronym : " + acronym); + logger.debug("Getting ruleset with acronym : {}", acronym); RuleSet ruleSet = null; try { for (List ruleSets : this.rulesetMap.values()) { @@ -239,7 +247,7 @@ public class ValidatorServiceImpl implements ValidatorService { } return ruleSet; } catch (Exception e) { - LOGGER.error("Error getting ruleset", e); + logger.error("Error getting ruleset", e); return null; } } @@ -278,7 +286,7 @@ public class ValidatorServiceImpl implements ValidatorService { @Override public InterfaceInformation getInterfaceInformation(String baseUrl) throws ValidationServiceException { try { - LOGGER.debug("Getting interface information with url: " + baseUrl); + logger.debug("Getting interface information with url: {}", baseUrl); InterfaceInformation interfaceInformation = new InterfaceInformation(); interfaceInformation.setIdentified(this.identifyRepo(baseUrl)); if (interfaceInformation.isIdentified()) @@ -286,7 +294,7 @@ public class ValidatorServiceImpl implements ValidatorService { return interfaceInformation; } catch (Exception e) { - LOGGER.error("Error getting interface information with url: " + baseUrl, e); + logger.error("Error getting interface information with url: " + baseUrl, e); throw new ValidationServiceException("login.generalError", ValidationServiceException.ErrorCode.GENERAL_ERROR); } } @@ -300,4 +308,19 @@ public class ValidatorServiceImpl implements ValidatorService { .collect(Collectors.toList()), limit); } + @Override + public void onComplete(String repoId, String interfaceId, String jobId, String issuerEmail, boolean isUpdate, boolean isSuccess, int scoreUsage, int scoreContent) throws Exception { + emailUtils.sendUponJobCompletion(repoId,interfaceId,scoreUsage,scoreContent,isSuccess,isUpdate,issuerEmail, jobId); + InterfaceComplianceRequestId requestId = InterfaceComplianceRequestId.of(repoId, interfaceId); + Optional request = interfaceComplianceService.getById(requestId); + if (request.isPresent()) { + logger.info("Changing compatibility level. Request: {}", request); + + if (scoreContent > 50) { + repositoryService.updateInterfaceCompliance(repoId, interfaceId, request.get().getDesiredCompatibilityLevel()); + } + interfaceComplianceService.delete(requestId); + } + } + } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/aai/registry/RegistryCalls.java b/src/main/java/eu/dnetlib/repo/manager/service/aai/registry/RegistryCalls.java index f71ffb7..dc7c5f1 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/aai/registry/RegistryCalls.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/aai/registry/RegistryCalls.java @@ -3,17 +3,21 @@ package eu.dnetlib.repo.manager.service.aai.registry; import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; +import com.nimbusds.jose.util.StandardCharset; import eu.dnetlib.repo.manager.domain.dto.Role; import eu.dnetlib.repo.manager.domain.dto.User; import eu.dnetlib.repo.manager.service.aai.registry.utils.RegistryUtils; import eu.dnetlib.repo.manager.utils.HttpUtils; -import org.apache.log4j.Logger; import org.mitre.openid.connect.model.OIDCAuthenticationToken; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; +import javax.validation.constraints.NotNull; +import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; @@ -23,7 +27,7 @@ import java.util.Map; @Service public class RegistryCalls implements AaiRegistryService { - private static final Logger logger = Logger.getLogger(RegistryCalls.class); + private static final Logger logger = LoggerFactory.getLogger(RegistryCalls.class); private final String coid; public final HttpUtils httpUtils; @@ -98,11 +102,7 @@ public class RegistryCalls implements AaiRegistryService { try { OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); String sub = authentication.getUserInfo().getSub(); - Map params = new HashMap<>(); - 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; + return getCoPersonIdByIdentifier(sub); } catch (Exception e) { logger.error("Get User info: An error occurred ", e); return null; @@ -122,7 +122,12 @@ public class RegistryCalls implements AaiRegistryService { Map params = new HashMap<>(); params.put("coid", coid); if (name != null) { - params.put("name", URLEncoder.encode(name).toLowerCase()); + try { + params.put("name", URLEncoder.encode(name, StandardCharset.UTF_8.name()).toLowerCase()); + } catch (UnsupportedEncodingException uee) { + logger.error(uee.getMessage()); + return new JsonArray(); + } } JsonElement response = httpUtils.get("cous.json", params); return (response != null) ? response.getAsJsonObject().get("Cous").getAsJsonArray() : new JsonArray(); @@ -165,15 +170,18 @@ public class RegistryCalls implements AaiRegistryService { @Override public JsonArray getRolesWithStatus(Integer coPersonId, RoleStatus status) { JsonArray roles = getRoles(coPersonId); - if (status == null) { - return roles; + if (roles == null) { + roles = new JsonArray(); } JsonArray activeRoles = new JsonArray(); - for (JsonElement role : roles) { - if (role.getAsJsonObject().get("Status").getAsString().equalsIgnoreCase(status.toString())) { - activeRoles.add(role); + if (status != null) { + for (JsonElement role : roles) { + if (role.getAsJsonObject().get("Status").getAsString().equalsIgnoreCase(status.toString())) { + activeRoles.add(role); + } } } + assert activeRoles != null; return activeRoles; } @@ -296,8 +304,11 @@ public class RegistryCalls implements AaiRegistryService { JsonArray infos = getUserEmailByCouId(couId, false); infos.forEach(info -> { + JsonObject jsonInfo = info.getAsJsonObject(); + User user = new User(); - user.setEmail(info.getAsJsonObject().get("email").getAsString()); + user.setEmail(jsonInfo.get("email").getAsString()); + // TODO: should add firstname and lastname and sub of user users.add(user); }); @@ -315,9 +326,10 @@ public class RegistryCalls implements AaiRegistryService { JsonArray infos = (response != null) ? response.getAsJsonObject().get("Names").getAsJsonArray() : new JsonArray(); JsonArray names = new JsonArray(); infos.forEach(info -> { + JsonObject jsonInfo = info.getAsJsonObject(); JsonObject user = new JsonObject(); - user.addProperty("name", info.getAsJsonObject().get("Given").getAsString() + " " + info.getAsJsonObject().get("Family").getAsString()); - user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString()); + user.addProperty("name", jsonInfo.get("Given").getAsString() + " " + jsonInfo.get("Family").getAsString()); + user.addProperty("memberSince", jsonInfo.get("Created").getAsString()); names.add(user); }); return names; @@ -334,9 +346,10 @@ public class RegistryCalls implements AaiRegistryService { JsonArray infos = (response != null) ? response.getAsJsonObject().get("Identifiers").getAsJsonArray() : new JsonArray(); JsonArray emails = new JsonArray(); infos.forEach(info -> { + JsonObject jsonInfo = info.getAsJsonObject(); JsonObject user = new JsonObject(); - user.addProperty("id", info.getAsJsonObject().get("Identifier").getAsString()); - user.addProperty("memberSince", info.getAsJsonObject().get("Created").getAsString()); + user.addProperty("id", jsonInfo.get("Identifier").getAsString()); + user.addProperty("memberSince", jsonInfo.get("Created").getAsString()); emails.add(user); }); return emails; @@ -379,7 +392,11 @@ public class RegistryCalls implements AaiRegistryService { params.put("copersonid", coPersonId.toString()); JsonElement response = httpUtils.get("names.json", params); 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 @@ -426,8 +443,9 @@ public class RegistryCalls implements AaiRegistryService { int total = couIds.size(); for (JsonElement cou : cous) { if (count < total) { - if (idNameMap.containsKey(cou.getAsJsonObject().get("Id").getAsInt())) { - idNameMap.put(cou.getAsJsonObject().get("Id").getAsInt(), cou.getAsJsonObject().get("Name").getAsString()); + JsonObject jsonCou = cou.getAsJsonObject(); + if (idNameMap.containsKey(jsonCou.get("Id").getAsInt())) { + idNameMap.put(jsonCou.get("Id").getAsInt(), jsonCou.get("Name").getAsString()); count++; } } else { diff --git a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/AggregatorsHystrixCommand.java b/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/AggregatorsHystrixCommand.java deleted file mode 100644 index 653fb72..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/AggregatorsHystrixCommand.java +++ /dev/null @@ -1,48 +0,0 @@ -package eu.dnetlib.repo.manager.service.customHystrixCommands; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.Map; - -public class AggregatorsHystrixCommand extends HystrixCommand { - - RestTemplate restTemplate; - String baseAddress; - - public AggregatorsHystrixCommand(String baseAddress, RestTemplate restTemplate) { - super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); - this.baseAddress = baseAddress; - this.restTemplate = restTemplate; - } - - @Override - protected String run() throws Exception { - String url = baseAddress + "/resources" + - "?query= " + - " oaftype exact datasource and " + - " ( datasourcetypename exact Institutional Repository Aggregator " + - " or datasourcetypename exact Publication Repository Aggregator )"; - - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(url) - .queryParam("page", 0) - .queryParam("size", 0) - .queryParam("format", "json") - .build().encode(); - - ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); - Map metadata = (Map) ((Map) rs.getBody()).get("meta"); - return String.valueOf(metadata.get("total")); - } - - @Override - protected String getFallback() { - return null; - } -} \ No newline at end of file diff --git a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/DataRepositoriesHystrixCommand.java b/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/DataRepositoriesHystrixCommand.java deleted file mode 100644 index 8bdd0ca..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/DataRepositoriesHystrixCommand.java +++ /dev/null @@ -1,47 +0,0 @@ -package eu.dnetlib.repo.manager.service.customHystrixCommands; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.Map; - -public class DataRepositoriesHystrixCommand extends HystrixCommand { - - RestTemplate restTemplate ; - private String baseAddress; - - public DataRepositoriesHystrixCommand(String baseAddress,RestTemplate restTemplate) { - super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); - this.baseAddress = baseAddress; - this.restTemplate = restTemplate; - } - - @Override - protected String run() { - String url = baseAddress+"/resources" + - "?query= " + - " oaftype exact datasource and " + - " datasourcetypename exact Data Repository "; - - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(url) - .queryParam("page",0) - .queryParam("size",0) - .queryParam("format","json") - .build().encode(); - - ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class); - Map metadata = (Map) ((Map)rs.getBody()).get("meta"); - return String.valueOf(metadata.get("total")); - } - - @Override - protected String getFallback() { - return null; - } -} \ No newline at end of file diff --git a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/DatasetsHystrixCommand.java b/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/DatasetsHystrixCommand.java deleted file mode 100644 index 7353716..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/DatasetsHystrixCommand.java +++ /dev/null @@ -1,44 +0,0 @@ -package eu.dnetlib.repo.manager.service.customHystrixCommands; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.Map; - -public class DatasetsHystrixCommand extends HystrixCommand { - - RestTemplate restTemplate; - String baseAddress; - - public DatasetsHystrixCommand(String baseAddress,RestTemplate restTemplate) { - super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); - this.restTemplate = restTemplate; - this.baseAddress = baseAddress; - } - - @Override - protected String run() throws Exception { - String url = baseAddress+"/datasets/count"; - - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(url) - .queryParam("page", 0) - .queryParam("size", 0) - .queryParam("format", "json") - .build().encode(); - - ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); - Map metadata = (Map) (rs.getBody()); - return String.valueOf(metadata.get("total")); - } - - @Override - protected String getFallback() { - return null; - } -} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/JournalHystrixCommand.java b/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/JournalHystrixCommand.java deleted file mode 100644 index 0ba0bba..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/JournalHystrixCommand.java +++ /dev/null @@ -1,48 +0,0 @@ -package eu.dnetlib.repo.manager.service.customHystrixCommands; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.Map; - -public class JournalHystrixCommand extends HystrixCommand { - - RestTemplate restTemplate; - private String baseAddress; - - public JournalHystrixCommand(String baseAddress, RestTemplate restTemplate) { - super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); - this.baseAddress = baseAddress; - this.restTemplate = restTemplate; - } - - @Override - protected String run() throws Exception { - String url = baseAddress+"/resources"+ - "?query= " + - " oaftype exact datasource and " + - " datasourcetypename exact Journal"; - - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(url) - .queryParam("page", 0) - .queryParam("size", 0) - .queryParam("format", "json") - .build().encode(); - - ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); - Map metadata = (Map) ((Map) rs.getBody()).get("meta"); - return String.valueOf(metadata.get("total")); - } - - @Override - protected String getFallback() { - return null; - } - -} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/LastYearUsageStatsHystrixCommand.java b/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/LastYearUsageStatsHystrixCommand.java deleted file mode 100644 index eb9561a..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/LastYearUsageStatsHystrixCommand.java +++ /dev/null @@ -1,52 +0,0 @@ -package eu.dnetlib.repo.manager.service.customHystrixCommands; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class LastYearUsageStatsHystrixCommand extends HystrixCommand { - - RestTemplate restTemplate; - String usagestatsBaseAddress; - - public LastYearUsageStatsHystrixCommand(String usagestatsBaseAddress,RestTemplate restTemplate) { - super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); - this.usagestatsBaseAddress = usagestatsBaseAddress; - this.restTemplate = restTemplate; - } - - @Override - protected Map run() throws Exception { - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(usagestatsBaseAddress + "/totals") - .build().encode(); - - ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class); - - List yearly_stats = (List) ((Map)rs.getBody()).get("yearly_stats"); - Map lastYear = (Map) yearly_stats.get(yearly_stats.size()-1); - Integer downloads = (Integer) lastYear.get("downloads"); - Integer views = (Integer) lastYear.get("views"); - Integer year = (Integer) lastYear.get("year"); - - Map usagestats = new HashMap<>(); - usagestats.put("number",String.valueOf(downloads+views)); - usagestats.put("year",year); - - return usagestats; - } - - @Override - protected Map getFallback() { - return null; - } - -} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/LiteratureHystrixCommand.java b/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/LiteratureHystrixCommand.java deleted file mode 100644 index 2242e6f..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/LiteratureHystrixCommand.java +++ /dev/null @@ -1,49 +0,0 @@ -package eu.dnetlib.repo.manager.service.customHystrixCommands; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.Map; - -public class LiteratureHystrixCommand extends HystrixCommand { - - RestTemplate restTemplate; - private String baseAddress; - - public LiteratureHystrixCommand(String baseAddress , RestTemplate restTemplate) { - super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); - this.baseAddress = baseAddress; - this.restTemplate = restTemplate; - } - - @Override - protected String run() throws Exception { - String url = baseAddress+"/resources"+ - "?query= " + - " oaftype exact datasource and " + - " ( datasourcetypename exact Institutional Repository " + - " or datasourcetypename exact Publication Repository )"; - - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(url) - .queryParam("page", 0) - .queryParam("size", 0) - .queryParam("format", "json") - .build().encode(); - - ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); - Map metadata = (Map) ((Map) rs.getBody()).get("meta"); - return String.valueOf(metadata.get("total")); - } - - @Override - protected String getFallback() { - return null; - } - -} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/PublicationHystrixCommand.java b/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/PublicationHystrixCommand.java deleted file mode 100644 index 094198a..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/PublicationHystrixCommand.java +++ /dev/null @@ -1,45 +0,0 @@ -package eu.dnetlib.repo.manager.service.customHystrixCommands; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.Map; - -public class PublicationHystrixCommand extends HystrixCommand { - - RestTemplate restTemplate; - private String baseAddress; - - public PublicationHystrixCommand(String baseAddress, RestTemplate restTemplate) { - super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); - this.baseAddress = baseAddress; - this.restTemplate = restTemplate; - } - - @Override - protected String run() throws Exception { - String url = baseAddress + "/publications/count"; - - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(url) - .queryParam("page", 0) - .queryParam("size", 0) - .queryParam("format", "json") - .build().encode(); - - ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); - Map metadata = (Map) (rs.getBody()); - return String.valueOf(metadata.get("total")); - } - - @Override - protected String getFallback() { - return null; - } - -} \ No newline at end of file diff --git a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/SoftwareHystrixCommand.java b/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/SoftwareHystrixCommand.java deleted file mode 100644 index 09b9f07..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/SoftwareHystrixCommand.java +++ /dev/null @@ -1,44 +0,0 @@ -package eu.dnetlib.repo.manager.service.customHystrixCommands; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.Map; - -public class SoftwareHystrixCommand extends HystrixCommand { - - String baseAddress; - RestTemplate restTemplate; - - public SoftwareHystrixCommand(String baseAddress, RestTemplate restTemplate) { - super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); - this.baseAddress = baseAddress; - this.restTemplate = restTemplate; - } - - @Override - protected String run() { - String url = baseAddress + "/software/count"; - - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(url) - .queryParam("page", 0) - .queryParam("size", 0) - .queryParam("format", "json") - .build().encode(); - - ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET, null, Map.class); - Map metadata = (Map) (rs.getBody()); - return String.valueOf(metadata.get("total")); - } - - @Override - protected String getFallback() { - return null; - } -} diff --git a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/UsageStatsTotalHystrixCommand.java b/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/UsageStatsTotalHystrixCommand.java deleted file mode 100644 index c878d9b..0000000 --- a/src/main/java/eu/dnetlib/repo/manager/service/customHystrixCommands/UsageStatsTotalHystrixCommand.java +++ /dev/null @@ -1,43 +0,0 @@ -package eu.dnetlib.repo.manager.service.customHystrixCommands; - -import com.netflix.hystrix.HystrixCommand; -import com.netflix.hystrix.HystrixCommandGroupKey; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -import java.util.Map; - -public class UsageStatsTotalHystrixCommand extends HystrixCommand { - - RestTemplate restTemplate ; - String usagestatsEvents; - - public UsageStatsTotalHystrixCommand(String usagestatsEvents,RestTemplate restTemplate) { - super(HystrixCommandGroupKey.Factory.asKey("StatisticsGroup")); - this.usagestatsEvents = usagestatsEvents; - this.restTemplate = restTemplate; - } - - @Override - protected Integer run() { - UriComponents uriComponents = UriComponentsBuilder - .fromHttpUrl(usagestatsEvents) - .build().encode(); - - ResponseEntity rs = restTemplate.exchange(uriComponents.toUri(), HttpMethod.GET,null,Map.class); - Map metadata = (Map) ((Map)rs.getBody()).get("totals"); -// String rs = restTemplate.getForObject(uriComponents.toUri(), String.class); -// JSONObject resultSet = new JSONObject(rs); -// JSONObject totals = resultSet.getJSONObject("totals"); - return (Integer) metadata.get("events"); - } - - @Override - protected Integer getFallback() { - return null; - } - -} \ No newline at end of file diff --git a/src/main/java/eu/dnetlib/repo/manager/service/security/AaiRoleMappingService.java b/src/main/java/eu/dnetlib/repo/manager/service/security/AaiRoleMappingService.java index 2938066..96c91d6 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/security/AaiRoleMappingService.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/security/AaiRoleMappingService.java @@ -1,6 +1,7 @@ package eu.dnetlib.repo.manager.service.security; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; @@ -14,7 +15,7 @@ import java.util.stream.Collectors; @Service("roleMappingService") public class AaiRoleMappingService implements RoleMappingService { - private static final Logger logger = Logger.getLogger(AaiRoleMappingService.class); + private static final Logger logger = LoggerFactory.getLogger(AaiRoleMappingService.class); @Value("${services.provide.aai.registry.production:true}") private boolean production; diff --git a/src/main/java/eu/dnetlib/repo/manager/service/security/AuthoritiesMapper.java b/src/main/java/eu/dnetlib/repo/manager/service/security/AuthoritiesMapper.java index 567cca9..352d2a8 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/security/AuthoritiesMapper.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/security/AuthoritiesMapper.java @@ -2,10 +2,13 @@ package eu.dnetlib.repo.manager.service.security; import com.google.gson.JsonArray; import com.google.gson.JsonElement; -import org.apache.log4j.Logger; +import com.nimbusds.jose.util.StandardCharset; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; +import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.util.*; import java.util.regex.Matcher; @@ -13,8 +16,8 @@ import java.util.regex.Pattern; public class AuthoritiesMapper { - private static final Logger logger = Logger.getLogger(AuthoritiesMapper.class); - private static final String ENTITLEMENT_REGEX = "urn:geant:openaire[.]eu:group:([^:]*):?(.*)?:role=member#aai[.]openaire[.]eu"; + private static final Logger logger = LoggerFactory.getLogger(AuthoritiesMapper.class); + private static final Pattern ENTITLEMENT_REGEX = Pattern.compile("urn:geant:openaire[.]eu:group:([^:]*):?(.*)?:role=member#aai[.]openaire[.]eu"); private AuthoritiesMapper() { } @@ -25,22 +28,24 @@ public class AuthoritiesMapper { return authorities; } - public static List entitlementRoles(JsonArray entitlements) { + public static List entitlementRoles(JsonArray entitlements) throws UnsupportedEncodingException { List roles = new ArrayList<>(); if (entitlements != null) { for (JsonElement obj : entitlements) { - Matcher matcher = Pattern.compile(ENTITLEMENT_REGEX).matcher(obj.getAsString()); + Matcher matcher = ENTITLEMENT_REGEX.matcher(obj.getAsString()); if (matcher.find()) { StringBuilder sb = new StringBuilder(); - if (matcher.group(1) != null && matcher.group(1).length() > 0) { - sb.append(matcher.group(1)); + String group1 = 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(matcher.group(2)); + sb.append(group2); } String role = sb.toString().replace("+", " "); - roles.add(URLDecoder.decode(role)); + roles.add(URLDecoder.decode(role, StandardCharset.UTF_8.name())); } } } @@ -50,18 +55,20 @@ public class AuthoritiesMapper { private static void entityRoles(JsonArray entitlements, Set authorities) { if (entitlements != null) { for (JsonElement obj : entitlements) { - Matcher matcher = Pattern.compile(ENTITLEMENT_REGEX).matcher(obj.getAsString()); + Matcher matcher = ENTITLEMENT_REGEX.matcher(obj.getAsString()); if (matcher.find()) { StringBuilder sb = new StringBuilder(); - if (matcher.group(1) != null && matcher.group(1).length() > 0) { - sb.append(matcher.group(1).replace("+-+", "_").replaceAll("[+.]", "_").toUpperCase()); + String group1 = matcher.group(1); + 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("_"); - if (matcher.group(2).equals("admins")) { + if (group2.equals("admins")) { sb.append("MANAGER"); } else { - sb.append(matcher.group(2).toUpperCase()); + sb.append(group2.toUpperCase()); } } authorities.add(new SimpleGrantedAuthority(sb.toString())); diff --git a/src/main/java/eu/dnetlib/repo/manager/service/security/AuthoritiesUpdater.java b/src/main/java/eu/dnetlib/repo/manager/service/security/AuthoritiesUpdater.java index 2750adc..1729c98 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/security/AuthoritiesUpdater.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/security/AuthoritiesUpdater.java @@ -1,7 +1,8 @@ package eu.dnetlib.repo.manager.service.security; -import org.apache.log4j.Logger; import org.mitre.openid.connect.model.OIDCAuthenticationToken; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.core.Authentication; import org.springframework.security.core.GrantedAuthority; @@ -9,8 +10,8 @@ import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.oauth2.common.exceptions.UnauthorizedClientException; import org.springframework.security.web.context.HttpSessionSecurityContextRepository; -import org.springframework.session.Session; import org.springframework.session.FindByIndexNameSessionRepository; +import org.springframework.session.Session; import org.springframework.stereotype.Service; import java.util.Collection; @@ -21,25 +22,23 @@ import java.util.Map; @Service public class AuthoritiesUpdater extends HttpSessionSecurityContextRepository { - private static final Logger logger = Logger.getLogger(AuthoritiesUpdater.class); + private static final Logger logger = LoggerFactory.getLogger(AuthoritiesUpdater.class); @Autowired FindByIndexNameSessionRepository sessions; public void update(String id, Update update) { if (sessions != null) { - Map map = sessions. - findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, id); + Map map = sessions.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, id); if (map != null) { - logger.debug(map.values().toArray().length); - for (Session session : map.values()) { + for ( Object sessionObject : map.values()) { + Session session = (Session) sessionObject; logger.debug(session.getId()); if (!session.isExpired()) { SecurityContext securityContext = session.getAttribute(SPRING_SECURITY_CONTEXT_KEY); Authentication authentication = securityContext.getAuthentication(); if (authentication instanceof OIDCAuthenticationToken) { OIDCAuthenticationToken authOIDC = (OIDCAuthenticationToken) authentication; - logger.debug(update.authorities(authOIDC.getAuthorities())); securityContext.setAuthentication(new OIDCAuthenticationToken(authOIDC.getSub(), authOIDC.getIssuer(), authOIDC.getUserInfo(), update.authorities(authOIDC.getAuthorities()), authOIDC.getIdToken(), authOIDC.getAccessTokenValue(), authOIDC.getRefreshTokenValue())); @@ -65,7 +64,7 @@ public class AuthoritiesUpdater extends HttpSessionSecurityContextRepository { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth instanceof OIDCAuthenticationToken) { OIDCAuthenticationToken oidcAuth = (OIDCAuthenticationToken) auth; - this.addRole(oidcAuth.getUserInfo().getEmail(), role); + this.addRole(oidcAuth.getUserInfo().getSub(), role); } else { throw new UnauthorizedClientException("User auth is not instance of OIDCAuthenticationToken"); } @@ -83,7 +82,7 @@ public class AuthoritiesUpdater extends HttpSessionSecurityContextRepository { Authentication auth = SecurityContextHolder.getContext().getAuthentication(); if (auth instanceof OIDCAuthenticationToken) { OIDCAuthenticationToken oidcAuth = (OIDCAuthenticationToken) auth; - this.removeRole(oidcAuth.getUserInfo().getEmail(), role); + this.removeRole(oidcAuth.getUserInfo().getSub(), role); } } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/security/AuthorizationService.java b/src/main/java/eu/dnetlib/repo/manager/service/security/AuthorizationService.java index 15f1707..3696321 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/security/AuthorizationService.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/security/AuthorizationService.java @@ -38,22 +38,22 @@ public interface AuthorizationService { /** * Add a user as admin to a resource. * - * @param id Resource id - * @param email User email + * @param resourceId Resource id + * @param email User email * @return * @throws ResourceNotFoundException */ - boolean addAdmin(String id, String email) throws ResourceNotFoundException; + boolean addAdmin(String resourceId, String email) throws ResourceNotFoundException; /** * Remove user from resource admins. * - * @param id Resource id - * @param email User email + * @param resourceId Resource id + * @param email User email * @return * @throws ResourceNotFoundException */ - boolean removeAdmin(String id, String email) throws ResourceNotFoundException; + boolean removeAdmin(String resourceId, String email) throws ResourceNotFoundException; /** @@ -69,6 +69,6 @@ public interface AuthorizationService { * @param email * @return */ - Collection getUserRoles(String email); + Collection getUserRolesByEmail(String email); } diff --git a/src/main/java/eu/dnetlib/repo/manager/service/security/AuthorizationServiceImpl.java b/src/main/java/eu/dnetlib/repo/manager/service/security/AuthorizationServiceImpl.java index 12e4b25..cd6812e 100644 --- a/src/main/java/eu/dnetlib/repo/manager/service/security/AuthorizationServiceImpl.java +++ b/src/main/java/eu/dnetlib/repo/manager/service/security/AuthorizationServiceImpl.java @@ -1,29 +1,25 @@ package eu.dnetlib.repo.manager.service.security; -import com.google.gson.JsonArray; import com.google.gson.JsonElement; import eu.dnetlib.repo.manager.domain.dto.User; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.service.aai.registry.AaiRegistryService; -import org.apache.log4j.LogManager; -import org.apache.log4j.Logger; import org.mitre.openid.connect.model.OIDCAuthenticationToken; import org.mitre.openid.connect.model.UserInfo; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Service; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; -import java.util.stream.Collectors; @Service("authorizationService") public class AuthorizationServiceImpl implements AuthorizationService { - private static final Logger logger = LogManager.getLogger(AuthorizationServiceImpl.class); + private static final Logger logger = LoggerFactory.getLogger(AuthorizationServiceImpl.class); public static final String SUPER_ADMINISTRATOR = "SUPER_ADMINISTRATOR"; public static final String CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR = "CONTENT_PROVIDER_DASHBOARD_ADMINISTRATOR"; @@ -74,33 +70,26 @@ public class AuthorizationServiceImpl implements AuthorizationService { @Override public List getAdminsOfRepo(String repoId) { - List userList = new ArrayList<>(); // find couId by role name String role = roleMappingService.getRoleIdByRepoId(repoId); Integer couId = aaiRegistryService.getCouId(role); - if (couId != null) { - JsonArray users = aaiRegistryService.getUsersByCouId(couId); - for (JsonElement jsonElement : users) { - userList.add(jsonElement.toString()); - } - } return aaiRegistryService.getUsers(couId); } @Override - public boolean addAdmin(String id, String email) throws ResourceNotFoundException { + public boolean addAdmin(String resourceId, String email) throws ResourceNotFoundException { Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email); if (coPersonId != null) { - String role = roleMappingService.getRoleIdByRepoId(id); + String role = roleMappingService.getRoleIdByRepoId(resourceId); Integer couId = aaiRegistryService.getCouId(role); if (couId != null) { Integer roleId = aaiRegistryService.getRoleId(coPersonId, couId); aaiRegistryService.assignMemberRole(coPersonId, couId, roleId); // Add role to user current authorities - authoritiesUpdater.addRole(email, roleMappingService.convertRepoIdToAuthority(id)); + authoritiesUpdater.addRole(email, roleMappingService.convertRepoIdToAuthority(resourceId)); return true; } else { @@ -112,10 +101,10 @@ public class AuthorizationServiceImpl implements AuthorizationService { } @Override - public boolean removeAdmin(String id, String email) throws ResourceNotFoundException { + public boolean removeAdmin(String resourceId, String email) throws ResourceNotFoundException { Integer coPersonId = aaiRegistryService.getCoPersonIdByEmail(email); if (coPersonId != null) { - String role = roleMappingService.getRoleIdByRepoId(id); + String role = roleMappingService.getRoleIdByRepoId(resourceId); Integer couId = aaiRegistryService.getCouId(role); Integer roleId = null; if (couId != null) { @@ -125,7 +114,7 @@ public class AuthorizationServiceImpl implements AuthorizationService { aaiRegistryService.removeMemberRole(coPersonId, couId, roleId); // Remove role from user current authorities - authoritiesUpdater.removeRole(email, roleMappingService.convertRepoIdToAuthority(id)); + authoritiesUpdater.removeRole(email, roleMappingService.convertRepoIdToAuthority(resourceId)); return true; } else { @@ -140,14 +129,16 @@ public class AuthorizationServiceImpl implements AuthorizationService { public Collection getUserRoles() { Collection roles; UserInfo userInfo = ((OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()).getUserInfo(); - roles = getUserRoles(userInfo.getEmail()); + roles = getUserRolesByEmail(userInfo.getEmail()); - logger.debug(String.format("User Roles: %s", String.join(",", roles))); + if (logger.isDebugEnabled()) { + logger.debug("User Roles: {}", String.join(",", roles)); + } return roles; } @Override - public Collection getUserRoles(String email) { + public Collection getUserRolesByEmail(String email) { int coPersonId = aaiRegistryService.getCoPersonIdByEmail(email); List list = new ArrayList<>(); for (JsonElement element : aaiRegistryService.getRolesWithStatus(coPersonId, AaiRegistryService.RoleStatus.ACTIVE)) { diff --git a/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java b/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java index cca43fe..38f5e2e 100644 --- a/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java +++ b/src/main/java/eu/dnetlib/repo/manager/utils/Converter.java @@ -4,29 +4,29 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import eu.dnetlib.repo.manager.domain.*; import org.apache.commons.codec.digest.DigestUtils; -import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; @Component public class Converter { - private static final Logger LOGGER = Logger.getLogger(Converter.class); + private static final Logger logger = LoggerFactory.getLogger(Converter.class); private final ObjectMapper objectMapper; public Converter() { - objectMapper = new ObjectMapper(); + objectMapper = new ObjectMapper()/*.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)*/; } public Repository toRepository(Object repositoryObject) { @@ -51,16 +51,7 @@ public class Converter { return resultSet; } - public List toRepositoryInterfaceList(JSONObject json) throws JSONException { - - List resultSet = new ArrayList<>(); - JSONArray rs = json.getJSONArray("api"); - for (int i = 0; i < rs.length(); i++) - resultSet.add(toRepositoryInterface(rs.getJSONObject(i))); - return resultSet; - } - - public List toRepositoryInterfaceList(List apiDetailsList) throws JSONException { + public List toRepositoryInterfaceList(List apiDetailsList) { List resultSet = new ArrayList<>(); @@ -85,27 +76,16 @@ public class Converter { String line; List list = new ArrayList<>(); try { - //InputStream in = Converter.class.getResourceAsStream("resources/eu/dnetlib/repo/manager/service/utils/"+filename); - InputStream in = Converter.class.getClass().getResourceAsStream("/eu/**/" + filename); - BufferedReader br = new BufferedReader(new InputStreamReader(in)); + InputStream in = Converter.class.getResourceAsStream("/eu/**/" + filename); + BufferedReader br = new BufferedReader(new InputStreamReader(in)); // It may throw an NPE. while ((line = br.readLine()) != null) { list.add(line.trim()); } br.close(); - } catch (IOException e) { - LOGGER.debug("Error opening file!"); - LOGGER.error(e); + } catch (Exception e) { + logger.error("Error opening file!", e); } - return list; - } - - public List toAggregationHistory(JSONArray aggregationInfo) throws JSONException { - List aggregationDetailsList = new ArrayList<>(); - - for (int i = 0; i < aggregationInfo.length(); i++) - aggregationDetailsList.add(toAggregationDetails(aggregationInfo.getJSONObject(i))); - - return aggregationDetailsList; + return list; // It may be empty. } public List toTimezones(List timezones) { @@ -128,29 +108,6 @@ public class Converter { return value.equals("null") ? null : Boolean.valueOf(value); } - private Date toDate(String date) { - - if (Objects.equals(date, "null")) - return null; - - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); - try { - return formatter.parse(date); - } catch (ParseException e) { - LOGGER.error(e); - } - return null; - } - - public String toString(Date date) { - - if (Objects.equals(date, null)) - return null; - - SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); - return formatter.format(date); - } - private Double toDouble(String number) { if (Objects.equals(number, "null")) return 0.0; @@ -158,20 +115,5 @@ public class Converter { return Double.valueOf(number); } - private AggregationDetails toAggregationDetails(JSONObject aggregationObject) throws JSONException { - - AggregationDetails aggregationDetails = new AggregationDetails(); - - if (aggregationObject.has("collectionMode")) - aggregationDetails.setCollectionMode(aggregationObject.get("collectionMode").toString()); - if (aggregationObject.has("indexedVersion")) - aggregationDetails.setIndexedVersion(Boolean.parseBoolean(aggregationObject.get("indexedVersion").toString())); - - aggregationDetails.setAggregationStage(aggregationObject.get("aggregationStage").toString()); - aggregationDetails.setDate(toDate(aggregationObject.get("date").toString())); - aggregationDetails.setNumberOfRecords(Integer.parseInt(aggregationObject.get("numberOfRecords").toString())); - - return aggregationDetails; - } } diff --git a/src/main/java/eu/dnetlib/repo/manager/utils/CrisValidatorUtils.java b/src/main/java/eu/dnetlib/repo/manager/utils/CrisValidatorUtils.java index 174be07..9d47bf4 100644 --- a/src/main/java/eu/dnetlib/repo/manager/utils/CrisValidatorUtils.java +++ b/src/main/java/eu/dnetlib/repo/manager/utils/CrisValidatorUtils.java @@ -3,6 +3,7 @@ package eu.dnetlib.repo.manager.utils; import eu.dnetlib.domain.functionality.validator.JobResultEntry; import eu.dnetlib.domain.functionality.validator.StoredJob; 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 java.text.DateFormat; @@ -20,14 +21,17 @@ public class CrisValidatorUtils { StoredJob sj = new StoredJob(); sj.setId(job.getId().hashCode()); sj.setValidationStatus(job.getStatus()); - if (job.getDateFinished() != null) { - sj.setEnded(DATE_FORMATTER.format(job.getDateFinished())); - sj.setDuration(DURATION_FORMATTER.format(new Date(job.getDateFinished().getTime() - job.getDateStarted().getTime()))); + + Date dateFinished = job.getDateFinished(); + 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 { sj.setEnded("-"); sj.setDuration("-"); } - sj.setStarted(DATE_FORMATTER.format(job.getDateStarted())); + sj.setStarted(DATE_FORMATTER.format(dateStarted)); sj.setUserEmail(job.getUser()); sj.setCris(true); @@ -56,21 +60,24 @@ public class CrisValidatorUtils { public static List crisResultsToJobEntries(Job crisJob) { List jobResultEntries = new ArrayList<>(); for (RuleResults ruleResults : crisJob.getRuleResults()) { + Rule rule = ruleResults.getRule(); JobResultEntry jobResultEntry = new JobResultEntry(); - jobResultEntry.setName(ruleResults.getRule().getName()); - jobResultEntry.setRuleId(ruleResults.getRule().getId()); - jobResultEntry.setDescription(ruleResults.getRule().getDescription()); + jobResultEntry.setName(rule.getName()); + jobResultEntry.setRuleId(rule.getId()); + jobResultEntry.setDescription(rule.getDescription()); jobResultEntry.setMandatory(true); - jobResultEntry.setWeight(Math.round(ruleResults.getRule().getWeight())); - jobResultEntry.setType(ruleResults.getRule().getType()); + jobResultEntry.setWeight(Math.round(rule.getWeight())); + jobResultEntry.setType(rule.getType()); jobResultEntry.setHasErrors(false); - if (ruleResults.getCount() == 0) { + long countRuleResults = ruleResults.getCount(); + if (countRuleResults == 0) { jobResultEntry.setSuccesses("-"); } else { - jobResultEntry.setSuccesses(ruleResults.getCount() - ruleResults.getFailed() + "/" + ruleResults.getCount()); + jobResultEntry.setSuccesses(countRuleResults - ruleResults.getFailed() + "/" + countRuleResults); } - if (ruleResults.getErrorMessages() != null && !ruleResults.getErrorMessages().isEmpty()) { - jobResultEntry.setErrors(ruleResults.getErrorMessages()); + List errorMessages = ruleResults.getErrorMessages(); + if (errorMessages != null && !errorMessages.isEmpty()) { + jobResultEntry.setErrors(errorMessages); jobResultEntry.setHasErrors(true); } jobResultEntries.add(jobResultEntry); diff --git a/src/main/java/eu/dnetlib/repo/manager/utils/DateUtils.java b/src/main/java/eu/dnetlib/repo/manager/utils/DateUtils.java new file mode 100644 index 0000000..33710ac --- /dev/null +++ b/src/main/java/eu/dnetlib/repo/manager/utils/DateUtils.java @@ -0,0 +1,49 @@ +package eu.dnetlib.repo.manager.utils; + + + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; +import java.util.Objects; + +public class DateUtils { + + private static final Logger logger = LoggerFactory.getLogger(DateUtils.class); + + public static Date toDate(String date) { + + if (Objects.equals(date, "null")) + return null; + + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + try { + return formatter.parse(date); + } catch (ParseException e) { + logger.error(e.getMessage(), e); + } + return null; + } + + public static String toString(Date date) { + + if (Objects.equals(date, null)) + return null; + + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd"); + return formatter.format(date); + } + + public static String getYear(String date) { + Calendar calendar = Calendar.getInstance(); + calendar.setTime(Objects.requireNonNull(toDate(date))); + return String.valueOf(calendar.get(Calendar.YEAR)); + } + + private DateUtils() { + } +} diff --git a/src/main/java/eu/dnetlib/repo/manager/utils/HttpUtils.java b/src/main/java/eu/dnetlib/repo/manager/utils/HttpUtils.java index bb2c7be..957da55 100644 --- a/src/main/java/eu/dnetlib/repo/manager/utils/HttpUtils.java +++ b/src/main/java/eu/dnetlib/repo/manager/utils/HttpUtils.java @@ -4,19 +4,20 @@ import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.apache.commons.codec.binary.Base64; -import org.apache.log4j.Logger; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.*; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.Map; @Component public class HttpUtils { - private static final Logger logger = Logger.getLogger(HttpUtils.class); + private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); @Value("${services.provide.aai.registry.url}") private String registryUrl; @@ -78,20 +79,24 @@ public class HttpUtils { private HttpHeaders createHeaders(String username, String password) { return new HttpHeaders() {{ String auth = username + ":" + password; - byte[] encodedAuth = Base64.encodeBase64( - auth.getBytes(Charset.forName("US-ASCII"))); + byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.US_ASCII)); String authHeader = "Basic " + new String(encodedAuth); set("Authorization", authHeader); }}; } private JsonElement getResponseEntityAsJsonElement(ResponseEntity 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 { - return new JsonParser().parse(responseEntity.getBody()); + return new JsonParser().parse(responseBody); } catch (Exception e) { - logger.warn("Could not parse response body", e); + logger.warn("Could not parse response body", e); // Will return null. } } return null; diff --git a/src/main/java/eu/dnetlib/repo/manager/utils/OaiTools.java b/src/main/java/eu/dnetlib/repo/manager/utils/OaiTools.java index 93c0d5f..1e030e6 100644 --- a/src/main/java/eu/dnetlib/repo/manager/utils/OaiTools.java +++ b/src/main/java/eu/dnetlib/repo/manager/utils/OaiTools.java @@ -1,7 +1,8 @@ package eu.dnetlib.repo.manager.utils; -import org.apache.log4j.Logger; import org.dom4j.io.DOMWriter; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import se.kb.oai.pmh.*; @@ -24,16 +25,15 @@ public class OaiTools { disableSslVerification(); } - private static Logger LOGGER = Logger.getLogger(OaiTools.class); + private static Logger logger = LoggerFactory.getLogger(OaiTools.class); public static List getSetsOfRepo(String baseUrl) throws Exception { try { - LOGGER.debug("Getting sets of repository " + baseUrl); + logger.debug("Getting sets of repository: {}", baseUrl); OaiPmhServer harvester = new OaiPmhServer(baseUrl); SetsList setList = harvester.listSets(); ResumptionToken token = setList.getResumptionToken(); - List sets = new ArrayList(); - sets.addAll(setList.asList()); + List sets = new ArrayList<>(setList.asList()); while (token != null) { setList = harvester.listSets(token); token = setList.getResumptionToken(); @@ -49,14 +49,14 @@ public class OaiTools { return ret; } catch (Exception e) { - LOGGER.error("Error getting sets of repository " + baseUrl, e); + logger.error("Error getting sets of repository " + baseUrl, e); return new ArrayList(); //throw e; } } 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); @@ -71,7 +71,7 @@ public class OaiTools { return verifyIdentify(d); } catch (Exception e) { - LOGGER.debug("Error verifying identify response", e); + logger.debug("Error verifying identify response", e); throw e; } } @@ -112,7 +112,7 @@ public class OaiTools { private static void disableSslVerification() { try { - LOGGER.debug("disabling ssl verification"); + logger.debug("disabling ssl verification"); // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { @@ -140,9 +140,9 @@ public class OaiTools { // Install the all-trusting host verifier HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); } catch (NoSuchAlgorithmException e) { - LOGGER.error("disabling ssl verification", e); + logger.error("disabling ssl verification", e); } catch (KeyManagementException e) { - LOGGER.error("error while disabling ssl verification", e); + logger.error("error while disabling ssl verification", e); } } } diff --git a/src/main/java/eu/dnetlib/repo/manager/controllers/RestTemplateResponseErrorHandler.java b/src/main/java/eu/dnetlib/repo/manager/utils/RestTemplateResponseErrorHandler.java similarity index 60% rename from src/main/java/eu/dnetlib/repo/manager/controllers/RestTemplateResponseErrorHandler.java rename to src/main/java/eu/dnetlib/repo/manager/utils/RestTemplateResponseErrorHandler.java index 488b557..42bf5f3 100644 --- a/src/main/java/eu/dnetlib/repo/manager/controllers/RestTemplateResponseErrorHandler.java +++ b/src/main/java/eu/dnetlib/repo/manager/utils/RestTemplateResponseErrorHandler.java @@ -1,4 +1,4 @@ -package eu.dnetlib.repo.manager.controllers; +package eu.dnetlib.repo.manager.utils; import eu.dnetlib.repo.manager.exception.EndPointException; import org.springframework.http.HttpStatus; @@ -16,19 +16,17 @@ public class RestTemplateResponseErrorHandler implements ResponseErrorHandler { @Override public boolean hasError(ClientHttpResponse httpResponse) throws IOException { - return (httpResponse.getStatusCode().series() == CLIENT_ERROR - || httpResponse.getStatusCode().series() == SERVER_ERROR); + HttpStatus.Series seriesError = httpResponse.getStatusCode().series(); + return ( (seriesError == CLIENT_ERROR) || (seriesError == SERVER_ERROR) ); } @Override public void handleError(ClientHttpResponse httpResponse) throws IOException { - if (httpResponse.getStatusCode().series() == HttpStatus.Series.SERVER_ERROR) { + HttpStatus statusCode = httpResponse.getStatusCode(); + if ( statusCode == HttpStatus.NOT_FOUND ) + throw new IOException(); + else if (statusCode.series() == SERVER_ERROR) throw new EndPointException(); - } else if (httpResponse.getStatusCode().series() == HttpStatus.Series.CLIENT_ERROR) { - if (httpResponse.getStatusCode() == HttpStatus.NOT_FOUND) { - throw new IOException(); - } - } } } diff --git a/src/main/resources/application-context.xml b/src/main/resources/application-context.xml index 7d4c104..4a5fb66 100644 --- a/src/main/resources/application-context.xml +++ b/src/main/resources/application-context.xml @@ -1,9 +1,9 @@ + http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 178f05a..02c21d1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,14 +1,30 @@ +springdoc.swagger-ui: + disable-swagger-default-url: true + version: 3 + +spring: + jpa: + hibernate: + ddl-auto: update + + datasource: + url: ${services.provide.db.url} + username: ${services.provide.db.username} + password: ${services.provide.db.password} + driverClassName: ${services.provide.db.driverClassName} + services: provide: + dev-machine: 88.197.53.71 aai: baseURL: https://aai.openaire.eu oidc: domain: .openaire.eu id: XX issuer: ${services.provide.aai.baseURL}/oidc/ - redirectURL: http://88.197.53.69/api/openid_connect_login + redirectURL: http://localhost:${server.port}${server.servlet.context-path}/openid_connect_login secret: XX - webURL: http://88.197.53.69/join + webURL: http://localhost:4200/join registry: coid: XX password: XX @@ -17,21 +33,21 @@ services: username: provide_openaire adminEmail: XX analyticsURL: https://analytics.openaire.eu/addsite.php? - baseUrl: https://beta.provide.openaire.eu/ + baseUrl: https://dev-openaire.d4science.org/openaire broker: api: api/ openaire: openaireBroker port: 8080 url: https://broker1-dev-dnet.d4science.org clients: - dsm: https://beta.services.openaire.eu/openaire + dsm: https://dev-openaire.d4science.org/openaire search: https://beta.services.openaire.eu/search/v2/api usageEvents: http://beta.lbs.openaire.eu:8080/ajax/summary usagestats: https://services.openaire.eu/usagestats db: driverClassName: org.postgresql.Driver password: dnetPwd - url: jdbc:postgresql://localhost:5432/repomanager + url: jdbc:postgresql://${services.provide.dev-machine}:5432/repomanager username: dnet iSLookUpService: url: https://dev-openaire.d4science.org:443/is/services/isLookUp @@ -46,8 +62,8 @@ services: replyTo: XX username: XX redis: - host: localhost - password: 2106875370 + host: vereniki.athenarc.gr + password: XX port: 6379 topic_types: url: https://beta.services.openaire.eu/provision/mvc/vocabularies/dnet:topic_types.json @@ -60,4 +76,4 @@ services: results: url: https://beta.provide.openaire.eu/compatibility/browseHistory/ validatorService: - url: http://88.197.53.69:8080/uoa-validator-service/services/validatorWebService + url: http://${services.provide.dev-machine}:8080/uoa-validator-service/services/validatorWebService diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml index ab51f28..a98fd99 100644 --- a/src/main/resources/log4j2.xml +++ b/src/main/resources/log4j2.xml @@ -6,11 +6,11 @@ + + + - - - diff --git a/src/main/webapp/WEB-INF/applicationContext.xml b/src/main/webapp/WEB-INF/applicationContext.xml index b744c56..43855f7 100644 --- a/src/main/webapp/WEB-INF/applicationContext.xml +++ b/src/main/webapp/WEB-INF/applicationContext.xml @@ -1,12 +1,11 @@ diff --git a/src/test/java/metrics/PrometheusTest.java b/src/test/java/eu/dnetlib/repo/manager/integration/metrics/PrometheusTest.java similarity index 97% rename from src/test/java/metrics/PrometheusTest.java rename to src/test/java/eu/dnetlib/repo/manager/integration/metrics/PrometheusTest.java index 8c34c6c..897aaea 100644 --- a/src/test/java/metrics/PrometheusTest.java +++ b/src/test/java/eu/dnetlib/repo/manager/integration/metrics/PrometheusTest.java @@ -1,4 +1,4 @@ -package metrics; +package eu.dnetlib.repo.manager.integration.metrics; import eu.dnetlib.repo.manager.controllers.PrometheusController; import eu.dnetlib.repo.manager.service.PiWikService; diff --git a/src/test/java/eu/dnetlib/repo/manager/integration/service/InterfaceComplianceRequestTests.java b/src/test/java/eu/dnetlib/repo/manager/integration/service/InterfaceComplianceRequestTests.java new file mode 100644 index 0000000..8a19e7d --- /dev/null +++ b/src/test/java/eu/dnetlib/repo/manager/integration/service/InterfaceComplianceRequestTests.java @@ -0,0 +1,58 @@ +package eu.dnetlib.repo.manager.integration.service; + +import eu.dnetlib.repo.manager.domain.InterfaceComplianceRequest; +import eu.dnetlib.repo.manager.domain.InterfaceComplianceRequestId; +import eu.dnetlib.repo.manager.repository.InterfaceComplianceRequestsRepository; +import org.junit.Assert; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Date; + +@RunWith(SpringRunner.class) +@DataJpaTest +//@AutoConfigureTestDatabase +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +//@Transactional(propagation = Propagation.NOT_SUPPORTED) +class InterfaceComplianceRequestTests { + + @Autowired + private InterfaceComplianceRequestsRepository repository; + + @Test + void getRequestById() { + InterfaceComplianceRequest request = createRequest("3"); + repository.save(request); + long total = repository.count(); + Assert.assertEquals(1, total); + InterfaceComplianceRequest r = repository.findById(createRequestId()).orElse(null); + Assert.assertNotNull(r); + } + + @AfterEach + public void deleteRequestAfterTests() { + repository.deleteById(createRequestId()); + } + + private InterfaceComplianceRequestId createRequestId() { + InterfaceComplianceRequestId id = new InterfaceComplianceRequestId(); + id.setRepositoryId("repository"); + id.setInterfaceId("interface"); + return id; + } + + private InterfaceComplianceRequest createRequest(String compatibilityLevel) { + InterfaceComplianceRequest request = new InterfaceComplianceRequest(); + InterfaceComplianceRequestId id = createRequestId(); + request.setRepositoryId(id.getRepositoryId()); + request.setInterfaceId(id.getInterfaceId()); + request.setDesiredCompatibilityLevel(compatibilityLevel); + request.setSubmissionDate(new Date()); + return request; + } +} diff --git a/src/test/java/eu/dnetlib/repo/manager/unit/InterfaceComplianceRequestTest.java b/src/test/java/eu/dnetlib/repo/manager/unit/InterfaceComplianceRequestTest.java new file mode 100644 index 0000000..ed2efcf --- /dev/null +++ b/src/test/java/eu/dnetlib/repo/manager/unit/InterfaceComplianceRequestTest.java @@ -0,0 +1,27 @@ +package eu.dnetlib.repo.manager.unit; + +import eu.dnetlib.repo.manager.domain.InterfaceComplianceRequest; +import eu.dnetlib.repo.manager.domain.dto.InterfaceComplianceRequestDTO; +import org.junit.Assert; +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + + +@SpringBootTest(classes = InterfaceComplianceRequest.class) +@RunWith(SpringRunner.class) +class InterfaceComplianceRequestTest { + + @Test + void interfaceComplianceRequest_contains_date_test() { + InterfaceComplianceRequest request = new InterfaceComplianceRequest(); + Assert.assertNotNull(request.getSubmissionDate()); + + InterfaceComplianceRequestDTO dto = new InterfaceComplianceRequestDTO(); + Assert.assertNull(dto.getSubmissionDate()); + + request = InterfaceComplianceRequest.from(dto); + Assert.assertNotNull(request.getSubmissionDate()); + } +} diff --git a/src/test/resources/application-context.xml b/src/test/resources/application-context.xml deleted file mode 100644 index 7d4c104..0000000 --- a/src/test/resources/application-context.xml +++ /dev/null @@ -1,42 +0,0 @@ - - - - - - - - - - - - - - - - - - - - classpath*:/gr/**/springContext-*.properties - classpath*:/eu/**/springContext-*.properties - - classpath*:/application.properties - classpath*:email-texts.properties - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml new file mode 100644 index 0000000..16f944c --- /dev/null +++ b/src/test/resources/application.yml @@ -0,0 +1,13 @@ +spring: + datasource: + url: jdbc:h2:mem:test + driverClassName: org.h2.Driver + h2: + console: + enabled: true + jpa: + show-sql: true + hibernate: + ddl-auto: update + main: + banner-mode: console diff --git a/src/test/resources/applicationContext.xml b/src/test/resources/applicationContext.xml deleted file mode 100644 index 7928037..0000000 --- a/src/test/resources/applicationContext.xml +++ /dev/null @@ -1,70 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - classpath*:/gr/**/springContext-*.properties - classpath*:/eu/**/springContext-*.properties - - classpath*:/application.properties - classpath*:/email-texts.properties - - - - - - - - - - - - - - \ No newline at end of file