throws WebApplicationException
{
log.info("Adding bitstream to item(id=" + itemId + ").");
org.dspace.core.Context context = null;
Bitstream bitstream = null;
try
{
context = createContext(getUser(headers));
org.dspace.content.Item dspaceItem = findItem(context, itemId, org.dspace.core.Constants.WRITE);
writeStats(dspaceItem, UsageEvent.Action.UPDATE, user_ip, user_agent, xforwarderfor, headers, request, context);
// Is better to add bitstream to ORIGINAL bundle or to item own?
log.trace("Creating bitstream in item.");
org.dspace.content.Bundle bundle = null;
org.dspace.content.Bitstream dspaceBitstream = null;
if ((dspaceItem.getBundles().length == 0) || (dspaceItem.getBundles() == null))
{
log.trace("Creating bundle in item.");
dspaceBitstream = dspaceItem.createSingleBitstream(inputStream);
}
else
{
log.trace("Getting bundle from item.");
bundle = dspaceItem.getBundles()[0];
dspaceBitstream = bundle.createBitstream(inputStream);
}
dspaceBitstream.setSource("DSpace Rest api");
// Set bitstream name and description
if (name != null)
{
if (BitstreamResource.getMimeType(name) == null)
{
dspaceBitstream.setFormat(BitstreamFormat.findUnknown(context));
}
else
{
dspaceBitstream.setFormat(BitstreamFormat.findByMIMEType(context, BitstreamResource.getMimeType(name)));
}
dspaceBitstream.setName(name);
}
if (description != null)
{
dspaceBitstream.setDescription(description);
}
dspaceBitstream.update();
// Create policy for bitstream
if (groupId != null)
{
Bundle[] bundles = dspaceBitstream.getBundles();
for (Bundle dspaceBundle : bundles)
{
List<org.dspace.authorize.ResourcePolicy> bitstreamsPolicies = dspaceBundle.getBitstreamPolicies();
// Remove default 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);
}
org.dspace.authorize.ResourcePolicy dspacePolicy = org.dspace.authorize.ResourcePolicy.create(context);
dspacePolicy.setAction(org.dspace.core.Constants.READ);
dspacePolicy.setGroup(Group.find(context, groupId));
dspacePolicy.setResourceID(dspaceBitstream.getID());
dspacePolicy.setResource(dspaceBitstream);
dspacePolicy.setResourceType(org.dspace.core.Constants.BITSTREAM);
if ((year != null) || (month != null) || (day != null))
{
Date date = new Date();
if (year != null)
{
date.setYear(year - 1900);
}
if (month != null)
{
date.setMonth(month - 1);
}
if (day != null)
{
date.setDate(day);
}
date.setHours(0);
date.setMinutes(0);
date.setSeconds(0);
dspacePolicy.setStartDate(date);
}
dspacePolicy.update();
bitstreamsPolicies.add(dspacePolicy);
dspaceBundle.replaceAllBitstreamPolicies(bitstreamsPolicies);
dspaceBundle.update();
}
}
dspaceBitstream = org.dspace.content.Bitstream.find(context, dspaceBitstream.getID());
bitstream = new Bitstream(dspaceBitstream, "");
context.complete();
}
catch (SQLException e)
{
processException("Could not create bitstream in item(id=" + itemId + "), SQLException. Message: " + e, context);
}
catch (AuthorizeException e)
{
processException("Could not create bitstream in item(id=" + itemId + "), AuthorizeException. Message: " + e, context);
}
catch (IOException e)
{
processException("Could not create bitstream in item(id=" + itemId + "), IOException Message: " + e, context);
}
catch (ContextException e)
{
processException(
"Could not create bitstream in item(id=" + itemId + "), ContextException Message: " + e.getMessage(), context);
}
finally
{
processFinally(context);
}
log.info("Bitstream(id=" + bitstream.getId() + ") was successfully added into item(id=" + itemId + ").");
return bitstream;
}