From 56eb48686d6b9609baebe123b7f0439f2615311e Mon Sep 17 00:00:00 2001 From: "konstantina.galouni" Date: Thu, 22 Apr 2021 09:55:57 +0000 Subject: [PATCH] [Trunk | Orcid Service]: 1. OrcidServiceCheckDeployController.java: [NEW] Controller added and a. "hello()" method (@RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET)) which just prints and returns a greeting message. b. "checkEverything()" method @RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET) only accessible by PORTAL ADMINS which checks connection with db and prints properties. 2. ResultIdAndWork.java & Work.java: Added field "dashboard" (in ResultInWork class, default value is "production_explpre") and its getters and setters. 3. UserTokens.java: Added field "creationDate" and its getters and setters. 4. UserTokensService.java: Call setCreationDate() method with current Date in "saveUserTokens()" method. 5. WorkController.java: Call setDashboard() method with dashboard value from parameter of type ResultIdAndWork in "saveWork()" method. 6. update_db.js: Script with methods "setUserTokenCreationDate()" (use date of first work for this orcid) and "setDashboardInWorks()" (set all to "production_explore"). --- .../OrcidServiceCheckDeployController.java | 74 +++++++++++++++++++ .../controllers/WorkController.java | 6 +- .../entities/ResultIdAndWork.java | 11 ++- .../uoaorcidservice/entities/UserTokens.java | 11 +++ .../uoaorcidservice/entities/Work.java | 9 +++ .../services/UserTokensService.java | 2 + update_db.js | 54 ++++++++++++++ 7 files changed, 160 insertions(+), 7 deletions(-) create mode 100644 src/main/java/eu/dnetlib/uoaorcidservice/controllers/OrcidServiceCheckDeployController.java create mode 100644 update_db.js diff --git a/src/main/java/eu/dnetlib/uoaorcidservice/controllers/OrcidServiceCheckDeployController.java b/src/main/java/eu/dnetlib/uoaorcidservice/controllers/OrcidServiceCheckDeployController.java new file mode 100644 index 0000000..25e7ef9 --- /dev/null +++ b/src/main/java/eu/dnetlib/uoaorcidservice/controllers/OrcidServiceCheckDeployController.java @@ -0,0 +1,74 @@ +package eu.dnetlib.uoaorcidservice.controllers; + +import com.mongodb.BasicDBObject; +import com.mongodb.CommandResult; +import com.mongodb.DBObject; +import eu.dnetlib.uoaorcidservice.configuration.mongo.MongoConnection; +import eu.dnetlib.uoaorcidservice.configuration.properties.MongoConfig; +import eu.dnetlib.uoaorcidservice.configuration.properties.OrcidConfig; +import eu.dnetlib.uoaorcidservice.handlers.utils.AESUtils; +import org.apache.log4j.Logger; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.MongoTemplate; +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 OrcidServiceCheckDeployController { + private final Logger log = Logger.getLogger(this.getClass()); + + @Autowired + private MongoConnection mongoConnection; + + @Autowired + private MongoConfig mongoConfig; + + @Autowired + private OrcidConfig orcidConfig; + + @Autowired + private AESUtils aesUtils; + + @RequestMapping(value = {"", "/health_check"}, method = RequestMethod.GET) + public String hello() { + log.debug("Hello from uoa-orcid-service!"); + return "Hello from uoa-orcid-service!"; + } + + @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)") + @RequestMapping(value = "/health_check/advanced", method = RequestMethod.GET) + public Map checkEverything() { + Map response = new HashMap<>(); + + MongoTemplate mt = mongoConnection.getMongoTemplate(); + DBObject ping = new BasicDBObject("ping", "1"); + try { + CommandResult answer = mt.getDb().command(ping); + response.put("Mongo try: error", answer.getErrorMessage()); + } catch (Exception e) { + response.put("Mongo catch: error", e.getMessage()); + } + + response.put("orcidservice.mongodb.database", mongoConfig.getDatabase()); + response.put("orcidservice.mongodb.host", mongoConfig.getHost()); + response.put("orcidservice.mongodb.port", mongoConfig.getPort()+""); + response.put("orcidservice.mongodb.username", mongoConfig.getUsername() == null ? null : "[unexposed value]"); + response.put("orcidservice.mongodb.password", mongoConfig.getPassword() == null ? null : "[unexposed value]"); + + response.put("orcidservice.orcid.apiURL", orcidConfig.getApiURL()); + response.put("orcidservice.orcid.tokenURL", orcidConfig.getTokenURL()); + response.put("orcidservice.orcid.clientId", orcidConfig.getClientId() == null ? null : "[unexposed value]"); + response.put("orcidservice.orcid.clientSecret", orcidConfig.getClientSecret() == null ? null : "[unexposed value]"); + + response.put("orcidservice.encryption.password", aesUtils.getPassword() == null ? null : "[unexposed value]"); + + return response; + } +} diff --git a/src/main/java/eu/dnetlib/uoaorcidservice/controllers/WorkController.java b/src/main/java/eu/dnetlib/uoaorcidservice/controllers/WorkController.java index 25a7e7d..5d868a9 100644 --- a/src/main/java/eu/dnetlib/uoaorcidservice/controllers/WorkController.java +++ b/src/main/java/eu/dnetlib/uoaorcidservice/controllers/WorkController.java @@ -7,15 +7,11 @@ import eu.dnetlib.uoaorcidservice.entities.ResultIdAndWork; import eu.dnetlib.uoaorcidservice.entities.UserTokens; import eu.dnetlib.uoaorcidservice.entities.Work; import eu.dnetlib.uoaorcidservice.handlers.ConflictException; -import eu.dnetlib.uoaorcidservice.handlers.ForbiddenException; import eu.dnetlib.uoaorcidservice.services.UserTokensService; import eu.dnetlib.uoaorcidservice.services.WorkService; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.util.Pair; import org.springframework.http.*; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.security.access.AccessDeniedException; import org.springframework.security.access.AuthorizationServiceException; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; @@ -34,7 +30,6 @@ import java.security.spec.InvalidKeySpecException; import java.util.*; @RestController -//@RequestMapping("/orcid") @PreAuthorize("isAuthenticated()") @CrossOrigin(origins = "*") public class WorkController { @@ -167,6 +162,7 @@ public class WorkController { workToSave.setOrcid(userOrcid); workToSave.setCreationDate(date); workToSave.setUpdateDate(date); + workToSave.setDashboard(result.getDashboard()); HttpHeaders responseHeaders = response.getHeaders(); String locationPath = responseHeaders.getLocation().toString(); diff --git a/src/main/java/eu/dnetlib/uoaorcidservice/entities/ResultIdAndWork.java b/src/main/java/eu/dnetlib/uoaorcidservice/entities/ResultIdAndWork.java index 5108ae6..11776cc 100644 --- a/src/main/java/eu/dnetlib/uoaorcidservice/entities/ResultIdAndWork.java +++ b/src/main/java/eu/dnetlib/uoaorcidservice/entities/ResultIdAndWork.java @@ -1,11 +1,18 @@ package eu.dnetlib.uoaorcidservice.entities; -import java.util.List; - public class ResultIdAndWork { + private String dashboard = "production_explore"; String[] pids; Object work; + public String getDashboard() { + return dashboard; + } + + public void setDashboard(String dashboard) { + this.dashboard = dashboard; + } + public String[] getPids() { return pids; } diff --git a/src/main/java/eu/dnetlib/uoaorcidservice/entities/UserTokens.java b/src/main/java/eu/dnetlib/uoaorcidservice/entities/UserTokens.java index 0adbd2e..71aa6c9 100644 --- a/src/main/java/eu/dnetlib/uoaorcidservice/entities/UserTokens.java +++ b/src/main/java/eu/dnetlib/uoaorcidservice/entities/UserTokens.java @@ -3,6 +3,8 @@ package eu.dnetlib.uoaorcidservice.entities; import com.fasterxml.jackson.annotation.JsonProperty; import org.springframework.data.annotation.Id; +import java.util.Date; + public class UserTokens { @Id //@JsonProperty("_id") @@ -16,6 +18,7 @@ public class UserTokens { private String refresh_token; private String expires_in; private String scope; + private Date creationDate; // public String getId() { @@ -89,4 +92,12 @@ public class UserTokens { public void setName(String name) { this.name = name; } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } } diff --git a/src/main/java/eu/dnetlib/uoaorcidservice/entities/Work.java b/src/main/java/eu/dnetlib/uoaorcidservice/entities/Work.java index 27b25de..1f9fe98 100644 --- a/src/main/java/eu/dnetlib/uoaorcidservice/entities/Work.java +++ b/src/main/java/eu/dnetlib/uoaorcidservice/entities/Work.java @@ -15,6 +15,7 @@ public class Work { private String orcid; private Date creationDate; private Date updateDate; + private String dashboard; public String getId() { return id; @@ -63,4 +64,12 @@ public class Work { public void setUpdateDate(Date updateDate) { this.updateDate = updateDate; } + + public String getDashboard() { + return dashboard; + } + + public void setDashboard(String dashboard) { + this.dashboard = dashboard; + } } diff --git a/src/main/java/eu/dnetlib/uoaorcidservice/services/UserTokensService.java b/src/main/java/eu/dnetlib/uoaorcidservice/services/UserTokensService.java index eccf46f..a2eaf8b 100644 --- a/src/main/java/eu/dnetlib/uoaorcidservice/services/UserTokensService.java +++ b/src/main/java/eu/dnetlib/uoaorcidservice/services/UserTokensService.java @@ -21,6 +21,7 @@ import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.InvalidKeySpecException; +import java.util.Date; import java.util.List; @Service @@ -97,6 +98,7 @@ public class UserTokensService { } public void saveUserTokens(UserTokens userTokens) throws InvalidKeySpecException, NoSuchAlgorithmException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, IOException { + userTokens.setCreationDate(new Date()); userTokens.setAaiId(rolesUtils.getAaiId()); userTokens = encryptTokens(userTokens); diff --git a/update_db.js b/update_db.js new file mode 100644 index 0000000..aff464e --- /dev/null +++ b/update_db.js @@ -0,0 +1,54 @@ +//version compatibility: 1.0.0-SNAPSHOT +print("here"); + +function setUserTokenCreationDate(){ + print("\n\n setUserTokenCreationDate \n\n"); + + works = db.work.aggregate( + [ + { $sort: { orcid: 1, creationDate: 1 } }, + { + $group: + { + _id: "$orcid", + creationDate: { $first: "$creationDate" } + } + } + ] + ); + + while (works.hasNext()) { + var work = works.next(); + print("\n\n"); + print("DATE OF FIRST WORK FOR ORCID: ",tojson(work)); + userTokens = db.userTokens.find({"orcid": work['_id']}).map(function (userToken) { return userToken; }); + + for(var i=0; i