Messages clietn added

This commit is contained in:
lucio.lelii 2021-04-29 16:58:14 +02:00
parent 74281ed64f
commit e7495bb8b2
9 changed files with 562 additions and 216 deletions

20
pom.xml
View File

@ -18,7 +18,7 @@
<groupId>org.gcube.common</groupId>
<artifactId>storagehub-client-library</artifactId>
<version>1.2.2</version>
<version>1.3.0-SNAPSHOT</version>
<name>storagehub-client-library</name>
<dependencyManagement>
@ -26,7 +26,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>1.3.1</version>
<version>2.0.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
@ -54,13 +54,9 @@
<dependency>
<groupId>org.gcube.common</groupId>
<artifactId>storagehub-model</artifactId>
<version>[1.0.0,2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.3.0</version>
<version>[1.0.0-SNAPSHOT, 2.0.0-SNAPSHOT)</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
@ -99,7 +95,6 @@
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.13</version>
<scope>test</scope>
</dependency>
<dependency>
@ -108,7 +103,12 @@
<version>2.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>5.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -7,6 +7,7 @@ import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.common.storagehub.client.Constants;
import org.gcube.common.storagehub.client.proxies.GroupManagerClient;
import org.gcube.common.storagehub.client.proxies.ItemManagerClient;
import org.gcube.common.storagehub.client.proxies.MessageManagerClient;
import org.gcube.common.storagehub.client.proxies.UserManagerClient;
import org.gcube.common.storagehub.client.proxies.WorkspaceManagerClient;
@ -20,6 +21,7 @@ public abstract class AbstractPlugin<S,P> implements Plugin<S,P> {
private static final WorkspaceManagerPlugin workspace_plugin = new WorkspaceManagerPlugin();
private static final UserManagerPlugin user_plugin = new UserManagerPlugin();
private static final GroupManagerPlugin group_plugin = new GroupManagerPlugin();
private static final MessageManagerPlugin messages_plugin = new MessageManagerPlugin();
public static ProxyBuilder<ItemManagerClient> item() {
@ -37,6 +39,10 @@ public abstract class AbstractPlugin<S,P> implements Plugin<S,P> {
public static ProxyBuilder<WorkspaceManagerClient> workspace() {
return new ProxyBuilderImpl<GXWebTargetAdapterRequest,WorkspaceManagerClient>(workspace_plugin);
}
public static ProxyBuilder<MessageManagerClient> messages() {
return new ProxyBuilderImpl<GXWebTargetAdapterRequest,MessageManagerClient>(messages_plugin);
}
public final String name;

View File

@ -0,0 +1,53 @@
package org.gcube.common.storagehub.client.plugins;
import javax.xml.transform.dom.DOMResult;
import javax.xml.ws.EndpointReference;
import org.gcube.common.calls.jaxrs.GcubeService;
import org.gcube.common.calls.jaxrs.TargetFactory;
import org.gcube.common.clients.config.ProxyConfig;
import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.common.gxrest.response.entity.SerializableErrorEntityTextReader;
import org.gcube.common.storagehub.client.Constants;
import org.gcube.common.storagehub.client.MyInputStreamProvider;
import org.gcube.common.storagehub.client.proxies.DefaultMessageManager;
import org.gcube.common.storagehub.client.proxies.MessageManagerClient;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.w3c.dom.Node;
public class MessageManagerPlugin extends AbstractPlugin<GXWebTargetAdapterRequest, MessageManagerClient> {
public MessageManagerPlugin() {
super("storagehub/workspace");
}
@Override
public Exception convert(Exception e, ProxyConfig<?, ?> arg1) {
return e;
}
@Override
public MessageManagerClient newProxy(ProxyDelegate<GXWebTargetAdapterRequest> delegate) {
return new DefaultMessageManager(delegate);
}
@Override
public GXWebTargetAdapterRequest resolve(EndpointReference epr, ProxyConfig<?, ?> config)
throws Exception {
DOMResult result = new DOMResult();
epr.writeTo(result);
Node node =result.getNode();
Node child=node.getFirstChild();
String address = child.getTextContent();
//GXWebTargetAdapterRequest request = GXWebTargetAdapterRequest.newRequest(address).path(this.name).path("items");
GcubeService service = GcubeService.service().withName(Constants.MANAGER_QNAME).andPath("messages");
GXWebTargetAdapterRequest requestAdapter = TargetFactory.stubFor(service).getAsGxRest(address);
requestAdapter.register(SerializableErrorEntityTextReader.class);
requestAdapter.register(MyInputStreamProvider.class);
requestAdapter.register(MultiPartFeature.class);
return requestAdapter;
}
}

View File

@ -587,12 +587,13 @@ public class DefaultItemManager implements ItemManagerClient {
Objects.requireNonNull(id, "id cannot be null");
Objects.requireNonNull(id, "name cannot be null");
GXWebTargetAdapterRequest myManager = manager.path(id).path("items").path(name);
GXWebTargetAdapterRequest myManager = manager.path(id).path("items");
Map<String, Object[]> params = new HashMap<>();
if (excludeNodes !=null && excludeNodes.length>0)
params.put("exclude",excludeNodes);
params.put("name", new Object[] {name});
GXInboundResponse response = myManager.queryParams(params).get();
if (response.isErrorResponse()) {

View File

@ -0,0 +1,257 @@
package org.gcube.common.storagehub.client.proxies;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedHashMap;
import javax.ws.rs.core.MultivaluedMap;
import org.gcube.common.clients.Call;
import org.gcube.common.clients.delegates.ProxyDelegate;
import org.gcube.common.gxrest.request.GXWebTargetAdapterRequest;
import org.gcube.common.gxrest.response.inbound.GXInboundResponse;
import org.gcube.common.storagehub.model.exceptions.BackendGenericError;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.common.storagehub.model.exceptions.UserNotAuthorizedException;
import org.gcube.common.storagehub.model.items.Item;
import org.gcube.common.storagehub.model.messages.Message;
import org.gcube.common.storagehub.model.service.ItemList;
import org.gcube.common.storagehub.model.types.MessageList;
public class DefaultMessageManager implements MessageManagerClient {
private final ProxyDelegate<GXWebTargetAdapterRequest> delegate;
public DefaultMessageManager(ProxyDelegate<GXWebTargetAdapterRequest> config){
this.delegate = config;
}
@Override
public Message get(String id) throws StorageHubException {
Call<GXWebTargetAdapterRequest, Message> call = new Call<GXWebTargetAdapterRequest, Message>() {
@Override
public Message call(GXWebTargetAdapterRequest manager) throws Exception {
Objects.requireNonNull(id, "id cannot be null");
GXWebTargetAdapterRequest myManager = manager.path(id);
GXInboundResponse response = myManager.get();
if (response.isErrorResponse()) {
if (response.hasException())
throw response.getException();
else
throw new BackendGenericError("HTTP error code is "+response.getHTTPCode());
}
Message item = response.getSource().readEntity(Message.class);
return item;
}
};
try {
return delegate.make(call);
}catch(StorageHubException e) {
throw e;
}catch(Exception e1) {
throw new RuntimeException(e1);
}
}
@Override
public List<Message> getReceivedMessages(int reduceBody) throws StorageHubException {
return getMessages("inbox", reduceBody);
}
@Override
public List<Message> getSentMessages(int reduceBody) throws StorageHubException {
return getMessages("sent", reduceBody);
}
@Override
public List<Message> getReceivedMessages() throws StorageHubException {
return getMessages("inbox", -1);
}
@Override
public List<Message> getSentMessages() throws StorageHubException {
return getMessages("sent", -1);
}
@Override
public void setRead(String id, Boolean value) throws StorageHubException {
setBooleanProp("hl:read", value, id);
}
@Override
public void setOpened(String id, Boolean value) throws StorageHubException {
setBooleanProp("hl:open", value, id);
}
private void setBooleanProp(String prop, Boolean bool, String id) throws StorageHubException {
Call<GXWebTargetAdapterRequest, Void> call = new Call<GXWebTargetAdapterRequest, Void>() {
@Override
public Void call(GXWebTargetAdapterRequest manager) throws Exception {
GXWebTargetAdapterRequest myManager = manager.path(id).path(prop);
GXInboundResponse response = myManager.put(Entity.json(Boolean.TRUE));
if (response.isErrorResponse()) {
if (response.hasException())
throw response.getException();
else
throw new BackendGenericError("HTTP error code is "+response.getHTTPCode());
}
return null;
}
};
try {
delegate.make(call);
}catch(StorageHubException e) {
throw e;
}catch(Exception e1) {
throw new RuntimeException(e1);
}
}
@Override
public String sendMessage(List<String> recipients, String subject, String body, List<String> attachments) throws StorageHubException {
Call<GXWebTargetAdapterRequest, String> call = new Call<GXWebTargetAdapterRequest, String>() {
@Override
public String call(GXWebTargetAdapterRequest manager) throws Exception {
Objects.requireNonNull(recipients, "recipients cannot be null");
Objects.requireNonNull(subject, "subject cannot be null");
Objects.requireNonNull(body, "body cannot be null");
GXWebTargetAdapterRequest myManager = manager.path("send");
MultivaluedMap<String, Object> formData = new MultivaluedHashMap<String, Object>();
recipients.forEach(r-> formData.add("to[]", r));
formData.add("subject", subject);
formData.add("body", body);
if (attachments!=null)
attachments.forEach(a -> formData.add("attachemnts[]", a));
System.out.println(formData.toString());
GXInboundResponse response = myManager.post(Entity.entity(formData, MediaType.APPLICATION_FORM_URLENCODED));
if (response.isErrorResponse()) {
if (response.hasException())
throw response.getException();
else
throw new BackendGenericError("HTTP error code is "+response.getHTTPCode());
}
return response.getSource().readEntity(String.class);
}
};
try {
return delegate.make(call);
}catch(StorageHubException e) {
throw e;
}catch(Exception e1) {
throw new RuntimeException(e1);
}
}
@Override
public void delete(String id) throws StorageHubException {
Call<GXWebTargetAdapterRequest, Void> call = new Call<GXWebTargetAdapterRequest, Void>() {
@Override
public Void call(GXWebTargetAdapterRequest manager) throws Exception {
Objects.requireNonNull(id, "id cannot be null");
GXWebTargetAdapterRequest myManager = manager.path(id);
GXInboundResponse response = myManager.delete();
if (response.isErrorResponse()) {
if (response.hasException())
throw response.getException();
else
throw new BackendGenericError("HTTP error code is "+response.getHTTPCode());
}
return null;
}
};
try {
delegate.make(call);
}catch(StorageHubException e) {
throw e;
}catch(Exception e1) {
throw new RuntimeException(e1);
}
}
public List<? extends Item> getAttachments(String messageId) throws StorageHubException{
Call<GXWebTargetAdapterRequest, ItemList> call = new Call<GXWebTargetAdapterRequest, ItemList>() {
@Override
public ItemList call(GXWebTargetAdapterRequest manager) throws Exception {
GXWebTargetAdapterRequest myManager = manager.path(messageId).path("attachments");
GXInboundResponse response = myManager.get();
if (response.isErrorResponse()) {
if (response.hasException())
throw response.getException();
else if (response.getHTTPCode()==403)
throw new UserNotAuthorizedException("the call to this method is not allowed for the user");
else
throw new BackendGenericError();
}
return response.getSource().readEntity(ItemList.class);
}
};
try {
return delegate.make(call).getItemlist();
}catch(StorageHubException e) {
throw e;
}catch(Exception e1) {
throw new RuntimeException(e1);
}
}
private List<Message> getMessages(final String path, int reduceBody ) throws StorageHubException{
Call<GXWebTargetAdapterRequest, List<Message>> call = new Call<GXWebTargetAdapterRequest, List<Message>>() {
@Override
public List<Message> call(GXWebTargetAdapterRequest manager) throws Exception {
GXWebTargetAdapterRequest myManager = manager.path(path);
if (reduceBody>0)
myManager.queryParams(Collections.singletonMap("reduceBody", new Object[]{reduceBody}));
GXInboundResponse response = myManager.get();
if (response.isErrorResponse()) {
if (response.hasException())
throw response.getException();
else if (response.getHTTPCode()==403)
throw new UserNotAuthorizedException("the call to this method is not allowed for the user");
else
throw new BackendGenericError();
}
return response.getSource().readEntity(MessageList.class).getMessages();
}
};
try {
return delegate.make(call);
}catch(StorageHubException e) {
throw e;
}catch(Exception e1) {
throw new RuntimeException(e1);
}
}
}

View File

@ -0,0 +1,32 @@
package org.gcube.common.storagehub.client.proxies;
import java.util.List;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.common.storagehub.model.items.Item;
import org.gcube.common.storagehub.model.messages.Message;
public interface MessageManagerClient {
Message get(String id) throws StorageHubException;
List<? extends Item> getAttachments(String id) throws StorageHubException;
List<Message> getReceivedMessages(int reduceBody) throws StorageHubException;
List<Message> getSentMessages(int reduceBody) throws StorageHubException;
List<Message> getReceivedMessages() throws StorageHubException;
List<Message> getSentMessages() throws StorageHubException;
void delete(String id) throws StorageHubException;
String sendMessage(List<String> recipients, String subject, String body, List<String> attachments)
throws StorageHubException;
void setRead(String id, Boolean value) throws StorageHubException;
void setOpened(String id, Boolean value) throws StorageHubException;
}

View File

@ -1,107 +0,0 @@
package org.gcube.data.access.fs;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
public class AddUserToVRes {
List<String> DORNE_VRE = Arrays.asList("mister.brown",
"jesus.santamariafernandez",
"noah.matovu",
"andrea.rossi",
"francesco.mangiacrapa",
"mister.pink",
"kostas.kakaletris",
"massimiliano.assante",
"mister.white",
"mister.blonde",
"aureliano.gentile",
"mister.blue",
"mister.orange",
"lucio.lelii");
List<String> PARTHENOS_VRE = Arrays.asList("francesco.mangiacrapa",
"massimiliano.assante",
"costantino.perciante",
"luca.frosini",
"pasquale.pagano",
"alessia.bardi",
"roberto.cirillo");
List<String> PRE_VRE = Arrays.asList("fabio.sinibaldi",
"valentina.marioli",
"statistical.manager",
"roberto.cirillo",
"francesco.mangiacrapa",
"leonardo.candela",
"costantino.perciante",
"mariaantonietta.digirolamo",
"gantzoulatos",
"massimiliano.assante",
"lucio.lelii",
"panagiota.koltsida",
"ngalante",
"efthymios",
"nikolas.laskaris",
"andrea.dellamico",
"gianpaolo.coro",
"giancarlo.panichi",
"kostas.kakaletris",
"scarponi",
"andrea.rossi",
"pasquale.pagano",
"mister.blue",
"m.assante",
"yannis.marketakis",
"grsf.publisher",
"kgiannakelos",
"mister.pink",
"luca.frosini",
"dkatris",
"paolo.fabriani",
"mister.brown",
"mister.white",
"mister.orange",
"gabriele.giammatteo");
@Test
public void add() throws Exception{
String group = "pred4s-preprod-preVRE";
URL addGroupUrl = new URL("http://storagehub.pre.d4science.net/storagehub/workspace/groups/"+group+"?gcube-token=");
for (String user : PRE_VRE) {
try {
HttpURLConnection connection =(HttpURLConnection)addGroupUrl.openConnection();
connection.setRequestMethod("PUT");
StringBuilder postData = new StringBuilder();
postData.append("userId");
postData.append("=");
postData.append(user);
/*postData.append("&");
postData.append("password");
postData.append("=");
postData.append("pwd"+user.hashCode());*/
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
connection.setDoOutput(true);
connection.getOutputStream().write(postDataBytes);
Reader in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
for (int c; (c = in.read()) >= 0;)
System.out.print((char)c);
}catch (Exception e) {
System.out.println("error for user "+user);
e.printStackTrace();
}
}
}
}

View File

@ -7,10 +7,11 @@ import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.Properties;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
@ -24,6 +25,7 @@ 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.FolderItem;
import org.gcube.common.storagehub.model.items.Item;
import org.junit.BeforeClass;
import org.junit.Test;
@ -32,61 +34,127 @@ import org.junit.Test;
public class Items {
private static final String propFile = "C:\\Users\\tilli\\Documents\\eclipse\\tokens.properties";
private static final String tokens = "dev-root";
//private static final String tokens = "prod-root";
@BeforeClass
public static void setUp(){
SecurityTokenProvider.instance.set("b7c80297-e4ed-42ab-ab42-fdc0b8b0eabf-98187548");
ScopeProvider.instance.set("/gcube");
try(InputStream is = new FileInputStream(new File(propFile))){
Properties prop = new Properties();
prop.load(is);
String value =(String)prop.get(tokens);
String[] splitValue = value.split(",");
String context = splitValue[0];
String token = splitValue[1];
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(context);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void search() throws Exception{
public void addUserToVRe() throws StorageHubException {
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());
String vresFile = "C:\\Users\\tilli\\Downloads\\vresToAddGCat.txt";
try(InputStream is = new FileInputStream(new File(vresFile))){
Properties prop = new Properties();
prop.load(is);
Enumeration<Object> enumer = prop.keys();
while (enumer.hasMoreElements()) {
try {
String vre= (String) enumer.nextElement();
shc.getVreFolderManager(vre).addUser("gCat");
System.out.println("added to "+vre);
}catch (Exception e) {
e.printStackTrace();
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void search() {
try {
StorageHubClient shc = new StorageHubClient();
List<? extends Item> s = shc.getWSRoot().search("WD%",false).getItems();
s.forEach(i -> System.out.println(i.getName()+" "+i.getId()));
}catch (Exception e ) {
e.printStackTrace();
}
}
@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 setAdmin() throws Exception{
try {
StorageHubClient shc = new StorageHubClient();
shc.getVreFolderManager("gcube-devsec-devVRE").setAdmin("giancarlo.panichi");
}catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void removeAdmin() throws Exception{
try {
StorageHubClient shc = new StorageHubClient();
shc.getVreFolderManager("gcube-devsec-devVRE").removeAdmin("luca.frosini");
}catch (Exception e) {
e.printStackTrace();
}
}
@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");
@ -112,14 +180,14 @@ public class Items {
@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");
try(InputStream is = new FileInputStream(new File("c:\\Users\\tilli\\Downloads\\expired.png"))){
file = shc.getWSRoot().uploadFile(is, "expired/exp.png", "test");
}
//file.copy(shc.getWSRoot(), "firstCopy.jpg");
}
@ -127,8 +195,8 @@ public class Items {
public void download() throws Exception {
StorageHubClient shc = new StorageHubClient();
StreamDescriptor streamDescr = shc.open("abb59b44-e3cb-408d-a1ff-73d6d8ad2ca1").asFile().downloadSpecificVersion("1.1");
StreamDescriptor streamDescr = shc.open("abb59b44-e3cb-408d-a1ff-73d6d8ad2ca1").asFile().download();
System.out.println("length "+streamDescr.getContentLenght());
long start = System.currentTimeMillis();
@ -141,10 +209,10 @@ public class Items {
}
}
System.out.println("file written "+output.getAbsolutePath()+" in "+(System.currentTimeMillis()-start));
}
@Test
@ -159,31 +227,31 @@ public class Items {
@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 {
@ -226,14 +294,33 @@ public class Items {
@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();
StorageHubClient shc = new StorageHubClient();
FolderContainer container = shc.getWSRoot();
try {
FolderContainer attachmentFolder = container.openByRelativePath("Attachment").asFolder();
FolderItem folder = attachmentFolder.get();
} catch (StorageHubException e) {
System.out.println("creating folder");
container.newFolder("Attachment","Folder created automatically by the System");
}
}
@Test
public void createFolderWhenNotExists() throws Exception{
StorageHubClient shc = new StorageHubClient();
FolderContainer container = shc.openVREFolder();
try {
FolderContainer attachmentFolder = container.openByRelativePath("Attachment-Lucio").asFolder();
System.out.println("fodler name is "+attachmentFolder.get().getName());
} catch (StorageHubException e) {
System.out.println("creating folder");
container.newFolder("Attachment-Lucio","Folder created automatically by the System");
}
}
@Test
public void downloadFile() throws Exception{
StorageHubClient shc = new StorageHubClient();
@ -310,7 +397,7 @@ public class Items {
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();
@ -328,21 +415,21 @@ public class Items {
}
@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");
}
}
@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";

View File

@ -4,12 +4,14 @@ import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -25,14 +27,13 @@ import org.gcube.common.storagehub.client.StreamDescriptor;
import org.gcube.common.storagehub.client.plugins.AbstractPlugin;
import org.gcube.common.storagehub.client.proxies.GroupManagerClient;
import org.gcube.common.storagehub.client.proxies.ItemManagerClient;
import org.gcube.common.storagehub.client.proxies.UserManagerClient;
import org.gcube.common.storagehub.client.proxies.MessageManagerClient;
import org.gcube.common.storagehub.client.proxies.WorkspaceManagerClient;
import org.gcube.common.storagehub.model.Metadata;
import org.gcube.common.storagehub.model.Paths;
import org.gcube.common.storagehub.model.acls.ACL;
import org.gcube.common.storagehub.model.acls.AccessType;
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.gcube.common.storagehub.model.exceptions.UserNotAuthorizedException;
import org.gcube.common.storagehub.model.expressions.GenericSearchableItem;
import org.gcube.common.storagehub.model.expressions.OrderField;
import org.gcube.common.storagehub.model.expressions.SearchableItem;
@ -54,59 +55,87 @@ public class TestCall {
private static Logger log = LoggerFactory.getLogger(TestCall.class);
private static final String propFile = "C:\\Users\\tilli\\Documents\\eclipse\\tokens.properties";
private static final String tokens = "dev-root";
@BeforeClass
public static void setUp(){
try(InputStream is = new FileInputStream(new File(propFile))){
Properties prop = new Properties();
prop.load(is);
String value =(String)prop.get(tokens);
String[] splitValue = value.split(",");
String context = splitValue[0];
String token = splitValue[1];
SecurityTokenProvider.instance.set(token);
ScopeProvider.instance.set(context);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void removeUserFromGroup() throws StorageHubException {
GroupManagerClient client = AbstractPlugin.groups().build();
client.removeUserFromGroup("andrea.rossi", "gcube-devsec-devVRE");
public void sendMessages() throws StorageHubException {
MessageManagerClient client = AbstractPlugin.messages().build();
for (int i=1500; i<3000; i++ ) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
client.sendMessage(Arrays.asList("andrea.rossi"), "testMessage"+i, "body for ttrtetatrtrrttrtrtrar\ndfafsdfadsfafdsdsfafdsafaddsffdsf\nfsdafdjdjskhjghshjhdahjkasdhjjhsd"
+ "\ndfsjkfdasjklòjffafadjksò\ndfskfjklaajjfadsjkòfjòfdsajòfadòjk\ndsaffadsffbdsfajfdsbsafdsjkfdsakjlsdafjfkdsjklajlkfadsjfsasjd\n"
+ "dsfjkfdsajhdsfsdfahhflhjfdkshlkjfhjafdsjhlkjkhfjkdshjkfhsjklsdah fhadfjahlfdsjfdhjfads fdsfdhsajdfsahjfdsaljhhfahafshjsfdalhlfash\n"
+ "dfsakldfjdajfdsjòdsfaj fdsajkadskk dfsjòkdsfaj sjdfsjdja aòfs jsfadfsdò saj òa fafòadsj jsafads\n"
+ "dsfjfdhdsaljkhfdsljhdfsajkhsf fdshsajkhshjksdfhjkfdsh dsfjkh h dfshj ds\n"
+ "dfjdfahjklhjhaajkshkshjdsafhldslhkdsfaljkadsfhlfjasjkhdasfjhkhfdf ads hldfsasahfdsdfshjfdsa jjhfdsh dfhjs hsajfdhdfhfdshhjdfsjhdf\n"
+ "fdsjadsjkdfahjlkdfshkjdshjkfsdhjklsadfjkhjsaslfdhjalkdsfhjfshkfjlsldshjfsdhj\ndastest\n\n\nMessage"+i, null );
System.out.println("sending message "+i);
}
}
@Test
public void addUserToGroup() throws StorageHubException {
GroupManagerClient client = AbstractPlugin.groups().build();
client.addUserToGroup("andrea.rossi", "gcube-devsec-devVRE");
}
@Test(expected=UserNotAuthorizedException.class)
public void createNotAuthorized() throws Exception{
ItemManagerClient itemclient = AbstractPlugin.item().build();
itemclient.createFolder("3bc977be-37f0-4518-888f-a7cb96c9be8e", "cannot create", "", false);
}
@Test
public void createFolderAndShare() throws Exception{
ItemManagerClient itemclient = AbstractPlugin.item().build();
itemclient.createFolder("fakeId", "test", "test", false);
/*WorkspaceManagerClient client = AbstractPlugin.workspace().build();
Item ws = client.getWorkspace();
String id = itemclient.createFolder(ws.getId(), "ok7SharingTest", "shared folder for test SHM");
String sharedId = itemclient.shareFolder(id, new HashSet<String>(Arrays.asList("giancarlo.panichi")), AccessType.WRITE_OWNER);
itemclient.uploadFile(new FileInputStream("/home/lucio/Downloads/upload.pdf"), sharedId, "sharedFile.pdf" , "shared file in a shared folder");
*/
*/
}
@Test
public void shareAnAlreadySharedFolder() throws Exception{
ItemManagerClient itemclient = AbstractPlugin.item().build();
itemclient.shareFolder("86e8472a-6f66-4608-9d70-20102c9172ce", new HashSet<>(Arrays.asList("costantino.perciante")), AccessType.READ_ONLY);
}
@Test
public void restore() throws Exception{
WorkspaceManagerClient client = AbstractPlugin.workspace().build();
System.out.println(client.restoreFromTrash("4fc0a9df-9a51-42ef-98f2-06c21bd0669b", "f3d336cc-cd00-48ba-8339-2bffcbef825e"));
}
@Test
public void unshareFolder() throws Exception{
ItemManagerClient itemclient = AbstractPlugin.item().build();
itemclient.unshareFolder("86e8472a-6f66-4608-9d70-20102c9172ce", new HashSet<>(Arrays.asList("giancarlo.panichi")));
@ -114,7 +143,6 @@ public class TestCall {
}
@Test
public void delete() throws Exception{
ItemManagerClient itemclient = AbstractPlugin.item().build();
itemclient.delete("7af3d5cb-5e74-4a80-be81-acb2fec74cd9");
@ -123,7 +151,6 @@ public class TestCall {
@Test
public void getById() throws Exception{
final ItemManagerClient client = AbstractPlugin.item().build();
List<? extends Item> items = client.getAnchestors("29b417e2-dc2f-419a-be0b-7f49e76c9d7c", "hl:accounting", "jcr:content");
@ -136,7 +163,6 @@ public class TestCall {
System.in.read();
}
@Test
public void setMetadata() throws Exception{
final ItemManagerClient client = AbstractPlugin.item().build();
Metadata meta = new Metadata();
@ -147,7 +173,6 @@ public class TestCall {
client.setMetadata("8822478a-4fd3-41d5-87de-9ff161d0935e", meta);
}
@Test
public void createGcubeItem() throws Exception {
final ItemManagerClient client = AbstractPlugin.item().build();
GCubeItem item = new GCubeItem();
@ -163,7 +188,6 @@ public class TestCall {
client.createGcubeItem(ws.getId(), item);
}
@Test
public void upload() throws Exception{
//final ItemManagerClient client = AbstractPlugin.item().at(new URI("http://workspace-repository1-d.d4science.org:8080/storagehub")).build();
@ -186,7 +210,6 @@ public class TestCall {
}
@Test
public void uploadArchive() throws Exception{
final ItemManagerClient client = AbstractPlugin.item().at(new URI("http://workspace-repository1-d.d4science.org:8080/storagehub")).build();
@ -196,7 +219,6 @@ public class TestCall {
@Test
public void getACL() throws Exception{
final ItemManagerClient client = AbstractPlugin.item().build();
try {
@ -212,7 +234,6 @@ public class TestCall {
}
}
@Test
public void download() throws Exception{
ItemManagerClient client = AbstractPlugin.item().build();
StreamDescriptor streamDescr = client.download("6875651d-6510-4b82-a0f3-cc3356c1a143");
@ -228,7 +249,6 @@ public class TestCall {
System.out.println("file written "+output.getAbsolutePath());
}
@Test
public void getCount() throws Exception{
final ItemManagerClient client = AbstractPlugin.item().build();
long start = System.currentTimeMillis();
@ -239,7 +259,6 @@ public class TestCall {
System.in.read();
}
@Test
public void getVreFolder() {
ItemManagerClient itemclient = AbstractPlugin.item().build();
WorkspaceManagerClient wsclient = AbstractPlugin.workspace().build();
@ -250,9 +269,9 @@ public class TestCall {
}
@Test
public void getRecents() {
ItemManagerClient itemclient = AbstractPlugin.item().build();
WorkspaceManagerClient wsclient = AbstractPlugin.workspace().build();
List<? extends Item> items = wsclient.getRecentModifiedFilePerVre();
@ -262,7 +281,6 @@ public class TestCall {
System.out.println(item.getName()+ " "+item.getPath());
}
@Test
public void createFolder() throws Exception{
long start= System.currentTimeMillis();
ItemManagerClient itemclient = AbstractPlugin.item().build();
@ -275,7 +293,6 @@ public class TestCall {
System.out.println("creating folder took total "+(System.currentTimeMillis()-start));
}
@Test
public void share() throws Exception {
ItemManagerClient itemclient = AbstractPlugin.item().build();
itemclient.shareFolder("4fd4a4ca-c615-4076-8eaa-70268e4f6166", new HashSet<>(Arrays.asList("francesco.mangiacrapa","massimiliano.assante","giancarlo.panichi")), AccessType.WRITE_OWNER);
@ -324,8 +341,8 @@ public class TestCall {
boolean b = m.find();
System.out.println("result: "+!b);
}
/* private InputStream getThumbnailAsPng(ImagePlus img, int thumbWidth,
int thumbHeight) throws IOException {