mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-17 06:53:09 +00:00
DS-1986 Get new context each time, close in finally, for all REST methods
This commit is contained in:
@@ -36,7 +36,6 @@ import java.sql.SQLException;
|
|||||||
@Path("/bitstreams")
|
@Path("/bitstreams")
|
||||||
public class BitstreamResource {
|
public class BitstreamResource {
|
||||||
Logger log = Logger.getLogger(BitstreamResource.class);
|
Logger log = Logger.getLogger(BitstreamResource.class);
|
||||||
private static org.dspace.core.Context context;
|
|
||||||
|
|
||||||
private static final boolean writeStatistics;
|
private static final boolean writeStatistics;
|
||||||
|
|
||||||
@@ -50,12 +49,9 @@ public class BitstreamResource {
|
|||||||
@Path("/{bitstream_id}")
|
@Path("/{bitstream_id}")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||||
public Bitstream getBitstream(@PathParam("bitstream_id") Integer bitstream_id, @QueryParam("expand") String expand) {
|
public Bitstream getBitstream(@PathParam("bitstream_id") Integer bitstream_id, @QueryParam("expand") String expand) {
|
||||||
|
org.dspace.core.Context context = null;
|
||||||
try {
|
try {
|
||||||
if(context == null || !context.isValid()) {
|
context = new org.dspace.core.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
org.dspace.content.Bitstream bitstream = org.dspace.content.Bitstream.find(context, bitstream_id);
|
org.dspace.content.Bitstream bitstream = org.dspace.content.Bitstream.find(context, bitstream_id);
|
||||||
|
|
||||||
@@ -67,6 +63,14 @@ public class BitstreamResource {
|
|||||||
} catch(SQLException e) {
|
} catch(SQLException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
|
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,
|
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,
|
@QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor,
|
||||||
@Context HttpHeaders headers, @Context HttpServletRequest request) {
|
@Context HttpHeaders headers, @Context HttpServletRequest request) {
|
||||||
|
org.dspace.core.Context context = null;
|
||||||
try {
|
try {
|
||||||
if(context == null || !context.isValid() ) {
|
context = new org.dspace.core.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
org.dspace.content.Bitstream bitstream = org.dspace.content.Bitstream.find(context, bitstream_id);
|
org.dspace.content.Bitstream bitstream = org.dspace.content.Bitstream.find(context, bitstream_id);
|
||||||
if(AuthorizeManager.authorizeActionBoolean(context, bitstream, org.dspace.core.Constants.READ)) {
|
if(AuthorizeManager.authorizeActionBoolean(context, bitstream, org.dspace.core.Constants.READ)) {
|
||||||
if(writeStatistics){
|
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();
|
return Response.ok(bitstream.retrieve()).type(bitstream.getFormat().getMIMEType()).build();
|
||||||
@@ -102,10 +103,18 @@ public class BitstreamResource {
|
|||||||
} catch (AuthorizeException e) {
|
} catch (AuthorizeException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
|
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,
|
String xforwarderfor, HttpHeaders headers,
|
||||||
HttpServletRequest request) {
|
HttpServletRequest request) {
|
||||||
|
|
||||||
|
@@ -38,11 +38,8 @@ http://localhost:8080/<webapp>/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 final boolean writeStatistics;
|
private static final boolean writeStatistics;
|
||||||
|
|
||||||
static{
|
static{
|
||||||
@@ -53,12 +50,9 @@ public class CollectionsResource {
|
|||||||
@Path("/")
|
@Path("/")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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) {
|
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 {
|
try {
|
||||||
if(context == null || !context.isValid() ) {
|
context = new org.dspace.core.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
org.dspace.content.Collection[] collections;
|
org.dspace.content.Collection[] collections;
|
||||||
|
|
||||||
@@ -82,6 +76,14 @@ public class CollectionsResource {
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
|
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("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,
|
@QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor,
|
||||||
@Context HttpHeaders headers, @Context HttpServletRequest request) {
|
@Context HttpHeaders headers, @Context HttpServletRequest request) {
|
||||||
|
org.dspace.core.Context context = null;
|
||||||
try {
|
try {
|
||||||
if(context == null || !context.isValid() ) {
|
context = new org.dspace.core.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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){
|
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);
|
return new org.dspace.rest.common.Collection(collection, expand, context, limit, offset);
|
||||||
} else {
|
} else {
|
||||||
@@ -111,10 +110,18 @@ public class CollectionsResource {
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
|
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,
|
String xforwarderfor, HttpHeaders headers,
|
||||||
HttpServletRequest request) {
|
HttpServletRequest request) {
|
||||||
|
|
||||||
|
@@ -32,18 +32,14 @@ import java.sql.SQLException;
|
|||||||
@Path("/handle")
|
@Path("/handle")
|
||||||
public class HandleResource {
|
public class HandleResource {
|
||||||
private static Logger log = Logger.getLogger(HandleResource.class);
|
private static Logger log = Logger.getLogger(HandleResource.class);
|
||||||
private static org.dspace.core.Context context;
|
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/{prefix}/{suffix}")
|
@Path("/{prefix}/{suffix}")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@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) {
|
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 {
|
try {
|
||||||
if(context == null || !context.isValid() ) {
|
context = new org.dspace.core.Context();
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
org.dspace.content.DSpaceObject dso = HandleManager.resolveToObject(context, prefix + "/" + suffix);
|
org.dspace.content.DSpaceObject dso = HandleManager.resolveToObject(context, prefix + "/" + suffix);
|
||||||
if(dso == null) {
|
if(dso == null) {
|
||||||
@@ -68,6 +64,14 @@ public class HandleResource {
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -44,8 +44,6 @@ public class ItemsResource {
|
|||||||
private static final Logger log = Logger.getLogger(ItemsResource.class);
|
private static final Logger log = Logger.getLogger(ItemsResource.class);
|
||||||
//ItemList - Not Implemented
|
//ItemList - Not Implemented
|
||||||
|
|
||||||
private static org.dspace.core.Context context;
|
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
@Path("/{item_id}")
|
@Path("/{item_id}")
|
||||||
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
|
||||||
@@ -53,19 +51,15 @@ public class ItemsResource {
|
|||||||
@QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor,
|
@QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent, @QueryParam("xforwarderfor") String xforwarderfor,
|
||||||
@Context HttpHeaders headers, @Context HttpServletRequest request) throws WebApplicationException {
|
@Context HttpHeaders headers, @Context HttpServletRequest request) throws WebApplicationException {
|
||||||
|
|
||||||
|
org.dspace.core.Context context = null;
|
||||||
try {
|
try {
|
||||||
if(context == null || !context.isValid()) {
|
context = new org.dspace.core.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);
|
|
||||||
}
|
|
||||||
|
|
||||||
org.dspace.content.Item item = org.dspace.content.Item.find(context, item_id);
|
org.dspace.content.Item item = org.dspace.content.Item.find(context, item_id);
|
||||||
|
|
||||||
if(AuthorizeManager.authorizeActionBoolean(context, item, org.dspace.core.Constants.READ)) {
|
if(AuthorizeManager.authorizeActionBoolean(context, item, org.dspace.core.Constants.READ)) {
|
||||||
if(writeStatistics){
|
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);
|
return new org.dspace.rest.common.Item(item, expand, context);
|
||||||
} else {
|
} else {
|
||||||
@@ -75,11 +69,19 @@ public class ItemsResource {
|
|||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
|
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,
|
String xforwarderfor, HttpHeaders headers,
|
||||||
HttpServletRequest request) {
|
HttpServletRequest request) {
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user