task_28150 #19

Merged
francesco.mangiacrapa merged 14 commits from task_28150 into master 2024-10-04 15:07:01 +02:00
9 changed files with 172 additions and 166 deletions
Showing only changes of commit d4189e0c98 - Show all commits

View File

@ -22,54 +22,52 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public class WorkspaceManager {
private static final String APP_FOLDER=".GNA_RECORDS";
private static final String APP_FOLDER = ".GNA_RECORDS";
private StorageHubClient sgClient=null;
private StorageHubClient sgClient = null;
@Getter
private FolderContainer appBase=null;
private FolderContainer appBase = null;
@Getter
@Setter
@AllArgsConstructor
@RequiredArgsConstructor
public static class FolderOptions{
public static class FolderOptions {
@NonNull
private String folderName;
private String folderDescription;
private FolderContainer parent;
}
@Getter
@Setter
@AllArgsConstructor
@RequiredArgsConstructor
public static class FileOptions{
public static class FileOptions {
@NonNull
private String fileName;
@NonNull
private InputStream is;
private String fileDescription;
private FolderContainer parent;
//Added by Francesco, see #28150
// Added by Francesco, see #28150
private Long size;
}
public Archive getConfiguration(){
public Archive getConfiguration() {
Archive toReturn = new Archive("W-STORAGE");
toReturn.put("folder_id",appBase.getId());
toReturn.put("folder_id", appBase.getId());
return toReturn;
}
public WorkspaceManager() throws ConfigurationException, StorageHubException {
sgClient= ImplementationProvider.get().getProvidedObjectByClass(StorageHubClient.class);
appBase=getApplicationBaseFolder(sgClient);
sgClient = ImplementationProvider.get().getProvidedObjectByClass(StorageHubClient.class);
appBase = getApplicationBaseFolder(sgClient);
}
public FolderContainer createFolder(FolderOptions opts) throws StorageHubException {
if(opts.getParent()==null)
if (opts.getParent() == null)
opts.setParent(appBase);
return createFolderRoutine(opts);
}
@ -86,11 +84,10 @@ public class WorkspaceManager {
sgClient.open(id).asFolder().delete();
}
public FolderContainer getSubFolder(FolderContainer parentFolder,String path) throws StorageHubException {
return getSubFolder(parentFolder,path,"");
public FolderContainer getSubFolder(FolderContainer parentFolder, String path) throws StorageHubException {
return getSubFolder(parentFolder, path, "");
}
/**
* Returns sub folder. Creates it if missing
*
@ -99,26 +96,26 @@ public class WorkspaceManager {
* @return
* @throws StorageHubException
*/
public FolderContainer getSubFolder(FolderContainer parentFolder,String path, String description) throws StorageHubException {
try{
public FolderContainer getSubFolder(FolderContainer parentFolder, String path, String description)
throws StorageHubException {
try {
return parentFolder.openByRelativePath(path).asFolder();
}catch(StorageHubException e) {
log.debug("Missing subPath "+path);
FolderContainer targetParent=parentFolder;
String targetName=path;
if(path.contains("/")) {
String parent=path.substring(0, path.lastIndexOf("/"));
log.debug("Checking intermediate "+parent);
targetParent=getSubFolder(parentFolder,parent);
targetName=path.substring(path.lastIndexOf("/")+1);
} catch (StorageHubException e) {
log.debug("Missing subPath " + path);
FolderContainer targetParent = parentFolder;
String targetName = path;
if (path.contains("/")) {
String parent = path.substring(0, path.lastIndexOf("/"));
log.debug("Checking intermediate " + parent);
targetParent = getSubFolder(parentFolder, parent);
targetName = path.substring(path.lastIndexOf("/") + 1);
}
FolderOptions opts = new FolderOptions(targetName,description,targetParent);
FolderOptions opts = new FolderOptions(targetName, description, targetParent);
log.debug("Creating FOLDER {}", opts);
return createFolder(opts);
}
}
// public WorkspaceContent storeToWS(FileOptions opts) throws FileNotFoundException, StorageHubException {
// FileContainer item=createFileRoutine(opts);
// item=sgClient.open(item.getId()).asFile();
@ -133,12 +130,11 @@ public class WorkspaceManager {
// }
public RegisteredFile registerFile(FileOptions opts) throws StorageHubException {
FileContainer item=createFileRoutine(opts);
item=sgClient.open(item.getId()).asFile();
FileContainer item = createFileRoutine(opts);
item = sgClient.open(item.getId()).asFile();
RegisteredFile file = new RegisteredFile();
RegisteredFile file=new RegisteredFile();
file.setLink(item.getPublicLink().toString());
file.setMimetype(item.get().getContent().getMimeType());
file.setStorageID(item.getId());
@ -146,26 +142,25 @@ public class WorkspaceManager {
return file;
}
// public void deleteFromWS(WorkspaceContent toDelete) throws StorageHubException {
// sgClient.open(toDelete.getStorageID()).asFile().forceDelete();
// }
public void deleteItem(String itemId)throws StorageHubException{
public void deleteItem(String itemId) throws StorageHubException {
sgClient.open(itemId).asItem().forceDelete();
}
// STATIC SYNCH METHODS
// STATIC SYNCH METHODS
@Synchronized
public static FolderContainer getApplicationBaseFolder(StorageHubClient sgClient) throws StorageHubException {
FolderContainer vre=sgClient.openVREFolder();
FolderContainer vre = sgClient.openVREFolder();
try {
return vre.openByRelativePath(APP_FOLDER).asFolder();
}catch(StorageHubException e) {
} catch (StorageHubException e) {
log.debug("APP Folder missing. Initializing..");
FolderContainer toReturn= vre.newFolder(APP_FOLDER, "Base folder for GNA records");
FolderContainer toReturn = vre.newFolder(APP_FOLDER, "Base folder for GNA records");
toReturn.setHidden();
return toReturn;
}
@ -174,14 +169,14 @@ public class WorkspaceManager {
@Synchronized
private static FolderContainer createFolderRoutine(FolderOptions opts) throws StorageHubException {
opts.setFolderName(Files.fixFilename(opts.getFolderName()));
return opts.getParent().newFolder(opts.getFolderName(),opts.getFolderDescription());
return opts.getParent().newFolder(opts.getFolderName(), opts.getFolderDescription());
}
@Synchronized
private static FileContainer createFileRoutine(FileOptions opts) throws StorageHubException {
// Updated by Francesco, see #28150
log.info("Uploading file name: {}, in the parent folder id: {}, filesize is: {}", opts.getFileName(),
opts.getParent().getId(), opts.getSize());
log.info("Uploading file name: {}, in the parent folder id: {}, filesize is: " + opts.getSize(),
opts.getFileName(), opts.getParent().getId());
opts.setFileName(Files.fixFilename(opts.getFileName()));
if (opts.getSize() == null)
return opts.getParent().uploadFile(opts.getIs(), opts.getFileName(), opts.getFileDescription());

View File

@ -11,9 +11,9 @@ import org.gcube.application.geoportal.common.model.document.access.Access;
import org.gcube.application.geoportal.common.model.document.relationships.RelationshipNavigationObject;
import org.gcube.application.geoportal.common.model.rest.CreateRelationshipRequest;
import org.gcube.application.geoportal.common.model.rest.DeleteRelationshipRequest;
import org.gcube.application.geoportal.common.model.rest.PerformStepRequest;
import org.gcube.application.geoportal.common.model.rest.QueryRequest;
import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.model.rest.PerformStepRequest;
public interface Projects<P extends Project> {

View File

@ -13,111 +13,121 @@ import org.gcube.application.geoportal.common.model.rest.RegisterFileSetRequest;
import org.gcube.application.geoportal.common.model.rest.TempFile;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FileSets {
public static class RequestBuilder {
RegisterFileSetRequest theRequest=new RegisterFileSetRequest();
public static class RequestBuilder {
RegisterFileSetRequest theRequest = new RegisterFileSetRequest();
public RequestBuilder addAll(Collection<? extends TempFile> toAdd){
if(theRequest.getStreams()==null)
theRequest.setStreams(new ArrayList<TempFile>());
theRequest.getStreams().addAll(toAdd);
return this;
}
public RequestBuilder add(TempFile... f){
if(theRequest.getStreams()==null)
theRequest.setStreams(new ArrayList<TempFile>());
for(TempFile temp: f )
theRequest.getStreams().add(temp);
return this;
}
public RequestBuilder addAll(Collection<? extends TempFile> toAdd) {
if (theRequest.getStreams() == null)
theRequest.setStreams(new ArrayList<TempFile>());
theRequest.getStreams().addAll(toAdd);
return this;
}
public RequestBuilder add(TempFile f){
if(theRequest.getStreams()==null)
theRequest.setStreams(new ArrayList<TempFile>());
theRequest.getStreams().add(f);
return this;
}
public RequestBuilder add(TempFile... f) {
if (theRequest.getStreams() == null)
theRequest.setStreams(new ArrayList<TempFile>());
for (TempFile temp : f)
theRequest.getStreams().add(temp);
return this;
}
public RequestBuilder setFieldDefinitionPath(String path){
theRequest.setFieldDefinitionPath(path);
return this;
}
public RequestBuilder add(TempFile f) {
if (theRequest.getStreams() == null)
theRequest.setStreams(new ArrayList<TempFile>());
theRequest.getStreams().add(f);
return this;
}
public RequestBuilder setParentPath(String path){
theRequest.setParentPath(path);
return this;
}
public RequestBuilder setFieldDefinitionPath(String path) {
theRequest.setFieldDefinitionPath(path);
return this;
}
public RequestBuilder setFieldName(String fieldName){
theRequest.setFieldName(fieldName);
return this;
}
public RequestBuilder setParentPath(String path) {
theRequest.setParentPath(path);
return this;
}
public RequestBuilder setClashPolicy(RegisterFileSetRequest.ClashOptions path){
theRequest.setClashOption(path);
return this;
}
public RequestBuilder setFieldName(String fieldName) {
theRequest.setFieldName(fieldName);
return this;
}
public RequestBuilder setAttributes(Document attributes){
theRequest.setAttributes(attributes);
return this;
}
public RequestBuilder setClashPolicy(RegisterFileSetRequest.ClashOptions path) {
theRequest.setClashOption(path);
return this;
}
public RegisterFileSetRequest getTheRequest(){
if(theRequest.getClashOption()==null)
// default Clash Policy
setClashPolicy(RegisterFileSetRequest.ClashOptions.REPLACE_EXISTING);
return theRequest;}
public RequestBuilder setAttributes(Document attributes) {
theRequest.setAttributes(attributes);
return this;
}
public RequestBuilder setAccess(Access access){
theRequest.setToSetAccess(access);
return this;
}
public RegisterFileSetRequest getTheRequest() {
if (theRequest.getClashOption() == null)
// default Clash Policy
setClashPolicy(RegisterFileSetRequest.ClashOptions.REPLACE_EXISTING);
return theRequest;
}
}
public RequestBuilder setAccess(Access access) {
theRequest.setToSetAccess(access);
return this;
}
}
public static RequestBuilder build(String parent,String fieldName, String fieldDefinition) {
return new RequestBuilder().setParentPath(parent).setFieldDefinitionPath(fieldDefinition).setFieldName(fieldName);
}
public static RequestBuilder build(String parent, String fieldName, String fieldDefinition) {
return new RequestBuilder().setParentPath(parent).setFieldDefinitionPath(fieldDefinition)
.setFieldName(fieldName);
}
public static RequestBuilder build(String parent,String fieldName, String fieldDefinition, TempFile...files) {
return build(parent,fieldName,fieldDefinition).add(files);
}
public static RequestBuilder build(String parent, String fieldName, String fieldDefinition, TempFile... files) {
return build(parent, fieldName, fieldDefinition).add(files);
}
public static TempFile asTemp(StorageUtils storage,InputStreamDescriptor descriptor) throws RemoteBackendException, FileNotFoundException {
return storage.putOntoStorage(descriptor.getStream(), descriptor.getFilename());
}
public static TempFile asTemp(StorageUtils storage, InputStreamDescriptor descriptor)
throws RemoteBackendException, FileNotFoundException {
return storage.putOntoStorage(descriptor.getStream(), descriptor.getFilename());
}
public static TempFile[] asTemp(StorageUtils storage,InputStreamDescriptor... descriptors) throws RemoteBackendException, FileNotFoundException {
ArrayList<TempFile> toReturn=new ArrayList<TempFile>();
for(InputStreamDescriptor desc:descriptors)
toReturn.add(storage.putOntoStorage(desc.getStream(), desc.getFilename()));
return toReturn.toArray(new TempFile[toReturn.size()]);
}
public static TempFile[] asTemp(StorageUtils storage, InputStreamDescriptor... descriptors)
throws RemoteBackendException, FileNotFoundException {
ArrayList<TempFile> toReturn = new ArrayList<TempFile>();
for (InputStreamDescriptor desc : descriptors)
toReturn.add(storage.putOntoStorage(desc.getStream(), desc.getFilename()));
return toReturn.toArray(new TempFile[toReturn.size()]);
}
public static RegisterFileSetRequest prepareRequestFromFolder(StorageUtils storage,
String parentPath,String fieldName,String fieldDefinition, File directory) throws FileNotFoundException {
public static RegisterFileSetRequest prepareRequestFromFolder(StorageUtils storage, String parentPath,
String fieldName, String fieldDefinition, File directory) throws FileNotFoundException {
File[] children =directory.listFiles();
InputStreamDescriptor[] iss=new InputStreamDescriptor[children.length];
return prepareRequest(storage,parentPath,fieldName,fieldDefinition,children);
File[] children = directory.listFiles();
InputStreamDescriptor[] iss = new InputStreamDescriptor[children.length];
return prepareRequest(storage, parentPath, fieldName, fieldDefinition, children);
}
}
public static RegisterFileSetRequest prepareRequest(StorageUtils storage,
String parentPath,String fieldName,String fieldDefinition, File... toUpload) throws FileNotFoundException {
public static RegisterFileSetRequest prepareRequest(StorageUtils storage, String parentPath, String fieldName,
String fieldDefinition, File... toUpload) throws FileNotFoundException {
FileSets.RequestBuilder builder = FileSets.build(parentPath,fieldName,fieldDefinition);
for (File f : toUpload) {
if(!f.isDirectory()) {
TempFile file = FileSets.asTemp(storage, new InputStreamDescriptor(new FileInputStream(f), f.getName()));
//Added by Francesco, see #28150
file.setSize(f.length());
builder.add(file);
}
}
return builder.getTheRequest();
}
FileSets.RequestBuilder builder = FileSets.build(parentPath, fieldName, fieldDefinition);
for (File f : toUpload) {
if (!f.isDirectory()) {
long fileSize = f.length();
TempFile file = FileSets.asTemp(storage,
new InputStreamDescriptor(new FileInputStream(f), f.getName()));
// Added by Francesco, see #28150
log.info("PrepareRequest for tempfile name " + file.getFilename() + " size: " + file.getSize());
file.setSize(fileSize);
builder.add(file);
}
}
return builder.getTheRequest();
}
}

View File

@ -1,8 +1,6 @@
package org.gcube.application.geoportal.common;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.common.utils.Files;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
@ -11,7 +9,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.Map;
import static org.junit.Assert.assertTrue;
import org.gcube.application.geoportal.common.utils.Files;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FilesTests {

View File

@ -1,5 +1,8 @@
package org.gcube.application.geoportal.common;
import java.util.EnumSet;
import java.util.Set;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
@ -7,9 +10,6 @@ import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
import java.util.EnumSet;
import java.util.Set;
public class JacksonProvider implements JSONSerializationProvider {

View File

@ -1,16 +1,6 @@
package org.gcube.application.geoportal.common;
import lombok.extern.slf4j.Slf4j;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.common.utils.ContextUtils;
import org.gcube.application.geoportal.common.utils.StorageUtils;
import org.gcube.application.geoportal.common.utils.tests.GCubeTest;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assume.assumeTrue;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@ -22,7 +12,16 @@ import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import static org.junit.Assume.assumeTrue;
import org.gcube.application.geoportal.common.rest.InterfaceConstants;
import org.gcube.application.geoportal.common.utils.ContextUtils;
import org.gcube.application.geoportal.common.utils.StorageUtils;
import org.gcube.application.geoportal.common.utils.tests.GCubeTest;
import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanager.storageclient.wrapper.AccessType;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
import org.gcube.contentmanager.storageclient.wrapper.StorageClient;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class StorageUtilsTest {

View File

@ -1,10 +1,11 @@
package org.gcube.application.geoportal.common;
import lombok.extern.slf4j.Slf4j;
import java.util.Properties;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TokenSetter {

View File

@ -1,13 +1,12 @@
package org.gcube.application.geoportal.common.legacy;
import static org.junit.Assert.assertEquals;
import org.gcube.application.geoportal.common.model.legacy.AccessPolicy;
import org.gcube.application.geoportal.common.model.legacy.Concessione;
import org.gcube.application.geoportal.common.model.legacy.RelazioneScavo;
import org.gcube.application.geoportal.common.model.legacy.report.ConstraintCheck;
import org.gcube.application.geoportal.common.model.legacy.report.ValidationReport;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DefaultsTests {

View File

@ -1,12 +1,8 @@
package org.gcube.application.geoportal.common.model;
import com.jayway.jsonpath.JsonPath;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet;
import org.gcube.application.geoportal.common.model.document.lifecycle.TriggeredEvents;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration;
import org.gcube.application.geoportal.common.utils.Files;
import org.junit.Test;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import java.io.File;
import java.io.IOException;
@ -15,7 +11,13 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import static junit.framework.TestCase.*;
import org.gcube.application.geoportal.common.model.document.Project;
import org.gcube.application.geoportal.common.model.document.filesets.RegisteredFileSet;
import org.gcube.application.geoportal.common.model.document.lifecycle.TriggeredEvents;
import org.gcube.application.geoportal.common.model.useCaseDescriptor.HandlerDeclaration;
import org.gcube.application.geoportal.common.utils.Files;
import com.jayway.jsonpath.JsonPath;
public class JSONPathTests {