Search in sources :

Example 1 with UserSession

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();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) UserTrackingException(org.opencastproject.usertracking.api.UserTrackingException) UserSession(org.opencastproject.usertracking.api.UserSession) UserActionImpl(org.opencastproject.usertracking.impl.UserActionImpl) URISyntaxException(java.net.URISyntaxException) UserSessionImpl(org.opencastproject.usertracking.impl.UserSessionImpl) URI(java.net.URI) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) RestQuery(org.opencastproject.util.doc.rest.RestQuery) PUT(javax.ws.rs.PUT)

Example 2 with UserSession

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();
        }
    }
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) UserTrackingException(org.opencastproject.usertracking.api.UserTrackingException) UserSession(org.opencastproject.usertracking.api.UserSession) NoResultException(javax.persistence.NoResultException) ConfigurationException(org.osgi.service.cm.ConfigurationException) ParseException(java.text.ParseException) UserTrackingException(org.opencastproject.usertracking.api.UserTrackingException) NotFoundException(org.opencastproject.util.NotFoundException)

Example 3 with UserSession

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;
}
Also used : Query(javax.persistence.Query) UserSession(org.opencastproject.usertracking.api.UserSession) NoResultException(javax.persistence.NoResultException)

Example 4 with 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());
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) UserSession(org.opencastproject.usertracking.api.UserSession) Test(org.junit.Test)

Example 5 with UserSession

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());
}
Also used : UserAction(org.opencastproject.usertracking.api.UserAction) UserActionList(org.opencastproject.usertracking.api.UserActionList) UserSession(org.opencastproject.usertracking.api.UserSession) Test(org.junit.Test)

Aggregations

UserSession (org.opencastproject.usertracking.api.UserSession)10 UserAction (org.opencastproject.usertracking.api.UserAction)6 Test (org.junit.Test)4 NoResultException (javax.persistence.NoResultException)3 UserTrackingException (org.opencastproject.usertracking.api.UserTrackingException)3 ParseException (java.text.ParseException)2 EntityManager (javax.persistence.EntityManager)2 EntityTransaction (javax.persistence.EntityTransaction)2 Query (javax.persistence.Query)2 UserActionList (org.opencastproject.usertracking.api.UserActionList)2 NotFoundException (org.opencastproject.util.NotFoundException)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 LinkedList (java.util.LinkedList)1 PUT (javax.ws.rs.PUT)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 UserActionImpl (org.opencastproject.usertracking.impl.UserActionImpl)1