mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-12 04:23:13 +00:00
Merge pull request #895 from ctu-developers/DS-2511
DS-2511: Repaired resource policy endpoints.
This commit is contained in:
@@ -146,25 +146,16 @@ public class BitstreamResource extends Resource
|
|||||||
|
|
||||||
log.info("Reading bitstream(id=" + bitstreamId + ") policies.");
|
log.info("Reading bitstream(id=" + bitstreamId + ") policies.");
|
||||||
org.dspace.core.Context context = null;
|
org.dspace.core.Context context = null;
|
||||||
List<ResourcePolicy> policies = new ArrayList<ResourcePolicy>();
|
ResourcePolicy[] policies = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
context = createContext(getUser(headers));
|
context = createContext(getUser(headers));
|
||||||
org.dspace.content.Bitstream dspaceBitstream = findBitstream(context, bitstreamId, org.dspace.core.Constants.READ);
|
org.dspace.content.Bitstream dspaceBitstream = findBitstream(context, bitstreamId, org.dspace.core.Constants.READ);
|
||||||
|
AuthorizeManager.getPolicies(context, dspaceBitstream);
|
||||||
|
|
||||||
|
policies = new Bitstream(dspaceBitstream,"policies").getPolicies();
|
||||||
|
|
||||||
Bundle[] bundles = dspaceBitstream.getBundles();
|
|
||||||
for (Bundle bundle : bundles)
|
|
||||||
{
|
|
||||||
List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies = bundle.getBitstreamPolicies();
|
|
||||||
for (org.dspace.authorize.ResourcePolicy policy : bitstreamsPolicies)
|
|
||||||
{
|
|
||||||
if (policy.getResourceID() == bitstreamId)
|
|
||||||
{
|
|
||||||
policies.add(new ResourcePolicy(policy));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
context.complete();
|
context.complete();
|
||||||
log.trace("Policies for bitstream(id=" + bitstreamId + ") was successfully read.");
|
log.trace("Policies for bitstream(id=" + bitstreamId + ") was successfully read.");
|
||||||
|
|
||||||
@@ -184,7 +175,7 @@ public class BitstreamResource extends Resource
|
|||||||
processFinally(context);
|
processFinally(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
return policies.toArray(new ResourcePolicy[0]);
|
return policies;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -359,42 +350,26 @@ public class BitstreamResource extends Resource
|
|||||||
*/
|
*/
|
||||||
@POST
|
@POST
|
||||||
@Path("/{bitstream_id}/policy")
|
@Path("/{bitstream_id}/policy")
|
||||||
|
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
|
||||||
public javax.ws.rs.core.Response addBitstreamPolicy(@PathParam("bitstream_id") Integer bitstreamId, ResourcePolicy policy,
|
public javax.ws.rs.core.Response addBitstreamPolicy(@PathParam("bitstream_id") Integer bitstreamId, ResourcePolicy policy,
|
||||||
@Context HttpHeaders headers)
|
@QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent,
|
||||||
|
@QueryParam("xforwardedfor") String xforwardedfor, @Context HttpHeaders headers, @Context HttpServletRequest request)
|
||||||
|
throws WebApplicationException
|
||||||
{
|
{
|
||||||
|
|
||||||
log.info("Adding bitstream(id=" + bitstreamId + ") READ policy with permission for group(id=" + policy.getGroupId()
|
log.info("Adding bitstream(id=" + bitstreamId + ") " + policy.getAction() + " policy with permission for group(id=" + policy.getGroupId()
|
||||||
+ ").");
|
+ ").");
|
||||||
org.dspace.core.Context context = null;
|
org.dspace.core.Context context = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
context = createContext(getUser(headers));
|
context = createContext(getUser(headers));
|
||||||
org.dspace.content.Bitstream dspaceBitstream = findBitstream(context, bitstreamId, org.dspace.core.Constants.READ);
|
org.dspace.content.Bitstream dspaceBitstream = findBitstream(context, bitstreamId, org.dspace.core.Constants.WRITE);
|
||||||
|
|
||||||
Bundle[] bundles = dspaceBitstream.getBundles();
|
writeStats(dspaceBitstream, UsageEvent.Action.UPDATE, user_ip, user_agent, xforwardedfor, headers,
|
||||||
|
request, context);
|
||||||
|
|
||||||
for (Bundle bundle : bundles)
|
addPolicyToBitstream(context, policy, dspaceBitstream);
|
||||||
{
|
|
||||||
List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies = bundle.getBitstreamPolicies();
|
|
||||||
|
|
||||||
org.dspace.authorize.ResourcePolicy dspacePolicy = org.dspace.authorize.ResourcePolicy.create(context);
|
|
||||||
dspacePolicy.setAction(policy.getActionInt());
|
|
||||||
dspacePolicy.setGroup(Group.find(context, policy.getGroupId()));
|
|
||||||
dspacePolicy.setResourceID(dspaceBitstream.getID());
|
|
||||||
dspacePolicy.setResource(dspaceBitstream);
|
|
||||||
dspacePolicy.setResourceType(org.dspace.core.Constants.BITSTREAM);
|
|
||||||
dspacePolicy.setStartDate(policy.getStartDate());
|
|
||||||
dspacePolicy.setEndDate(policy.getEndDate());
|
|
||||||
dspacePolicy.setRpDescription(policy.getRpDescription());
|
|
||||||
dspacePolicy.setRpName(policy.getRpName());
|
|
||||||
dspacePolicy.update();
|
|
||||||
// dspacePolicy.setRpType(org.dspace.authorize.ResourcePolicy.TYPE_CUSTOM);
|
|
||||||
bitstreamsPolicies.add(dspacePolicy);
|
|
||||||
|
|
||||||
bundle.replaceAllBitstreamPolicies(bitstreamsPolicies);
|
|
||||||
bundle.update();
|
|
||||||
}
|
|
||||||
|
|
||||||
context.complete();
|
context.complete();
|
||||||
log.trace("Policy for bitstream(id=" + bitstreamId + ") was successfully added.");
|
log.trace("Policy for bitstream(id=" + bitstreamId + ") was successfully added.");
|
||||||
@@ -485,43 +460,14 @@ public class BitstreamResource extends Resource
|
|||||||
|
|
||||||
if (bitstream.getPolicies() != null)
|
if (bitstream.getPolicies() != null)
|
||||||
{
|
{
|
||||||
Bundle[] bundles = dspaceBitstream.getBundles();
|
log.trace("Updating bitstream policies.");
|
||||||
ResourcePolicy[] policies = bitstream.getPolicies();
|
|
||||||
for (Bundle bundle : bundles)
|
// Remove all old bitstream policies.
|
||||||
{
|
AuthorizeManager.removeAllPolicies(context,dspaceBitstream);
|
||||||
List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies = bundle.getBitstreamPolicies();
|
|
||||||
// Remove old bitstream policies
|
|
||||||
List<org.dspace.authorize.ResourcePolicy> policiesToRemove = new ArrayList<org.dspace.authorize.ResourcePolicy>();
|
|
||||||
for (org.dspace.authorize.ResourcePolicy policy : bitstreamsPolicies)
|
|
||||||
{
|
|
||||||
if (policy.getResourceID() == dspaceBitstream.getID())
|
|
||||||
{
|
|
||||||
policiesToRemove.add(policy);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (org.dspace.authorize.ResourcePolicy policy : policiesToRemove)
|
|
||||||
{
|
|
||||||
bitstreamsPolicies.remove(policy);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add all new bitstream policies
|
// Add all new bitstream policies
|
||||||
for (ResourcePolicy policy : policies)
|
for (ResourcePolicy policy : bitstream.getPolicies()) {
|
||||||
{
|
addPolicyToBitstream(context, policy, dspaceBitstream);
|
||||||
org.dspace.authorize.ResourcePolicy dspacePolicy = org.dspace.authorize.ResourcePolicy.create(context);
|
|
||||||
dspacePolicy.setAction(policy.getActionInt());
|
|
||||||
dspacePolicy.setGroup(Group.find(context, policy.getGroupId()));
|
|
||||||
dspacePolicy.setResourceID(dspaceBitstream.getID());
|
|
||||||
dspacePolicy.setResource(dspaceBitstream);
|
|
||||||
dspacePolicy.setResourceType(org.dspace.core.Constants.BITSTREAM);
|
|
||||||
dspacePolicy.setStartDate(policy.getStartDate());
|
|
||||||
dspacePolicy.setEndDate(policy.getEndDate());
|
|
||||||
dspacePolicy.setRpDescription(policy.getRpDescription());
|
|
||||||
dspacePolicy.setRpName(policy.getRpName());
|
|
||||||
dspacePolicy.update();
|
|
||||||
bitstreamsPolicies.add(dspacePolicy);
|
|
||||||
}
|
|
||||||
bundle.replaceAllBitstreamPolicies(bitstreamsPolicies);
|
|
||||||
bundle.update();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -730,55 +676,52 @@ public class BitstreamResource extends Resource
|
|||||||
@DELETE
|
@DELETE
|
||||||
@Path("/{bitstream_id}/policy/{policy_id}")
|
@Path("/{bitstream_id}/policy/{policy_id}")
|
||||||
public javax.ws.rs.core.Response deleteBitstreamPolicy(@PathParam("bitstream_id") Integer bitstreamId,
|
public javax.ws.rs.core.Response deleteBitstreamPolicy(@PathParam("bitstream_id") Integer bitstreamId,
|
||||||
@PathParam("policy_id") Integer policyId, @Context HttpHeaders headers)
|
@PathParam("policy_id") Integer policyId, @QueryParam("userIP") String user_ip, @QueryParam("userAgent") String user_agent,
|
||||||
|
@QueryParam("xforwardedfor") String xforwardedfor, @Context HttpHeaders headers, @Context HttpServletRequest request)
|
||||||
|
throws WebApplicationException
|
||||||
{
|
{
|
||||||
|
log.info("Deleting policy(id=" + policyId + ") from bitstream(id=" + bitstreamId + ").");
|
||||||
log.info("Deleting bitstream(id=" + bitstreamId + ") READ policy(id=" + policyId + ").");
|
|
||||||
org.dspace.core.Context context = null;
|
org.dspace.core.Context context = null;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
context = createContext(getUser(headers));
|
context = createContext(getUser(headers));
|
||||||
org.dspace.content.Bitstream dspaceBitstream = findBitstream(context, bitstreamId, org.dspace.core.Constants.READ);
|
org.dspace.content.Bitstream dspaceBitstream = findBitstream(context, bitstreamId, org.dspace.core.Constants.WRITE);
|
||||||
|
|
||||||
Bundle[] bundles = dspaceBitstream.getBundles();
|
writeStats(dspaceBitstream, UsageEvent.Action.UPDATE, user_ip, user_agent, xforwardedfor, headers,
|
||||||
|
request, context);
|
||||||
|
|
||||||
for (Bundle bundle : bundles)
|
// Check if resource policy exists in bitstream.
|
||||||
{
|
boolean found = false;
|
||||||
List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies = bundle.getBitstreamPolicies();
|
List<org.dspace.authorize.ResourcePolicy> policies = AuthorizeManager.getPolicies(context, dspaceBitstream);
|
||||||
|
for(org.dspace.authorize.ResourcePolicy policy : policies) {
|
||||||
for (org.dspace.authorize.ResourcePolicy policy : bitstreamsPolicies)
|
if(policy.getID() == policyId) {
|
||||||
{
|
found = true;
|
||||||
if (policy.getID() == policyId.intValue())
|
|
||||||
{
|
|
||||||
bitstreamsPolicies.remove(policy);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bundle.replaceAllBitstreamPolicies(bitstreamsPolicies);
|
if(found) {
|
||||||
bundle.update();
|
removePolicyFromBitstream(context, policyId, bitstreamId);
|
||||||
|
} else {
|
||||||
|
context.abort();
|
||||||
|
throw new WebApplicationException(Response.Status.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
context.complete();
|
context.complete();
|
||||||
log.trace("Policy for bitstream(id=" + bitstreamId + ") was successfully added.");
|
log.trace("Policy for bitstream(id=" + bitstreamId + ") was successfully removed.");
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (SQLException e)
|
catch (SQLException e)
|
||||||
{
|
{
|
||||||
processException("Someting went wrong while deleting READ policy(id=" + policyId + ") to bitstream(id=" + bitstreamId
|
processException("Someting went wrong while deleting policy(id=" + policyId + ") to bitstream(id=" + bitstreamId
|
||||||
+ "), SQLException! Message: " + e, context);
|
+ "), SQLException! Message: " + e, context);
|
||||||
}
|
}
|
||||||
catch (ContextException e)
|
catch (ContextException e)
|
||||||
{
|
{
|
||||||
processException("Someting went wrong while deleting READ policy(id=" + policyId + ") to bitstream(id=" + bitstreamId
|
processException("Someting went wrong while deleting policy(id=" + policyId + ") to bitstream(id=" + bitstreamId
|
||||||
+ "), ContextException. Message: " + e.getMessage(), context);
|
+ "), ContextException. Message: " + e.getMessage(), context);
|
||||||
}
|
}
|
||||||
catch (AuthorizeException e)
|
|
||||||
{
|
|
||||||
processException("Someting went wrong while deleting READ policy(id=" + policyId + ") to bitstream(id=" + bitstreamId
|
|
||||||
+ "), AuthorizeException! Message: " + e, context);
|
|
||||||
}
|
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
processFinally(context);
|
processFinally(context);
|
||||||
@@ -799,6 +742,41 @@ public class BitstreamResource extends Resource
|
|||||||
return URLConnection.guessContentTypeFromName(name);
|
return URLConnection.guessContentTypeFromName(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add policy(org.dspace.rest.common.ResourcePolicy) to bitstream.
|
||||||
|
* @param context Context to create DSpace ResourcePolicy.
|
||||||
|
* @param policy Policy which will be added to bitstream.
|
||||||
|
* @param dspaceBitstream
|
||||||
|
* @throws SQLException
|
||||||
|
* @throws AuthorizeException
|
||||||
|
*/
|
||||||
|
private void addPolicyToBitstream(org.dspace.core.Context context, ResourcePolicy policy, org.dspace.content.Bitstream dspaceBitstream) throws SQLException, AuthorizeException {
|
||||||
|
org.dspace.authorize.ResourcePolicy dspacePolicy = org.dspace.authorize.ResourcePolicy.create(context);
|
||||||
|
dspacePolicy.setAction(policy.getActionInt());
|
||||||
|
dspacePolicy.setGroup(Group.find(context, policy.getGroupId()));
|
||||||
|
dspacePolicy.setResourceID(dspaceBitstream.getID());
|
||||||
|
dspacePolicy.setResource(dspaceBitstream);
|
||||||
|
dspacePolicy.setResourceType(org.dspace.core.Constants.BITSTREAM);
|
||||||
|
dspacePolicy.setStartDate(policy.getStartDate());
|
||||||
|
dspacePolicy.setEndDate(policy.getEndDate());
|
||||||
|
dspacePolicy.setRpDescription(policy.getRpDescription());
|
||||||
|
dspacePolicy.setRpName(policy.getRpName());
|
||||||
|
|
||||||
|
dspacePolicy.update();
|
||||||
|
dspaceBitstream.updateLastModified();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove policy from bitstream. But only if resourceID of policy is same as bitstream id.
|
||||||
|
* @param context Context to delete policy.
|
||||||
|
* @param policyID Id of resource policy, which will be deleted.
|
||||||
|
* @param bitstreamID Id of bitstream.
|
||||||
|
* @throws SQLException
|
||||||
|
*/
|
||||||
|
private void removePolicyFromBitstream(org.dspace.core.Context context, int policyID, int bitstreamID) throws SQLException {
|
||||||
|
DatabaseManager.updateQuery(context, "DELETE FROM resourcepolicy WHERE POLICY_ID = ? AND RESOURCE_ID = ?", policyID,bitstreamID);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find bitstream from DSpace database. This encapsulatets the
|
* Find bitstream from DSpace database. This encapsulatets the
|
||||||
* org.dspace.content.Bitstream.find method with a check whether the item exists and
|
* org.dspace.content.Bitstream.find method with a check whether the item exists and
|
||||||
|
@@ -543,11 +543,9 @@ public class ItemsResource extends Resource
|
|||||||
date.setSeconds(0);
|
date.setSeconds(0);
|
||||||
dspacePolicy.setStartDate(date);
|
dspacePolicy.setStartDate(date);
|
||||||
}
|
}
|
||||||
dspacePolicy.update();
|
|
||||||
bitstreamsPolicies.add(dspacePolicy);
|
|
||||||
|
|
||||||
dspaceBundle.replaceAllBitstreamPolicies(bitstreamsPolicies);
|
dspacePolicy.update();
|
||||||
dspaceBundle.update();
|
dspaceBitstream.updateLastModified();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -76,7 +76,12 @@ public class Bitstream extends DSpaceObject {
|
|||||||
|
|
||||||
if(expandFields.contains("parent") || expandFields.contains("all")) {
|
if(expandFields.contains("parent") || expandFields.contains("all")) {
|
||||||
parentObject = new DSpaceObject(bitstream.getParentObject());
|
parentObject = new DSpaceObject(bitstream.getParentObject());
|
||||||
} else if(expandFields.contains("policies") || expandFields.contains("all")) {
|
} else {
|
||||||
|
this.addExpand("parent");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(expandFields.contains("policies") || expandFields.contains("all")) {
|
||||||
|
// Find policies without context.
|
||||||
List<ResourcePolicy> tempPolicies = new ArrayList<ResourcePolicy>();
|
List<ResourcePolicy> tempPolicies = new ArrayList<ResourcePolicy>();
|
||||||
Bundle[] bundles = bitstream.getBundles();
|
Bundle[] bundles = bitstream.getBundles();
|
||||||
for (Bundle bundle : bundles) {
|
for (Bundle bundle : bundles) {
|
||||||
@@ -90,7 +95,6 @@ public class Bitstream extends DSpaceObject {
|
|||||||
|
|
||||||
policies = tempPolicies.toArray(new ResourcePolicy[0]);
|
policies = tempPolicies.toArray(new ResourcePolicy[0]);
|
||||||
} else {
|
} else {
|
||||||
this.addExpand("parent");
|
|
||||||
this.addExpand("policies");
|
this.addExpand("policies");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -11,6 +11,9 @@ import java.util.Date;
|
|||||||
|
|
||||||
import org.codehaus.jackson.annotate.JsonIgnore;
|
import org.codehaus.jackson.annotate.JsonIgnore;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "resourcepolicy")
|
||||||
public class ResourcePolicy{
|
public class ResourcePolicy{
|
||||||
|
|
||||||
public enum Action {
|
public enum Action {
|
||||||
|
Reference in New Issue
Block a user