This is an automated email from the ASF dual-hosted git repository. shuber pushed a commit to branch UNOMI-285-optional-sessions in repository https://gitbox.apache.org/repos/asf/unomi.git
commit 7e9226a975f29b4c0b8582df050ad8ed64e24842 Author: Serge Huber <[email protected]> AuthorDate: Tue Mar 10 10:31:17 2020 +0100 UNOMI-285 Make sessions optional in eventcollector and contextservlet --- .../baseplugin/actions/SetPropertyAction.java | 3 +++ .../META-INF/cxs/rules/sessionPageReferrer.json | 2 +- .../java/org/apache/unomi/web/ContextServlet.java | 20 ++++++++------- .../apache/unomi/web/EventsCollectorServlet.java | 30 ++++++++++++---------- .../java/org/apache/unomi/web/ServletCommon.java | 2 +- 5 files changed, 33 insertions(+), 24 deletions(-) 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 4fcdde7..4134cec 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 @@ -41,6 +41,9 @@ public class SetPropertyAction implements ActionExecutor { public int execute(Action action, Event event) { boolean storeInSession = Boolean.TRUE.equals(action.getParameterValues().get("storeInSession")); + if (storeInSession && event.getSession() == null) { + return EventService.NO_CHANGE; + } String propertyName = (String) action.getParameterValues().get("setPropertyName"); diff --git a/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/sessionPageReferrer.json b/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/sessionPageReferrer.json index ad09b7a..d3e2f10 100644 --- a/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/sessionPageReferrer.json +++ b/plugins/baseplugin/src/main/resources/META-INF/cxs/rules/sessionPageReferrer.json @@ -27,7 +27,7 @@ { "parameterValues": { "setPropertyName": "size", - "setPropertyValue": "script::session.size + 1", + "setPropertyValue": "script::session != null ? session.size + 1 : 0", "storeInSession": true }, "type": "setPropertyAction" 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 d4bc05c..961276b 100644 --- a/wab/src/main/java/org/apache/unomi/web/ContextServlet.java +++ b/wab/src/main/java/org/apache/unomi/web/ContextServlet.java @@ -211,10 +211,10 @@ public class ContextServlet extends HttpServlet { if (session == null || invalidateSession) { sessionProfile = privacyService.isRequireAnonymousBrowsing(profile) ? privacyService.getAnonymousProfile(profile) : profile; - session = new Session(sessionId, sessionProfile, timestamp, scope); if (StringUtils.isNotBlank(sessionId)) { // Only save session and send event if a session id was provided, otherwise keep transient session + session = new Session(sessionId, sessionProfile, timestamp, scope); changes |= EventService.SESSION_UPDATED; Event event = new Event("sessionCreated", session, profile, scope, null, session, timestamp); if (sessionProfile.isAnonymousProfile()) { @@ -241,7 +241,7 @@ public class ContextServlet extends HttpServlet { if (logger.isDebugEnabled()) { logger.debug("Received event {} for profile={} {} target={} timestamp={}", profileUpdated.getEventType(), profile.getItemId(), - " session=" + session.getItemId(), profileUpdated.getTarget(), timestamp); + " session=" + (session != null ? session.getItemId() : null), profileUpdated.getTarget(), timestamp); } changes |= eventService.send(profileUpdated); } @@ -336,13 +336,15 @@ public class ContextServlet extends HttpServlet { data.setProfileProperties(profileProperties); } - data.setSessionId(session.getItemId()); - if (contextRequest.getRequiredSessionProperties() != null) { - Map<String, Object> sessionProperties = new HashMap<>(session.getProperties()); - if (!contextRequest.getRequiredSessionProperties().contains("*")) { - sessionProperties.keySet().retainAll(contextRequest.getRequiredSessionProperties()); + if (session != null) { + data.setSessionId(session.getItemId()); + if (contextRequest.getRequiredSessionProperties() != null) { + Map<String, Object> sessionProperties = new HashMap<>(session.getProperties()); + if (!contextRequest.getRequiredSessionProperties().contains("*")) { + sessionProperties.keySet().retainAll(contextRequest.getRequiredSessionProperties()); + } + data.setSessionProperties(sessionProperties); } - data.setSessionProperties(sessionProperties); } processOverrides(contextRequest, profile, session); @@ -398,7 +400,7 @@ public class ContextServlet extends HttpServlet { if (contextRequest.getProfileOverrides().getProperties()!=null) { profile.setProperties(contextRequest.getProfileOverrides().getProperties()); } - if (contextRequest.getSessionPropertiesOverrides()!=null) { + if (contextRequest.getSessionPropertiesOverrides()!=null && session != null) { session.setProperties(contextRequest.getSessionPropertiesOverrides()); } } 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 a55adf8..2f27836 100644 --- a/wab/src/main/java/org/apache/unomi/web/EventsCollectorServlet.java +++ b/wab/src/main/java/org/apache/unomi/web/EventsCollectorServlet.java @@ -112,16 +112,10 @@ public class EventsCollectorServlet extends HttpServlet { if (sessionId == null) { sessionId = request.getParameter("sessionId"); } - if (sessionId == null) { - response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Check logs for more details"); - logger.error("No sessionId found in incoming request, aborting processing. See debug level for more information"); - if (logger.isDebugEnabled()) { - logger.debug("Request dump:" + HttpUtils.dumpRequestInfo(request)); - } - return; + Session session = null; + if (sessionId != null) { + session = profileService.loadSession(sessionId, timestamp); } - - Session session = profileService.loadSession(sessionId, timestamp); Profile profile = null; if (session == null) { String scope = "systemscope"; @@ -137,9 +131,16 @@ public class EventsCollectorServlet extends HttpServlet { } } } - // Create non persisted profile to create the session - profile = new Profile("temp_" + UUID.randomUUID().toString()); - profile.setProperty("firstVisit", timestamp); + String cookieProfileId = ServletCommon.getProfileIdCookieValue(request, profileIdCookieName); + if (StringUtils.isNotBlank(cookieProfileId)) { + profile = profileService.load(cookieProfileId); + } + if (profile == null) { + // Create non persisted profile to create the session + profile = new Profile("temp_" + UUID.randomUUID().toString()); + profile.setProperty("firstVisit", timestamp); + } + /* // Create anonymous profile so we don't keep track of the temp profile anywhere Profile anonymousProfile = privacyService.getAnonymousProfile(profile); // Create new session which should not be persisted as well as the temp profile @@ -147,6 +148,7 @@ public class EventsCollectorServlet extends HttpServlet { if (logger.isDebugEnabled()) { logger.debug("No session found for sessionId={}, creating new session!", sessionId); } + */ } else { Profile sessionProfile = session.getProfile(); if (sessionProfile.getItemId() != null) { @@ -181,7 +183,9 @@ public class EventsCollectorServlet extends HttpServlet { profileService.save(profile); } if ((changes & EventService.SESSION_UPDATED) == EventService.SESSION_UPDATED) { - profileService.saveSession(session); + if (session != null) { + profileService.saveSession(session); + } } response.setContentType("application/json"); diff --git a/wab/src/main/java/org/apache/unomi/web/ServletCommon.java b/wab/src/main/java/org/apache/unomi/web/ServletCommon.java index da27fca..44785aa 100644 --- a/wab/src/main/java/org/apache/unomi/web/ServletCommon.java +++ b/wab/src/main/java/org/apache/unomi/web/ServletCommon.java @@ -85,7 +85,7 @@ public class ServletCommon { eventToSend.getAttributes().put(Event.HTTP_REQUEST_ATTRIBUTE, request); eventToSend.getAttributes().put(Event.HTTP_RESPONSE_ATTRIBUTE, response); logger.debug("Received event " + event.getEventType() + " for profile=" + profile.getItemId() + " session=" - + session.getItemId() + " target=" + event.getTarget() + " timestamp=" + timestamp); + + (session!= null?session.getItemId():null) + " target=" + event.getTarget() + " timestamp=" + timestamp); changes = eventService.send(eventToSend); // If the event execution changes the profile we need to update it so the next event use the right profile if ((changes & EventService.PROFILE_UPDATED) == EventService.PROFILE_UPDATED) {
