Added method for proccessing finally statements.

Resource.writeStats should not throw exception, because it is not
important to abort method by writings statistic. But must abort own
context.
Added method for processing all finally statements.
Moved return to end of methods.
Repaired some bugs.
This commit is contained in:
ctu-developers
2014-10-13 11:39:09 +02:00
parent 57e72004be
commit 29b1d28566
6 changed files with 115 additions and 271 deletions

View File

@@ -120,11 +120,7 @@ public class BitstreamResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return bitstream;
@@ -185,11 +181,7 @@ public class BitstreamResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return policies.toArray(new ResourcePolicy[0]);
@@ -269,11 +261,7 @@ public class BitstreamResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return bitstreams.toArray(new Bitstream[0]);
@@ -311,6 +299,7 @@ public class BitstreamResource extends Resource
log.info("Reading data of bitstream(id=" + bitstreamId + ").");
org.dspace.core.Context context = null;
InputStream inputStream = null;
try
{
@@ -321,13 +310,10 @@ public class BitstreamResource extends Resource
request);
log.trace("Bitsream(id=" + bitstreamId + ") data was successfully read.");
InputStream inputStream = dspaceBitstream.retrieve();
inputStream = dspaceBitstream.retrieve();
String type = dspaceBitstream.getFormat().getMIMEType();
context.complete();
return Response.ok(inputStream).type(type).build();
}
catch (IOException e)
{
@@ -351,14 +337,10 @@ public class BitstreamResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return null;
return Response.ok(inputStream).type(type).build();
}
/**
@@ -435,11 +417,7 @@ public class BitstreamResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return Response.status(Status.OK).build();
}
@@ -567,11 +545,7 @@ public class BitstreamResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Bitstream metadata(id=" + bitstreamId + ") were successfully updated.");
@@ -658,11 +632,7 @@ public class BitstreamResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Bitstream(id=" + bitstreamId + ") data was successfully updated.");
@@ -737,11 +707,7 @@ public class BitstreamResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Bitstream(id=" + bitstreamId + ") was successfully deleted.");
@@ -815,11 +781,7 @@ public class BitstreamResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return Response.status(Status.OK).build();

View File

@@ -116,11 +116,7 @@ public class CollectionsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.trace("Collection(id=" + collectionId + ") has been successfully read.");
@@ -208,11 +204,7 @@ public class CollectionsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.trace("All collections were successfully read.");
@@ -261,6 +253,7 @@ public class CollectionsResource extends Resource
log.info("Reading collection(id=" + collectionId + ") items.");
org.dspace.core.Context context = null;
List<Item> items = null;
try
{
@@ -270,7 +263,7 @@ public class CollectionsResource extends Resource
writeStats(dspaceCollection, UsageEvent.Action.VIEW, user_ip, user_agent, xforwarderfor,
headers, request);
List<Item> items = new ArrayList<Item>();
items = new ArrayList<Item>();
org.dspace.content.ItemIterator dspaceItems = dspaceCollection.getItems();
for (int i = 0; (dspaceItems.hasNext()) && (i < (limit + offset)); i++)
{
@@ -287,8 +280,6 @@ public class CollectionsResource extends Resource
}
context.complete();
return items.toArray(new Item[0]);
}
catch (SQLException e)
{
@@ -300,15 +291,11 @@ public class CollectionsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.trace("All items in collection(id=" + collectionId + ") were successfully read.");
return null;
return items.toArray(new Item[0]);
}
/**
@@ -410,11 +397,7 @@ public class CollectionsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Item successfully created in collection(id=" + collectionId + "). Item handle=" + returnItem.getHandle());
@@ -488,11 +471,7 @@ public class CollectionsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Collection(id=" + collectionId + ") successfully updated.");
@@ -562,11 +541,7 @@ public class CollectionsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Collection(id=" + collectionId + ") was successfully deleted.");
@@ -672,11 +647,7 @@ public class CollectionsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Item(id=" + itemId + ") in collection(id=" + collectionId + ") was successfully deleted.");
@@ -740,11 +711,7 @@ public class CollectionsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
if (collection == null)

View File

@@ -104,11 +104,7 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
@@ -149,13 +145,14 @@ public class CommunitiesResource extends Resource
log.info("Reading all communities.(offset=" + offset + " ,limit=" + limit + ").");
org.dspace.core.Context context = null;
ArrayList<Community> communities = null;
try
{
context = createContext(getUser(headers));
org.dspace.content.Community[] dspaceCommunities = org.dspace.content.Community.findAll(context);
ArrayList<Community> communities = new ArrayList<Community>();
communities = new ArrayList<Community>();
if (!((limit != null) && (limit >= 0) && (offset != null) && (offset >= 0)))
{
@@ -176,10 +173,6 @@ public class CommunitiesResource extends Resource
}
context.complete();
log.trace("All communities successfully read.");
return communities.toArray(new Community[0]);
}
catch (SQLException e)
{
@@ -191,15 +184,11 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return null;
log.trace("All communities successfully read.");
return communities.toArray(new Community[0]);
}
/**
@@ -238,13 +227,14 @@ public class CommunitiesResource extends Resource
log.info("Reading all top communities.(offset=" + offset + " ,limit=" + limit + ").");
org.dspace.core.Context context = null;
ArrayList<Community> communities = null;
try
{
context = createContext(getUser(headers));
org.dspace.content.Community[] dspaceCommunities = org.dspace.content.Community.findAllTop(context);
ArrayList<Community> communities = new ArrayList<Community>();
communities = new ArrayList<Community>();
if (!((limit != null) && (limit >= 0) && (offset != null) && (offset >= 0)))
{
@@ -265,10 +255,6 @@ public class CommunitiesResource extends Resource
}
context.complete();
log.trace("All top communities successfully read.");
return communities.toArray(new Community[0]);
}
catch (SQLException e)
{
@@ -280,15 +266,11 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return null;
log.trace("All top communities successfully read.");
return communities.toArray(new Community[0]);
}
/**
@@ -327,6 +309,7 @@ public class CommunitiesResource extends Resource
log.info("Reading community(id=" + communityId + ") collections.");
org.dspace.core.Context context = null;
ArrayList<Collection> collections = null;
try
{
@@ -343,7 +326,7 @@ public class CommunitiesResource extends Resource
offset = 0;
}
ArrayList<Collection> collections = new ArrayList<Collection>();
collections = new ArrayList<Collection>();
org.dspace.content.Collection[] dspaceCollections = dspaceCommunity.getCollections();
for (int i = offset; (i < (offset + limit)) && (i < dspaceCollections.length); i++)
{
@@ -356,10 +339,6 @@ public class CommunitiesResource extends Resource
}
context.complete();
log.trace("Community(id=" + communityId + ") collections were successfully read.");
return collections.toArray(new Collection[0]);
}
catch (SQLException e)
{
@@ -373,15 +352,11 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return null;
log.trace("Community(id=" + communityId + ") collections were successfully read.");
return collections.toArray(new Collection[0]);
}
/**
@@ -420,6 +395,7 @@ public class CommunitiesResource extends Resource
log.info("Reading community(id=" + communityId + ") subcommunities.");
org.dspace.core.Context context = null;
ArrayList<Community> communities = null;
try
{
@@ -436,7 +412,7 @@ public class CommunitiesResource extends Resource
offset = 0;
}
ArrayList<Community> communities = new ArrayList<Community>();
communities = new ArrayList<Community>();
org.dspace.content.Community[] dspaceCommunities = dspaceCommunity.getSubcommunities();
for (int i = offset; (i < (offset + limit)) && (i < dspaceCommunities.length); i++)
{
@@ -449,10 +425,6 @@ public class CommunitiesResource extends Resource
}
context.complete();
log.trace("Community(id=" + communityId + ") subcommunities were successfully read.");
return communities.toArray(new Community[0]);
}
catch (SQLException e)
{
@@ -467,15 +439,11 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return null;
log.trace("Community(id=" + communityId + ") subcommunities were successfully read.");
return communities.toArray(new Community[0]);
}
/**
@@ -549,11 +517,7 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
@@ -635,11 +599,7 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
@@ -720,11 +680,7 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
@@ -797,13 +753,8 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
processFinally(context);
}
}
log.info("Community(id=" + communityId + ") has been successfully updated.");
return Response.ok().build();
@@ -867,11 +818,7 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
@@ -977,11 +924,7 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
@@ -1087,11 +1030,7 @@ public class CommunitiesResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}

View File

@@ -121,11 +121,7 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return item;
@@ -161,13 +157,14 @@ public class ItemsResource extends Resource
log.info("Reading items.(offset=" + offset + ",limit=" + limit + ").");
org.dspace.core.Context context = null;
List<Item> items = null;
try
{
context = createContext(getUser(headers));
ItemIterator dspaceItems = org.dspace.content.Item.findAllUnfiltered(context);
List<Item> items = new ArrayList<Item>();
items = new ArrayList<Item>();
if (!((limit != null) && (limit >= 0) && (offset != null) && (offset >= 0)))
{
@@ -189,12 +186,7 @@ public class ItemsResource extends Resource
}
}
}
context.complete();
log.trace("Items were successfully read.");
return items.toArray(new Item[0]);
}
catch (SQLException e)
{
@@ -206,14 +198,11 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return null;
log.trace("Items were successfully read.");
return items.toArray(new Item[0]);
}
/**
@@ -244,6 +233,7 @@ public class ItemsResource extends Resource
log.info("Reading item(id=" + itemId + ") metadata.");
org.dspace.core.Context context = null;
List<MetadataEntry> metadata = null;
try
{
@@ -252,12 +242,8 @@ public class ItemsResource extends Resource
writeStats(dspaceItem, UsageEvent.Action.VIEW, user_ip, user_agent, xforwarderfor, headers, request);
List<MetadataEntry> metadata = new org.dspace.rest.common.Item(dspaceItem, "metadata", context).getMetadata();
metadata = new org.dspace.rest.common.Item(dspaceItem, "metadata", context).getMetadata();
context.complete();
log.trace("Item(id=" + itemId + ") metadata were successfully read.");
return metadata.toArray(new MetadataEntry[0]);
}
catch (SQLException e)
{
@@ -269,13 +255,11 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
processFinally(context);
}
}
return null;
log.trace("Item(id=" + itemId + ") metadata were successfully read.");
return metadata.toArray(new MetadataEntry[0]);
}
/**
@@ -309,7 +293,7 @@ public class ItemsResource extends Resource
log.info("Reading item(id=" + itemId + ") bitstreams.(offset=" + offset + ",limit=" + limit + ")");
org.dspace.core.Context context = null;
List<Bitstream> bitstreams = null;
try
{
context = createContext(getUser(headers));
@@ -317,7 +301,6 @@ public class ItemsResource extends Resource
writeStats(dspaceItem, UsageEvent.Action.VIEW, user_ip, user_agent, xforwarderfor, headers, request);
List<Bitstream> bitstreams;
List<Bitstream> itemBitstreams = new Item(dspaceItem, "bitstreams", context).getBitstreams();
if ((offset + limit) > (itemBitstreams.size() - offset))
@@ -328,12 +311,7 @@ public class ItemsResource extends Resource
{
bitstreams = itemBitstreams.subList(offset, offset + limit);
}
context.complete();
log.trace("Item(id=" + itemId + ") bitstreams were successfully read.");
return bitstreams.toArray(new Bitstream[0]);
}
catch (SQLException e)
{
@@ -346,14 +324,11 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
return null;
log.trace("Item(id=" + itemId + ") bitstreams were successfully read.");
return bitstreams.toArray(new Bitstream[0]);
}
/**
@@ -425,11 +400,7 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Metadata to item(id=" + itemId + ") were successfully added.");
@@ -600,11 +571,7 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Bitstream(id=" + bitstream.getId() + ") was successfully added into item(id=" + itemId + ").");
@@ -690,11 +657,7 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Metadata of item(id=" + itemId + ") were successfully updated.");
@@ -763,11 +726,7 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Item(id=" + itemId + ") was successfully deleted.");
@@ -845,11 +804,7 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Item(id=" + itemId + ") metadata were successfully deleted.");
@@ -945,11 +900,7 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
log.info("Bitstream(id=" + bitstreamId + ") from item(id=" + itemId + ") was successfuly deleted .");
@@ -1071,11 +1022,7 @@ public class ItemsResource extends Resource
}
finally
{
if((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
processFinally(context);
}
if (items.size() == 0)

View File

@@ -82,7 +82,6 @@ public class Resource
{
if ((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
}
throw new ContextException("Could not create context, SQLException. Message: " + e, e);
@@ -128,16 +127,26 @@ public class Resource
log.debug("fired event");
context.complete();
}
catch (SQLException e)
{
processException("Could not write usageEvent, SQLException. Message: " + e, null); // Write statistic should not abort context.
if ((context != null) && (context.isValid()))
{
context.abort();
}
log.error("Could not write usageEvent, SQLException. Message: " + e);
}
catch (ContextException e)
{
processException("Could not write usageEvent, ContextException. Message: " + e.getMessage(), null); // Write statistic should not abort context.
log.error("Could not write usageEvent, ContextException. Message: " + e.getMessage());
}
finally
{
if ((context != null) && (context.isValid()))
{
context.abort();
log.error("Something get wrong. Aborting context in finally statement.");
}
}
}
@@ -162,6 +171,25 @@ public class Resource
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
/**
* Process finally statement. It will print message to logger error stream
* and abort DSpace context, if was not properly ended.
*
* @param context
* Context which must be aborted.
* @throws WebApplicationException
* This exception is throw for user of REST api.
*/
protected void processFinally(org.dspace.core.Context context) throws WebApplicationException
{
if ((context != null) && (context.isValid()))
{
context.abort();
log.error("Something get wrong. Aborting context in finally statement.");
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}
/**
* Split string with regex ".".
*

View File

@@ -97,8 +97,9 @@ public class TokenHolder
{
if ((context != null) && (context.isValid()))
{
log.error("Something get wrong. Aborting context in finally statement.");
context.abort();
log.error("Something get wrong. Aborting context in finally statement.");
throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
}
}