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();
}
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();
}
}
}
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;
}
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();
}
}
}
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;
}
}
Aggregations