*/
public DepositResult doDeposit(Deposit deposit)
throws SWORDErrorException, DSpaceSWORDException
{
// get the things out of the service that we need
Context context = swordService.getContext();
SWORDConfiguration swordConfig = swordService.getSwordConfig();
SWORDUrlManager urlManager = swordService.getUrlManager();
// FIXME: the spec is unclear what to do in this situation. I'm going
// the throw a 415 (ERROR_CONTENT) until further notice
//
// determine if this is an acceptable file format
if (!swordConfig.isAcceptableContentType(context, deposit.getContentType(), collection))
{
log.error("Unacceptable content type detected: " + deposit.getContentType() + " for collection " + collection.getID());
throw new SWORDErrorException(ErrorCodes.ERROR_CONTENT,
"Unacceptable content type in deposit request: " + deposit.getContentType());
}
// determine if this is an acceptable packaging type for the deposit
// if not, we throw a 415 HTTP error (Unsupported Media Type, ERROR_CONTENT)
if (!swordConfig.isSupportedMediaType(deposit.getPackaging(), this.collection))
{
log.error("Unacceptable packaging type detected: " + deposit.getPackaging() + "for collection" + collection.getID());
throw new SWORDErrorException(ErrorCodes.ERROR_CONTENT,
"Unacceptable packaging type in deposit request: " + deposit.getPackaging());
}
String tempDir = swordConfig.getTempDir();
String tempFile = tempDir + "/" + swordService.getTempFilename();
log.debug("Storing temporary file at " + tempFile);
if (swordConfig.isKeepOriginal())
{
try
{
swordService.message("DSpace will store an original copy of the deposit, " +
"as well as ingesting the item into the archive");
// first, store the temp file
InputStream is = deposit.getFile();
FileOutputStream fos = new FileOutputStream(tempFile);
Utils.copy(is, fos);
fos.close();
is.close();
// now create an input stream from that temp file to ingest
InputStream fis = new FileInputStream(tempFile);
deposit.setFile(fis);
}
catch (FileNotFoundException e)
{
log.error("caught exception: ", e);
throw new DSpaceSWORDException(e);
}
catch (IOException e)
{
log.error("caught exception: ", e);
throw new DSpaceSWORDException(e);
}
}
// Obtain the relevant ingester from the factory
SWORDIngester si = SWORDIngesterFactory.getInstance(context, deposit, collection);
swordService.message("Loaded ingester: " + si.getClass().getName());
// do the deposit
DepositResult result = si.ingest(swordService, deposit, collection);
swordService.message("Archive ingest completed successfully");
// if there's an item availalble, and we want to keep the original
// then do that
try
{
if (swordConfig.isKeepOriginal())
{
// in order to be allowed to add the file back to the item, we need to ignore authorisations
// for a moment
boolean ignoreAuth = context.ignoreAuthorization();
context.setIgnoreAuthorization(true);
String bundleName = ConfigurationManager.getProperty("sword.bundle.name");
if (bundleName == null || "".equals(bundleName))
{
bundleName = "SWORD";
}
Item item = result.getItem();
Bundle[] bundles = item.getBundles(bundleName);
Bundle swordBundle = null;
if (bundles.length > 0)
{
swordBundle = bundles[0];
}
if (swordBundle == null)
{
swordBundle = item.createBundle(bundleName);
}
String fn = swordService.getFilename(context, deposit, true);
FileInputStream fis = new FileInputStream(tempFile);
Bitstream bitstream = swordBundle.createBitstream(fis);
bitstream.setName(fn);
bitstream.setDescription("SWORD deposit package");
BitstreamFormat bf = BitstreamFormat.findByMIMEType(context, deposit.getContentType());
if (bf != null)
{
bitstream.setFormat(bf);
}
bitstream.update();
swordBundle.update();
item.update();
swordService.message("Original package stored as " + fn + ", in item bundle " + swordBundle);
// now reset the context ignore authorisation
context.setIgnoreAuthorization(ignoreAuth);
// set the media link for the created item
result.setMediaLink(urlManager.getMediaLink(bitstream));
}
else