Search in sources :

Example 1 with UserSessionImpl

use of org.opencastproject.usertracking.impl.UserSessionImpl 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)

Aggregations

URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)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 UserSession (org.opencastproject.usertracking.api.UserSession)1 UserTrackingException (org.opencastproject.usertracking.api.UserTrackingException)1 UserActionImpl (org.opencastproject.usertracking.impl.UserActionImpl)1 UserSessionImpl (org.opencastproject.usertracking.impl.UserSessionImpl)1 RestQuery (org.opencastproject.util.doc.rest.RestQuery)1