Improved lib
This commit is contained in:
parent
aa25166427
commit
a19f99d241
|
@ -53,14 +53,8 @@ public class StorageHubManagement {
|
||||||
ListResolverTyped listResolverTyped = folder.list();
|
ListResolverTyped listResolverTyped = folder.list();
|
||||||
List<ItemContainer<? extends Item>> containers = listResolverTyped.includeHidden().getContainers();
|
List<ItemContainer<? extends Item>> containers = listResolverTyped.includeHidden().getContainers();
|
||||||
for(ItemContainer<? extends Item> itemContainer : containers) {
|
for(ItemContainer<? extends Item> itemContainer : containers) {
|
||||||
Item item = itemContainer.get();
|
|
||||||
String name = item.getName();
|
|
||||||
ContainerType containerType = itemContainer.getType();
|
ContainerType containerType = itemContainer.getType();
|
||||||
StringWriter indent = new StringWriter(level + 1);
|
logItem(itemContainer, level);
|
||||||
for(int i = 0; i < level + 1; i++) {
|
|
||||||
indent.append('-');
|
|
||||||
}
|
|
||||||
logger.debug("{} {} {} (ID:{}) {}", indent.toString(), containerType, name, itemContainer.getId(), item.isHidden() ? " (hidden)" : "");
|
|
||||||
switch(containerType) {
|
switch(containerType) {
|
||||||
case FOLDER:
|
case FOLDER:
|
||||||
FolderContainer folderContainer = (FolderContainer) itemContainer;
|
FolderContainer folderContainer = (FolderContainer) itemContainer;
|
||||||
|
@ -161,9 +155,23 @@ public class StorageHubManagement {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected void logItem(ItemContainer<? extends Item> itemContainer) {
|
||||||
|
logItem(itemContainer, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void logItem(ItemContainer<? extends Item> itemContainer, int level) {
|
||||||
|
StringWriter indent = new StringWriter(level + 1);
|
||||||
|
for(int i = 0; i < level+1; i++) {
|
||||||
|
indent.append('-');
|
||||||
|
}
|
||||||
|
indent.append(" ");
|
||||||
|
Item item = itemContainer.get();
|
||||||
|
logger.debug("{}{} {} (ID:{}){}", indent.toString(), itemContainer.getType(), item.getName(), itemContainer.getId(), item.isHidden() ? " (hidden)" : "");
|
||||||
|
}
|
||||||
|
|
||||||
protected void tree(FolderContainer folderContainer) throws Exception {
|
protected void tree(FolderContainer folderContainer) throws Exception {
|
||||||
logger.debug("{} (ID:{})", folderContainer.get().getName(), folderContainer.getId());
|
logItem(folderContainer,0);
|
||||||
recursiveList(folderContainer, 0);
|
recursiveList(folderContainer, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public URL persistFile(InputStream inputStream, String fileName, String mimeType, Metadata metadata)
|
public URL persistFile(InputStream inputStream, String fileName, String mimeType, Metadata metadata)
|
||||||
|
|
|
@ -7,13 +7,13 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.gcube.common.authorization.client.Constants;
|
import org.gcube.common.authorization.utils.manager.SecretManager;
|
||||||
import org.gcube.common.authorization.client.exceptions.ObjectNotFound;
|
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
||||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
import org.gcube.common.authorization.utils.secret.JWTSecret;
|
||||||
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
import org.gcube.common.authorization.utils.secret.Secret;
|
||||||
import org.gcube.common.authorization.library.provider.ClientInfo;
|
import org.gcube.common.authorization.utils.secret.SecretUtility;
|
||||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
import org.gcube.common.keycloak.KeycloakClientFactory;
|
||||||
import org.gcube.common.authorization.library.utils.Caller;
|
import org.gcube.common.keycloak.model.TokenResponse;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.junit.AfterClass;
|
import org.junit.AfterClass;
|
||||||
import org.junit.BeforeClass;
|
import org.junit.BeforeClass;
|
||||||
|
@ -22,64 +22,97 @@ import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ContextTest {
|
public class ContextTest {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
private static final Logger logger = LoggerFactory.getLogger(ContextTest.class);
|
||||||
|
|
||||||
protected static Properties properties;
|
protected static final String CONFIG_INI_FILENAME = "config.ini";
|
||||||
protected static final String PROPERTIES_FILENAME = "token.properties";
|
|
||||||
|
|
||||||
public static final String DEFAULT_TEST_SCOPE_NAME;
|
public static final String GCUBE;
|
||||||
|
public static final String DEVNEXT;
|
||||||
|
public static final String NEXTNEXT;
|
||||||
|
public static final String DEVSEC;
|
||||||
|
public static final String DEVVRE;
|
||||||
|
|
||||||
|
public static final String ROOT;
|
||||||
|
|
||||||
|
protected static final Properties properties;
|
||||||
|
|
||||||
|
protected static final String CLIENT_ID_PROPERTY_KEY = "client_id";
|
||||||
|
protected static final String clientID;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
properties = new Properties();
|
GCUBE = "/gcube";
|
||||||
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(PROPERTIES_FILENAME);
|
DEVNEXT = GCUBE + "/devNext";
|
||||||
|
NEXTNEXT = DEVNEXT + "/NextNext";
|
||||||
|
DEVSEC = GCUBE + "/devsec";
|
||||||
|
DEVVRE = DEVSEC + "/devVRE";
|
||||||
|
|
||||||
|
ROOT = GCUBE;
|
||||||
|
|
||||||
|
properties = new Properties();
|
||||||
|
InputStream input = ContextTest.class.getClassLoader().getResourceAsStream(CONFIG_INI_FILENAME);
|
||||||
try {
|
try {
|
||||||
// load the properties file
|
// load the properties file
|
||||||
properties.load(input);
|
properties.load(input);
|
||||||
} catch(IOException e) {
|
|
||||||
|
clientID = properties.getProperty(CLIENT_ID_PROPERTY_KEY);
|
||||||
|
|
||||||
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
//DEFAULT_TEST_SCOPE_NAME = "/pred4s/preprod/preVRE";
|
|
||||||
DEFAULT_TEST_SCOPE_NAME = "/gcube/devNext/NextNext";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getCurrentScope(String token) throws ObjectNotFound, Exception {
|
public static void set(Secret secret) throws Exception {
|
||||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
SecretManagerProvider.instance.reset();
|
||||||
String context = authorizationEntry.getContext();
|
SecretManager secretManager = new SecretManager();
|
||||||
logger.info("Context of token {} is {}", token, context);
|
secretManager.addSecret(secret);
|
||||||
return context;
|
SecretManagerProvider.instance.set(secretManager);
|
||||||
|
SecretManagerProvider.instance.get().set();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setContextByName(String fullContextName) throws ObjectNotFound, Exception {
|
public static void setContextByName(String fullContextName) throws Exception {
|
||||||
String token = ContextTest.properties.getProperty(fullContextName);
|
Secret secret = getSecretByContextName(fullContextName);
|
||||||
setContext(token);
|
set(secret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void setContext(String token) throws ObjectNotFound, Exception {
|
|
||||||
SecurityTokenProvider.instance.set(token);
|
private static TokenResponse getJWTAccessToken(String context) throws Exception {
|
||||||
AuthorizationEntry authorizationEntry = Constants.authorizationService().get(token);
|
ScopeProvider.instance.set(context);
|
||||||
ClientInfo clientInfo = authorizationEntry.getClientInfo();
|
int index = context.indexOf('/', 1);
|
||||||
logger.debug("User : {} - Type : {}", clientInfo.getId(), clientInfo.getType().name());
|
String root = context.substring(0, index == -1 ? context.length() : index);
|
||||||
String qualifier = authorizationEntry.getQualifier();
|
String clientSecret = properties.getProperty(root);
|
||||||
Caller caller = new Caller(clientInfo, qualifier);
|
TokenResponse tr = KeycloakClientFactory.newInstance().queryUMAToken(clientID, clientSecret, context, null);
|
||||||
AuthorizationProvider.instance.set(caller);
|
return tr;
|
||||||
ScopeProvider.instance.set(getCurrentScope(token));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Secret getSecretByContextName(String context) throws Exception {
|
||||||
|
TokenResponse tr = getJWTAccessToken(context);
|
||||||
|
Secret secret = new JWTSecret(tr.getAccessToken());
|
||||||
|
return secret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setContext(String token) throws Exception {
|
||||||
|
Secret secret = getSecret(token);
|
||||||
|
set(secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Secret getSecret(String token) throws Exception {
|
||||||
|
Secret secret = SecretUtility.getSecretByTokenString(token);
|
||||||
|
return secret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@BeforeClass
|
@BeforeClass
|
||||||
public static void beforeClass() throws Exception {
|
public static void beforeClass() throws Exception {
|
||||||
setContextByName(DEFAULT_TEST_SCOPE_NAME);
|
setContextByName(GCUBE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
public static void afterClass() throws Exception {
|
public static void afterClass() throws Exception {
|
||||||
SecurityTokenProvider.instance.reset();
|
SecretManagerProvider.instance.reset();
|
||||||
ScopeProvider.instance.reset();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
package org.gcube.storagehub;
|
package org.gcube.storagehub;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.gcube.common.authorization.utils.manager.SecretManager;
|
|
||||||
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
|
||||||
import org.gcube.common.authorization.utils.secret.GCubeSecret;
|
|
||||||
import org.gcube.common.storagehub.client.dsl.ContainerType;
|
import org.gcube.common.storagehub.client.dsl.ContainerType;
|
||||||
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
||||||
import org.gcube.common.storagehub.client.dsl.FolderContainer;
|
import org.gcube.common.storagehub.client.dsl.FolderContainer;
|
||||||
|
@ -16,6 +14,7 @@ import org.gcube.common.storagehub.client.dsl.OpenResolver;
|
||||||
import org.gcube.common.storagehub.model.Metadata;
|
import org.gcube.common.storagehub.model.Metadata;
|
||||||
import org.gcube.common.storagehub.model.items.Item;
|
import org.gcube.common.storagehub.model.items.Item;
|
||||||
import org.gcube.common.storagehub.model.service.Version;
|
import org.gcube.common.storagehub.model.service.Version;
|
||||||
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -26,69 +25,77 @@ public class StorageHubManagementTest extends ContextTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void myTest() throws Exception {
|
public void myTest() throws Exception {
|
||||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
|
||||||
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
|
|
||||||
secretManager.startSession(gCubeSecret);
|
|
||||||
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
||||||
FolderContainer contextFolder = storageHubManagement.getContextFolder();
|
FolderContainer contextFolder = storageHubManagement.getContextFolder();
|
||||||
logger.debug("Context Folder ID : {} - Name : {}", contextFolder.getId(), contextFolder.get().getName());
|
logger.debug("Context Folder ID : {} - Name : {}", contextFolder.getId(), contextFolder.get().getName());
|
||||||
secretManager.endSession();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() throws Exception {
|
public void test() throws Exception {
|
||||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
|
||||||
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
|
|
||||||
secretManager.startSession(gCubeSecret);
|
|
||||||
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings("unused")
|
||||||
OpenResolver openResolver = storageHubManagement.storageHubClient.open("");
|
OpenResolver openResolver = storageHubManagement.storageHubClient.open("");
|
||||||
// openResolver.asItem().delete();
|
|
||||||
/*
|
/*
|
||||||
openResolver = storageHubManagement.storageHubClient.open("656cd713-bd79-4659-abd6-9f1baaedb5bc");
|
|
||||||
openResolver.asItem().delete();
|
openResolver.asItem().delete();
|
||||||
openResolver = storageHubManagement.storageHubClient.open("bd44d81e-0e2f-4527-b634-2e26e8908f36");
|
openResolver = storageHubManagement.storageHubClient.open("");
|
||||||
|
openResolver.asItem().delete();
|
||||||
|
openResolver = storageHubManagement.storageHubClient.open("");
|
||||||
|
openResolver.asItem().delete();
|
||||||
|
openResolver = storageHubManagement.storageHubClient.open("");
|
||||||
openResolver.asItem().delete();
|
openResolver.asItem().delete();
|
||||||
*/
|
*/
|
||||||
secretManager.endSession();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Ignore
|
||||||
@Test
|
@Test
|
||||||
public void listFolders() throws Exception {
|
public void listFolders() throws Exception {
|
||||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
List<String> contexts = new ArrayList<>();
|
||||||
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
|
contexts.add(GCUBE);
|
||||||
secretManager.startSession(gCubeSecret);
|
// contexts.add(DEVSEC);
|
||||||
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
// contexts.add(DEVVRE);
|
||||||
@SuppressWarnings("unused")
|
// contexts.add(DEVNEXT);
|
||||||
FolderContainer root = storageHubManagement.getWorkspaceRoot();
|
// contexts.add(NEXTNEXT);
|
||||||
FolderContainer contextFolder = storageHubManagement.getContextFolder();
|
|
||||||
ListResolverTyped listResolverTyped = contextFolder.list();
|
for(String context : contexts) {
|
||||||
List<ItemContainer<? extends Item>> containers = listResolverTyped.includeHidden().getContainers();
|
ContextTest.setContextByName(context);
|
||||||
for(ItemContainer<? extends Item> itemContainer : containers) {
|
|
||||||
Item item = itemContainer.get();
|
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
||||||
String name = item.getName();
|
|
||||||
ContainerType containerType = itemContainer.getType();
|
// FolderContainer root = storageHubManagement.getWorkspaceRoot();
|
||||||
if(containerType==ContainerType.FOLDER && name.compareTo(".catalogue")==0) {
|
// storageHubManagement.tree(root);
|
||||||
itemContainer.delete();
|
|
||||||
|
FolderContainer contextFolder = storageHubManagement.getContextFolder();
|
||||||
|
storageHubManagement.tree(contextFolder);
|
||||||
|
|
||||||
|
ListResolverTyped listResolverTyped = contextFolder.list();
|
||||||
|
List<ItemContainer<? extends Item>> containers = listResolverTyped.includeHidden().getContainers();
|
||||||
|
for(ItemContainer<? extends Item> itemContainer : containers) {
|
||||||
|
Item item = itemContainer.get();
|
||||||
|
String name = item.getName();
|
||||||
|
ContainerType containerType = itemContainer.getType();
|
||||||
|
if(containerType==ContainerType.FOLDER) {
|
||||||
|
if(name.compareTo("gCat")==0 || name.compareTo(".catalogue")==0 || name.compareTo("service-account-gcat")==0) {
|
||||||
|
logger.info("Catalogue folder found");
|
||||||
|
storageHubManagement.tree((FolderContainer) itemContainer);
|
||||||
|
/*
|
||||||
|
* // itemContainer.delete();
|
||||||
|
*/
|
||||||
|
}else {
|
||||||
|
storageHubManagement.logItem(itemContainer);
|
||||||
|
// storageHubManagement.tree((FolderContainer) itemContainer);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// FolderContainer dstFolder = storageHubManagement.getDestinationFolder("application/pdf");
|
|
||||||
// storageHubManagement.tree(root);
|
|
||||||
// storageHubManagement.tree(contextFolder);
|
|
||||||
// storageHubManagement.tree(dstFolder);
|
|
||||||
secretManager.endSession();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getFileInfo() throws Exception {
|
public void getFileInfo() throws Exception {
|
||||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
|
||||||
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
|
|
||||||
secretManager.startSession(gCubeSecret);
|
|
||||||
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
||||||
String id = "3daf465b-b84e-4d1c-9786-a388a267382c";
|
String id = "";
|
||||||
OpenResolver openResolver = storageHubManagement.storageHubClient.open(id);
|
OpenResolver openResolver = storageHubManagement.storageHubClient.open(id);
|
||||||
FileContainer fileContainer = (FileContainer) openResolver.asFile();
|
FileContainer fileContainer = (FileContainer) openResolver.asFile();
|
||||||
logger.debug("StorageHub ID {} - File Name {}", id, fileContainer.get().getName());
|
storageHubManagement.logItem(fileContainer);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
ListResolver listResolver = fileContainer.getAnchestors();
|
ListResolver listResolver = fileContainer.getAnchestors();
|
||||||
|
@ -106,27 +113,21 @@ public class StorageHubManagementTest extends ContextTest {
|
||||||
for(Version version : versions){
|
for(Version version : versions){
|
||||||
logger.debug("Version {} {}", version.getId(), version.getName());
|
logger.debug("Version {} {}", version.getId(), version.getName());
|
||||||
}
|
}
|
||||||
secretManager.endSession();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getFileInfoViaDirectoryListing() throws Exception {
|
public void getFileInfoViaDirectoryListing() throws Exception {
|
||||||
SecretManager secretManager = SecretManagerProvider.instance.get();
|
|
||||||
GCubeSecret gCubeSecret = new GCubeSecret(ContextTest.properties.getProperty(DEFAULT_TEST_SCOPE_NAME));
|
|
||||||
secretManager.startSession(gCubeSecret);
|
|
||||||
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
StorageHubManagement storageHubManagement = new StorageHubManagement();
|
||||||
String id = "22bd9034-1da0-45ac-868f-91d1e5438344";
|
String id = "";
|
||||||
OpenResolver openResolver = storageHubManagement.storageHubClient.open(id);
|
OpenResolver openResolver = storageHubManagement.storageHubClient.open(id);
|
||||||
FolderContainer folderContainer = (FolderContainer) openResolver.asFolder();
|
FolderContainer folderContainer = (FolderContainer) openResolver.asFolder();
|
||||||
String filename = "56a55300-9272-4c12-9ac8-ff61411a5976";
|
String filename = "";
|
||||||
ListResolver listResolver = folderContainer.findByName(filename);
|
ListResolver listResolver = folderContainer.findByName(filename);
|
||||||
List<ItemContainer<? extends Item>> itemContainers = listResolver.withMetadata().getContainers();
|
List<ItemContainer<? extends Item>> itemContainers = listResolver.withMetadata().getContainers();
|
||||||
for(ItemContainer<? extends Item> itemContainer : itemContainers) {
|
for(ItemContainer<? extends Item> itemContainer : itemContainers) {
|
||||||
|
|
||||||
FileContainer fileContainer = (FileContainer) itemContainer;
|
FileContainer fileContainer = (FileContainer) itemContainer;
|
||||||
|
storageHubManagement.logItem(fileContainer);
|
||||||
|
|
||||||
logger.debug("StorageHub ID {} - File Name {}", id, fileContainer.get().getName());
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
ListResolver listResolver = fileContainer.getAnchestors();
|
ListResolver listResolver = fileContainer.getAnchestors();
|
||||||
|
@ -145,7 +146,7 @@ public class StorageHubManagementTest extends ContextTest {
|
||||||
logger.debug("Version {} {}", version.getId(), version.getName());
|
logger.debug("Version {} {}", version.getId(), version.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
secretManager.endSession();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
/*.gcubekey
|
/*.gcubekey
|
||||||
/*.properties
|
/*.properties
|
||||||
|
/config.ini
|
||||||
|
|
Loading…
Reference in New Issue