You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by dg...@apache.org on 2017/10/25 10:12:12 UTC
incubator-unomi git commit: UNOMI-131 create non persisted session
and profile in event collector to allow call with new session,
add updateProfileProperties event in the list of protected event
Repository: incubator-unomi
Updated Branches:
refs/heads/master f76ef4996 -> 3e3c8cce0
UNOMI-131 create non persisted session and profile in event collector to allow call with new session, add updateProfileProperties event in the list of protected event
Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/3e3c8cce
Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/3e3c8cce
Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/3e3c8cce
Branch: refs/heads/master
Commit: 3e3c8cce029ad617d8be5d2f3233c91c89349d30
Parents: f76ef49
Author: dgaillard <dg...@jahia.com>
Authored: Wed Oct 25 12:11:59 2017 +0200
Committer: dgaillard <dg...@jahia.com>
Committed: Wed Oct 25 12:12:09 2017 +0200
----------------------------------------------------------------------
.../resources/OSGI-INF/blueprint/blueprint.xml | 1 +
wab/pom.xml | 4 +
.../unomi/web/EventsCollectorServlet.java | 111 +++++++++++--------
3 files changed, 72 insertions(+), 44 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/3e3c8cce/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git a/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index 75fbc01..6fb1481 100644
--- a/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ b/services/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -82,6 +82,7 @@
<value>sessionReassigned</value>
<value>profileUpdated</value>
<value>ruleFired</value>
+ <value>updateProfileProperties</value>
</set>
</property>
<property name="thirdPartyConfiguration">
http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/3e3c8cce/wab/pom.xml
----------------------------------------------------------------------
diff --git a/wab/pom.xml b/wab/pom.xml
index acdf449..a639fec 100644
--- a/wab/pom.xml
+++ b/wab/pom.xml
@@ -41,6 +41,10 @@
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.apache.unomi</groupId>
<artifactId>unomi-api</artifactId>
<version>1.3.0-incubating-SNAPSHOT</version>
http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/3e3c8cce/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 f8a9119..480be81 100644
--- a/wab/src/main/java/org/apache/unomi/web/EventsCollectorServlet.java
+++ b/wab/src/main/java/org/apache/unomi/web/EventsCollectorServlet.java
@@ -19,6 +19,7 @@ package org.apache.unomi.web;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.commons.lang3.StringUtils;
import org.apache.unomi.api.*;
import org.apache.unomi.api.services.EventService;
import org.apache.unomi.api.services.PrivacyService;
@@ -36,7 +37,9 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
+import java.util.HashMap;
import java.util.List;
+import java.util.UUID;
public class EventsCollectorServlet extends HttpServlet {
private static final Logger logger = LoggerFactory.getLogger(EventsCollectorServlet.class.getName());
@@ -84,50 +87,8 @@ public class EventsCollectorServlet extends HttpServlet {
timestamp.setTime(Long.parseLong(request.getParameter("timestamp")));
}
-// logger.debug(HttpUtils.dumpRequestInfo(request));
-
HttpUtils.setupCORSHeaders(request, response);
- String sessionId = request.getParameter("sessionId");
- if (sessionId == null) {
- 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 = profileService.loadSession(sessionId, timestamp);
- if (session == null) {
- logger.error("No session found for sessionId={}, aborting request !", sessionId);
- return;
- }
-
- String profileIdCookieName = "context-profile-id";
-
- Profile sessionProfile = session.getProfile();
- Profile profile = null;
- if (sessionProfile.getItemId() != null) {
- // Reload up-to-date profile
- profile = profileService.load(sessionProfile.getItemId());
- if (profile == null || profile instanceof Persona) {
- logger.error("No valid profile found or persona found for profileId={}, aborting request !", session.getProfileId());
- return;
- }
- } else {
- // Session uses anonymous profile, try to find profile from cookie
- Cookie[] cookies = request.getCookies();
- for (Cookie cookie : cookies) {
- if (profileIdCookieName.equals(cookie.getName())) {
- profile = profileService.load(cookie.getValue());
- }
- }
- if (profile == null) {
- logger.error("No valid profile found or persona found for profileId={}, aborting request !", session.getProfileId());
- return;
- }
- }
-
String payload = HttpUtils.getPayload(request);
if (payload == null){
logger.error("No event payload found for request, aborting !");
@@ -148,6 +109,69 @@ public class EventsCollectorServlet extends HttpServlet {
return;
}
+ String sessionId = request.getParameter("sessionId");
+ if (sessionId == null) {
+ 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 = profileService.loadSession(sessionId, timestamp);
+ Profile sessionProfile;
+ Profile profile = null;
+ if (session == null) {
+ String scope = "systemscope";
+ // Get the first available scope that is not equal to systemscope to create the session otherwise systemscope will be used
+ for (Event event : events.getEvents()) {
+ if (StringUtils.isNotBlank(event.getEventType())) {
+ if (StringUtils.isNotBlank(event.getScope()) && !event.getScope().equals("systemscope")) {
+ scope = event.getScope();
+ break;
+ } else if (event.getSource() != null && StringUtils.isNotBlank(event.getSource().getScope()) && !event.getSource().getScope().equals("systemscope")) {
+ scope = event.getSource().getScope();
+ break;
+ }
+ }
+ }
+ // 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
+ session = new Session(sessionId, anonymousProfile, timestamp, scope);
+ if (logger.isDebugEnabled()) {
+ logger.debug("No session found for sessionId={}, creating new session!", sessionId);
+ }
+ sessionProfile = session.getProfile();
+ } else {
+ String profileIdCookieName = "context-profile-id";
+
+ sessionProfile = session.getProfile();
+ if (sessionProfile.getItemId() != null) {
+ // Reload up-to-date profile
+ profile = profileService.load(sessionProfile.getItemId());
+ if (profile == null || profile instanceof Persona) {
+ logger.error("No valid profile found or persona found for profileId={}, aborting request !", session.getProfileId());
+ return;
+ }
+ } else {
+ // Session uses anonymous profile, try to find profile from cookie
+ Cookie[] cookies = request.getCookies();
+ for (Cookie cookie : cookies) {
+ if (profileIdCookieName.equals(cookie.getName())) {
+ profile = profileService.load(cookie.getValue());
+ }
+ }
+ if (profile == null) {
+ logger.error("No valid profile found or persona found for profileId={}, aborting request !", session.getProfileId());
+ return;
+ }
+ }
+ }
+
String thirdPartyId = eventService.authenticateThirdPartyServer(((HttpServletRequest)request).getHeader("X-Unomi-Peer"), request.getRemoteAddr());
int changes = 0;
@@ -163,7 +187,7 @@ public class EventsCollectorServlet extends HttpServlet {
}
if (!eventService.isEventAllowed(event, thirdPartyId)) {
- logger.debug("Event is not allowed : {}", event.getEventType());
+ logger.warn("Event is not allowed : {}", event.getEventType());
continue;
}
if (filteredEventTypes != null && filteredEventTypes.contains(event.getEventType())) {
@@ -190,7 +214,6 @@ public class EventsCollectorServlet extends HttpServlet {
profileService.saveSession(session);
}
-
PrintWriter responseWriter = response.getWriter();
responseWriter.append("{\"updated\":" + changes + "}");
responseWriter.flush();