Search in sources :

Example 1 with UserTrackingException

use of org.opencastproject.usertracking.api.UserTrackingException 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 UserTrackingException

use of org.opencastproject.usertracking.api.UserTrackingException 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 UserTrackingException

use of org.opencastproject.usertracking.api.UserTrackingException in project opencast by opencast.

the class UserTrackingRestService method statsAsXml.

@GET
@Produces(MediaType.TEXT_XML)
@Path("/stats.xml")
@RestQuery(name = "statsasxml", description = "Get the statistics for an episode", returnDescription = "The statistics.", restParameters = { @RestParameter(name = "id", description = "The ID of the single episode to return the statistics for, if it exists", isRequired = false, type = Type.STRING) }, reponses = { @RestResponse(responseCode = SC_OK, description = "An XML representation of the episode's statistics") })
public StatsImpl statsAsXml(@QueryParam("id") String mediapackageId) {
    StatsImpl s = new StatsImpl();
    s.setMediapackageId(mediapackageId);
    try {
        s.setViews(usertrackingService.getViews(mediapackageId));
    } catch (UserTrackingException e) {
        throw new WebApplicationException(e);
    }
    return s;
}
Also used : UserTrackingException(org.opencastproject.usertracking.api.UserTrackingException) WebApplicationException(javax.ws.rs.WebApplicationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) RestQuery(org.opencastproject.util.doc.rest.RestQuery)

Example 4 with UserTrackingException

use of org.opencastproject.usertracking.api.UserTrackingException in project opencast by opencast.

the class UserTrackingServiceImpl method addUserTrackingEvent.

public UserAction addUserTrackingEvent(UserAction a, UserSession session) throws UserTrackingException {
    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);
        a.setSession(userSession);
        em.persist(a);
        tx.commit();
        return a;
    } catch (Exception e) {
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new UserTrackingException(e);
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}
Also used : EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager) 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 5 with UserTrackingException

use of org.opencastproject.usertracking.api.UserTrackingException in project opencast by opencast.

the class UserTrackingServiceImpl method getUserAction.

/**
 * {@inheritDoc}
 *
 * @see org.opencastproject.usertracking.api.UserTrackingService#getUserAction(java.lang.Long)
 */
@Override
public UserAction getUserAction(Long id) throws UserTrackingException, NotFoundException {
    EntityManager em = null;
    UserActionImpl result = null;
    try {
        em = emf.createEntityManager();
        result = em.find(UserActionImpl.class, id);
    } catch (Exception e) {
        throw new UserTrackingException(e);
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
    if (result == null) {
        throw new NotFoundException("No UserAction found with id='" + id + "'");
    } else {
        return result;
    }
}
Also used : EntityManager(javax.persistence.EntityManager) UserTrackingException(org.opencastproject.usertracking.api.UserTrackingException) NotFoundException(org.opencastproject.util.NotFoundException) 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)

Aggregations

UserTrackingException (org.opencastproject.usertracking.api.UserTrackingException)5 ParseException (java.text.ParseException)3 EntityManager (javax.persistence.EntityManager)3 NoResultException (javax.persistence.NoResultException)3 UserSession (org.opencastproject.usertracking.api.UserSession)3 NotFoundException (org.opencastproject.util.NotFoundException)3 ConfigurationException (org.osgi.service.cm.ConfigurationException)3 EntityTransaction (javax.persistence.EntityTransaction)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 RestQuery (org.opencastproject.util.doc.rest.RestQuery)2 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Query (javax.persistence.Query)1 GET (javax.ws.rs.GET)1 PUT (javax.ws.rs.PUT)1 UserAction (org.opencastproject.usertracking.api.UserAction)1 UserActionImpl (org.opencastproject.usertracking.impl.UserActionImpl)1 UserSessionImpl (org.opencastproject.usertracking.impl.UserSessionImpl)1