use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserTrackingRestService method addFootprint.
@PUT
@Path("")
@Produces(MediaType.TEXT_XML)
@RestQuery(name = "add", description = "Record a user action", returnDescription = "An XML representation of the user action", restParameters = { @RestParameter(name = "id", description = "The episode identifier", isRequired = true, type = Type.STRING), @RestParameter(name = "type", description = "The episode identifier", isRequired = true, type = Type.STRING), @RestParameter(name = "in", description = "The beginning of the time range", isRequired = true, type = Type.STRING), @RestParameter(name = "out", description = "The end of the time range", isRequired = false, type = Type.STRING), @RestParameter(name = "playing", description = "Whether the player is currently playing", isRequired = false, type = Type.STRING) }, reponses = { @RestResponse(responseCode = SC_CREATED, description = "An XML representation of the user action") })
public Response addFootprint(@FormParam("id") String mediapackageId, @FormParam("in") String inString, @FormParam("out") String outString, @FormParam("type") String type, @FormParam("playing") String isPlaying, @Context HttpServletRequest request) {
String sessionId = request.getSession().getId();
String userId = securityService.getUser().getUsername();
// Parse the in and out strings, which might be empty (hence, we can't let jax-rs handle them properly)
if (StringUtils.isEmpty(inString)) {
throw new WebApplicationException(Response.status(Status.BAD_REQUEST).entity("in must be a non null integer").build());
}
Integer in = null;
try {
in = Integer.parseInt(StringUtils.trim(inString));
} catch (NumberFormatException e) {
throw new WebApplicationException(e, Response.status(Status.BAD_REQUEST).entity("in must be a non null integer").build());
}
Integer out = null;
if (StringUtils.isEmpty(outString)) {
out = in;
} else {
try {
out = Integer.parseInt(StringUtils.trim(outString));
} catch (NumberFormatException e) {
throw new WebApplicationException(e, Response.status(Status.BAD_REQUEST).entity("out must be a non null integer").build());
}
}
// MH-8616 the connection might be via a proxy
String clientIP = request.getHeader("X-FORWARDED-FOR");
if (clientIP == null) {
clientIP = request.getRemoteAddr();
}
logger.debug("Got client ip: {}", clientIP);
UserSession s = new UserSessionImpl();
s.setSessionId(sessionId);
s.setUserIp(clientIP);
s.setUserId(userId);
// Column length is currently 255, let's limit it to that.
String userAgent = StringUtils.trimToNull(request.getHeader("User-Agent"));
if (userAgent != null && userAgent.length() > 255) {
s.setUserAgent(userAgent.substring(0, 255));
} else {
s.setUserAgent(userAgent);
}
UserActionImpl a = new UserActionImpl();
a.setMediapackageId(mediapackageId);
a.setSession(s);
a.setInpoint(in);
a.setOutpoint(out);
a.setType(type);
a.setIsPlaying(Boolean.valueOf(isPlaying));
try {
if ("FOOTPRINT".equals(type)) {
a = (UserActionImpl) usertrackingService.addUserFootprint(a, s);
} else {
a = (UserActionImpl) usertrackingService.addUserTrackingEvent(a, s);
}
} catch (UserTrackingException e) {
throw new WebApplicationException(e);
}
URI uri;
try {
uri = new URI(UrlSupport.concat(new String[] { serverUrl, serviceUrl, "action", a.getId().toString(), ".xml" }));
} catch (URISyntaxException e) {
throw new WebApplicationException(e);
}
return Response.created(uri).entity(a).build();
}
use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserTrackingServiceImpl method addUserFootprint.
@SuppressWarnings("unchecked")
public UserAction addUserFootprint(UserAction a, UserSession session) throws UserTrackingException {
a.setType(FOOTPRINT_KEY);
EntityManager em = null;
EntityTransaction tx = null;
if (!logIp)
session.setUserIp("-omitted-");
if (!logUser)
session.setUserId("-omitted-");
if (!logSession)
session.setSessionId("-omitted-");
try {
em = emf.createEntityManager();
tx = em.getTransaction();
tx.begin();
UserSession userSession = populateSession(em, session);
Query q = em.createNamedQuery("findLastUserFootprintOfSession");
q.setMaxResults(1);
q.setParameter("session", userSession);
Collection<UserAction> userActions = q.getResultList();
if (userActions.size() >= 1) {
UserAction last = userActions.iterator().next();
if (last.getMediapackageId().equals(a.getMediapackageId()) && last.getType().equals(a.getType()) && last.getOutpoint() == a.getInpoint()) {
// We are assuming in this case that the sessions match and are unchanged (IP wise, for example)
last.setOutpoint(a.getOutpoint());
a = last;
a.setId(last.getId());
} else {
a.setSession(userSession);
em.persist(a);
}
} else {
a.setSession(userSession);
em.persist(a);
}
tx.commit();
return a;
} catch (Exception e) {
if (tx != null && tx.isActive()) {
tx.rollback();
}
throw new UserTrackingException(e);
} finally {
if (em != null && em.isOpen()) {
em.close();
}
}
}
use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserTrackingServiceImpl method populateSession.
private UserSession populateSession(EntityManager em, UserSession session) {
// Try and find the session. If not found, persist it
Query q = em.createNamedQuery("findUserSessionBySessionId");
q.setMaxResults(1);
q.setParameter("sessionId", session.getSessionId());
UserSession userSession = null;
try {
userSession = (UserSession) q.getSingleResult();
} catch (NoResultException n) {
userSession = session;
em.persist(userSession);
}
return userSession;
}
use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserActionImplTest method testBackwardLength.
/**
* Ensures a negative length user action calculates its length correctly.
*/
@Test
public void testBackwardLength() {
UserSession us = new UserSessionImpl();
us.setSessionId("4");
us.setUserId("testing user");
us.setUserIp("127.0.0.1");
UserAction ua = new UserActionImpl();
ua.setId(4L);
ua.setInpoint(100);
ua.setIsPlaying(true);
ua.setMediapackageId("false");
ua.setOutpoint(90);
ua.setSession(us);
ua.setType("test");
Assert.assertEquals(new Long(4), ua.getId());
Assert.assertEquals(100, ua.getInpoint());
Assert.assertEquals(true, ua.getIsPlaying());
Assert.assertEquals("false", ua.getMediapackageId());
Assert.assertEquals(90, ua.getOutpoint());
Assert.assertEquals("4", ua.getSession().getSessionId());
Assert.assertEquals("test", ua.getType());
Assert.assertEquals("testing user", ua.getSession().getUserId());
Assert.assertEquals("127.0.0.1", ua.getSession().getUserIp());
Assert.assertEquals(-10, ua.getLength());
}
use of org.opencastproject.usertracking.api.UserSession in project opencast by opencast.
the class UserActionListImplTest method testBasicAddition.
/**
* Tests that adding events to a UserActionList works using the basic .add method
*/
@Test
public void testBasicAddition() {
UserSession us1 = new UserSessionImpl();
us1.setSessionId("4");
us1.setUserId("testing user");
us1.setUserIp("127.0.0.1");
UserSession us2 = new UserSessionImpl();
us2.setSessionId("6");
us2.setUserId("testing user 2");
us2.setUserIp("127.0.0.5");
UserAction ua1 = new UserActionImpl();
ua1.setId(4L);
ua1.setInpoint(90);
ua1.setIsPlaying(true);
ua1.setMediapackageId("false");
ua1.setOutpoint(100);
ua1.setSession(us1);
ua1.setType("test");
UserAction ua2 = new UserActionImpl();
ua2.setId(6L);
ua2.setInpoint(5);
ua2.setIsPlaying(false);
ua2.setMediapackageId("other");
ua2.setOutpoint(20);
ua2.setSession(us2);
ua2.setType("other test");
UserActionList ual1 = new UserActionListImpl();
ual1.add(ua1);
ual1.setTotal(1);
Assert.assertEquals(1, ual1.getTotal());
Assert.assertEquals(1, ual1.getUserActions().size());
Assert.assertEquals(new Long(4), ual1.getUserActions().get(0).getId());
Assert.assertEquals(90, ual1.getUserActions().get(0).getInpoint());
Assert.assertEquals(true, ual1.getUserActions().get(0).getIsPlaying());
Assert.assertEquals("false", ual1.getUserActions().get(0).getMediapackageId());
Assert.assertEquals(100, ual1.getUserActions().get(0).getOutpoint());
Assert.assertEquals("4", ual1.getUserActions().get(0).getSession().getSessionId());
Assert.assertEquals("test", ual1.getUserActions().get(0).getType());
Assert.assertEquals("testing user", ual1.getUserActions().get(0).getSession().getUserId());
Assert.assertEquals("127.0.0.1", ual1.getUserActions().get(0).getSession().getUserIp());
Assert.assertEquals(10, ual1.getUserActions().get(0).getLength());
UserActionList ual2 = new UserActionListImpl();
ual2.add(ua2);
ual2.setTotal(1);
Assert.assertEquals(1, ual2.getTotal());
Assert.assertEquals(1, ual2.getUserActions().size());
Assert.assertEquals(new Long(6), ual2.getUserActions().get(0).getId());
Assert.assertEquals(5, ual2.getUserActions().get(0).getInpoint());
Assert.assertEquals(false, ual2.getUserActions().get(0).getIsPlaying());
Assert.assertEquals("other", ual2.getUserActions().get(0).getMediapackageId());
Assert.assertEquals(20, ual2.getUserActions().get(0).getOutpoint());
Assert.assertEquals("6", ual2.getUserActions().get(0).getSession().getSessionId());
Assert.assertEquals("other test", ual2.getUserActions().get(0).getType());
Assert.assertEquals("testing user 2", ual2.getUserActions().get(0).getSession().getUserId());
Assert.assertEquals("127.0.0.5", ual2.getUserActions().get(0).getSession().getUserIp());
Assert.assertEquals(15, ual2.getUserActions().get(0).getLength());
}
Aggregations