Added usage events for collections view and communities view

This commit is contained in:
Anja Le Blanc
2013-10-28 15:29:30 +00:00
parent 4d46c3a505
commit f5027c3bcb
2 changed files with 110 additions and 6 deletions

View File

@@ -10,12 +10,20 @@ package org.dspace.rest;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeManager; import org.dspace.authorize.AuthorizeManager;
import org.dspace.core.Context; import org.dspace.content.DSpaceObject;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
import org.dspace.rest.common.Collection;
import org.dspace.usage.UsageEvent;
import org.dspace.utils.DSpace;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContext; import javax.servlet.ServletContext;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
@@ -29,10 +37,17 @@ http://localhost:8080/<webapp>/collections
@Path("/collections") @Path("/collections")
public class CollectionsResource { public class CollectionsResource {
private static Logger log = Logger.getLogger(CollectionsResource.class); private static Logger log = Logger.getLogger(CollectionsResource.class);
@javax.ws.rs.core.Context ServletContext servletContext; @javax.ws.rs.core.Context ServletContext servletContext;
private static org.dspace.core.Context context; private static org.dspace.core.Context context;
private static final boolean writeStatistics;
static{
writeStatistics=ConfigurationManager.getBooleanProperty("rest","stats",false);
}
/* /*
The "GET" annotation indicates this method will respond to HTTP Get requests. The "GET" annotation indicates this method will respond to HTTP Get requests.
@@ -103,16 +118,22 @@ public class CollectionsResource {
@GET @GET
@Path("/{collection_id}") @Path("/{collection_id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public org.dspace.rest.common.Collection getCollection(@PathParam("collection_id") Integer collection_id, @QueryParam("expand") String expand, @QueryParam("limit") @DefaultValue("100") Integer limit, @QueryParam("offset") @DefaultValue("0") Integer offset) { public org.dspace.rest.common.Collection getCollection(@PathParam("collection_id") Integer collection_id, @QueryParam("expand") String expand,
@QueryParam("limit") @DefaultValue("100") Integer limit, @QueryParam("offset") @DefaultValue("0") Integer offset,
@QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor,
@Context HttpHeaders headers, @Context HttpServletRequest request) {
try { try {
if(context == null || !context.isValid() ) { if(context == null || !context.isValid() ) {
context = new Context(); context = new org.dspace.core.Context();
//Failed SQL is ignored as a failed SQL statement, prevent: current transaction is aborted, commands ignored until end of transaction block //Failed SQL is ignored as a failed SQL statement, prevent: current transaction is aborted, commands ignored until end of transaction block
context.getDBConnection().setAutoCommit(true); context.getDBConnection().setAutoCommit(true);
} }
org.dspace.content.Collection collection = org.dspace.content.Collection.find(context, collection_id); org.dspace.content.Collection collection = org.dspace.content.Collection.find(context, collection_id);
if(AuthorizeManager.authorizeActionBoolean(context, collection, org.dspace.core.Constants.READ)) { if(AuthorizeManager.authorizeActionBoolean(context, collection, org.dspace.core.Constants.READ)) {
if(writeStatistics){
writeStats(collection_id, user_ip, user_agent, xforwarderfor, headers, request);
}
return new org.dspace.rest.common.Collection(collection, expand, context, limit, offset); return new org.dspace.rest.common.Collection(collection, expand, context, limit, offset);
} else { } else {
throw new WebApplicationException(Response.Status.UNAUTHORIZED); throw new WebApplicationException(Response.Status.UNAUTHORIZED);
@@ -122,4 +143,36 @@ public class CollectionsResource {
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
} }
} }
private void writeStats(Integer collection_id, String user_ip, String user_agent,
String xforwarderfor, HttpHeaders headers,
HttpServletRequest request) {
try{
DSpaceObject collection = DSpaceObject.find(context, Constants.COLLECTION, collection_id);
if(user_ip==null || user_ip.length()==0){
new DSpace().getEventService().fireEvent(
new UsageEvent(
UsageEvent.Action.VIEW,
request,
context,
collection));
} else{
new DSpace().getEventService().fireEvent(
new UsageEvent(
UsageEvent.Action.VIEW,
user_ip,
user_agent,
xforwarderfor,
context,
collection));
}
log.debug("fired event");
} catch(SQLException ex){
log.error("SQL exception can't write usageEvent \n" + ex);
}
}
} }

View File

@@ -9,11 +9,19 @@ package org.dspace.rest;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.dspace.authorize.AuthorizeManager; import org.dspace.authorize.AuthorizeManager;
import org.dspace.core.Context; import org.dspace.content.DSpaceObject;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
import org.dspace.usage.UsageEvent;
import org.dspace.utils.DSpace;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletContext;
import javax.ws.rs.*; import javax.ws.rs.*;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
@@ -30,6 +38,12 @@ public class CommunitiesResource {
private static org.dspace.core.Context context; private static org.dspace.core.Context context;
private static final boolean writeStatistics;
static{
writeStatistics=ConfigurationManager.getBooleanProperty("rest","stats",false);
}
/* /*
The "GET" annotation indicates this method will respond to HTTP Get requests. The "GET" annotation indicates this method will respond to HTTP Get requests.
The "Produces" annotation indicates the MIME response the method will return. The "Produces" annotation indicates the MIME response the method will return.
@@ -40,7 +54,7 @@ public class CommunitiesResource {
StringBuilder everything = new StringBuilder(); StringBuilder everything = new StringBuilder();
try { try {
if(context == null || !context.isValid() ) { if(context == null || !context.isValid() ) {
context = new Context(); context = new org.dspace.core.Context();
//Failed SQL is ignored as a failed SQL statement, prevent: current transaction is aborted, commands ignored until end of transaction block //Failed SQL is ignored as a failed SQL statement, prevent: current transaction is aborted, commands ignored until end of transaction block
context.getDBConnection().setAutoCommit(true); context.getDBConnection().setAutoCommit(true);
} }
@@ -90,7 +104,9 @@ public class CommunitiesResource {
@GET @GET
@Path("/{community_id}") @Path("/{community_id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML}) @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public org.dspace.rest.common.Community getCommunity(@PathParam("community_id") Integer community_id, @QueryParam("expand") String expand) { public org.dspace.rest.common.Community getCommunity(@PathParam("community_id") Integer community_id, @QueryParam("expand") String expand,
@QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor,
@Context HttpHeaders headers, @Context HttpServletRequest request) {
try { try {
if(context == null || !context.isValid() ) { if(context == null || !context.isValid() ) {
context = new org.dspace.core.Context(); context = new org.dspace.core.Context();
@@ -100,6 +116,9 @@ public class CommunitiesResource {
org.dspace.content.Community community = org.dspace.content.Community.find(context, community_id); org.dspace.content.Community community = org.dspace.content.Community.find(context, community_id);
if(AuthorizeManager.authorizeActionBoolean(context, community, org.dspace.core.Constants.READ)) { if(AuthorizeManager.authorizeActionBoolean(context, community, org.dspace.core.Constants.READ)) {
if(writeStatistics){
writeStats(community_id, user_ip, user_agent, xforwarderfor, headers, request);
}
return new org.dspace.rest.common.Community(community, expand, context); return new org.dspace.rest.common.Community(community, expand, context);
} else { } else {
throw new WebApplicationException(Response.Status.UNAUTHORIZED); throw new WebApplicationException(Response.Status.UNAUTHORIZED);
@@ -109,4 +128,36 @@ public class CommunitiesResource {
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR); throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
} //finally? } //finally?
} }
private void writeStats(Integer community_id, String user_ip, String user_agent,
String xforwarderfor, HttpHeaders headers,
HttpServletRequest request) {
try{
DSpaceObject community = DSpaceObject.find(context, Constants.COMMUNITY, community_id);
if(user_ip==null || user_ip.length()==0){
new DSpace().getEventService().fireEvent(
new UsageEvent(
UsageEvent.Action.VIEW,
request,
context,
community));
} else{
new DSpace().getEventService().fireEvent(
new UsageEvent(
UsageEvent.Action.VIEW,
user_ip,
user_agent,
xforwarderfor,
context,
community));
}
log.debug("fired event");
} catch(SQLException ex){
log.error("SQL exception can't write usageEvent \n" + ex);
}
}
} }