From b551d8fc7261d73407068813dcfee49c5ebded3b Mon Sep 17 00:00:00 2001 From: "k.triantafyllou" Date: Wed, 16 Oct 2024 23:55:37 +0300 Subject: [PATCH] Update to spring boot 3 and java 17 --- README.md | 94 +++++++------- pom.xml | 43 +------ .../IrishMonitorServiceApplication.java | 5 +- .../ServletInitializer.java | 3 +- .../configuration/GlobalVars.java | 2 +- .../configuration/SwaggerConfig.java | 120 ------------------ .../mongo/IrishMongoConnection.java | 33 ++--- .../properties/APIProperties.java | 38 ------ ...atsToolProperties.java => Properties.java} | 4 +- .../IrishServiceCheckDeployController.java | 32 ++--- .../StakeholderExtendedController.java | 10 +- .../controllers/UserProfileController.java | 62 +++------ .../dao/MongoDBUserProfileDAO.java | 20 --- .../dao/UserProfileDAO.java | 18 +-- .../security/IrishAuthoritiesMapper.java | 16 +-- .../services/CacheService.java | 20 +-- .../services/DeployService.java | 47 +++++++ .../services/StatsToolService.java | 24 ++-- src/main/resources/application.properties | 7 +- .../resources/irishmonitorservice.properties | 9 +- 20 files changed, 204 insertions(+), 403 deletions(-) delete mode 100644 src/main/java/eu/dnetlib/irishmonitorservice/configuration/SwaggerConfig.java delete mode 100644 src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/APIProperties.java rename src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/{StatsToolProperties.java => Properties.java} (92%) delete mode 100644 src/main/java/eu/dnetlib/irishmonitorservice/dao/MongoDBUserProfileDAO.java create mode 100644 src/main/java/eu/dnetlib/irishmonitorservice/services/DeployService.java diff --git a/README.md b/README.md index aa89091..2661e1d 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,14 @@ # Irish Monitor Service -Irish Monitor Service is a service that integrates both OpenAIRE Login API and +Irish Monitor Service is a service that integrates both OpenAIRE Login API and OpenAIRE Monitor API and provide all functionalities for National Open Access Monitor, Ireland. ## Maven Java Version: 1.8, Sprint boot Version: 1.5.8.RELEASE -This service has dependencies downloaded from [D4Science Nexus repository](https://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-releases), +This service has dependencies downloaded +from [D4Science Nexus repository](https://maven.research-infrastructures.eu/nexus/content/repositories/dnet45-releases), so there is a definition of this repository inside [pom.xml](pom.xml). ### Install @@ -21,17 +22,20 @@ so there is a definition of this repository inside [pom.xml](pom.xml). cd /etc/systemd/system sudo vim service-name.service -2. Add the following content by replacing () accordingly. +2. Add the following content by replacing and accordingly. [Unit] - Description=service-name + Description=irish-monitor-service After=syslog.target - + [Service] User=root - ExecStart=(/home/user/spring-boot/)irish-monitor-service.war (--spring.profiles.active=swagger) --server.port=(port) (--server.context-path=/irish-monitor-service) --spring.config.location=file:///(home/user/spring-boot/config/application.properties) + Environment="PORT=" + Environment="SERVICE_NAME=irish-monitor-service" + Environment="PROPERTIES_FOLDER=" + ExecStart=/bin/sh -c "java -jar /srv/spring-boot/${PORT}/${SERVICE_NAME}.war --server.port=${PORT} --server.servlet.context-path=/${SERVICE_NAME} --spring.config.location=file:///srv/spring-boot/${PROPERTIES_FOLDER}/dnet-override.properties" SuccessExitStatus=143 - + [Install] WantedBy=multi-user.target @@ -56,57 +60,55 @@ so there is a definition of this repository inside [pom.xml](pom.xml). In order to configure this service you have to set the following properties: - stats-tool.rfo=?json= - stats-tool.rpo=?json= - stats-tool.repository=?json= + stats-tool.host= # Stats tool URL with json param e.g https://stats.madgik.di.uoa.gr/stats-api/raw?json= +## Authentication / Authorization -## Login core - -This dependency provides the Authentication and Authorization functionality. +This dependency provides the Authentication and Authorization functionality. In order to configure it you have to set the following properties: - authentication.domain= - authentication.oidc.home=/openid_connect_login - authentication.oidc.scope=openid,profile,email,eduperson_entitlement,orcid - authentication.oidc.id= - authentication.oidc.secret= - authentication.oidc.issuer=https://aai.openaire.eu/auth/realms/openaire - authentication.oidc.redirect=/redirect - authentication.session=irishSession - authentication.redirect= - authentication.redis.host= - authentication.redis.port= - authentication.redis.passwork= - authentication.authorities-mapper=irish.eduperson_entitlement // DO NOT CHANGE - authentication.keycloak=true + authorization.security.redis.host= # Default localhost + authorization.security.redis.port= # Default 6379 + authorization.security.redis.password= # Default "" + authorization.security.domain= # e.g openaire.eu Default: di.uoa.gr + authorization.security.session= # Default openAIRESession + + authentication.client.name= # Required - Name of the provider - default value: openaire + authentication.client.issuer= # Required - Issuer url - default value: https://aai.openaire.eu/auth/realms/openaire + authentication.client.scope= # Required - scope properties of the provider - default value: openid,profile,email,eduperson_entitlement + authentication.client.id= # Required - Client id - default value: - + authentication.client.secret= # Required - Client secret - default value: - + authentication.client.redirect= # Required - Redirect endpoint of the service: {baseURL}/login-service/redirect + authentication.client.logout= # Optional - If custom logout endpoint is needed. (Needs a redirect param at the end) + authentication.redirect= # Required - Set the default redirect URL after a successful login / logout + authentication.accessToken= # Optional - Name of access token cookie, default value: AccessToken + authentication.authorities-mapper= # Optional - Add the field of User Info provider endpoint that containes authorities. e.g. eduperson_entitlement (Don't forget to include it scope) ## Monitor Service -This dependency provides the main service functionality +This dependency provides the main service functionality in order to create/edit/delete monitor profiles and their content. In order to configure it you have to set the following properties: - monitorservice.mongodb.host= - monitorservice.mongodb.port= - monitorservice.mongodb.database= - monitorservice.mongodb.username= - monitorservice.mongodb.password= + monitor-service.mongodb.host # Required - Host of mongo server - Default: localhost + monitor-service.mongodb.database # Required - Database name + monitor-service.mongodb.username # Optional - Username if needed + monitor-service.mongodb.password # Optional - Password if needed + monitor-service.mongodb.port # Required - Mongo server port - Default: 27017 ## Admin Tools Library (integrated in Monitor Service) -This dependency provides utilities to store dynamic HTML content, -send Email and verify Google recaptcha. In order to configure it +This dependency provides utilities to store dynamic HTML content, +send Email and verify Google recaptcha. In order to configure it you have to set the following properties: - admintoolslibrary.mail.from = - admintoolslibrary.mail.username = - admintoolslibrary.mail.password = - admintoolslibrary.mail.host = - admintoolslibrary.mail.port = - admintoolslibrary.mail.auth = true - admintoolslibrary.mail.sslProtocols = TLSv1.2 - admintoolslibrary.mail.defaultEncoding=UTF-8 - admintoolslibrary.mail.protocol= - admintoolslibrary.mail.testConnection=false - admintoolslibrary.google.secret = + admintoolslibrary.google.secret= # Required - Google secret of recaptcha client + admintoolslibrary.mail.host # Required - Mail host name + admintoolslibrary.mail.port # Required - Mail port + admintoolslibrary.mail.from # Required - From email address + admintoolslibrary.mail.protocol # Required - Mail protocol + admintoolslibrary.mail.defaultEncoding # Required - Default Encoding - default value: UTF-8 + admintoolslibrary.mail.sslProtocols # Optional - If ssl Protocols are needed (comma separated) + admintoolslibrary.mail.auth # Required (boolean) - If authentication is required for mail account - default value: false + admintoolslibrary.mail.username # Required (if auth = true) - Username of mail account + admintoolslibrary.mail.password # Required (if auth = true) - Password of mail account diff --git a/pom.xml b/pom.xml index 89ed9b4..a1b9caf 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 irish-monitor-service - 1.0.5-SNAPSHOT + 2.0.0-SNAPSHOT war irish-monitor-service @@ -13,7 +13,7 @@ eu.dnetlib uoa-spring-boot-parent - 1.0.0 + 2.0.3 UTF-8 @@ -22,31 +22,16 @@ E MMM dd HH:mm:ss z yyyy - - + eu.dnetlib uoa-monitor-service - 1.1.15 + 2.0.0-BETA library - eu.dnetlib uoa-login-core - 2.1.1 - - - - - io.springfox - springfox-swagger2 - ${swagger-version} - - - - io.springfox - springfox-swagger-ui - ${swagger-version} + 3.0.5 @@ -68,35 +53,17 @@ org.springframework.boot spring-boot-maven-plugin - ${spring-boot-version} eu.dnetlib.irishmonitorservice.IrishMonitorServiceApplication true - - - - repackage - - - org.apache.maven.plugins maven-war-plugin - 2.6 - - false - irish-monitor-service - - - src/main/resources - true - - diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/IrishMonitorServiceApplication.java b/src/main/java/eu/dnetlib/irishmonitorservice/IrishMonitorServiceApplication.java index 0139026..9de1c8c 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/IrishMonitorServiceApplication.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/IrishMonitorServiceApplication.java @@ -2,8 +2,7 @@ package eu.dnetlib.irishmonitorservice; import eu.dnetlib.authentication.configuration.AuthenticationConfiguration; import eu.dnetlib.irishmonitorservice.configuration.GlobalVars; -import eu.dnetlib.irishmonitorservice.configuration.properties.APIProperties; -import eu.dnetlib.irishmonitorservice.configuration.properties.StatsToolProperties; +import eu.dnetlib.irishmonitorservice.configuration.properties.Properties; import eu.dnetlib.uoamonitorservice.UoaMonitorServiceConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -22,7 +21,7 @@ import org.springframework.scheduling.annotation.EnableScheduling; @PropertySource("classpath:irishmonitorservice.properties"), @PropertySource(value = "classpath:dnet-override.properties", ignoreResourceNotFound = true) }) -@EnableConfigurationProperties({GlobalVars.class, APIProperties.class, StatsToolProperties.class}) +@EnableConfigurationProperties({GlobalVars.class, Properties.class}) @EnableCaching @EnableScheduling @Import({UoaMonitorServiceConfiguration.class, AuthenticationConfiguration.class}) diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/ServletInitializer.java b/src/main/java/eu/dnetlib/irishmonitorservice/ServletInitializer.java index f0c2994..48c8363 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/ServletInitializer.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/ServletInitializer.java @@ -1,8 +1,7 @@ package eu.dnetlib.irishmonitorservice; import org.springframework.boot.builder.SpringApplicationBuilder; -import org.springframework.boot.web.support.SpringBootServletInitializer; -//import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; +import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; public class ServletInitializer extends SpringBootServletInitializer { diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/GlobalVars.java b/src/main/java/eu/dnetlib/irishmonitorservice/configuration/GlobalVars.java index 63f8e72..7d90348 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/GlobalVars.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/configuration/GlobalVars.java @@ -4,7 +4,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; import java.util.Date; -@ConfigurationProperties("irishmonitorservice.globalVars") +@ConfigurationProperties("irish-monitor-service.global-vars") public class GlobalVars { public static Date date = new Date(); private Date buildDate; diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/SwaggerConfig.java b/src/main/java/eu/dnetlib/irishmonitorservice/configuration/SwaggerConfig.java deleted file mode 100644 index 774229e..0000000 --- a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/SwaggerConfig.java +++ /dev/null @@ -1,120 +0,0 @@ -package eu.dnetlib.irishmonitorservice.configuration; - -import eu.dnetlib.irishmonitorservice.configuration.properties.APIProperties; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Profile; -import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; -import springfox.documentation.builders.ApiInfoBuilder; -import springfox.documentation.builders.ParameterBuilder; -import springfox.documentation.builders.PathSelectors; -import springfox.documentation.builders.RequestHandlerSelectors; -import springfox.documentation.schema.ModelRef; -import springfox.documentation.service.ApiInfo; -import springfox.documentation.service.Parameter; -import springfox.documentation.spi.DocumentationType; -import springfox.documentation.spring.web.plugins.Docket; -import springfox.documentation.swagger2.annotations.EnableSwagger2; - -import java.util.Collections; -import java.util.List; - -/** - * Swagger configuration class - */ -@Configuration -@Profile({"swagger"}) -@EnableSwagger2 -public class SwaggerConfig extends WebMvcConfigurerAdapter { - - private final APIProperties apiProperties; - - @Autowired - public SwaggerConfig(APIProperties apiProperties) { - this.apiProperties = apiProperties; - } - - - @Bean - public Docket createRestApi() { - return new Docket(DocumentationType.SWAGGER_2) -// .globalOperationParameters(globalParameterList()) - .apiInfo(apiInfo()) - .select() - .apis(RequestHandlerSelectors.basePackage("eu.dnetlib.irishmonitorservice.controllers")) - .paths(PathSelectors.any()) - .build(); - } - - @Bean - public Docket createRestApiMonitorLibrary() { - return new Docket(DocumentationType.SWAGGER_2) - .apiInfo(apiInfo()) - .groupName("Monitor Library") - .select() - .apis(RequestHandlerSelectors.basePackage("eu.dnetlib.uoamonitorservice.controllers")) - .paths(PathSelectors.any()) - .build(); - } - - - @Bean - public Docket createRestApiLibrary() { - return new Docket(DocumentationType.SWAGGER_2) - .apiInfo(apiInfo()) - .groupName("Library") - .select() - .apis(RequestHandlerSelectors.basePackage("eu.dnetlib.uoaadmintoolslibrary.controllers")) - .paths(PathSelectors.any()) - .build(); - } - - @Bean - public Docket createRestApiAuthorizationLibrary() { - return new Docket(DocumentationType.SWAGGER_2) - .apiInfo(apiInfo()) - .groupName("Authorization Library") - .select() - .apis(RequestHandlerSelectors.basePackage("eu.dnetlib.uoaauthorizationlibrary.controllers")) - .paths(PathSelectors.any()) - .build(); - } - - private ApiInfo apiInfo() { - return new ApiInfoBuilder() - .title(this.apiProperties.getTitle()) - .description(this.apiProperties.getDescription()) - .version(this.apiProperties.getVersion()) - .build(); - } - - private List globalParameterList() { - Parameter authTokenHeader = new ParameterBuilder() - .name("Session") // name of the header - .modelRef(new ModelRef("string")) // data-type of the header - .required(false) - .parameterType("header") - .description("Session ID") - .build(); - return Collections.singletonList(authTokenHeader); - } - - @Override - public void addViewControllers(ViewControllerRegistry registry) { - registry.addRedirectViewController("/v2/api-docs", "/v2/api-docs"); - registry.addRedirectViewController("/swagger-resources/configuration/ui", "/swagger-resources/configuration/ui"); - registry.addRedirectViewController("/swagger-resources/configuration/security", "/swagger-resources/configuration/security"); - registry.addRedirectViewController("/swagger-resources", "/swagger-resources"); - } - - @Override - public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/swagger-ui.html**").addResourceLocations("classpath:/META-INF/resources/swagger-ui.html"); - registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); - } - -} - diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/mongo/IrishMongoConnection.java b/src/main/java/eu/dnetlib/irishmonitorservice/configuration/mongo/IrishMongoConnection.java index 4a85008..f8f6873 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/mongo/IrishMongoConnection.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/configuration/mongo/IrishMongoConnection.java @@ -1,15 +1,15 @@ package eu.dnetlib.irishmonitorservice.configuration.mongo; -import com.mongodb.MongoClient; +import com.mongodb.MongoClientSettings; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import eu.dnetlib.uoamonitorservice.configuration.properties.MongoConfig; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.data.mongodb.MongoDbFactory; import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.data.mongodb.core.SimpleMongoDbFactory; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import java.util.Collections; @@ -18,27 +18,30 @@ import java.util.Collections; @EnableMongoRepositories(basePackages = {"eu.dnetlib.irishmonitorservice.dao"}, mongoTemplateRef = "mongoIrishTemplate") public class IrishMongoConnection { - @Autowired - private MongoConfig mongoConfig; + private final MongoConfig mongoConfig; - @Bean - public MongoDbFactory mongoDbFactory() { - return new SimpleMongoDbFactory(getMongoClient(), mongoConfig.getDatabase()); + @Autowired + public IrishMongoConnection(MongoConfig mongoConfig) { + this.mongoConfig = mongoConfig; } @Bean(name = "mongoIrishTemplate") public MongoTemplate getMongoTemplate() { - return new MongoTemplate(mongoDbFactory()); + return new MongoTemplate(getMongoClient(), mongoConfig.getDatabase()); } private MongoClient getMongoClient() { - if(mongoConfig.getUsername() != null && mongoConfig.getPassword() != null){ - return new MongoClient(Collections.singletonList( - new ServerAddress(mongoConfig.getHost(), mongoConfig.getPort())), - Collections.singletonList(MongoCredential.createCredential(mongoConfig.getUsername(), mongoConfig.getDatabase(), mongoConfig.getPassword().toCharArray()))); - } else { - return new MongoClient(Collections.singletonList(new ServerAddress(mongoConfig.getHost(), mongoConfig.getPort()))); + MongoClientSettings.Builder builder = MongoClientSettings.builder() + .applyToClusterSettings(b -> b.hosts(Collections.singletonList(new ServerAddress(mongoConfig.getHost(), mongoConfig.getPort())))); + if(mongoConfig.getUsername() != null && mongoConfig.getPassword() != null) { + MongoCredential credential = MongoCredential.createCredential( + mongoConfig.getUsername(), + mongoConfig.getDatabase(), + mongoConfig.getPassword().toCharArray() + ); + builder.credential(credential); } + return MongoClients.create(builder.build()); } diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/APIProperties.java b/src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/APIProperties.java deleted file mode 100644 index 230f412..0000000 --- a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/APIProperties.java +++ /dev/null @@ -1,38 +0,0 @@ -package eu.dnetlib.irishmonitorservice.configuration.properties; - -import org.springframework.boot.context.properties.ConfigurationProperties; - -@ConfigurationProperties("api") -public class APIProperties { - - private String title; - private String description; - private String version; - - public APIProperties() { - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } -} diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/StatsToolProperties.java b/src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/Properties.java similarity index 92% rename from src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/StatsToolProperties.java rename to src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/Properties.java index 52da695..e6f688b 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/StatsToolProperties.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/configuration/properties/Properties.java @@ -3,13 +3,13 @@ package eu.dnetlib.irishmonitorservice.configuration.properties; import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties("stats-tool") -public class StatsToolProperties { +public class Properties { private String host; private Indicators rpo; private Indicators rfo; private Indicators repository; - public StatsToolProperties() { + public Properties() { } public String getHost() { diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/controllers/IrishServiceCheckDeployController.java b/src/main/java/eu/dnetlib/irishmonitorservice/controllers/IrishServiceCheckDeployController.java index 543fe3f..f6394af 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/controllers/IrishServiceCheckDeployController.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/controllers/IrishServiceCheckDeployController.java @@ -1,47 +1,37 @@ package eu.dnetlib.irishmonitorservice.controllers; -import eu.dnetlib.irishmonitorservice.configuration.GlobalVars; +import eu.dnetlib.irishmonitorservice.services.DeployService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import java.util.HashMap; import java.util.Map; @RestController -@CrossOrigin(origins = "*") public class IrishServiceCheckDeployController { private final Logger log = LogManager.getLogger(this.getClass()); + private final DeployService service; + @Autowired - private GlobalVars globalVars; + public IrishServiceCheckDeployController(DeployService service) { + this.service = service; + } @RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET) - public String hello() { + public ResponseEntity hello() { log.debug("Hello from irish-monitor-service!"); - return "Hello from irish-monitor-service!"; + return ResponseEntity.ok("Hello from irish-monitor-service!"); } @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") @RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET) - public Map checkEverything() { - Map response = new HashMap<>(); - - if(globalVars.date != null) { - response.put("Date of deploy", globalVars.date.toString()); - } - if(globalVars.getBuildDate() != null) { - response.put("Date of build", globalVars.getBuildDate()); - } - if(globalVars.getVersion() != null) { - response.put("Version", globalVars.getVersion()); - } - - return response; + public ResponseEntity> checkEverything() { + return ResponseEntity.ok(this.service.getProperties()); } } diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/controllers/StakeholderExtendedController.java b/src/main/java/eu/dnetlib/irishmonitorservice/controllers/StakeholderExtendedController.java index 035b683..542c343 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/controllers/StakeholderExtendedController.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/controllers/StakeholderExtendedController.java @@ -4,13 +4,13 @@ import eu.dnetlib.irishmonitorservice.entities.SortBy; import eu.dnetlib.irishmonitorservice.entities.StakeholderExtended; import eu.dnetlib.irishmonitorservice.services.StakeholderExtendedService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.io.UnsupportedEncodingException; import java.util.List; @RestController -@CrossOrigin(origins = "*") @RequestMapping("extended") public class StakeholderExtendedController { private final StakeholderExtendedService service; @@ -21,12 +21,12 @@ public class StakeholderExtendedController { } @RequestMapping(value = "", method = RequestMethod.GET) - public List getStakeholders(@RequestParam("type") String type, @RequestParam(value = "sort", required = false) SortBy sort) throws UnsupportedEncodingException { - return this.service.sortBy(this.service.getVisibleStakeholdersExtended(type), sort); + public ResponseEntity> getStakeholders(@RequestParam("type") String type, @RequestParam(value = "sort", required = false) SortBy sort) throws UnsupportedEncodingException { + return ResponseEntity.ok(this.service.sortBy(this.service.getVisibleStakeholdersExtended(type), sort)); } @RequestMapping(value = "/{stakeholderId}", method = RequestMethod.GET) - public StakeholderExtended getStakeholder(@PathVariable String stakeholderId) throws UnsupportedEncodingException { - return this.service.getStakeholderExtended(stakeholderId); + public ResponseEntity getStakeholder(@PathVariable String stakeholderId) throws UnsupportedEncodingException { + return ResponseEntity.ok(this.service.getStakeholderExtended(stakeholderId)); } } diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/controllers/UserProfileController.java b/src/main/java/eu/dnetlib/irishmonitorservice/controllers/UserProfileController.java index 76c42f2..0d9c7fb 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/controllers/UserProfileController.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/controllers/UserProfileController.java @@ -2,65 +2,45 @@ package eu.dnetlib.irishmonitorservice.controllers; import eu.dnetlib.irishmonitorservice.dao.UserProfileDAO; import eu.dnetlib.irishmonitorservice.entities.UserProfile; -import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException; -import eu.dnetlib.uoaadmintoolslibrary.handlers.ForbiddenException; -import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils; +import eu.dnetlib.uoaadmintoolslibrary.utils.RolesUtils; +import eu.dnetlib.uoaauthorizationlibrary.authorization.exceptions.http.NotFoundException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.web.bind.annotation.*; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; @RestController -@CrossOrigin(origins = "*") @RequestMapping("/user") @PreAuthorize("isAuthenticated()") public class UserProfileController { private final Logger log = LogManager.getLogger(this.getClass()); - @Autowired - private RolesUtils rolesUtils; + private final RolesUtils rolesUtils; + private final UserProfileDAO dao; @Autowired - private UserProfileDAO userDAO; + public UserProfileController(RolesUtils rolesUtils, UserProfileDAO dao) { + this.rolesUtils = rolesUtils; + this.dao = dao; + } @RequestMapping(value = "/save", method = RequestMethod.POST) - public UserProfile saveUserProfile(@RequestBody UserProfile userProfile) { - String aaiId = rolesUtils.getAaiId(); - UserProfile existingUserProfile = userDAO.findByAaiId(aaiId); - if(existingUserProfile != null) { - userProfile.setId(existingUserProfile.getId()); + public ResponseEntity saveUserProfile(@RequestBody UserProfile userProfile) { + userProfile.setAaiId(rolesUtils.getAaiId()); + if(dao.findByAaiId(userProfile.getAaiId()).isPresent()) { + userProfile.setId(dao.findByAaiId(userProfile.getAaiId()).get().getId()); } - userProfile.setAaiId(aaiId); // users can only save/ update their own profiles - return userDAO.save(userProfile); + return ResponseEntity.ok(dao.save(userProfile)); } -// @RequestMapping(value = "/delete/{aaiId}", method = RequestMethod.DELETE) -// public Boolean deleteUserProfileByAaiId(@PathVariable("aaiId") String aaiId) { -// // maybe portal admin could delete any user profile? -// -// String currentAaiId = rolesUtils.getAaiId(); -// if(aaiId != null && currentAaiId.equals(aaiId)) { -// throw new ForbiddenException("User with id: "+currentAaiId + " has not access to update user with id: "+aaiId); -// } -// UserProfile user = userDAO.findByAaiId(aaiId); -// if(user == null) { -// // EXCEPTION - Entity Not Found -// throw new ContentNotFoundException("Delete user profile: No user profile found for : " + aaiId); -// } else { -// userDAO.delete(user.getId()); -// } -// return true; -// } - @RequestMapping(value = "", method = RequestMethod.GET) - public UserProfile getUserProfile() { - String aaiId = rolesUtils.getAaiId(); - UserProfile user = userDAO.findByAaiId(aaiId); - if(user == null) { - // EXCEPTION - Entity Not Found - throw new ContentNotFoundException("No user profile found for: " + aaiId); - } - return user; + public ResponseEntity getUserProfile() { + return ResponseEntity.ok(this.dao.findByAaiId(this.rolesUtils.getAaiId()) + .orElseThrow(() -> new NotFoundException("No user profile found for: " + this.rolesUtils.getAaiId()))); } } diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/dao/MongoDBUserProfileDAO.java b/src/main/java/eu/dnetlib/irishmonitorservice/dao/MongoDBUserProfileDAO.java deleted file mode 100644 index 8ee3335..0000000 --- a/src/main/java/eu/dnetlib/irishmonitorservice/dao/MongoDBUserProfileDAO.java +++ /dev/null @@ -1,20 +0,0 @@ -package eu.dnetlib.irishmonitorservice.dao; - -import eu.dnetlib.irishmonitorservice.entities.UserProfile; -import org.springframework.data.mongodb.repository.MongoRepository; - -import java.util.List; - -public interface MongoDBUserProfileDAO extends UserProfileDAO, MongoRepository { - List findAll(); - - UserProfile findById(String Id); - - UserProfile findByAaiId(String aaiId); - - UserProfile save(UserProfile user); - - void deleteAll(); - - void delete(String id); -} diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/dao/UserProfileDAO.java b/src/main/java/eu/dnetlib/irishmonitorservice/dao/UserProfileDAO.java index 44e7c51..589be0d 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/dao/UserProfileDAO.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/dao/UserProfileDAO.java @@ -1,19 +1,13 @@ package eu.dnetlib.irishmonitorservice.dao; import eu.dnetlib.irishmonitorservice.entities.UserProfile; +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.stereotype.Repository; import java.util.List; +import java.util.Optional; -public interface UserProfileDAO { - List findAll(); - - UserProfile findById(String Id); - - UserProfile findByAaiId(String aaiId); - - UserProfile save(UserProfile user); - - void deleteAll(); - - void delete(String id); +@Repository +public interface UserProfileDAO extends MongoRepository { + Optional findByAaiId(String aaiId); } diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/security/IrishAuthoritiesMapper.java b/src/main/java/eu/dnetlib/irishmonitorservice/security/IrishAuthoritiesMapper.java index 6b9eae3..bc04217 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/security/IrishAuthoritiesMapper.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/security/IrishAuthoritiesMapper.java @@ -2,14 +2,15 @@ package eu.dnetlib.irishmonitorservice.security; import com.google.gson.JsonArray; import com.nimbusds.jwt.JWT; +import eu.dnetlib.authentication.security.DefaultAuthoritiesMapper; +import eu.dnetlib.authentication.security.OpenAIREAuthoritiesMapper; import eu.dnetlib.authentication.utils.AuthoritiesMapper; -import eu.dnetlib.uoaauthorizationlibrary.security.AuthorizationService; -import org.mitre.openid.connect.client.OIDCAuthoritiesMapper; -import org.mitre.openid.connect.model.UserInfo; +import eu.dnetlib.uoaauthorizationlibrary.authorization.security.AuthorizationService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; +import org.springframework.security.oauth2.core.oidc.user.OidcUser; import org.springframework.stereotype.Component; import java.util.Collection; @@ -19,7 +20,7 @@ import java.util.stream.Collectors; @ConditionalOnProperty( value = "authentication.authorities-mapper", havingValue = "irish.eduperson_entitlement") -public class IrishAuthoritiesMapper implements OIDCAuthoritiesMapper { +public class IrishAuthoritiesMapper extends OpenAIREAuthoritiesMapper { private final String domain = "IRISH_"; private final AuthorizationService authorizationService; @@ -29,11 +30,8 @@ public class IrishAuthoritiesMapper implements OIDCAuthoritiesMapper { } @Override - public Collection mapAuthorities(JWT jwtToken, UserInfo userInfo) { - JsonArray entitlements = userInfo.getSource().getAsJsonArray("eduperson_entitlement"); - return AuthoritiesMapper.map(entitlements).stream() - .filter(this::filter) - .map(this::map).collect(Collectors.toSet()); + public Collection mapAuthorities(OidcUser oidcUser) { + return super.mapAuthorities(oidcUser).stream().filter(this::filter).map(this::map).collect(Collectors.toSet()); } diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/services/CacheService.java b/src/main/java/eu/dnetlib/irishmonitorservice/services/CacheService.java index ec02d56..13fb52f 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/services/CacheService.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/services/CacheService.java @@ -1,12 +1,10 @@ package eu.dnetlib.irishmonitorservice.services; -import eu.dnetlib.irishmonitorservice.entities.Data; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.CacheEvict; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -30,20 +28,16 @@ public class CacheService { } public List>> getResponse(String type) throws UnsupportedEncodingException { - switch (type) { - case "funder": - return this.statsToolService.getFunders(); - case "organization": - return this.statsToolService.getOrganizations(); - case "datasource": - return this.statsToolService.getDataSources(); - default: - return null; - } + return switch (type) { + case "funder" -> this.statsToolService.getFunders(); + case "organization" -> this.statsToolService.getOrganizations(); + case "datasource" -> this.statsToolService.getDataSources(); + default -> null; + }; } @Scheduled(cron = "0 0 0 * * *") // Reset cache every day at 00:00 - public void clearCache() throws UnsupportedEncodingException { + public void clearCache() { this.cacheManager.getCache("funder"); this.cacheManager.getCache("organization"); this.cacheManager.getCache("datasource"); diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/services/DeployService.java b/src/main/java/eu/dnetlib/irishmonitorservice/services/DeployService.java new file mode 100644 index 0000000..8f55678 --- /dev/null +++ b/src/main/java/eu/dnetlib/irishmonitorservice/services/DeployService.java @@ -0,0 +1,47 @@ +package eu.dnetlib.irishmonitorservice.services; + +import eu.dnetlib.irishmonitorservice.configuration.mongo.IrishMongoConnection; +import eu.dnetlib.irishmonitorservice.configuration.properties.Properties; +import eu.dnetlib.uoamonitorservice.configuration.GlobalVars; +import org.bson.Document; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +@Service +public class DeployService { + private final IrishMongoConnection mongoConnection; + private final Properties properties; + private final GlobalVars globalVars; + + @Autowired + public DeployService(IrishMongoConnection mongoConnection, Properties properties, GlobalVars globalVars) { + this.mongoConnection = mongoConnection; + this.properties = properties; + this.globalVars = globalVars; + } + + public Map getProperties() { + Map response = new HashMap<>(); + MongoTemplate mt = mongoConnection.getMongoTemplate(); + Document ping = new Document("ping", 1); + try { + Document answer = mt.executeCommand(ping); + response.put("Mongo try: error", String.valueOf(answer.getDouble("ok") == 1.0)); + } catch (Exception e) { + response.put("Mongo catch: error", e.getMessage()); + } + response.put("stats-tool.host", properties.getHost()); + response.put("Date of deploy", GlobalVars.date.toString()); + if (globalVars.getBuildDate() != null) { + response.put("Date of build", globalVars.getBuildDate()); + } + if (globalVars.getVersion() != null) { + response.put("Version", globalVars.getVersion()); + } + return response; + } +} diff --git a/src/main/java/eu/dnetlib/irishmonitorservice/services/StatsToolService.java b/src/main/java/eu/dnetlib/irishmonitorservice/services/StatsToolService.java index ce8711f..f9c4c1c 100644 --- a/src/main/java/eu/dnetlib/irishmonitorservice/services/StatsToolService.java +++ b/src/main/java/eu/dnetlib/irishmonitorservice/services/StatsToolService.java @@ -1,7 +1,7 @@ package eu.dnetlib.irishmonitorservice.services; import eu.dnetlib.irishmonitorservice.configuration.properties.Indicators; -import eu.dnetlib.irishmonitorservice.configuration.properties.StatsToolProperties; +import eu.dnetlib.irishmonitorservice.configuration.properties.Properties; import eu.dnetlib.irishmonitorservice.entities.Data; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -17,6 +17,7 @@ import org.springframework.web.util.UriComponentsBuilder; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.List; @@ -24,28 +25,31 @@ import java.util.List; public class StatsToolService { private final Logger logger = LogManager.getLogger(StatsToolService.class); - @Autowired - private StatsToolProperties properties; + private final Properties properties; + private final RestTemplate restTemplate; @Autowired - private RestTemplate restTemplate; + public StatsToolService(Properties properties, RestTemplate restTemplate) { + this.properties = properties; + this.restTemplate = restTemplate; + } @Cacheable(value = "funder") - public List>> getFunders() throws UnsupportedEncodingException { + public List>> getFunders() { return this.getData(this.properties.getRfo()); } @Cacheable(value = "organization") - public List>> getOrganizations() throws UnsupportedEncodingException { + public List>> getOrganizations() { return this.getData(this.properties.getRpo()); } @Cacheable(value = "datasource") - public List>> getDataSources() throws UnsupportedEncodingException { + public List>> getDataSources() { return this.getData(this.properties.getRepository()); } - public List>> getData(Indicators indicators) throws UnsupportedEncodingException { + public List>> getData(Indicators indicators) { List>> data = new ArrayList<>(); if(indicators != null) { data.add(this.getData(properties.getHost(), indicators.getPublications())); @@ -55,11 +59,11 @@ public class StatsToolService { return data; } - private List> getData(String service, String json) throws UnsupportedEncodingException { + private List> getData(String service, String json) { if(service == null || json == null) { return new ArrayList<>(); } else { - URI uri = UriComponentsBuilder.fromUriString(service + URLDecoder.decode(json, "UTF-8")).build().encode().toUri(); + URI uri = UriComponentsBuilder.fromUriString(service + URLDecoder.decode(json, StandardCharsets.UTF_8)).build().encode().toUri(); try { ResponseEntity response = restTemplate.getForEntity(uri, Data.class); if(response.getStatusCode() == HttpStatus.OK) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index a265757..74b6bbb 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,3 @@ -#static properties -api.title = Irish Monitor Service Documentation Swagger -api.description = Irish Monitor Service is a service that integrates both OpenAIRE login and OpenAIRE Monitor API and provide all functionalities for National Open Access Monitor, Ireland -api.version = ${project.version} +# temp until refactoring the code in admin tools library +spring.main.allow-circular-references=true + diff --git a/src/main/resources/irishmonitorservice.properties b/src/main/resources/irishmonitorservice.properties index 66c8568..c238402 100644 --- a/src/main/resources/irishmonitorservice.properties +++ b/src/main/resources/irishmonitorservice.properties @@ -1,7 +1,10 @@ -irishmonitorservice.globalVars.buildDate=@timestamp@ -irishmonitorservice.globalVars.version=@version@ +irish-monitor-service.global-vars.buildDate=@timestamp@ +irish-monitor-service.global-vars.version=@version@ + +api.title = Irish Monitor Service Documentation Swagger +api.description = Irish Monitor Service is a service that integrates both OpenAIRE login and OpenAIRE Monitor API and provide all functionalities for National Open Access Monitor, Ireland +api.version = ${project.version} -stats-tool.host=https://stats.madgik.di.uoa.gr/stats-api/raw?json= stats-tool.rfo.publications=%7B%22orderBy%22%3A%22yAxis%22%2C%22verbose%22%3Afalse%2C%22series%22%3A%5B%7B%22query%22%3A%7B%22parameters%22%3A%5B%5D%2C%22select%22%3A%5B%7B%22field%22%3A%22publication%22%2C%22aggregate%22%3A%22count%22%7D%2C%7B%22field%22%3A%22publication.project.funder.id%22%2C%22aggregate%22%3Anull%7D%5D%2C%22filters%22%3A%5B%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22publication.project.funder.country%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22IE%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%5D%2C%22entity%22%3A%22publication%22%2C%22profile%22%3A%22ie_monitor%22%2C%22useCache%22%3Afalse%7D%7D%5D%7D stats-tool.rpo.publications=%7B%22orderBy%22%3A%22yAxis%22%2C%22verbose%22%3Afalse%2C%22series%22%3A%5B%7B%22query%22%3A%7B%22parameters%22%3A%5B%5D%2C%22select%22%3A%5B%7B%22field%22%3A%22publication%22%2C%22aggregate%22%3A%22count%22%7D%2C%7B%22field%22%3A%22publication.organization.id%22%2C%22aggregate%22%3Anull%7D%5D%2C%22filters%22%3A%5B%7B%22groupFilters%22%3A%5B%7B%22field%22%3A%22publication.organization.country%22%2C%22type%22%3A%22%3D%22%2C%22values%22%3A%5B%22IE%22%5D%7D%5D%2C%22op%22%3A%22AND%22%7D%5D%2C%22entity%22%3A%22publication%22%2C%22profile%22%3A%22ie_monitor%22%2C%22useCache%22%3Afalse%7D%7D%5D%7D