Repository: incubator-unomi Updated Branches: refs/heads/master 4b68e2f74 -> f061241c5
UNOMI-39 : handle anonymous browsing Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/f061241c Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/f061241c Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/f061241c Branch: refs/heads/master Commit: f061241c50313e7740c9647be83809a5b2f208f8 Parents: 4b68e2f Author: Thomas Draier <[email protected]> Authored: Wed Jul 6 16:07:35 2016 +0200 Committer: Thomas Draier <[email protected]> Committed: Wed Jul 6 16:07:35 2016 +0200 ---------------------------------------------------------------------- .../main/java/org/apache/unomi/api/Profile.java | 6 ++ .../unomi/api/services/PrivacyService.java | 7 ++- .../privacy/rest/PrivacyServiceEndPoint.java | 6 +- .../privacy/internal/PrivacyServiceImpl.java | 64 +++++++++++--------- .../AllEventToProfilePropertiesAction.java | 3 + .../actions/EvaluateProfileSegmentsAction.java | 3 + .../actions/EventToProfilePropertyAction.java | 3 + .../actions/MergeProfilesOnPropertyAction.java | 2 +- .../baseplugin/actions/SetPropertyAction.java | 7 ++- .../BooleanConditionESQueryBuilder.java | 3 +- .../org/apache/unomi/web/ContextServlet.java | 34 ++++++----- .../unomi/web/EventsCollectorServlet.java | 26 +++----- 12 files changed, 96 insertions(+), 68 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/api/src/main/java/org/apache/unomi/api/Profile.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/unomi/api/Profile.java b/api/src/main/java/org/apache/unomi/api/Profile.java index 7163e00..4490bb1 100644 --- a/api/src/main/java/org/apache/unomi/api/Profile.java +++ b/api/src/main/java/org/apache/unomi/api/Profile.java @@ -194,6 +194,12 @@ public class Profile extends Item { this.scores = scores; } + @XmlTransient + public boolean isAnonymousProfile() { + Boolean anonymous = (Boolean) getSystemProperties().get("isAnonymousProfile"); + return anonymous != null && anonymous; + } + @Override public String toString() { return new StringBuilder(512).append("{id: \"").append(getItemId()).append("\", segments: ") http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/api/src/main/java/org/apache/unomi/api/services/PrivacyService.java ---------------------------------------------------------------------- diff --git a/api/src/main/java/org/apache/unomi/api/services/PrivacyService.java b/api/src/main/java/org/apache/unomi/api/services/PrivacyService.java index 999db48..6d22678 100644 --- a/api/src/main/java/org/apache/unomi/api/services/PrivacyService.java +++ b/api/src/main/java/org/apache/unomi/api/services/PrivacyService.java @@ -17,6 +17,7 @@ package org.apache.unomi.api.services; +import org.apache.unomi.api.Profile; import org.apache.unomi.api.ServerInfo; import java.util.List; @@ -36,9 +37,11 @@ public interface PrivacyService { Boolean deleteProfileData(String profileId); - Boolean setAnonymous(String profileId, boolean anonymous); + Boolean setRequireAnonymousBrowsing(String profileId, boolean anonymous); - Boolean isAnonymous(String profileId); + Boolean isRequireAnonymousBrowsing(String profileId); + + Profile getAnonymousProfile(); List<String> getFilteredEventTypes(String profileId); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/extensions/privacy-extension/rest/src/main/java/org/apache/unomi/privacy/rest/PrivacyServiceEndPoint.java ---------------------------------------------------------------------- diff --git a/extensions/privacy-extension/rest/src/main/java/org/apache/unomi/privacy/rest/PrivacyServiceEndPoint.java b/extensions/privacy-extension/rest/src/main/java/org/apache/unomi/privacy/rest/PrivacyServiceEndPoint.java index 45a6146..8310863 100644 --- a/extensions/privacy-extension/rest/src/main/java/org/apache/unomi/privacy/rest/PrivacyServiceEndPoint.java +++ b/extensions/privacy-extension/rest/src/main/java/org/apache/unomi/privacy/rest/PrivacyServiceEndPoint.java @@ -80,20 +80,20 @@ public class PrivacyServiceEndPoint { @GET @Path("/profiles/{profileId}/anonymous") public Boolean isAnonymous(@PathParam("profileId") String profileId) { - return privacyService.isAnonymous(profileId); + return privacyService.isRequireAnonymousBrowsing(profileId); } @POST @Path("/profiles/{profileId}/anonymous") public Response activateAnonymousSurfing(@PathParam("profileId") String profileId) { - privacyService.setAnonymous(profileId, true); + privacyService.setRequireAnonymousBrowsing(profileId, true); return Response.ok().build(); } @DELETE @Path("/profiles/{profileId}/anonymous") public Response deactivateAnonymousSurfing(@PathParam("profileId") String profileId) { - privacyService.setAnonymous(profileId, false); + privacyService.setRequireAnonymousBrowsing(profileId, false); return Response.ok().build(); } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/extensions/privacy-extension/services/src/main/java/org/apache/unomi/privacy/internal/PrivacyServiceImpl.java ---------------------------------------------------------------------- diff --git a/extensions/privacy-extension/services/src/main/java/org/apache/unomi/privacy/internal/PrivacyServiceImpl.java b/extensions/privacy-extension/services/src/main/java/org/apache/unomi/privacy/internal/PrivacyServiceImpl.java index 9ba5ddd..c885dc9 100644 --- a/extensions/privacy-extension/services/src/main/java/org/apache/unomi/privacy/internal/PrivacyServiceImpl.java +++ b/extensions/privacy-extension/services/src/main/java/org/apache/unomi/privacy/internal/PrivacyServiceImpl.java @@ -89,28 +89,28 @@ public class PrivacyServiceImpl implements PrivacyService { @Override public String anonymizeBrowsingData(String profileId) { - Profile profile = profileService.load(profileId); - if (profile == null) { - return profileId; - } - Profile newProfile = new Profile(UUID.randomUUID().toString()); - // first we copy all the profile data to the new Profile - newProfile.setMergedWith(profile.getMergedWith()); - newProfile.setProperties(profile.getProperties()); - newProfile.setScores(profile.getScores()); - newProfile.setSegments(profile.getSegments()); - newProfile.setSystemProperties(profile.getSystemProperties()); - newProfile.setScope(profile.getScope()); - profileService.save(newProfile); - // then we clear the old profile of all data - profile.setMergedWith(null); - profile.setProperties(new HashMap<String, Object>()); - profile.setScores(new HashMap<String, Integer>()); - profile.setSegments(new HashSet<String>()); - profile.setSystemProperties(new HashMap<String, Object>()); - profile.setScope(null); - profileService.save(profile); - return newProfile.getItemId(); +// Profile profile = profileService.load(profileId); +// if (profile == null) { +// return profileId; +// } +// Profile newProfile = new Profile(UUID.randomUUID().toString()); +// // first we copy all the profile data to the new Profile +// newProfile.setMergedWith(profile.getMergedWith()); +// newProfile.setProperties(profile.getProperties()); +// newProfile.setScores(profile.getScores()); +// newProfile.setSegments(profile.getSegments()); +// newProfile.setSystemProperties(profile.getSystemProperties()); +// newProfile.setScope(profile.getScope()); +// profileService.save(newProfile); +// // then we clear the old profile of all data +// profile.setMergedWith(null); +// profile.setProperties(new HashMap<String, Object>()); +// profile.setScores(new HashMap<String, Integer>()); +// profile.setSegments(new HashSet<String>()); +// profile.setSystemProperties(new HashMap<String, Object>()); +// profile.setScope(null); +// profileService.save(profile); + return null; } @Override @@ -132,23 +132,31 @@ public class PrivacyServiceImpl implements PrivacyService { } @Override - public Boolean setAnonymous(String profileId, boolean anonymous) { + public Boolean setRequireAnonymousBrowsing(String profileId, boolean anonymous) { Profile profile = profileService.load(profileId); if (profile == null) { return false; } - profile.setProperty("anonymous", anonymous); + profile.getSystemProperties().put("requireAnonymousProfile", anonymous); profileService.save(profile); return true; } - public Boolean isAnonymous(String profileId) { + public Boolean isRequireAnonymousBrowsing(String profileId) { Profile profile = profileService.load(profileId); if (profile == null) { - return null; + return false; } - Boolean anonymous = (Boolean) profile.getProperty("anonymous"); - return anonymous; + Boolean anonymous = (Boolean) profile.getSystemProperties().get("requireAnonymousProfile"); + return anonymous != null && anonymous; + } + + public Profile getAnonymousProfile() { + String id = UUID.randomUUID().toString(); + Profile anonymousProfile = new Profile(id); + anonymousProfile.getSystemProperties().put("isAnonymousProfile", true); + profileService.save(anonymousProfile); + return anonymousProfile; } @Override http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/AllEventToProfilePropertiesAction.java ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/AllEventToProfilePropertiesAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/AllEventToProfilePropertiesAction.java index 341db32..33b79e9 100644 --- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/AllEventToProfilePropertiesAction.java +++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/AllEventToProfilePropertiesAction.java @@ -37,6 +37,9 @@ public class AllEventToProfilePropertiesAction implements ActionExecutor { @SuppressWarnings({ "unchecked", "rawtypes" }) public int execute(Action action, Event event) { + if (event.getProfile().isAnonymousProfile()) { + return EventService.NO_CHANGE; + } boolean changed = false; Map<String, Object> properties = new HashMap<String,Object>(); if (event.getProperties() != null) { http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EvaluateProfileSegmentsAction.java ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EvaluateProfileSegmentsAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EvaluateProfileSegmentsAction.java index cd7ac85..2002eff 100644 --- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EvaluateProfileSegmentsAction.java +++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EvaluateProfileSegmentsAction.java @@ -41,6 +41,9 @@ public class EvaluateProfileSegmentsAction implements ActionExecutor { @Override public int execute(Action action, Event event) { + if (event.getProfile().isAnonymousProfile()) { + return EventService.NO_CHANGE; + } boolean updated = false; SegmentsAndScores segmentsAndScoringForProfile = segmentService.getSegmentsAndScoresForProfile(event.getProfile()); Set<String> segments = segmentsAndScoringForProfile.getSegments(); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EventToProfilePropertyAction.java ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EventToProfilePropertyAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EventToProfilePropertyAction.java index 2e7617c..f8391d7 100644 --- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EventToProfilePropertyAction.java +++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/EventToProfilePropertyAction.java @@ -28,6 +28,9 @@ import org.apache.unomi.api.services.EventService; public class EventToProfilePropertyAction implements ActionExecutor { public int execute(Action action, Event event) { + if (event.getProfile().isAnonymousProfile()) { + return EventService.NO_CHANGE; + } String eventPropertyName = (String) action.getParameterValues().get("eventPropertyName"); String profilePropertyName = (String) action.getParameterValues().get("profilePropertyName"); if (event.getProfile().getProperty(profilePropertyName) == null || !event.getProfile().getProperty(profilePropertyName).equals(event.getProperty(eventPropertyName))) { http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/MergeProfilesOnPropertyAction.java ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/MergeProfilesOnPropertyAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/MergeProfilesOnPropertyAction.java index 64ca3e9..2edc98b 100644 --- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/MergeProfilesOnPropertyAction.java +++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/MergeProfilesOnPropertyAction.java @@ -95,7 +95,7 @@ public class MergeProfilesOnPropertyAction implements ActionExecutor { public int execute(Action action, Event event) { Profile profile = event.getProfile(); - if (profile instanceof Persona) { + if (profile instanceof Persona || profile.isAnonymousProfile()) { return EventService.NO_CHANGE; } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java index 55c1210..0b1a877 100644 --- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java +++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/actions/SetPropertyAction.java @@ -36,6 +36,12 @@ public class SetPropertyAction implements ActionExecutor { } public int execute(Action action, Event event) { + boolean storeInSession = Boolean.TRUE.equals(action.getParameterValues().get("storeInSession")); + + if (event.getProfile().isAnonymousProfile() && !storeInSession) { + return EventService.NO_CHANGE; + } + Object propertyValue = action.getParameterValues().get("setPropertyValue"); Object propertyValueInteger = action.getParameterValues().get("setPropertyValueInteger"); @@ -50,7 +56,6 @@ public class SetPropertyAction implements ActionExecutor { } String propertyName = (String) action.getParameterValues().get("setPropertyName"); - boolean storeInSession = Boolean.TRUE.equals(action.getParameterValues().get("storeInSession")); Object target = storeInSession ? event.getSession() : event.getProfile(); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/BooleanConditionESQueryBuilder.java ---------------------------------------------------------------------- diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/BooleanConditionESQueryBuilder.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/BooleanConditionESQueryBuilder.java index 919ce54..c41309b 100644 --- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/BooleanConditionESQueryBuilder.java +++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/BooleanConditionESQueryBuilder.java @@ -23,6 +23,7 @@ import org.apache.unomi.persistence.elasticsearch.conditions.ConditionESQueryBui import org.elasticsearch.index.query.FilterBuilder; import org.elasticsearch.index.query.FilterBuilders; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -46,7 +47,7 @@ public class BooleanConditionESQueryBuilder implements ConditionESQueryBuilder { FilterBuilder[] l = new FilterBuilder[conditionCount]; for (int i = 0; i < conditionCount; i++) { - l[i] = dispatcher.buildFilter(conditions.get(i), context); + l[i] = dispatcher.buildFilter(conditions.get(i)); } return isAndOperator ? FilterBuilders.andFilter(l) : FilterBuilders.orFilter(l); http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/wab/src/main/java/org/apache/unomi/web/ContextServlet.java ---------------------------------------------------------------------- diff --git a/wab/src/main/java/org/apache/unomi/web/ContextServlet.java b/wab/src/main/java/org/apache/unomi/web/ContextServlet.java index 5b8b333..d45f263 100644 --- a/wab/src/main/java/org/apache/unomi/web/ContextServlet.java +++ b/wab/src/main/java/org/apache/unomi/web/ContextServlet.java @@ -58,7 +58,6 @@ public class ContextServlet extends HttpServlet { private String profileIdCookieName = "context-profile-id"; private String profileIdCookieDomain; -// private String personaIdCookieName = "context-persona-id"; @Override @@ -86,7 +85,6 @@ public class ContextServlet extends HttpServlet { Profile profile = null; String cookieProfileId = null; - String cookiePersonaId = null; Cookie[] cookies = httpServletRequest.getCookies(); for (Cookie cookie : cookies) { if (profileIdCookieName.equals(cookie.getName())) { @@ -161,8 +159,7 @@ public class ContextServlet extends HttpServlet { profile = checkMergedProfile(response, profile, session); } } - - } else if (cookieProfileId == null || !cookieProfileId.equals(profile.getItemId())) { + } else if ((cookieProfileId == null || !cookieProfileId.equals(profile.getItemId())) && !profile.isAnonymousProfile()) { // profile if stored in session but not in cookie HttpUtils.sendProfileCookie(profile, response, profileIdCookieName, profileIdCookieDomain); } @@ -193,6 +190,13 @@ public class ContextServlet extends HttpServlet { } ContextResponse data = new ContextResponse(); + data.setProfileId(profile.isAnonymousProfile() ? cookieProfileId : profile.getItemId()); + + if (privacyService.isRequireAnonymousBrowsing(profile.getItemId())) { + profile = privacyService.getAnonymousProfile(); + session.setProfile(profile); + changes = EventService.SESSION_UPDATED; + } if(contextRequest != null){ changes |= handleRequest(contextRequest, profile, session, data, request, response, timestamp); @@ -232,7 +236,7 @@ public class ContextServlet extends HttpServlet { private Profile checkMergedProfile(ServletResponse response, Profile profile, Session session) { String profileId; - if (profile != null && profile.getMergedWith() != null) { + if (profile != null && profile.getMergedWith() != null && !profile.isAnonymousProfile()) { profileId = profile.getMergedWith(); Profile profileToDelete = profile; profile = profileService.load(profileId); @@ -282,8 +286,6 @@ public class ContextServlet extends HttpServlet { } } - data.setProfileId(profile.getItemId()); - if (contextRequest.isRequireSegments()) { data.setProfileSegments(profile.getSegments()); } @@ -331,16 +333,18 @@ public class ContextServlet extends HttpServlet { } private void processOverrides(ContextRequest contextRequest, Profile profile, Session session) { - if (contextRequest.getSegmentOverrides() != null) { - profile.setSegments(contextRequest.getSegmentOverrides()); - } + if (profile instanceof Persona) { + if (contextRequest.getSegmentOverrides() != null) { + profile.setSegments(contextRequest.getSegmentOverrides()); + } - if (contextRequest.getProfilePropertiesOverrides() != null) { - profile.setProperties(contextRequest.getProfilePropertiesOverrides()); - } + if (contextRequest.getProfilePropertiesOverrides() != null) { + profile.setProperties(contextRequest.getProfilePropertiesOverrides()); + } - if (contextRequest.getSessionPropertiesOverrides() != null) { - session.setProperties(contextRequest.getSessionPropertiesOverrides()); // we do this just in case a cache is behind this + if (contextRequest.getSessionPropertiesOverrides() != null) { + session.setProperties(contextRequest.getSessionPropertiesOverrides()); // we do this just in case a cache is behind this + } } } http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/f061241c/wab/src/main/java/org/apache/unomi/web/EventsCollectorServlet.java ---------------------------------------------------------------------- diff --git a/wab/src/main/java/org/apache/unomi/web/EventsCollectorServlet.java b/wab/src/main/java/org/apache/unomi/web/EventsCollectorServlet.java index 24aa727..5e7f608 100644 --- a/wab/src/main/java/org/apache/unomi/web/EventsCollectorServlet.java +++ b/wab/src/main/java/org/apache/unomi/web/EventsCollectorServlet.java @@ -35,7 +35,6 @@ import java.io.IOException; import java.io.PrintWriter; import java.util.Date; import java.util.List; -import java.util.Map; public class EventsCollectorServlet extends HttpServlet { private static final Logger logger = LoggerFactory.getLogger(EventsCollectorServlet.class.getName()); @@ -102,22 +101,6 @@ public class EventsCollectorServlet extends HttpServlet { return; } - Profile realProfile = profile; - Boolean profileIsAnonymous = privacyService.isAnonymous(profile.getItemId()); - if (profileIsAnonymous != null && profileIsAnonymous.booleanValue()) { - // we are surfing anonymously, we must use the global anonymous profile if it exists, or create it if - // it doesn't. - Profile anonymousProfile = profileService.load(PrivacyService.GLOBAL_ANONYMOUS_PROFILE_ID); - if (anonymousProfile == null) { - anonymousProfile = new Profile(PrivacyService.GLOBAL_ANONYMOUS_PROFILE_ID); - profileService.save(profile); - } - realProfile = profile; - profile = anonymousProfile; - } - - List<String> filteredEventTypes = privacyService.getFilteredEventTypes(profile.getItemId()); - ObjectMapper mapper = CustomObjectMapper.getObjectMapper(); JsonFactory factory = mapper.getFactory(); EventsCollectorRequest events = null; @@ -134,6 +117,15 @@ public class EventsCollectorServlet extends HttpServlet { String thirdPartyId = eventService.authenticateThirdPartyServer(((HttpServletRequest)request).getHeader("X-Unomi-Peer"), request.getRemoteAddr()); int changes = 0; + + if (privacyService.isRequireAnonymousBrowsing(profile.getItemId())) { + profile = privacyService.getAnonymousProfile(); + session.setProfile(profile); + changes = EventService.SESSION_UPDATED; + } + + List<String> filteredEventTypes = privacyService.getFilteredEventTypes(profile.getItemId()); + for (Event event : events.getEvents()){ if(event.getEventType() != null){ Event eventToSend = new Event(event.getEventType(), session, profile, event.getScope(), event.getSource(), event.getTarget(), event.getProperties(), timestamp);
