mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-11 03:53:07 +00:00
Added statistics recording to API
(required changes in DSpace API!) Also some changes to make REST API work better with XML output. Added Checksum and sequenceId to Bitstream.
This commit is contained in:
@@ -10,10 +10,17 @@ package org.dspace.rest;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.dspace.authorize.AuthorizeException;
|
||||
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.Bitstream;
|
||||
import org.dspace.usage.UsageEvent;
|
||||
import org.dspace.utils.DSpace;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.ws.rs.*;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.io.IOException;
|
||||
@@ -30,6 +37,12 @@ import java.sql.SQLException;
|
||||
public class BitstreamResource {
|
||||
Logger log = Logger.getLogger(BitstreamResource.class);
|
||||
private static org.dspace.core.Context context;
|
||||
|
||||
private static final boolean writeStatistics;
|
||||
|
||||
static{
|
||||
writeStatistics=ConfigurationManager.getBooleanProperty("rest","stats",false);
|
||||
}
|
||||
|
||||
//BitstreamList - Not Implemented
|
||||
|
||||
@@ -39,7 +52,7 @@ public class BitstreamResource {
|
||||
public Bitstream getBitstream(@PathParam("bitstream_id") Integer bitstream_id, @QueryParam("expand") String expand) {
|
||||
try {
|
||||
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
|
||||
context.getDBConnection().setAutoCommit(true);
|
||||
}
|
||||
@@ -59,7 +72,9 @@ public class BitstreamResource {
|
||||
|
||||
@GET
|
||||
@Path("/{bitstream_id}/retrieve")
|
||||
public javax.ws.rs.core.Response getFile(@PathParam("bitstream_id") final Integer bitstream_id) {
|
||||
public javax.ws.rs.core.Response getFile(@PathParam("bitstream_id") final Integer bitstream_id,
|
||||
@QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor,
|
||||
@Context HttpHeaders headers, @Context HttpServletRequest request) {
|
||||
try {
|
||||
if(context == null || !context.isValid() ) {
|
||||
context = new org.dspace.core.Context();
|
||||
@@ -69,6 +84,10 @@ public class BitstreamResource {
|
||||
|
||||
org.dspace.content.Bitstream bitstream = org.dspace.content.Bitstream.find(context, bitstream_id);
|
||||
if(AuthorizeManager.authorizeActionBoolean(context, bitstream, org.dspace.core.Constants.READ)) {
|
||||
if(writeStatistics){
|
||||
writeStats(bitstream_id, user_ip, user_agent, xforwarderfor, headers, request);
|
||||
}
|
||||
|
||||
return Response.ok(bitstream.retrieve()).type(bitstream.getFormat().getMIMEType()).build();
|
||||
} else {
|
||||
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
|
||||
@@ -85,4 +104,37 @@ public class BitstreamResource {
|
||||
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeStats(Integer bitstream_id, String user_ip, String user_agent,
|
||||
String xforwarderfor, HttpHeaders headers,
|
||||
HttpServletRequest request) {
|
||||
|
||||
try{
|
||||
DSpaceObject bitstream = DSpaceObject.find(context, Constants.BITSTREAM, bitstream_id);
|
||||
|
||||
if(user_ip==null || user_ip.length()==0){
|
||||
new DSpace().getEventService().fireEvent(
|
||||
new UsageEvent(
|
||||
UsageEvent.Action.VIEW,
|
||||
request,
|
||||
context,
|
||||
bitstream));
|
||||
} else{
|
||||
new DSpace().getEventService().fireEvent(
|
||||
new UsageEvent(
|
||||
UsageEvent.Action.VIEW,
|
||||
user_ip,
|
||||
user_agent,
|
||||
xforwarderfor,
|
||||
context,
|
||||
bitstream));
|
||||
}
|
||||
log.debug("fired event");
|
||||
|
||||
} catch(SQLException ex){
|
||||
log.error("SQL exception can't write usageEvent \n" + ex);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user