This is an automated email from the ASF dual-hosted git repository.

dgriffon pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/unomi.git


The following commit(s) were added to refs/heads/master by this push:
     new d0f37fb95 UNOMI-825: add concealed profile properties (#656)
d0f37fb95 is described below

commit d0f37fb951676075718f5cb8cf0cc97a1cc9f4cb
Author: David Griffon <dgrif...@jahia.com>
AuthorDate: Wed Apr 17 09:05:36 2024 +0200

    UNOMI-825: add concealed profile properties (#656)
---
 .../org/apache/unomi/itests/ContextServletIT.java  | 48 ++++++++++++++++++++++
 .../unomi/rest/endpoints/ContextJsonEndpoint.java  | 17 ++++----
 2 files changed, 58 insertions(+), 7 deletions(-)

diff --git a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java 
b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
index 10ed9e137..dfaaa6641 100644
--- a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
@@ -854,6 +854,54 @@ public class ContextServletIT extends BaseIT {
                 DEFAULT_TRYING_TIMEOUT, DEFAULT_TRYING_TRIES);
     }
 
+    @Test
+    public void testConcealedProperties() throws Exception {
+        String sessionId = "test-concealed-property-session-id";
+        // Add custom profile property type
+        PropertyType customPropertyType = new PropertyType(new 
Metadata("customProperty"));
+        customPropertyType.setValueTypeId("text");
+        profileService.setPropertyType(customPropertyType);
+        // New profile with the custom property type
+        Profile profile = new Profile("test-profile-id" + 
System.currentTimeMillis());
+        profile.setProperty("customProperty", "concealedValue");
+        profileService.save(profile);
+
+        Thread.sleep(2000);
+        // Get it from all properties
+        ContextRequest contextRequest = new ContextRequest();
+        contextRequest.setRequiredProfileProperties(Arrays.asList("*"));
+        contextRequest.setProfileId(profile.getItemId());
+        contextRequest.setSessionId(sessionId);
+        HttpPost request = new HttpPost(getFullUrl(CONTEXT_URL));
+        request.setEntity(new 
StringEntity(objectMapper.writeValueAsString(contextRequest), 
ContentType.APPLICATION_JSON));
+        assertEquals(TestUtils.executeContextJSONRequest(request, 
sessionId).getContextResponse().getProfileProperties().get("customProperty"), 
("concealedValue"));
+        // set the property as  concealed
+        customPropertyType.getMetadata().getSystemTags().add("concealed");
+        profileService.deletePropertyType(customPropertyType.getItemId());
+        profileService.setPropertyType(customPropertyType);
+        // Not in all properties
+        request.setEntity(new 
StringEntity(objectMapper.writeValueAsString(contextRequest), 
ContentType.APPLICATION_JSON));
+        assertNull(TestUtils.executeContextJSONRequest(request, 
sessionId).getContextResponse().getProfileProperties().get("customProperty"));
+        // Got it explicitly
+        
contextRequest.setRequiredProfileProperties(Arrays.asList("customProperty"));
+        request.setEntity(new 
StringEntity(objectMapper.writeValueAsString(contextRequest), 
ContentType.APPLICATION_JSON));
+        assertEquals(TestUtils.executeContextJSONRequest(request, 
sessionId).getContextResponse().getProfileProperties().get("customProperty"), 
("concealedValue"));
+        // Got it with all
+        contextRequest.setRequiredProfileProperties(Arrays.asList("*", 
"customProperty"));
+        request.setEntity(new 
StringEntity(objectMapper.writeValueAsString(contextRequest), 
ContentType.APPLICATION_JSON));
+        assertEquals(TestUtils.executeContextJSONRequest(request, 
sessionId).getContextResponse().getProfileProperties().get("customProperty"), 
("concealedValue"));
+
+        // remove the concealed tag on the property type
+        customPropertyType.getMetadata().getSystemTags().remove("concealed");
+        profileService.deletePropertyType(customPropertyType.getItemId());
+        profileService.setPropertyType(customPropertyType);
+
+        // Got it from all properties
+        contextRequest.setRequiredProfileProperties(Arrays.asList("*"));
+        request.setEntity(new 
StringEntity(objectMapper.writeValueAsString(contextRequest), 
ContentType.APPLICATION_JSON));
+        assertEquals(TestUtils.executeContextJSONRequest(request, 
sessionId).getContextResponse().getProfileProperties().get("customProperty"), 
("concealedValue"));
+    }
+
     private Boolean getPersistedControlGroupStatus(SystemPropertiesItem 
systemPropertiesItem, String personalizationId) {
         if(systemPropertiesItem.getSystemProperties() != null && 
systemPropertiesItem.getSystemProperties().containsKey("personalizationStrategyStatus"))
 {
             List<Map<String, Object>> personalizationStrategyStatus = 
(List<Map<String, Object>>) 
systemPropertiesItem.getSystemProperties().get("personalizationStrategyStatus");
diff --git 
a/rest/src/main/java/org/apache/unomi/rest/endpoints/ContextJsonEndpoint.java 
b/rest/src/main/java/org/apache/unomi/rest/endpoints/ContextJsonEndpoint.java
index 407eb5f87..df04e6d6c 100644
--- 
a/rest/src/main/java/org/apache/unomi/rest/endpoints/ContextJsonEndpoint.java
+++ 
b/rest/src/main/java/org/apache/unomi/rest/endpoints/ContextJsonEndpoint.java
@@ -42,13 +42,8 @@ import javax.ws.rs.*;
 import javax.ws.rs.core.Context;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
+import java.util.*;
+import java.util.stream.Collectors;
 
 @WebService
 @Consumes(MediaType.APPLICATION_JSON)
@@ -78,6 +73,8 @@ public class ContextJsonEndpoint {
     private RestServiceUtils restServiceUtils;
     @Reference
     private SchemaService schemaService;
+    @Reference
+    private ProfileService profileService;
 
     @OPTIONS
     @Path("/context.js")
@@ -224,6 +221,12 @@ public class ContextJsonEndpoint {
             Map<String, Object> profileProperties = new 
HashMap<>(eventsRequestContext.getProfile().getProperties());
             if (!contextRequest.getRequiredProfileProperties().contains("*")) {
                 
profileProperties.keySet().retainAll(contextRequest.getRequiredProfileProperties());
+            } else {
+                // get public properties + explicit properties
+                Set<String> concealedProperties = 
profileService.getPropertyTypeBySystemTag("concealed").stream().map(Item::getItemId).collect(Collectors.toSet());
+                // remove requested properties from the filtered properties
+                
concealedProperties.removeAll(contextRequest.getRequiredProfileProperties().stream().filter(p
 -> !p.equals("*")).collect(Collectors.toList()));
+                profileProperties.keySet().removeAll(concealedProperties);
             }
             data.setProfileProperties(profileProperties);
         }

Reply via email to