|
|
|
@ -50,6 +50,7 @@ import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
|
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.IdNotFoundException;
|
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.InvalidCallParameters;
|
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.InvalidItemException;
|
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.ItemLockedException;
|
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
|
|
|
|
|
import org.gcube.common.storagehub.model.exceptions.UserNotAuthorizedException;
|
|
|
|
|
import org.gcube.common.storagehub.model.items.AbstractFileItem;
|
|
|
|
@ -102,7 +103,7 @@ public class ItemsCreator {
|
|
|
|
|
|
|
|
|
|
@Inject Node2ItemConverter node2Item;
|
|
|
|
|
@Inject Item2NodeConverter item2Node;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//@Path("/{id}/create/{type:(?!FILE)[^/?$]*}")
|
|
|
|
|
@POST
|
|
|
|
@ -120,21 +121,25 @@ public class ItemsCreator {
|
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
|
|
|
|
|
|
log.info("time to connect to repo {}",(System.currentTimeMillis()-start));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Node destination;
|
|
|
|
|
try {
|
|
|
|
|
destination = ses.getNodeByIdentifier(id);
|
|
|
|
|
destination = ses.getNodeByIdentifier(id);
|
|
|
|
|
}catch(ItemNotFoundException inf) {
|
|
|
|
|
throw new IdNotFoundException(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!node2Item.checkNodeType(destination, FolderItem.class))
|
|
|
|
|
throw new InvalidItemException("the destination item is not a folder");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
authChecker.checkWriteAuthorizationControl(ses, destination.getIdentifier(), true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
|
|
|
|
|
try {
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
|
|
|
|
|
}catch (LockException le) {
|
|
|
|
|
throw new ItemLockedException(le);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Node newNode;
|
|
|
|
|
try {
|
|
|
|
|
newNode = Utils.createFolderInternally(ses, destination, name, description, hidden, login, accountingHandler);
|
|
|
|
@ -142,7 +147,7 @@ public class ItemsCreator {
|
|
|
|
|
} finally {
|
|
|
|
|
ses.getWorkspace().getLockManager().unlock(destination.getPath());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info("item with id {} correctly created",newNode.getIdentifier());
|
|
|
|
|
toReturn = newNode.getIdentifier();
|
|
|
|
|
}catch(StorageHubException she ){
|
|
|
|
@ -154,7 +159,7 @@ public class ItemsCreator {
|
|
|
|
|
}finally{
|
|
|
|
|
if (ses!=null)
|
|
|
|
|
ses.logout();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return toReturn;
|
|
|
|
|
}
|
|
|
|
@ -167,25 +172,29 @@ public class ItemsCreator {
|
|
|
|
|
log.info("create Gcube item called");
|
|
|
|
|
Session ses = null;
|
|
|
|
|
String toReturn = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
final String login = AuthorizationProvider.instance.get().getClient().getId();
|
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Node destination;
|
|
|
|
|
try {
|
|
|
|
|
destination = ses.getNodeByIdentifier(id);
|
|
|
|
|
destination = ses.getNodeByIdentifier(id);
|
|
|
|
|
}catch(ItemNotFoundException inf) {
|
|
|
|
|
throw new IdNotFoundException(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!node2Item.checkNodeType(destination, FolderItem.class))
|
|
|
|
|
throw new InvalidItemException("the destination item is not a folder");
|
|
|
|
|
|
|
|
|
|
authChecker.checkWriteAuthorizationControl(ses, destination.getIdentifier(), true);
|
|
|
|
|
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
|
|
|
|
|
}catch (LockException le) {
|
|
|
|
|
throw new ItemLockedException(le);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Node newNode;
|
|
|
|
|
try {
|
|
|
|
|
newNode = Utils.createGcubeItemInternally(ses, destination, item.getName(), item.getDescription(), login, item, accountingHandler);
|
|
|
|
@ -208,7 +217,7 @@ public class ItemsCreator {
|
|
|
|
|
}
|
|
|
|
|
return toReturn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
|
|
|
@ -228,18 +237,18 @@ public class ItemsCreator {
|
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
|
|
|
|
|
|
Node destination = ses.getNodeByIdentifier(id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info("create file called with filename {} in dir {} ", name, destination.getPath() );
|
|
|
|
|
|
|
|
|
|
if (!node2Item.checkNodeType(destination, FolderItem.class))
|
|
|
|
|
throw new InvalidItemException("the destination item is not a folder");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info("session: {}",ses.toString());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Node newNode = createFileItemInternally(ses, destination, stream, name, description, login);
|
|
|
|
|
ses.save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
versionHandler.checkinContentNode(newNode, ses);
|
|
|
|
|
log.info("file with id {} correctly created",newNode.getIdentifier());
|
|
|
|
|
toReturn = newNode.getIdentifier();
|
|
|
|
@ -259,12 +268,12 @@ public class ItemsCreator {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return toReturn;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Node createFileItemInternally(Session ses, Node destinationNode, InputStream stream, String name, String description, String login) throws RepositoryException, UserNotAuthorizedException, BackendGenericError{
|
|
|
|
|
private Node createFileItemInternally(Session ses, Node destinationNode, InputStream stream, String name, String description, String login) throws RepositoryException, UserNotAuthorizedException, ItemLockedException, BackendGenericError{
|
|
|
|
|
|
|
|
|
|
ContentHandler handler = getContentHandler(stream , name, destinationNode.getPath(), login);
|
|
|
|
|
|
|
|
|
@ -275,12 +284,17 @@ public class ItemsCreator {
|
|
|
|
|
|
|
|
|
|
log.debug("item prepared, fulfilling content");
|
|
|
|
|
log.debug("content prepared");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Node newNode;
|
|
|
|
|
try {
|
|
|
|
|
newNode = ses.getNode(org.gcube.common.storagehub.model.Paths.append(org.gcube.common.storagehub.model.Paths.getPath(destinationNode.getPath()), name).toPath());
|
|
|
|
|
authChecker.checkWriteAuthorizationControl(ses, newNode.getIdentifier(), false);
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(newNode.getPath(), true, true, 0,login);
|
|
|
|
|
try {
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(newNode.getPath(), true, true, 0,login);
|
|
|
|
|
}catch (LockException le) {
|
|
|
|
|
throw new ItemLockedException(le);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
versionHandler.checkoutContentNode(newNode, ses);
|
|
|
|
|
log.trace("replacing content of class {}",item.getContent().getClass());
|
|
|
|
@ -291,7 +305,11 @@ public class ItemsCreator {
|
|
|
|
|
}
|
|
|
|
|
}catch(PathNotFoundException pnf) {
|
|
|
|
|
authChecker.checkWriteAuthorizationControl(ses, destinationNode.getIdentifier(), true);
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destinationNode.getPath(), false, true, 0,login);
|
|
|
|
|
try {
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destinationNode.getPath(), false, true, 0,login);
|
|
|
|
|
}catch (LockException le) {
|
|
|
|
|
throw new ItemLockedException(le);
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
newNode = item2Node.getNode(destinationNode, item);
|
|
|
|
|
}finally {
|
|
|
|
@ -300,11 +318,11 @@ public class ItemsCreator {
|
|
|
|
|
versionHandler.makeVersionableContent(newNode, ses);
|
|
|
|
|
accountingHandler.createFolderAddObj(name, item.getClass().getSimpleName(), item.getContent().getMimeType(), ses, newNode, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return newNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@POST
|
|
|
|
|
@Consumes(MediaType.MULTIPART_FORM_DATA)
|
|
|
|
@ -324,77 +342,78 @@ public class ItemsCreator {
|
|
|
|
|
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
|
|
|
|
|
|
|
|
|
|
Node destination = ses.getNodeByIdentifier(id);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!node2Item.checkNodeType(destination, FolderItem.class))
|
|
|
|
|
throw new InvalidItemException("the destination item is not a folder");
|
|
|
|
|
|
|
|
|
|
authChecker.checkWriteAuthorizationControl(ses, destination.getIdentifier() , true);
|
|
|
|
|
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
|
|
|
|
|
Node parentDirectoryNode = null;
|
|
|
|
|
authChecker.checkWriteAuthorizationControl(ses, destination.getIdentifier() , true);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
parentDirectoryNode = Utils.createFolderInternally(ses, destination, parentFolderName, "", false, login, accountingHandler);
|
|
|
|
|
|
|
|
|
|
Set<Node> fileNodes = new HashSet<>();
|
|
|
|
|
ses.getWorkspace().getLockManager().lock(destination.getPath(), false, true, 0,login);
|
|
|
|
|
}catch (LockException le) {
|
|
|
|
|
throw new ItemLockedException(le);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Node parentDirectoryNode = Utils.createFolderInternally(ses, destination, parentFolderName, "", false, login, accountingHandler);
|
|
|
|
|
try {
|
|
|
|
|
if (ses.getWorkspace().getLockManager().isLocked(destination.getPath()))
|
|
|
|
|
ses.getWorkspace().getLockManager().unlock(destination.getPath());
|
|
|
|
|
} catch (Throwable t){
|
|
|
|
|
log.warn("error unlocking {}", destination.getPath(), t);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HashMap<String, Node> directoryNodeMap = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
try (ArchiveInputStream input = new ArchiveStreamFactory()
|
|
|
|
|
.createArchiveInputStream(new BufferedInputStream(stream, 1024*64))){
|
|
|
|
|
ArchiveEntry entry;
|
|
|
|
|
while ((entry = input.getNextEntry()) != null) {
|
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
|
Set<Node> fileNodes = new HashSet<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HashMap<String, Node> directoryNodeMap = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
try (ArchiveInputStream input = new ArchiveStreamFactory()
|
|
|
|
|
.createArchiveInputStream(new BufferedInputStream(stream, 1024*64))){
|
|
|
|
|
ArchiveEntry entry;
|
|
|
|
|
while ((entry = input.getNextEntry()) != null) {
|
|
|
|
|
if (entry.isDirectory()) {
|
|
|
|
|
String entirePath = entry.getName();
|
|
|
|
|
String name = entirePath.replaceAll("(.*/)*(.*)/", "$2");
|
|
|
|
|
String parentPath = entirePath.replaceAll("(.*/)*(.*)/", "$1");
|
|
|
|
|
log.debug("creating directory with entire path {}, name {}, parentPath {} ", entirePath, name, parentPath);
|
|
|
|
|
Node createdNode;
|
|
|
|
|
if (parentPath.isEmpty()) {
|
|
|
|
|
createdNode = Utils.createFolderInternally(ses, parentDirectoryNode, name, "", false, login, accountingHandler);
|
|
|
|
|
}else {
|
|
|
|
|
Node parentNode = directoryNodeMap.get(parentPath);
|
|
|
|
|
createdNode = Utils.createFolderInternally(ses, parentNode, name, "", false, login, accountingHandler);
|
|
|
|
|
}
|
|
|
|
|
directoryNodeMap.put(entirePath, createdNode);
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
String entirePath = entry.getName();
|
|
|
|
|
String name = entirePath.replaceAll("(.*/)*(.*)/", "$2");
|
|
|
|
|
String parentPath = entirePath.replaceAll("(.*/)*(.*)/", "$1");
|
|
|
|
|
log.debug("creating directory with entire path {}, name {}, parentPath {} ", entirePath, name, parentPath);
|
|
|
|
|
Node createdNode;
|
|
|
|
|
if (parentPath.isEmpty()) {
|
|
|
|
|
createdNode = Utils.createFolderInternally(ses, parentDirectoryNode, name, "", false, login, accountingHandler);
|
|
|
|
|
}else {
|
|
|
|
|
String name = entirePath.replaceAll("(.*/)*(.*)", "$2");
|
|
|
|
|
String parentPath = entirePath.replaceAll("(.*/)*(.*)", "$1");
|
|
|
|
|
log.debug("creating file with entire path {}, name {}, parentPath {} ", entirePath, name, parentPath);
|
|
|
|
|
Node fileNode = null;
|
|
|
|
|
if (parentPath.isEmpty())
|
|
|
|
|
fileNode = createFileItemInternally(ses, parentDirectoryNode, input, name, "", login);
|
|
|
|
|
else {
|
|
|
|
|
Node parentNode = directoryNodeMap.get(parentPath);
|
|
|
|
|
createdNode = Utils.createFolderInternally(ses, parentNode, name, "", false, login, accountingHandler);
|
|
|
|
|
}
|
|
|
|
|
directoryNodeMap.put(entirePath, createdNode);
|
|
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
try {
|
|
|
|
|
String entirePath = entry.getName();
|
|
|
|
|
String name = entirePath.replaceAll("(.*/)*(.*)", "$2");
|
|
|
|
|
String parentPath = entirePath.replaceAll("(.*/)*(.*)", "$1");
|
|
|
|
|
log.debug("creating file with entire path {}, name {}, parentPath {} ", entirePath, name, parentPath);
|
|
|
|
|
Node fileNode = null;
|
|
|
|
|
if (parentPath.isEmpty())
|
|
|
|
|
fileNode = createFileItemInternally(ses, parentDirectoryNode, input, name, "", login);
|
|
|
|
|
else {
|
|
|
|
|
Node parentNode = directoryNodeMap.get(parentPath);
|
|
|
|
|
fileNode = createFileItemInternally(ses, parentNode, input, name, "", login);
|
|
|
|
|
}
|
|
|
|
|
fileNodes.add(fileNode);
|
|
|
|
|
}catch(Exception e) {
|
|
|
|
|
log.warn("error getting file {}",entry.getName(),e);
|
|
|
|
|
fileNode = createFileItemInternally(ses, parentNode, input, name, "", login);
|
|
|
|
|
}
|
|
|
|
|
fileNodes.add(fileNode);
|
|
|
|
|
}catch(Exception e) {
|
|
|
|
|
log.warn("error getting file {}",entry.getName(),e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ses.save();
|
|
|
|
|
for (Node node : fileNodes)
|
|
|
|
|
versionHandler.checkinContentNode(node, ses);
|
|
|
|
|
toReturn = parentDirectoryNode.getIdentifier();
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
try {
|
|
|
|
|
if (ses.getWorkspace().getLockManager().isLocked(destination.getPath()))
|
|
|
|
|
ses.getWorkspace().getLockManager().unlock(destination.getPath());
|
|
|
|
|
} catch (Throwable t){
|
|
|
|
|
log.warn("error unlocking {}", destination.getPath(), t);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ses.save();
|
|
|
|
|
for (Node node : fileNodes)
|
|
|
|
|
versionHandler.checkinContentNode(node, ses);
|
|
|
|
|
toReturn = parentDirectoryNode.getIdentifier();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}catch(RepositoryException | ArchiveException | IOException re){
|
|
|
|
|
log.error("jcr error extracting archive", re);
|
|
|
|
|
GXOutboundErrorResponse.throwException(new BackendGenericError("jcr error extracting archive", re));
|
|
|
|
@ -412,7 +431,7 @@ public class ItemsCreator {
|
|
|
|
|
|
|
|
|
|
private ContentHandler getContentHandler(InputStream stream , String name, String path, String login) throws BackendGenericError {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final MultipleOutputStream mos;
|
|
|
|
|
try{
|
|
|
|
|
mos = new MultipleOutputStream(stream, 2);
|
|
|
|
@ -489,7 +508,7 @@ public class ItemsCreator {
|
|
|
|
|
try {
|
|
|
|
|
mos.startWriting();
|
|
|
|
|
log.debug("TIMING: writing the stream - finished in {}",System.currentTimeMillis()-start);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ContentHandler handler = detectorF.get();
|
|
|
|
|
MetaInfo info = uploaderF.get();
|
|
|
|
|
handler.getContent().setData(NodeConstants.CONTENT_NAME);
|
|
|
|
|