DS-1986 Get new context each time, close in finally, for all REST methods

This commit is contained in:
Peter Dietz
2014-07-03 15:34:33 -04:00
parent 9db37af66c
commit 859e69285f
4 changed files with 67 additions and 45 deletions

View File

@@ -36,7 +36,6 @@ import java.sql.SQLException;
@Path("/bitstreams")
public class BitstreamResource {
Logger log = Logger.getLogger(BitstreamResource.class);
private static org.dspace.core.Context context;
private static final boolean writeStatistics;
@@ -50,12 +49,9 @@ public class BitstreamResource {
@Path("/{bitstream_id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Bitstream getBitstream(@PathParam("bitstream_id") Integer bitstream_id, @QueryParam("expand") String expand) {
org.dspace.core.Context context = null;
try {
if(context == null || !context.isValid()) {
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);
}
context = new org.dspace.core.Context();
org.dspace.content.Bitstream bitstream = org.dspace.content.Bitstream.find(context, bitstream_id);
@@ -67,6 +63,14 @@ public class BitstreamResource {
} catch(SQLException e) {
log.error(e.getMessage());
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
} finally {
if(context != null) {
try {
context.complete();
} catch (SQLException e) {
log.error(e.getMessage() + " occurred while trying to close");
}
}
}
}
@@ -75,17 +79,14 @@ public class BitstreamResource {
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) {
org.dspace.core.Context context = null;
try {
if(context == null || !context.isValid() ) {
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);
}
context = new org.dspace.core.Context();
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);
writeStats(context, bitstream_id, user_ip, user_agent, xforwarderfor, headers, request);
}
return Response.ok(bitstream.retrieve()).type(bitstream.getFormat().getMIMEType()).build();
@@ -102,10 +103,18 @@ public class BitstreamResource {
} catch (AuthorizeException e) {
log.error(e.getMessage());
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
} finally {
if(context != null) {
try {
context.complete();
} catch (SQLException e) {
log.error(e.getMessage() + " occurred while trying to close");
}
}
}
}
private void writeStats(Integer bitstream_id, String user_ip, String user_agent,
private void writeStats(org.dspace.core.Context context, Integer bitstream_id, String user_ip, String user_agent,
String xforwarderfor, HttpHeaders headers,
HttpServletRequest request) {

View File

@@ -37,11 +37,8 @@ http://localhost:8080/<webapp>/collections
@Path("/collections")
public class CollectionsResource {
private static Logger log = Logger.getLogger(CollectionsResource.class);
@javax.ws.rs.core.Context ServletContext servletContext;
private static org.dspace.core.Context context;
private static final boolean writeStatistics;
@@ -53,12 +50,9 @@ public class CollectionsResource {
@Path("/")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public org.dspace.rest.common.Collection[] list(@QueryParam("expand") String expand, @QueryParam("limit") @DefaultValue("100") Integer limit, @QueryParam("offset") @DefaultValue("0") Integer offset) {
org.dspace.core.Context context = null;
try {
if(context == null || !context.isValid() ) {
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);
}
context = new org.dspace.core.Context();
org.dspace.content.Collection[] collections;
@@ -82,6 +76,14 @@ public class CollectionsResource {
} catch (SQLException e) {
log.error(e.getMessage());
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
} finally {
if(context != null) {
try {
context.complete();
} catch (SQLException e) {
log.error(e.getMessage() + " occurred while trying to close");
}
}
}
}
@@ -92,17 +94,14 @@ public class CollectionsResource {
@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) {
org.dspace.core.Context context = null;
try {
if(context == null || !context.isValid() ) {
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);
}
context = new org.dspace.core.Context();
org.dspace.content.Collection collection = org.dspace.content.Collection.find(context, collection_id);
if(AuthorizeManager.authorizeActionBoolean(context, collection, org.dspace.core.Constants.READ)) {
if(writeStatistics){
writeStats(collection_id, user_ip, user_agent, xforwarderfor, headers, request);
writeStats(context, collection_id, user_ip, user_agent, xforwarderfor, headers, request);
}
return new org.dspace.rest.common.Collection(collection, expand, context, limit, offset);
} else {
@@ -111,10 +110,18 @@ public class CollectionsResource {
} catch (SQLException e) {
log.error(e.getMessage());
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
} finally {
if(context != null) {
try {
context.complete();
} catch (SQLException e) {
log.error(e.getMessage() + " occurred while trying to close");
}
}
}
}
private void writeStats(Integer collection_id, String user_ip, String user_agent,
private void writeStats(org.dspace.core.Context context, Integer collection_id, String user_ip, String user_agent,
String xforwarderfor, HttpHeaders headers,
HttpServletRequest request) {

View File

@@ -32,18 +32,14 @@ import java.sql.SQLException;
@Path("/handle")
public class HandleResource {
private static Logger log = Logger.getLogger(HandleResource.class);
private static org.dspace.core.Context context;
@GET
@Path("/{prefix}/{suffix}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public org.dspace.rest.common.DSpaceObject getObject(@PathParam("prefix") String prefix, @PathParam("suffix") String suffix, @QueryParam("expand") String expand) {
org.dspace.core.Context context = null;
try {
if(context == null || !context.isValid() ) {
context = new 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);
}
context = new org.dspace.core.Context();
org.dspace.content.DSpaceObject dso = HandleManager.resolveToObject(context, prefix + "/" + suffix);
if(dso == null) {
@@ -68,6 +64,14 @@ public class HandleResource {
} catch (SQLException e) {
log.error(e.getMessage());
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
} finally {
if(context != null) {
try {
context.complete();
} catch (SQLException e) {
log.error(e.getMessage() + " occurred while trying to close");
}
}
}
}
}

View File

@@ -44,28 +44,22 @@ public class ItemsResource {
private static final Logger log = Logger.getLogger(ItemsResource.class);
//ItemList - Not Implemented
private static org.dspace.core.Context context;
@GET
@Path("/{item_id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public org.dspace.rest.common.Item getItem(@PathParam("item_id") Integer item_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) throws WebApplicationException {
org.dspace.core.Context context = null;
try {
if(context == null || !context.isValid()) {
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);
}
context = new org.dspace.core.Context();
org.dspace.content.Item item = org.dspace.content.Item.find(context, item_id);
if(AuthorizeManager.authorizeActionBoolean(context, item, org.dspace.core.Constants.READ)) {
if(writeStatistics){
writeStats(item_id, user_ip, user_agent, xforwarderfor, headers, request);
writeStats(context, item_id, user_ip, user_agent, xforwarderfor, headers, request);
}
return new org.dspace.rest.common.Item(item, expand, context);
} else {
@@ -75,11 +69,19 @@ public class ItemsResource {
} catch (SQLException e) {
log.error(e.getMessage());
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
} finally {
if(context != null) {
try {
context.complete();
} catch (SQLException e) {
log.error(e.getMessage() + " occurred while trying to close");
}
}
}
}
private void writeStats(Integer item_id, String user_ip, String user_agent,
private void writeStats(org.dspace.core.Context context, Integer item_id, String user_ip, String user_agent,
String xforwarderfor, HttpHeaders headers,
HttpServletRequest request) {