2018-02-15 18:13:50 +01:00
|
|
|
package org.gcube.data.access.fs;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
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-06-22 10:52:56 +02:00
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
|
import java.util.List;
|
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;
|
2018-07-05 12:31:52 +02:00
|
|
|
import org.gcube.common.calls.jaxrs.JaxRSRequestFilter;
|
2018-03-29 18:19:01 +02:00
|
|
|
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;
|
|
|
|
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-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-03-29 18:19:01 +02:00
|
|
|
@BeforeClass
|
|
|
|
public static void setUp(){
|
2018-07-05 12:31:52 +02:00
|
|
|
SecurityTokenProvider.instance.set("b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548");
|
|
|
|
ScopeProvider.instance.set("/gcube");
|
2018-03-29 18:19:01 +02:00
|
|
|
}
|
2018-04-18 15:55:20 +02:00
|
|
|
|
2018-03-29 18:19:01 +02:00
|
|
|
@Test
|
2018-07-05 12:31:52 +02:00
|
|
|
public void uploadFile() throws Exception {
|
2018-03-29 18:19:01 +02:00
|
|
|
StorageHubClient shc = new StorageHubClient();
|
2018-07-05 12:31:52 +02:00
|
|
|
/*
|
|
|
|
Client client = ClientBuilder.newClient().property(ClientProperties.FOLLOW_REDIRECTS, Boolean.FALSE).property(ClientProperties.CHUNKED_ENCODING_SIZE, 1024*1024);
|
|
|
|
|
|
|
|
WebTarget webTarget = client.target("http://workspace-repository1-d.d4science.org/storagehub/workspace/items/bc1c9525-43f7-4565-b5ea-0a0f9d7853a0/create/FILE?gcube-token=595ca591-9921-423c-bfca-f8be19f05882-98187548");
|
|
|
|
|
|
|
|
Invocation.Builder builder = webTarget.register(MultiPartFeature.class).request();
|
|
|
|
|
|
|
|
try (FormDataMultiPart multipart = new FormDataMultiPart();InputStream is = new FileInputStream(new File("/home/lucio/Downloads/1GB.zip"))){
|
|
|
|
|
|
|
|
multipart.field("name", "5Giga");
|
|
|
|
multipart.field("description", "");
|
|
|
|
|
|
|
|
StreamDataBodyPart fileDataBodyPart = new StreamDataBodyPart("file",is);
|
|
|
|
multipart.bodyPart(fileDataBodyPart);
|
|
|
|
|
|
|
|
String response = builder.post(Entity.entity(multipart, MediaType.MULTIPART_FORM_DATA_TYPE),String.class);
|
|
|
|
System.out.println(response);
|
|
|
|
}*/
|
2018-04-18 15:55:20 +02:00
|
|
|
|
2018-06-22 17:42:29 +02:00
|
|
|
String afi = null;
|
2018-07-05 15:37:29 +02:00
|
|
|
InputStream is = new FileInputStream(new File("/home/lucio/Downloads/test5Gb.zip"));
|
|
|
|
shc.getWSRoot().uploadFile(is, "5g.zip", "descr");
|
2018-06-22 16:27:21 +02:00
|
|
|
|
2018-07-05 15:37:29 +02:00
|
|
|
System.out.println(afi);
|
|
|
|
/*} catch (Exception e) {
|
2018-04-18 15:55:20 +02:00
|
|
|
e.printStackTrace();
|
2018-07-05 15:37:29 +02:00
|
|
|
}*/
|
2018-06-22 17:42:29 +02:00
|
|
|
|
2018-03-29 18:19:01 +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-06-26 15:30:30 +02:00
|
|
|
@Test
|
|
|
|
public void findByName() throws Exception{
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
List<? extends Item> containers = shc.getWSRoot().findByName("DataMiner").getItems();
|
|
|
|
for (Item container : containers) {
|
|
|
|
System.out.println("name is :"+container.getName());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-27 15:36:50 +02:00
|
|
|
@Test
|
|
|
|
public void delete() throws Exception{
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
shc.open("8dc6fca2-f0f5-4813-b854-f12e414e3a28").asFile().delete();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-22 10:52:56 +02:00
|
|
|
@Test
|
|
|
|
public void downloadFile() {
|
|
|
|
StorageHubClient shc = new StorageHubClient();
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-18 15:55:20 +02:00
|
|
|
|
2018-02-15 18:13:50 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|