From c8bc40913791441f3e076f07fc79313cc8e134b3 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Mon, 27 Nov 2023 16:46:50 +0100 Subject: [PATCH 1/7] Add logging of downloading restricted bitstreams. --- .../clarin/ClarinMatomoBitstreamTracker.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java index f3b6dc9ea4c4..c52a3ac015cb 100644 --- a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java +++ b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java @@ -22,6 +22,7 @@ import org.dspace.content.service.ItemService; import org.dspace.content.service.clarin.ClarinItemService; import org.dspace.core.Context; +import org.dspace.eperson.EPerson; import org.dspace.services.ConfigurationService; import org.dspace.services.factory.DSpaceServicesFactory; import org.matomo.java.tracking.CustomVariable; @@ -132,6 +133,19 @@ public void trackBitstreamDownload(Context context, HttpServletRequest request, return; } + // Log the user which is downloading the restricted bitstream + this.logUserDownloadingBitstream(context, bit); + // Track the bitstream downloading event trackPage(context, request, item, "Bitstream Download / Single File"); } + + private void logUserDownloadingBitstream(Context context, Bitstream bit) { + EPerson eperson = context.getCurrentUser(); + if (Objects.isNull(eperson)) { + log.error("Cannot log user which is downloading restricted bitstream."); + } + + log.info("User: " + eperson.getFullName() + " with ID: " + eperson.getID() + " is downloading restricted" + + " bitstream " + bit.getName() + " with ID: " + bit.getID()); + } } From 39343bb91dbe8bfa5c29b2514e7effec3ced0a61 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Tue, 28 Nov 2023 16:34:07 +0100 Subject: [PATCH 2/7] Log downloading of every bitstream not only restricted ones. --- .../app/statistics/clarin/ClarinMatomoBitstreamTracker.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java index c52a3ac015cb..d27302057740 100644 --- a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java +++ b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java @@ -142,10 +142,12 @@ public void trackBitstreamDownload(Context context, HttpServletRequest request, private void logUserDownloadingBitstream(Context context, Bitstream bit) { EPerson eperson = context.getCurrentUser(); if (Objects.isNull(eperson)) { - log.error("Cannot log user which is downloading restricted bitstream."); + log.info("ANONYMOUS user is trying to download bitstream " + bit.getName() + " with ID: " + + bit.getID()); + return; } - log.info("User: " + eperson.getFullName() + " with ID: " + eperson.getID() + " is downloading restricted" + + log.info("User: " + eperson.getFullName() + " with ID: " + eperson.getID() + " is downloading" + " bitstream " + bit.getName() + " with ID: " + bit.getID()); } } From 8e3c24c53c2591e669e4abf6d7aac729c0ddbc2c Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Tue, 28 Nov 2023 16:34:35 +0100 Subject: [PATCH 3/7] Added docs --- .../app/statistics/clarin/ClarinMatomoBitstreamTracker.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java index d27302057740..720d6e520f3d 100644 --- a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java +++ b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java @@ -139,6 +139,11 @@ public void trackBitstreamDownload(Context context, HttpServletRequest request, trackPage(context, request, item, "Bitstream Download / Single File"); } + /** + * Log the user which is downloading the bitstream + * @param context DSpace context object + * @param bit Bitstream which is downloading + */ private void logUserDownloadingBitstream(Context context, Bitstream bit) { EPerson eperson = context.getCurrentUser(); if (Objects.isNull(eperson)) { From 5f14d0647d79a39a2e931fb2ed07b94ae33fb2ef Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 30 Nov 2023 09:05:38 +0100 Subject: [PATCH 4/7] Updated comment --- .../app/statistics/clarin/ClarinMatomoBitstreamTracker.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java index 720d6e520f3d..770f0810bfca 100644 --- a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java +++ b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java @@ -133,7 +133,7 @@ public void trackBitstreamDownload(Context context, HttpServletRequest request, return; } - // Log the user which is downloading the restricted bitstream + // Log the user which is downloading the bitstream this.logUserDownloadingBitstream(context, bit); // Track the bitstream downloading event trackPage(context, request, item, "Bitstream Download / Single File"); From 81e950a42178debdceb3913ee7eabb4cf90688e0 Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 30 Nov 2023 10:53:50 +0100 Subject: [PATCH 5/7] Refactored logging. --- .../clarin/ClarinMatomoBitstreamTracker.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java index 770f0810bfca..08287caa6ce8 100644 --- a/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java +++ b/dspace-api/src/main/java/org/dspace/app/statistics/clarin/ClarinMatomoBitstreamTracker.java @@ -8,6 +8,7 @@ package org.dspace.app.statistics.clarin; import java.sql.SQLException; +import java.text.MessageFormat; import java.util.List; import java.util.Objects; import javax.servlet.http.HttpServletRequest; @@ -146,13 +147,11 @@ public void trackBitstreamDownload(Context context, HttpServletRequest request, */ private void logUserDownloadingBitstream(Context context, Bitstream bit) { EPerson eperson = context.getCurrentUser(); - if (Objects.isNull(eperson)) { - log.info("ANONYMOUS user is trying to download bitstream " + bit.getName() + " with ID: " + - bit.getID()); - return; - } + String pattern = "The user name: {0}, uuid: {1} is downloading bitstream name: {2}, uuid: {3}."; + String logMessage = Objects.isNull(eperson) + ? MessageFormat.format(pattern, "ANONYMOUS", "null", bit.getName(), bit.getID()) + : MessageFormat.format(pattern, eperson.getFullName(), eperson.getID(), bit.getName(), bit.getID()); - log.info("User: " + eperson.getFullName() + " with ID: " + eperson.getID() + " is downloading" + - " bitstream " + bit.getName() + " with ID: " + bit.getID()); + log.info(logMessage); } } From 1b61fea3318cb580a301cdade8fc4e38e8c3ca9a Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 30 Nov 2023 15:15:48 +0100 Subject: [PATCH 6/7] The user registration is added when the eperson is created by the ui. --- .../clarin/ClarinUserRegistration.java | 4 ++ .../repository/EPersonRestRepository.java | 15 +++++++ .../app/rest/EPersonRestRepositoryIT.java | 45 ++++++++++++++++++- 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinUserRegistration.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinUserRegistration.java index a3e3666a01f4..8c8fd8def9b6 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinUserRegistration.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinUserRegistration.java @@ -28,8 +28,12 @@ @Table(name = "user_registration") public class ClarinUserRegistration implements ReloadableEntity { + // Anonymous user public static final String ANONYMOUS_USER_REGISTRATION = "anonymous"; + // Registered user without organization + public static final String UNKNOWN_USER_REGISTRATION = "Unknown"; + private static Logger log = org.apache.logging.log4j.LogManager.getLogger(ClarinUserRegistration.class); @Id diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java index 8dd6ed90b1b0..2d381a6abb55 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java @@ -7,6 +7,8 @@ */ package org.dspace.app.rest.repository; +import static org.dspace.content.clarin.ClarinUserRegistration.UNKNOWN_USER_REGISTRATION; + import java.io.IOException; import java.sql.SQLException; import java.util.Arrays; @@ -35,6 +37,8 @@ import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.service.AuthorizeService; import org.dspace.authorize.service.ValidatePasswordService; +import org.dspace.content.clarin.ClarinUserRegistration; +import org.dspace.content.service.clarin.ClarinUserRegistrationService; import org.dspace.core.Context; import org.dspace.eperson.EPerson; import org.dspace.eperson.EmptyWorkflowGroupException; @@ -79,6 +83,9 @@ public class EPersonRestRepository extends DSpaceObjectRestRepository idRef = new AtomicReference(); AtomicReference idRefNoEmbeds = new AtomicReference(); + AtomicReference idRefUserDataReg = new AtomicReference(); + AtomicReference idRefUserDataFullReg = new AtomicReference(); String authToken = getAuthToken(admin.getEmail(), password); @@ -155,11 +158,51 @@ public void createTest() throws Exception { .andExpect(content().contentType(contentType)) .andExpect(jsonPath("$", HalMatcher.matchNoEmbeds())) .andDo(result -> idRefNoEmbeds - .set(UUID.fromString(read(result.getResponse().getContentAsString(), "$.id"))));; + .set(UUID.fromString(read(result.getResponse().getContentAsString(), "$.id")))); + + // Check that the user registration for test data user has been created + getClient(authToken).perform(get("/api/core/clarinuserregistration/search/byEPerson") + .param("userUUID", String.valueOf(idRef.get())) + .contentType(contentType)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.page.totalElements", is(1))) + .andExpect(jsonPath( + "$._embedded.clarinuserregistrations[0].id", is(not(empty())))) + .andExpect(jsonPath( + "$._embedded.clarinuserregistrations[0].email", is("createtest@example.com"))) + .andExpect(jsonPath( + "$._embedded.clarinuserregistrations[0].confirmation", is(true))) + .andExpect(jsonPath( + "$._embedded.clarinuserregistrations[0].ePersonID", is(idRef.get().toString()))) + .andDo(result -> idRefUserDataReg + .set(read(result.getResponse().getContentAsString(), + "$._embedded.clarinuserregistrations[0].id"))); + + // Check that the user registration for test data full user has been created + getClient(authToken).perform(get("/api/core/clarinuserregistration/search/byEPerson") + .param("userUUID", String.valueOf(idRefNoEmbeds.get())) + .contentType(contentType)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.page.totalElements", is(1))) + .andExpect(jsonPath( + "$._embedded.clarinuserregistrations[0].id", is(not(empty())))) + .andExpect(jsonPath( + "$._embedded.clarinuserregistrations[0].email", + is("createtestfull@example.com"))) + .andExpect(jsonPath( + "$._embedded.clarinuserregistrations[0].confirmation", is(true))) + .andExpect(jsonPath( + "$._embedded.clarinuserregistrations[0].ePersonID", + is(idRefNoEmbeds.get().toString()))) + .andDo(result -> idRefUserDataFullReg + .set(read(result.getResponse().getContentAsString(), + "$._embedded.clarinuserregistrations[0].id"))); } finally { EPersonBuilder.deleteEPerson(idRef.get()); EPersonBuilder.deleteEPerson(idRefNoEmbeds.get()); + ClarinUserRegistrationBuilder.deleteClarinUserRegistration(idRefUserDataReg.get()); + ClarinUserRegistrationBuilder.deleteClarinUserRegistration(idRefUserDataFullReg.get()); } } From 1a0ab8c4eff4a9ee9e3a2af84bac89dc8ad8b2cd Mon Sep 17 00:00:00 2001 From: milanmajchrak Date: Thu, 30 Nov 2023 15:19:44 +0100 Subject: [PATCH 7/7] Revert "The user registration is added when the eperson is created by the ui." This reverts commit 1b61fea3318cb580a301cdade8fc4e38e8c3ca9a. --- .../clarin/ClarinUserRegistration.java | 4 -- .../repository/EPersonRestRepository.java | 15 ------- .../app/rest/EPersonRestRepositoryIT.java | 45 +------------------ 3 files changed, 1 insertion(+), 63 deletions(-) diff --git a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinUserRegistration.java b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinUserRegistration.java index 8c8fd8def9b6..a3e3666a01f4 100644 --- a/dspace-api/src/main/java/org/dspace/content/clarin/ClarinUserRegistration.java +++ b/dspace-api/src/main/java/org/dspace/content/clarin/ClarinUserRegistration.java @@ -28,12 +28,8 @@ @Table(name = "user_registration") public class ClarinUserRegistration implements ReloadableEntity { - // Anonymous user public static final String ANONYMOUS_USER_REGISTRATION = "anonymous"; - // Registered user without organization - public static final String UNKNOWN_USER_REGISTRATION = "Unknown"; - private static Logger log = org.apache.logging.log4j.LogManager.getLogger(ClarinUserRegistration.class); @Id diff --git a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java index 2d381a6abb55..8dd6ed90b1b0 100644 --- a/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java +++ b/dspace-server-webapp/src/main/java/org/dspace/app/rest/repository/EPersonRestRepository.java @@ -7,8 +7,6 @@ */ package org.dspace.app.rest.repository; -import static org.dspace.content.clarin.ClarinUserRegistration.UNKNOWN_USER_REGISTRATION; - import java.io.IOException; import java.sql.SQLException; import java.util.Arrays; @@ -37,8 +35,6 @@ import org.dspace.authorize.AuthorizeException; import org.dspace.authorize.service.AuthorizeService; import org.dspace.authorize.service.ValidatePasswordService; -import org.dspace.content.clarin.ClarinUserRegistration; -import org.dspace.content.service.clarin.ClarinUserRegistrationService; import org.dspace.core.Context; import org.dspace.eperson.EPerson; import org.dspace.eperson.EmptyWorkflowGroupException; @@ -83,9 +79,6 @@ public class EPersonRestRepository extends DSpaceObjectRestRepository idRef = new AtomicReference(); AtomicReference idRefNoEmbeds = new AtomicReference(); - AtomicReference idRefUserDataReg = new AtomicReference(); - AtomicReference idRefUserDataFullReg = new AtomicReference(); String authToken = getAuthToken(admin.getEmail(), password); @@ -158,51 +155,11 @@ public void createTest() throws Exception { .andExpect(content().contentType(contentType)) .andExpect(jsonPath("$", HalMatcher.matchNoEmbeds())) .andDo(result -> idRefNoEmbeds - .set(UUID.fromString(read(result.getResponse().getContentAsString(), "$.id")))); - - // Check that the user registration for test data user has been created - getClient(authToken).perform(get("/api/core/clarinuserregistration/search/byEPerson") - .param("userUUID", String.valueOf(idRef.get())) - .contentType(contentType)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.page.totalElements", is(1))) - .andExpect(jsonPath( - "$._embedded.clarinuserregistrations[0].id", is(not(empty())))) - .andExpect(jsonPath( - "$._embedded.clarinuserregistrations[0].email", is("createtest@example.com"))) - .andExpect(jsonPath( - "$._embedded.clarinuserregistrations[0].confirmation", is(true))) - .andExpect(jsonPath( - "$._embedded.clarinuserregistrations[0].ePersonID", is(idRef.get().toString()))) - .andDo(result -> idRefUserDataReg - .set(read(result.getResponse().getContentAsString(), - "$._embedded.clarinuserregistrations[0].id"))); - - // Check that the user registration for test data full user has been created - getClient(authToken).perform(get("/api/core/clarinuserregistration/search/byEPerson") - .param("userUUID", String.valueOf(idRefNoEmbeds.get())) - .contentType(contentType)) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.page.totalElements", is(1))) - .andExpect(jsonPath( - "$._embedded.clarinuserregistrations[0].id", is(not(empty())))) - .andExpect(jsonPath( - "$._embedded.clarinuserregistrations[0].email", - is("createtestfull@example.com"))) - .andExpect(jsonPath( - "$._embedded.clarinuserregistrations[0].confirmation", is(true))) - .andExpect(jsonPath( - "$._embedded.clarinuserregistrations[0].ePersonID", - is(idRefNoEmbeds.get().toString()))) - .andDo(result -> idRefUserDataFullReg - .set(read(result.getResponse().getContentAsString(), - "$._embedded.clarinuserregistrations[0].id"))); + .set(UUID.fromString(read(result.getResponse().getContentAsString(), "$.id"))));; } finally { EPersonBuilder.deleteEPerson(idRef.get()); EPersonBuilder.deleteEPerson(idRefNoEmbeds.get()); - ClarinUserRegistrationBuilder.deleteClarinUserRegistration(idRefUserDataReg.get()); - ClarinUserRegistrationBuilder.deleteClarinUserRegistration(idRefUserDataFullReg.get()); } }