storagehub-client-library/src/test/java/org/gcube/data/access/fs/Items.java

439 lines
13 KiB
Java

package org.gcube.data.access.fs;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.dsl.FileContainer;
import org.gcube.common.storagehub.client.dsl.FolderContainer;
import org.gcube.common.storagehub.client.dsl.ItemContainer;
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
import org.gcube.common.storagehub.client.proxies.UserManagerClient;
import org.gcube.common.storagehub.model.Metadata;
import org.gcube.common.storagehub.model.acls.AccessType;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.common.storagehub.model.items.Item;
import org.junit.BeforeClass;
import org.junit.Test;
public class Items {
@BeforeClass
public static void setUp(){
SecurityTokenProvider.instance.set("b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548");
ScopeProvider.instance.set("/gcube");
}
@Test
public void search() throws Exception{
StorageHubClient shc = new StorageHubClient();
/*List<? extends Item> s = shc.getWSRoot().search("WD%",false).getItems();
s.forEach(i -> System.out.println(i.getName()+" "+i.getId()));*/
Item item = shc.restoreThrashItem("b7ad6691-ae43-4b61-9538-0961b0b98c5d").get();
System.out.println("item id "+item.getId()+" path "+item.getPath()+" parent "+item.getParentId());
}
@Test
public void forceDelete() throws Exception{
StorageHubClient shc = new StorageHubClient();
//shc.open("41b904cd-128a-4121-8fd7-82498187ca06").asFolder().getAnchestors().getItems().forEach(i -> System.out.println(i.getTitle()));
shc.open("7ac99eea-d768-4864-a248-6d4ccf43d931").asFile().setDescription("new descr");
}
@Test
public void changeAcls() throws Exception{
StorageHubClient shc = new StorageHubClient();
System.out.println(shc.open("65e502ff-92ef-46cc-afbd-3e91f4b680be").asFolder().canWrite());
}
@Test
public void renameFile() throws Exception{
StorageHubClient shc = new StorageHubClient();
FolderContainer myRoot = shc.getWSRoot();
myRoot.findByName("");
FolderContainer children = myRoot.newFolder("build", "build folder");
FileContainer file = null;
try(InputStream is = new FileInputStream(new File("/home/lucio/Downloads/mg.jpg"))){
file = myRoot.uploadFile(is, "mg.jpg", "file");
} catch (Exception e) {
e.printStackTrace();
}
FileContainer copiedFile = file.copy(children, "mg.jpg");
try(InputStream is = new FileInputStream(new File("/home/lucio/Downloads/bck.jpg"))){
file = myRoot.uploadFile(is, "mg.jpg", "file");
} catch (Exception e) {
e.printStackTrace();
}
FileContainer secodncopiedFile = file.copy(children, "mg.jpg");
}
@Test
public void uploadAndcopyFile() throws Exception {
StorageHubClient shc = new StorageHubClient();
FileContainer file = null;
try(InputStream is = new FileInputStream(new File("/home/lucio/Downloads/_upwelling_index.py"))){
file = shc.getWSRoot().uploadFile(is, "._upwelling_index.py", "test");
}
//file.copy(shc.getWSRoot(), "firstCopy.jpg");
}
@Test
public void download() throws Exception {
StorageHubClient shc = new StorageHubClient();
StreamDescriptor streamDescr = shc.open("abb59b44-e3cb-408d-a1ff-73d6d8ad2ca1").asFile().downloadSpecificVersion("1.1");
System.out.println("length "+streamDescr.getContentLenght());
long start = System.currentTimeMillis();
File output = Files.createTempFile("down", streamDescr.getFileName()).toFile();
try (BufferedInputStream bi = new BufferedInputStream(streamDescr.getStream()); FileOutputStream fo = new FileOutputStream(output)){
byte[] buf = new byte[2046];
int read = -1;
while ((read=bi.read(buf))!=-1) {
fo.write(buf, 0, read);
}
}
System.out.println("file written "+output.getAbsolutePath()+" in "+(System.currentTimeMillis()-start));
}
@Test
public void emptyTrash() throws Exception {
StorageHubClient shc = new StorageHubClient();
shc.emptyTrash();
}
@Test
public void changeProp() throws Exception {
StorageHubClient shc = new StorageHubClient();
ItemContainer item = shc.open("6399daa7-2173-4314-b4f7-2afa24eae8f8").asItem();
Metadata first = item.get().getMetadata();
first.getMap().put("lucio", "ok");
item.setMetadata(first);
Metadata second = item.get().getMetadata();
for (Entry<String, Object> entry: second.getMap().entrySet())
System.out.println(entry.getKey()+" "+entry.getValue());
second.getMap().put("lucio", null);
second.getMap().put("lelii", "0");
item.setMetadata(second);
Metadata third = item.get().getMetadata();
for (Entry<String, Object> entry: third.getMap().entrySet())
System.out.println(entry.getKey()+" "+entry.getValue());
}
@Test
public void uploadArchive() throws Exception {
StorageHubClient shc = new StorageHubClient();
String afi = null;
try(InputStream is = new FileInputStream(new File("/tmp/down724121986692880606my new folder.zip"))){
shc.getWSRoot().uploadArchive(is, "testUpload2");
System.out.println(afi);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void getPublicLink() throws Exception{
StorageHubClient shc = new StorageHubClient();
System.out.println(shc.open("c2573eec-3942-47ec-94a7-04869e97bb69").asFile().getPublicLink());
}
@Test
public void findByName() throws Exception{
StorageHubClient shc = new StorageHubClient();
FolderContainer root = shc.open("2fd081dc-3b57-4e5f-aadf-38439aea4c1f").asFolder();
FolderContainer fc = root.newFolder("Computations", "description");
fc.share(new HashSet<>(Arrays.asList("giancarlo.panichi")), AccessType.READ_ONLY);
System.out.println(fc.get().getPath()+" "+fc.getId());
}
@Test
public void delete() throws Exception{
try {
StorageHubClient shc = new StorageHubClient();
shc.open("328d0097-e024-43fd-afc8-3926a34a5953").asFile().delete();
}catch(Exception e) {
e.printStackTrace();
}
}
@Test
public void downloadFile() throws Exception{
StorageHubClient shc = new StorageHubClient();
try {
for (ItemContainer<? extends Item> cont : shc.getWSRoot().list().getContainers()) {
if (cont.get().getTitle().equals("LucioShare1")) {
for (ItemContainer<? extends Item> c: ((FolderContainer)cont).list().getContainers()) {
c.get().getTitle();
}
}
}
}catch (StorageHubException e) {
e.printStackTrace();
}
try {
for (ItemContainer<? extends Item> cont : shc.getWSRoot().list().getContainers()) {
System.out.println(cont.get().getTitle());
}
}catch (StorageHubException e) {
e.printStackTrace();
}
/*
List<? extends Item> items = shc.getWSRoot().findByName("MyFo*").getItems();
for (Item item: items)
System.out.println(item.getName()+" "+item.getTitle()+" "+item.getClass().getSimpleName());
/*
URL link = shc.open("6c5fb143-3711-4097-9bdc-952e41355440").asFile().getPublicLink("1.1");
System.out.println(link.toString());
/*
File targetFile = new File("/tmp/"+descr.getFileName());
try {
java.nio.file.Files.copy(
descr.getStream(),
targetFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("file created "+targetFile.getAbsolutePath());
/*
List<ItemContainer<? extends Item>> containers = shc.getWSRoot().list().getContainers();
for (ItemContainer<? extends Item> container : containers) {
if (container.getType()==ContainerType.FILE) {
FileContainer file = (FileContainer) container;
StreamDescriptor descr = file.download();
File targetFile = new File("/tmp/"+descr.getFileName());
try {
java.nio.file.Files.copy(
descr.getStream(),
targetFile.toPath(),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("file created "+targetFile.getAbsolutePath());
break;
}
}*/
}
@Test
public void downloadFolderWithExcludes() throws Exception{
StorageHubClient shc = new StorageHubClient();
StreamDescriptor streamDescr = shc.open("6eb20db1-2921-41ec-ab79-909edd9b58fd").asItem().download("05098be5-61a2-423a-b382-9399a04df11e");
File tmpFile = Files.createTempFile(streamDescr.getFileName(),"").toFile();
System.out.println(tmpFile.getAbsolutePath());
try(FileOutputStream fos = new FileOutputStream(tmpFile)){
InputStream is = streamDescr.getStream();
byte[] buf = new byte[2048];
int read= -1;
while ((read=is.read(buf))!=-1) {
fos.write(buf, 0, read);
}
}
}
@Test
public void deleteUnusefulUsers() throws Exception{
final UserManagerClient client = AbstractPlugin.users().build();
List<String> users = client.getUsers();
StorageHubClient shc = new StorageHubClient();
for (String user : users)
if (user.startsWith("userm")) {
shc.deleteUserAccount(user);
System.out.println("user "+user+"removed");
}
}
/*
static String baseUrl="http://workspace-repository1-d.d4science.org/storagehub";
public static List<? extends Item> list(OrderBy order, Path path, ItemFilter<?> ... filters){
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(baseUrl+"/list/byPath?gcube-token=595ca591-9921-423c-bfca-f8be19f05882-98187548");
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
List<? extends Item> r = invocationBuilder.get(ItemList.class).getItemlist();
return r;
}
public static void createFolder(){
//Client client = ClientBuilder.newClient();
Client client = ClientBuilder.newBuilder()
.register(MultiPartFeature.class).build();
WebTarget webTarget = client.target(baseUrl+"/item/create?gcube-token=595ca591-9921-423c-bfca-f8be19f05882-98187548");
FolderItem folder= new FolderItem();
folder.setName("My third folder");
folder.setTitle("My third title");
final MultiPart multiPart = new FormDataMultiPart()
.field("item", new ItemWrapper<FolderItem>(folder), MediaType.APPLICATION_JSON_TYPE)
/* .field("characterProfile", jsonToSend, MediaType.APPLICATION_JSON_TYPE)
.field("filename", fileToUpload.getName(), MediaType.TEXT_PLAIN_TYPE)
.bodyPart(fileDataBodyPart)*/;
/*
Response res = webTarget.request().post(Entity.entity(multiPart, multiPart.getMediaType()));
System.out.println("status is "+res.getStatus());
}
public static void create() throws Exception{
ClientConfig clientConfig = new ClientConfig();
clientConfig.property("DEFAULT_CHUNK_SIZE", 2048);
Clie
//Client client = ClientBuilder.newClient();
Client client = ClientBuilder.newClient(clientConfig)
.register(MultiPartFeature.class);
WebTarget webTarget = client.target(baseUrl+"/item/create?gcube-token=595ca591-9921-423c-bfca-f8be19f05882-98187548");
GenericFileItem folder= new GenericFileItem();
folder.setName("testUpload.tar.gz");
folder.setTitle("testUpload.tar.gz");
FileDataBodyPart fileDataBodyPart = new FileDataBodyPart("file", new File("/home/lucio/Downloads/testUpload.tar.gz"));
final MultiPart multiPart = new FormDataMultiPart().field("item", new ItemWrapper<GenericFileItem>(folder), MediaType.APPLICATION_JSON_TYPE)
.bodyPart(fileDataBodyPart, MediaType.APPLICATION_OCTET_STREAM_TYPE);
multiPart.close();
Response res = webTarget.request().post(Entity.entity(multiPart, multiPart.getMediaType()));
System.out.println("status is "+res.getStatus());
}
public static void get() throws Exception{
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(baseUrl+"/item/6e9b8350-4854-4c22-8aa1-ba2d8135ad6d/download?gcube-token=950a0702-6ada-40e9-92dc-d243d1b45206-98187548");
Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_OCTET_STREAM);
Response res = invocationBuilder.get();
byte[] buf = new byte[1024];
/*while (is.read(buf)!=-1)
System.out.println("reading the buffer");
*/
/*
}
public static <T extends Item> T copy(T item, Path path){
return null;
}
public static <T extends Item> T move(T item, Path path){
return null;
}
public static <T extends Item> T unshareAll(T item){
return null;
}
*/
}