public void storeItem(Repository repository, StorageItem item)
throws UnsupportedStorageOperationException, LocalStorageException
{
final File target;
final ContentLocator originalContentLocator;
if (item instanceof StorageFileItem) {
originalContentLocator = ((StorageFileItem) item).getContentLocator();
}
else {
originalContentLocator = null;
}
try {
// set some sanity stuff
item.setStoredLocally(System.currentTimeMillis());
item.setRemoteChecked(item.getStoredLocally());
item.setExpired(false);
ContentLocator cl = null;
if (item instanceof StorageFileItem) {
StorageFileItem fItem = (StorageFileItem) item;
prepareStorageFileItemForStore(fItem);
cl = fItem.getContentLocator();
}
else if (item instanceof StorageLinkItem) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
getLinkPersister().writeLinkContent((StorageLinkItem) item, bos);
}
catch (IOException e) {
// should not happen, look at implementation
// we will handle here two byte array backed streams!
throw new LocalStorageException("Problem ", e);
}
cl = new ByteArrayContentLocator(bos.toByteArray(), "text/xml");
}
target = getFileFromBase(repository, item.getResourceStoreRequest());
getFSPeer().storeItem(repository, getBaseDir(repository, item.getResourceStoreRequest()), item, target, cl);
}
finally {
// NEXUS-5468: Ensure that in case of file item with prepared content
// (typically those coming from RRS, as the content is actually wrapped HTTP response body, hence not reusable)
// get closed irrelevant of the actual outcome. If all went right, stream was already closed,
// and we will be "punished" by one extra (redundant) call to Closeable#close().
if (originalContentLocator instanceof Closeable) {
IOUtils.closeQuietly((Closeable) originalContentLocator);
}
}
if (item instanceof StorageFileItem) {
// replace content locator transparently, if we just consumed a non-reusable one
// Hint: in general, those items coming from user uploads or remote proxy caching requests are non
// reusable ones
((StorageFileItem) item).setContentLocator(new FileContentLocator(target,
((StorageFileItem) item).getMimeType()));
}
final ContentLocator mdis =
item instanceof StorageFileItem ? ((StorageFileItem) item).getContentLocator() : null;
try {
repository.getAttributesHandler().storeAttributes(item, mdis);
}