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