added possibility to set owner on backends

This commit is contained in:
Lucio Lelii 2022-12-02 14:39:37 +01:00
parent 4083b7c120
commit 1f6329c38e
9 changed files with 54 additions and 46 deletions

View File

@ -37,6 +37,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<enunciate.version>2.14.0</enunciate.version>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencyManagement>

View File

@ -217,17 +217,18 @@ public class Utils {
public static String checkExistanceAndGetUniqueName(Session ses, Node destination, String name) throws BackendGenericError{
String escapedName = Text.escapeIllegalJcrChars(name);
try {
destination.getNode(name);
destination.getNode(escapedName);
}catch(PathNotFoundException pnf) {
return Text.escapeIllegalJcrChars(name);
return escapedName;
} catch (Exception e) {
throw new BackendGenericError(e);
}
try {
String filename = FilenameUtils.getBaseName(name);
String ext = FilenameUtils.getExtension(name);
String filename = FilenameUtils.getBaseName(escapedName);
String ext = FilenameUtils.getExtension(escapedName);
String nameTocheck = ext.isEmpty()? String.format("%s(*)",filename): String.format("%s(*).%s",filename, ext);
@ -244,7 +245,7 @@ public class Utils {
String newName = ext.isEmpty()? String.format("%s(%d)", filename,maxval+1) : String.format("%s(%d).%s", filename,maxval+1, ext) ;
return Text.escapeIllegalJcrChars(newName);
return newName;
} catch (Exception e) {
throw new BackendGenericError(e);
}

View File

@ -18,12 +18,14 @@ import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import org.apache.commons.io.FilenameUtils;
import org.gcube.common.storagehub.model.Constants;
import org.gcube.common.storagehub.model.Excludes;
import org.gcube.common.storagehub.model.Paths;
import org.gcube.common.storagehub.model.exceptions.InvalidItemException;
import org.gcube.common.storagehub.model.exceptions.PluginInitializationException;
import org.gcube.common.storagehub.model.exceptions.PluginNotFoundException;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.common.storagehub.model.exceptions.StorageIdNotFoundException;
import org.gcube.common.storagehub.model.items.AbstractFileItem;
import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.common.storagehub.model.items.Item;
@ -104,7 +106,7 @@ public class DownloadHandler {
StorageBackend sb = sbf.create(content.getPayloadBackend());
final InputStream streamToWrite = sb.download(content);
InputStream streamToWrite = sb.download(content);
if (withAccounting)
accountingHandler.createReadObj(fileItem.getTitle(), ses, (Node) fileItem.getRelatedNode(), login, true);
@ -133,8 +135,18 @@ public class DownloadHandler {
StorageBackendFactory sbf = storageBackendHandler.get(content.getPayloadBackend());
StorageBackend sb = sbf.create(content.getPayloadBackend());
final InputStream streamToWrite = sb.download(content);
InputStream streamToWrite = null;
try {
streamToWrite = sb.download(content);
}catch (StorageIdNotFoundException e) {
//TODO: temporary code, it will last until the MINIO porting will not finish
if (sbf.getName().equals(Constants.MONGO_STORAGE)) {
sbf = storageBackendHandler.get(Constants.DEFAULT_MINIO_STORAGE);
sbf.create(new PayloadBackend(Constants.DEFAULT_MINIO_STORAGE, null));
} else
throw e;
}
log.debug("retrieved storage id is {} with storageBackend {} (stream is null? {})",content.getStorageId(), sbf.getName(), streamToWrite==null );
String oldfilename = FilenameUtils.getBaseName(currentItem.getTitle());
@ -165,7 +177,7 @@ public class DownloadHandler {
StorageBackend sb = sbf.create(new PayloadBackend(storageName, null));
final InputStream streamToWrite = sb.download(storageId);
InputStream streamToWrite = sb.download(storageId);
Map<String, String> userMetadata = sb.getFileMetadata(storageId);

View File

@ -340,9 +340,9 @@ public class ItemHandler {
log.debug("UPLOAD: upload on {} - start",storageBackend.getClass());
if (fileDetails !=null && fileDetails.getSize()>0) {
log.debug("UPLOAD: file size set is {} Byte",fileDetails.getSize());
info = storageBackend.upload(is1, relPath, name, fileDetails.getSize());
info = storageBackend.upload(is1, relPath, name, fileDetails.getSize(), login);
} else
info = storageBackend.upload(is1, relPath, name);
info = storageBackend.upload(is1, relPath, name, login);
log.debug("UPLOAD: upload on storage - stop");
}catch (Throwable e) {
log.error("error writing content",e );

View File

@ -22,7 +22,7 @@ public class StorageOperationMediator {
@Inject
StorageBackendHandler storageBackendHandler;
public MetaInfo copy(Content source, PayloadBackend destination, String newName, String newParentPath) throws StorageHubException{
public MetaInfo copy(Content source, PayloadBackend destination, String newName, String newParentPath, String login) throws StorageHubException{
log.info("creating Storages for source {} and destination {}", source.getPayloadBackend(), destination.getStorageName());
@ -39,7 +39,7 @@ public class StorageOperationMediator {
}else {
log.info("source and destintiona are different storage");
InputStream stream = sourceSB.download(source);
MetaInfo info = destSB.upload(stream, newParentPath, newName, source.getSize());
MetaInfo info = destSB.upload(stream, newParentPath, newName, source.getSize(), login);
return info;
}
}

View File

@ -54,6 +54,7 @@ import org.gcube.common.storagehub.model.service.VersionList;
import org.gcube.common.storagehub.model.storages.MetaInfo;
import org.gcube.common.storagehub.model.types.ItemAction;
import org.gcube.common.storagehub.model.types.NodeProperty;
import org.gcube.contentmanagement.blobstorage.transport.backend.util.GetPayloadMap;
import org.gcube.data.access.storagehub.AuthorizationChecker;
import org.gcube.data.access.storagehub.Constants;
import org.gcube.data.access.storagehub.PathUtil;
@ -506,7 +507,7 @@ public class ItemsManager extends Impersonable{
ses.save();
}catch(RepositoryException re ){
log.error("jcr error getting rootSharedFolder", re);
log.error("jcr error publishing folder", re);
GXOutboundErrorResponse.throwException(new BackendGenericError(re));
}catch(StorageHubException she ){
log.error(she.getErrorMessage(), she);
@ -830,7 +831,7 @@ public class ItemsManager extends Impersonable{
Content contentToCopy = ((AbstractFileItem) item).getContent();
MetaInfo contentInfo = opMediator.copy(contentToCopy, destinationItem.getBackend(), destination.getPath(), uniqueName);
MetaInfo contentInfo = opMediator.copy(contentToCopy, destinationItem.getBackend(), destination.getPath(), uniqueName, currentUser);
Utils.setContentFromMetaInfo((AbstractFileItem) item, contentInfo);
item2Node.replaceContent(newNode, (AbstractFileItem) item, ItemAction.CLONED);
@ -876,14 +877,16 @@ public class ItemsManager extends Impersonable{
ses = repository.getRepository().login(CredentialHandler.getAdminCredentials(context));
authChecker.checkMoveOpsForProtectedFolders(ses, id);
authChecker.checkWriteAuthorizationControl(ses, currentUser, id, false);
final Node nodeToMove = ses.getNodeByIdentifier(id);
final Item item = node2Item.getItem(nodeToMove, null);
if (item instanceof SharedFolder && ((SharedFolder) item).isVreFolder() )
throw new InvalidItemException("vre folder");
if (item instanceof SharedFolder)
if (getSharedParentNode(nodeToMove).getIdentifier() == item.getId())
throw new InvalidItemException("root shared folder name cannot be modfied");
String uniqueName = Utils.checkExistanceAndGetUniqueName(ses, nodeToMove.getParent(), newName);
try {
@ -893,22 +896,12 @@ public class ItemsManager extends Impersonable{
throw new ItemLockedException(e);
}
try {
if (item instanceof SharedFolder) {
authChecker.checkAdministratorControl(ses, currentUser, (SharedFolder)item);
nodeToMove.setProperty(NodeProperty.TITLE.toString(), uniqueName);
Utils.setPropertyOnChangeNode(nodeToMove, currentUser, ItemAction.RENAMED);
accountingHandler.createRename(item.getTitle(), uniqueName, nodeToMove, currentUser, ses, false);
} else {
authChecker.checkWriteAuthorizationControl(ses, currentUser, id, false);
String newPath = String.format("%s/%s", nodeToMove.getParent().getPath(), uniqueName);
nodeToMove.setProperty(NodeProperty.TITLE.toString(), uniqueName);
Utils.setPropertyOnChangeNode(nodeToMove, currentUser, ItemAction.RENAMED);
accountingHandler.createRename(item.getTitle(), uniqueName, ses.getNode(newPath), currentUser, ses, false);
ses.move(nodeToMove.getPath(), newPath);
}
String newPath = String.format("%s/%s", nodeToMove.getParent().getPath(), uniqueName);
nodeToMove.setProperty(NodeProperty.TITLE.toString(), uniqueName);
Utils.setPropertyOnChangeNode(nodeToMove, currentUser, ItemAction.RENAMED);
ses.move(nodeToMove.getPath(), newPath);
accountingHandler.createRename(item.getTitle(), uniqueName, ses.getNode(newPath), currentUser, ses, false);
ses.save();
}finally {
ses.getWorkspace().getLockManager().unlock(nodeToMove.getPath());
ses.getWorkspace().getLockManager().unlock(nodeToMove.getParent().getPath());

View File

@ -180,7 +180,7 @@ public class WorkspaceManager extends Impersonable{
StorageBackend sb = sbf.create(payloadBackend);
log.info("UPLOAD: call started with file size {}",size);
MetaInfo info = sb.upload(stream, null, fileDetail.getFileName());
MetaInfo info = sb.upload(stream, null, fileDetail.getFileName(), currentUser);
log.debug("UPLOAD: call finished");
toReturn = publicLinkHandler.getForVolatile(info.getStorageId(), GCubeVolatileStorageBackendFactory.NAME, context);

View File

@ -67,9 +67,9 @@ public class GCubeMongoStorageBackend extends StorageBackend {
}
@Override
public MetaInfo upload(InputStream stream, String relPath, String name) {
public MetaInfo upload(InputStream stream, String relPath, String name, String user) {
log.debug("uploading file");
IClient storageClient = getStorageClient(SecretManagerProvider.instance.get().getOwner().getId()).getClient();
IClient storageClient = getStorageClient(user).getClient();
String uid = UUID.randomUUID().toString();
String remotePath= String.format("%s/%s-%s",relPath,uid,name);
String storageId =storageClient.put(true).LFile(stream).RFile(remotePath);
@ -80,13 +80,13 @@ public class GCubeMongoStorageBackend extends StorageBackend {
@Override
public MetaInfo upload(InputStream stream, String relPath, String name, Long size) {
return this.upload(stream, relPath, name);
public MetaInfo upload(InputStream stream, String relPath, String name, Long size, String user) {
return this.upload(stream, relPath, name, user);
}
@Override
public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size) {
public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size, String user) {
throw new UnsupportedOperationException();
}

View File

@ -101,24 +101,24 @@ public class S3Backend extends StorageBackend{
@Override
public MetaInfo upload(InputStream stream, String relativePath, String name) {
return this.upload(stream, relativePath, name, null);
public MetaInfo upload(InputStream stream, String relativePath, String name, String user) {
return this.upload(stream, relativePath, name, null, user);
}
@Override
public MetaInfo upload(InputStream stream, String relativePath, String name, Long size) {
public MetaInfo upload(InputStream stream, String relativePath, String name, Long size, String user) {
String storageId = keyGenerator.apply(null);
return upload(stream, relativePath, name, storageId, size);
return upload(stream, relativePath, name, storageId, size, user);
}
@Override
public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size) {
public MetaInfo upload(InputStream stream, String relativePath, String name, String storageId, Long size, String user) {
try {
Map<String, String> headers = new HashMap<>();
headers.put("X-Amz-Storage-Class", "REDUCED_REDUNDANCY");
Map<String, String> userMetadata = new HashMap<>();
userMetadata.put("user", SecretManagerProvider.instance.get().getOwner().getId());
userMetadata.put("user", user);
userMetadata.put("title", name);
client.putObject(