128 lines
4.5 KiB
Java
128 lines
4.5 KiB
Java
|
package org.gcube.data.access.fs;
|
||
|
|
||
|
import java.io.BufferedInputStream;
|
||
|
import java.io.File;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.util.List;
|
||
|
|
||
|
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;
|
||
|
import javax.ws.rs.core.Response;
|
||
|
|
||
|
import org.gcube.common.storagehub.model.Path;
|
||
|
import org.gcube.common.storagehub.model.items.FolderItem;
|
||
|
import org.gcube.common.storagehub.model.items.GenericFileItem;
|
||
|
import org.gcube.common.storagehub.model.items.Item;
|
||
|
import org.gcube.common.storagehub.model.query.ItemFilter;
|
||
|
import org.gcube.common.storagehub.model.query.OrderBy;
|
||
|
import org.gcube.common.storagehub.model.service.ItemList;
|
||
|
import org.gcube.common.storagehub.model.service.ItemWrapper;
|
||
|
import org.glassfish.jersey.client.ClientConfig;
|
||
|
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
|
||
|
import org.glassfish.jersey.media.multipart.MultiPart;
|
||
|
import org.glassfish.jersey.media.multipart.MultiPartFeature;
|
||
|
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
|
||
|
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
|
||
|
|
||
|
|
||
|
|
||
|
public class Items {
|
||
|
|
||
|
/*
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
}
|