2018-02-15 18:13:50 +01:00
|
|
|
package org.gcube.data.access.fs;
|
|
|
|
|
2018-09-26 15:24:21 +02:00
|
|
|
import java.io.BufferedInputStream;
|
2018-02-15 18:13:50 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2018-09-04 11:21:08 +02:00
|
|
|
import java.io.FileOutputStream;
|
2018-06-22 10:52:56 +02:00
|
|
|
import java.io.IOException;
|
2018-03-29 18:19:01 +02:00
|
|
|
import java.io.InputStream;
|
2018-09-04 11:21:08 +02:00
|
|
|
import java.nio.file.Files;
|
2018-06-22 10:52:56 +02:00
|
|
|
import java.nio.file.StandardCopyOption;
|
2018-09-04 11:21:08 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
2018-06-22 10:52:56 +02:00
|
|
|
import java.util.List;
|
2018-09-04 11:21:08 +02:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.stream.Collectors;
|
2018-02-15 18:13:50 +01:00
|
|
|
|
2018-07-05 12:31:52 +02:00
|
|
|
import javax.ws.rs.client.Client;
|
|
|
|
import javax.ws.rs.client.ClientBuilder;
|
|
|
|
import javax.ws.rs.client.Entity;
|
|
|
|
import javax.ws.rs.client.Invocation;
|
|
|
|
import javax.ws.rs.client.WebTarget;
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
|
2018-03-29 18:19:01 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2018-06-22 10:52:56 +02:00
|
|
|
import org.gcube.common.storagehub.client.StreamDescriptor;
|
|
|
|
import org.gcube.common.storagehub.client.dsl.ContainerType;
|
|
|
|
import org.gcube.common.storagehub.client.dsl.FileContainer;
|
2018-09-26 15:24:21 +02:00
|
|
|
import org.gcube.common.storagehub.client.dsl.FolderContainer;
|
2018-06-22 10:52:56 +02:00
|
|
|
import org.gcube.common.storagehub.client.dsl.ItemContainer;
|
2018-04-18 15:55:20 +02:00
|
|
|
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
|
2018-09-26 15:24:21 +02:00
|
|
|
import org.gcube.common.storagehub.model.items.FolderItem;
|
2018-06-22 10:52:56 +02:00
|
|
|
import org.gcube.common.storagehub.model.items.Item;
|
2018-07-05 12:31:52 +02:00
|
|
|
import org.glassfish.jersey.client.ClientProperties;
|
|
|
|
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
|
|
|
|
import org.glassfish.jersey.media.multipart.MultiPartFeature;
|
|
|
|
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
|
2018-03-29 18:19:01 +02:00
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Test;
|
2018-02-15 18:13:50 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Items {
|
2018-09-04 11:21:08 +02:00
|
|
|
|
2018-03-29 18:19:01 +02:00
|
|
|
@BeforeClass
|
|
|
|
public static void setUp(){
|
2018-09-26 15:24:21 +02:00
|
|
|
//SecurityTokenProvider.instance.set("b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548");
|
2018-10-26 11:04:32 +02:00
|
|
|
SecurityTokenProvider.instance.set("a5b623b6-6577-4271-aba6-7ada687d29cf-98187548");
|
2018-09-26 15:24:21 +02:00
|
|
|
|
2018-10-26 11:04:32 +02:00
|
|
|
ScopeProvider.instance.set("/gcube/preprod");
|
2018-03-29 18:19:01 +02:00
|
|
|
}
|
2018-07-30 12:38:34 +02:00
|
|
|
|
2018-09-20 14:52:23 +02:00
|
|
|
|
2018-10-11 18:36:21 +02:00
|
|
|
@Test
|
|
|
|
public void renameFile() throws Exception{
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
shc.open("8822478a-4fd3-41d5-87de-9ff161d0935e").asItem().rename("renamed");
|
|
|
|
|
|
|
|
}
|
2018-09-20 14:52:23 +02:00
|
|
|
|
|
|
|
|
2018-03-29 18:19:01 +02:00
|
|
|
@Test
|
2018-09-28 17:02:20 +02:00
|
|
|
public void uploadAndcopyFile() throws Exception {
|
2018-03-29 18:19:01 +02:00
|
|
|
StorageHubClient shc = new StorageHubClient();
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-09-28 17:02:20 +02:00
|
|
|
FileContainer file = null;
|
2018-09-28 15:36:08 +02:00
|
|
|
try(InputStream is = new FileInputStream(new File("/home/lucio/Downloads/TatyanaSondre.jpg"))){
|
2018-10-04 16:53:47 +02:00
|
|
|
file = shc.getWSRoot().uploadFile(is, "TatyanaSondre5.jpg", "descr");
|
2018-09-28 17:02:20 +02:00
|
|
|
|
|
|
|
|
2018-07-05 16:24:49 +02:00
|
|
|
} catch (Exception e) {
|
2018-04-18 15:55:20 +02:00
|
|
|
e.printStackTrace();
|
2018-07-05 16:24:49 +02:00
|
|
|
}
|
2018-09-28 17:02:20 +02:00
|
|
|
file.copy(shc.getWSRoot(), "firstCopy.jpg");
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-03-29 18:19:01 +02:00
|
|
|
}
|
2018-09-26 15:24:21 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void download() throws Exception {
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
|
2018-10-11 18:36:21 +02:00
|
|
|
FolderContainer openResolver = shc.open("894d23bf-e2e9-42b6-a781-b99bb18119c8").asFolder();
|
2018-09-26 15:24:21 +02:00
|
|
|
|
2018-10-04 16:53:47 +02:00
|
|
|
StreamDescriptor streamDescr = openResolver.download();
|
2018-09-26 15:24:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
File output = Files.createTempFile("down", streamDescr.getFileName()).toFile();
|
|
|
|
try (BufferedInputStream bi = new BufferedInputStream(streamDescr.getStream()); FileOutputStream fo = new FileOutputStream(output)){
|
|
|
|
byte[] buf = new byte[2048];
|
|
|
|
int read = -1;
|
|
|
|
while ((read=bi.read(buf))!=-1) {
|
|
|
|
fo.write(buf, 0, read);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
System.out.println("file written "+output.getAbsolutePath());
|
|
|
|
|
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-09-24 16:42:00 +02:00
|
|
|
@Test
|
|
|
|
public void emptyTrash() throws Exception {
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
2018-09-28 12:17:53 +02:00
|
|
|
|
2018-09-28 15:36:08 +02:00
|
|
|
shc.emptyTrash();
|
2018-09-24 16:42:00 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-30 12:38:34 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void uploadArchive() throws Exception {
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
|
|
|
|
String afi = null;
|
|
|
|
|
2018-10-04 16:53:47 +02:00
|
|
|
try(InputStream is = new FileInputStream(new File("/tmp/down724121986692880606my new folder.zip"))){
|
|
|
|
shc.getWSRoot().uploadArchive(is, "testUpload2");
|
2018-07-30 12:38:34 +02:00
|
|
|
|
|
|
|
System.out.println(afi);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-06-22 17:42:29 +02:00
|
|
|
@Test
|
|
|
|
public void getPublicLink() {
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
System.out.println(shc.open("c2573eec-3942-47ec-94a7-04869e97bb69").asFile().getPublicLink());
|
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-06-26 15:30:30 +02:00
|
|
|
@Test
|
|
|
|
public void findByName() throws Exception{
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
2018-09-26 15:24:21 +02:00
|
|
|
List<? extends Item> containers = shc.getWSRoot().list().ofType(FolderItem.class).getItems();
|
2018-06-26 15:30:30 +02:00
|
|
|
for (Item container : containers) {
|
2018-09-26 15:24:21 +02:00
|
|
|
System.out.println("name is :"+container.getClass().getSimpleName());
|
2018-06-26 15:30:30 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-06-27 15:36:50 +02:00
|
|
|
@Test
|
|
|
|
public void delete() throws Exception{
|
2018-10-04 16:53:47 +02:00
|
|
|
try {
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
shc.open("733a8235-e539-46c6-875d-3038579d4c6a").asFile().delete();
|
|
|
|
}catch(Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-06-27 15:36:50 +02:00
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-06-22 10:52:56 +02:00
|
|
|
@Test
|
|
|
|
public void downloadFile() {
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-06-22 10:52:56 +02:00
|
|
|
List<ItemContainer<? extends Item>> containers = shc.getWSRoot().list().getContainers();
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-06-22 10:52:56 +02:00
|
|
|
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());
|
2018-07-05 16:24:49 +02:00
|
|
|
|
|
|
|
try {
|
2018-06-22 10:52:56 +02:00
|
|
|
java.nio.file.Files.copy(
|
|
|
|
descr.getStream(),
|
2018-07-05 16:24:49 +02:00
|
|
|
targetFile.toPath(),
|
|
|
|
StandardCopyOption.REPLACE_EXISTING);
|
2018-06-22 10:52:56 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
System.out.println("file created "+targetFile.getAbsolutePath());
|
|
|
|
break;
|
2018-06-22 10:52:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-06-22 10:52:56 +02:00
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-09-04 11:21:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
@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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-09-04 11:21:08 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 15:39:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2018-02-15 18:13:50 +01:00
|
|
|
/*
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-02-15 18:13:50 +01:00
|
|
|
static String baseUrl="http://workspace-repository1-d.d4science.org/storagehub";
|
|
|
|
|
2018-07-05 16:24:49 +02:00
|
|
|
|
|
|
|
|
2018-02-15 18:13:50 +01:00
|
|
|
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)*/;
|
2018-07-05 16:24:49 +02:00
|
|
|
/*
|
2018-02-15 18:13:50 +01:00
|
|
|
Response res = webTarget.request().post(Entity.entity(multiPart, multiPart.getMediaType()));
|
|
|
|
System.out.println("status is "+res.getStatus());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void create() throws Exception{
|
2018-07-05 16:24:49 +02:00
|
|
|
|
|
|
|
|
2018-02-15 18:13:50 +01:00
|
|
|
ClientConfig clientConfig = new ClientConfig();
|
|
|
|
clientConfig.property("DEFAULT_CHUNK_SIZE", 2048);
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-02-15 18:13:50 +01:00
|
|
|
Clie
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-02-15 18:13:50 +01:00
|
|
|
//Client client = ClientBuilder.newClient();
|
|
|
|
Client client = ClientBuilder.newClient(clientConfig)
|
|
|
|
.register(MultiPartFeature.class);
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-02-15 18:13:50 +01:00
|
|
|
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");
|
2018-07-05 16:24:49 +02:00
|
|
|
|
2018-02-15 18:13:50 +01:00
|
|
|
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");
|
2018-07-05 16:24:49 +02:00
|
|
|
*/
|
2018-02-15 18:13:50 +01:00
|
|
|
|
2018-07-05 16:24:49 +02:00
|
|
|
/*
|
2018-02-15 18:13:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-07-05 16:24:49 +02:00
|
|
|
*/
|
2018-02-15 18:13:50 +01:00
|
|
|
|
2018-07-05 16:24:49 +02:00
|
|
|
}
|