Compare commits

...

5 Commits

Author SHA1 Message Date
roberto cirillo c6819e84a5 Added new constructor in order to specify the Backend as input
parameter;
upgraded to version: 3.0.0-SNAPSHOT
2021-01-08 17:06:34 +01:00
roberto cirillo 3fc8eda0a5 retrieve new parameters from ServiceEndpoint: backendToken, region 2020-12-22 17:59:21 +01:00
roberto cirillo bd082de4f2 added feature for retrieving a generic credential from a backend that is
not mongodb
2020-11-19 15:12:56 +01:00
roberto cirillo a6c9be2eaf added description in changelo, created new constructor on Configuration
class
2020-11-13 12:57:53 +01:00
roberto cirillo 055827e3f2 update to version 2.6.0-SNAPSHOT, added new input param backendType 2020-11-12 18:14:30 +01:00
9 changed files with 310 additions and 89 deletions

View File

@ -1,5 +1,10 @@
# Changelog for storage-manager-wrapper # Changelog for storage-manager-wrapper
## [v2.6.0-SNAPSHOT] 2020-11-12
* adding new constructor with the backendType as input parameter
* retrieving specific backend credentials if a specific backend si specified as input parameter
## [v2.5.3] 2019-03-20 ## [v2.5.3] 2019-03-20
* Added wrapper for HomeLibrary configuration related to the new preproduction infrastructure * Added wrapper for HomeLibrary configuration related to the new preproduction infrastructure

View File

@ -8,7 +8,7 @@
</parent> </parent>
<groupId>org.gcube.contentmanagement</groupId> <groupId>org.gcube.contentmanagement</groupId>
<artifactId>storage-manager-wrapper</artifactId> <artifactId>storage-manager-wrapper</artifactId>
<version>2.5.3</version> <version>3.0.0-SNAPSHOT</version>
<scm> <scm>
<connection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</connection> <connection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</connection>
<developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection> <developerConnection>scm:git:https://code-repo.d4science.org/gCubeSystem/${project.artifactId}.git</developerConnection>

View File

@ -1,15 +0,0 @@
log4j.rootLogger=DEBUG, A1, stdout
log4j.appender.A1=org.apache.log4j.RollingFileAppender
log4j.appender.A1.File=log.txt
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
# ***** Max file size is set to 100KB
log4j.appender.A1.MaxFileSize=100MB
# ***** Keep one backup file
log4j.appender.A1.MaxBackupIndex=1
#CONSOLE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%t] %-5p %c %d{dd MMM yyyy ;HH:mm:ss.SSS} - %m%n

View File

@ -0,0 +1,15 @@
package org.gcube.contentmanager.storageclient.wrapper;
public enum BackendType {
MongoDB, S3;
@Override
public String toString() {
switch(this) {
case MongoDB: return "MongoDB";
case S3: return "S3";
default: throw new IllegalArgumentException();
}
}
}

View File

@ -3,6 +3,7 @@ package org.gcube.contentmanager.storageclient.wrapper;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.TreeSet; import java.util.TreeSet;
import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint;
@ -28,6 +29,7 @@ public class Configuration {
private String sn; private String sn;
private String user; private String user;
private String password; private String password;
// if the backend is mongodb, this field is used for crypt/decrypt. If the backend is S3, this field is a token.
private String passPhrase; private String passPhrase;
private ISClientConnector isclient; private ISClientConnector isclient;
// the scope used for discovering the runtimeResource // the scope used for discovering the runtimeResource
@ -36,12 +38,17 @@ public class Configuration {
private String[] volatileHost; private String[] volatileHost;
private String[] persistentHosts; private String[] persistentHosts;
private String resolverHost; private String resolverHost;
private String region;
private static final Logger logger = LoggerFactory.getLogger(Configuration.class); private static final Logger logger = LoggerFactory.getLogger(Configuration.class);
private static final String DEFAULT_BACKEND_TYPE="MongoDB"; protected static final String DEFAULT_BACKEND_TYPE="MongoDB";
private static final String WRITE_CONCERN_PROPERTY_NAME="write_concern"; private static final String WRITE_CONCERN_PROPERTY_NAME="write_concern";
private static final String READ_PREFERENCE_PROPERTY_NAME="read_preference"; private static final String READ_PREFERENCE_PROPERTY_NAME="read_preference";
private static final String DB_PROPERTY_NAME="collection"; private static final String DB_PROPERTY_NAME="collection";
private static final String DB_STRING_SEPARATOR=";"; private static final String DB_STRING_SEPARATOR=";";
private static final String PASS_PHRASE_FIELD="PassPhrase";
private static final String TOKEN_FIELD="token";
/** /**
* home library need a special configuration * home library need a special configuration
*/ */
@ -73,34 +80,49 @@ public class Configuration {
setScopeString(scopeString); setScopeString(scopeString);
} }
/**
* Loads all the configuration parameters in a java object for a specific backend
*
* @param sc: service class of the remote resource
* @param sn service name of the remote resource
* @param scopeString string that identifies the scope
* @param owner user of the client library
* @param clientID
* @param accessType indicates the type of access to the storage
* @param memory indicates the type of memory used by the storage: Persistent or Volatile
*
*/
public Configuration(String sc, String sn, String scopeString, String owner, String clientID, String accessType, String memory, String backend){
this.sc=sc;
this.sn=sn;
this.owner=owner;
this.clientID=clientID;
this.typeAccess=accessType;
this.memoryType=memory;
setScopeString(scopeString);
setBackendType(backend);
}
/** /**
* Retrieve a valid configuration from IS for instantiating the engine * Retrieve a valid configuration from IS for instantiating the engine
*/ */
public void getConfiguration(){ public void getConfiguration(){
String[] newServer=null; String[] newServer=null;
// ISClientConnector isclient=getISClient();
String currentScope=ScopeProvider.instance.get(); String currentScope=ScopeProvider.instance.get();
logger.debug("Scope found on ScopeProvider instance is "+currentScope); logger.debug("Scope found on ScopeProvider instance is "+currentScope);
/*COMMENTED THE FOLLOWING LINES 20181214*/
if(RRScope == null){ if(RRScope == null){
// if(new ScopeBean(currentScope).is(Type.VRE)){
// logger.debug("If ScopeProvider scope is VRE scope RR scope became VO scope");
// RRScope=new ScopeBean(currentScope).enclosingScope().toString();
// }else{
// logger.debug("If ScopeProvider scope is not a VRE scope RR scope is ScopeProvider scope");
RRScope=currentScope; RRScope=currentScope;
// }
} }
/*END*/
logger.debug("RuntimeResource scope "+RRScope); logger.debug("RuntimeResource scope "+RRScope);
ServiceEndpoint resource=getISClient().getStorageEndpoint(RRScope); //if a specific backend is not passed as input param then take the default one
if (Objects.isNull(getBackendType()))
setBackendType(DEFAULT_BACKEND_TYPE);
ServiceEndpoint resource=getISClient(getBackendType()).getStorageEndpoint(RRScope);
if(resource ==null ) if(resource ==null )
throw new IllegalStateException("the storage resource is not present on IS in scope: "+RRScope); throw new IllegalStateException("the storage resource is not present on IS in scope: "+RRScope);
List<ServiceEndpoint> resolverResource =getISClient().getServiceEndpoint(Utils.URI_RESOLVER_RESOURCE_CATEGORY, Utils.URI_RESOLVER_RESOURCE_NAME); List<ServiceEndpoint> resolverResource =getISClient(getBackendType()).getServiceEndpoint(Utils.URI_RESOLVER_RESOURCE_CATEGORY, Utils.URI_RESOLVER_RESOURCE_NAME);
if(resolverResource !=null && resolverResource.size()> 0) if(resolverResource !=null && resolverResource.size()> 0)
setResolverHost(getISClient().getResolverHost(resolverResource.get(0))); setResolverHost(getISClient(getBackendType()).getResolverHost(resolverResource.get(0)));
else else
throw new IllegalStateException("the uri resolver resource is not present on IS in scope: "+currentScope); throw new IllegalStateException("the uri resolver resource is not present on IS in scope: "+currentScope);
// old method for retrieve hostedOn field in storage ServiceEndpoint resource // old method for retrieve hostedOn field in storage ServiceEndpoint resource
@ -108,13 +130,17 @@ public class Configuration {
logger.debug("server not set. Try to query IS in scope: "+scopeString); logger.debug("server not set. Try to query IS in scope: "+scopeString);
String[] serverFound=checkVarEnvMongo(); String[] serverFound=checkVarEnvMongo();
if(serverFound==null){ if(serverFound==null){
serverFound=getISClient().retrieveConnectionInfo(resource); serverFound=getISClient(getBackendType()).retrieveConnectionInfo(resource);
setUser(getISClient().getUsername()); setUser(getISClient(getBackendType()).getUsername());
setPassword(getISClient().password); setPassword(getISClient(getBackendType()).password);
setBackendType(getISClient().getBackendType(resource)); setRegion(getISClient(getBackendType()).getRegion());
if (Objects.isNull(getBackendType()))
setBackendType(getISClient(getBackendType()).getBackendType(resource));
}else{ }else{
setBackendType(checkVarEnvBackendType()); if (!Objects.isNull(getBackendType()))
if(getBackendType() == null) setBackendType(DEFAULT_BACKEND_TYPE); setBackendType(checkVarEnvBackendType());
else
setBackendType(DEFAULT_BACKEND_TYPE);
setUser(checkVarEnvUser()); setUser(checkVarEnvUser());
setPassword(checkVarEnvPassword()); setPassword(checkVarEnvPassword());
} }
@ -128,15 +154,46 @@ public class Configuration {
if(newServer==null){ if(newServer==null){
throw new IllegalStateException("Resource not found on Information System"); throw new IllegalStateException("Resource not found on Information System");
}else{ }else{
setEnvironment(setAreaStorage(getSc(), getSn())); setEnvironment(setAreaStorage(getSc(), getSn()));
setServerHosts(newServer, isclient, resource); // if the backend is mongodb we should set a separated server for volatile area
try { if(getBackendType().equals("MongoDB")) {
setPassPhrase(retrieveEncryptionPhrase()); setServerHosts(newServer, isclient, resource);
} catch (Exception e) { try {
e.printStackTrace(); setPassPhrase(retrieveEncryptionField(PASS_PHRASE_FIELD));
} catch (Exception e) {
e.printStackTrace();
}
}else {
// if the backend isn't mongodb, the passPhrase field will be filled with a token if it is present on ServiceEndpoint
try {
setPassPhrase(retrieveEncryptionField(TOKEN_FIELD));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
setPersistentHosts(newServer);
} }
} }
logger.debug("Configuration parameters retrieved: host: "+getPersistentHosts()+" user: "+getUser());
} }
// /**
// * Retrieve a valid configuration from IS for instantiating the engine
// */
// public void getConfiguration2(){
// String currentScope=ScopeProvider.instance.get();
// ServiceEndpoint resource=getISClient().getStorageEndpoint(currentScope);
// if((getMemoryType() != null) && ((getMemoryType().equals(MemoryType.VOLATILE.toString()) || (getMemoryType().equals(MemoryType.BOTH.toString()))))){
// setVolatileHost(isclient.getVolatileHost(resource));
// }
// }
//
private void setServerHosts(String[] newServer, ISClientConnector isclient, ServiceEndpoint resource) { private void setServerHosts(String[] newServer, ISClientConnector isclient, ServiceEndpoint resource) {
if((getMemoryType() != null) && ((getMemoryType().equals(MemoryType.VOLATILE.toString()) || (getMemoryType().equals(MemoryType.BOTH.toString()))))){ if((getMemoryType() != null) && ((getMemoryType().equals(MemoryType.VOLATILE.toString()) || (getMemoryType().equals(MemoryType.BOTH.toString()))))){
@ -192,11 +249,11 @@ public class Configuration {
String dbString=null; String dbString=null;
// check optional properties only if it is not a volatile storage instance // check optional properties only if it is not a volatile storage instance
if((getMemoryType() != null) && (!(getMemoryType().equals(MemoryType.VOLATILE.toString())))){ if((getMemoryType() != null) && (!(getMemoryType().equals(MemoryType.VOLATILE.toString())))){
write=getISClient().retrievePropertyValue(WRITE_CONCERN_PROPERTY_NAME, currentScope); write=getISClient(getBackendType()).retrievePropertyValue(WRITE_CONCERN_PROPERTY_NAME, currentScope);
logger.debug("read preference: read from service endpoint"); logger.debug("read preference: read from service endpoint");
read=getISClient().retrievePropertyValue(READ_PREFERENCE_PROPERTY_NAME, currentScope); read=getISClient(getBackendType()).retrievePropertyValue(READ_PREFERENCE_PROPERTY_NAME, currentScope);
logger.debug(" write preference: read from service endpoint"); logger.debug(" write preference: read from service endpoint");
dbString=getISClient().retrievePropertyValue(DB_PROPERTY_NAME, currentScope); dbString=getISClient(getBackendType()).retrievePropertyValue(DB_PROPERTY_NAME, currentScope);
if((write!=null) && (read!=null)){ if((write!=null) && (read!=null)){
engine.setWriteConcern(write); engine.setWriteConcern(write);
engine.setReadConcern(read); engine.setReadConcern(read);
@ -210,11 +267,12 @@ public class Configuration {
} }
if (dbNames.length > 1) if (dbNames.length > 1)
logger.debug("multiple collection discovered"); logger.debug("multiple collection discovered");
engine.setDbNames(dbNames); if (Objects.isNull(engine.getDbNames()))
engine.setDbNames(dbNames);
} }
// added db check also on volatile are // added db check also on volatile are
}else if((getMemoryType().equals(MemoryType.VOLATILE.toString()))){ }else if((getMemoryType().equals(MemoryType.VOLATILE.toString()))){
dbString=getISClient().retrievePropertyValue(DB_PROPERTY_NAME, currentScope); dbString=getISClient(getBackendType()).retrievePropertyValue(DB_PROPERTY_NAME, currentScope);
if(dbString!=null){ if(dbString!=null){
if (dbString.contains(DB_STRING_SEPARATOR)){ if (dbString.contains(DB_STRING_SEPARATOR)){
logger.debug("more than one collection read from ServiceEnpoint"); logger.debug("more than one collection read from ServiceEnpoint");
@ -326,19 +384,31 @@ public class Configuration {
return null; return null;
} }
@Deprecated
public String retrieveEncryptionPhrase() throws Exception { public String retrieveEncryptionPhrase() throws Exception {
String currentScope=ScopeProvider.instance.get(); String currentScope=ScopeProvider.instance.get();
logger.debug("retrieve encryption prhase on scope: "+currentScope); logger.debug("retrieve encryption prhase on scope: "+currentScope);
String encryptedKey=null; String encryptedKey=null;
// ISClientConnector isclient=getISClient(); // ISClientConnector isclient=getISClient();
logger.info("retrieve encryption phrase from scope "+currentScope); logger.info("retrieve encryption phrase from scope "+currentScope);
encryptedKey=getISClient().retrievePropertyValue("PassPhrase", currentScope); encryptedKey=getISClient(getBackendType()).retrievePropertyValue("PassPhrase", currentScope);
logger.info("encrypted prhase is "+encryptedKey); logger.info("encrypted prhase is "+encryptedKey);
String decryptString=org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedKey); String decryptString=org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedKey);
return decryptString; return decryptString;
} }
public String retrieveEncryptionField(String fieldName) throws Exception {
String currentScope=ScopeProvider.instance.get();
logger.debug("retrieve encryption prhase on scope: "+currentScope);
String encryptedKey=null;
// ISClientConnector isclient=getISClient();
logger.info("retrieve encryption phrase from scope "+currentScope);
encryptedKey=getISClient(getBackendType()).retrievePropertyValue(fieldName, currentScope);
logger.info("encrypted prhase is "+encryptedKey);
String decryptString=(!Objects.isNull(encryptedKey))?org.gcube.common.encryption.StringEncrypter.getEncrypter().decrypt(encryptedKey):null;
return decryptString;
}
public String getClientID() { public String getClientID() {
return clientID; return clientID;
@ -460,10 +530,14 @@ public class Configuration {
this.resolverHost = resolverHost; this.resolverHost = resolverHost;
} }
public ISClientConnector getISClient(){ private ISClientConnector getISClient(String backendType){
if (isclient == null) // if ((isclient == null) || (isclient.getBackendType().equals(backendType)))
isclient=new ISClientConnector(); // isclient=new ISClientConnector(backendType);
return isclient; //
// return isclient;
return isclient=((isclient == null) || (!isclient.getBackendType().equals(backendType)))?new ISClientConnector(backendType):isclient;
} }
public String getSc() { public String getSc() {
@ -481,5 +555,13 @@ public class Configuration {
public void setSn(String sn) { public void setSn(String sn) {
this.sn = sn; this.sn = sn;
} }
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
} }

View File

@ -8,8 +8,11 @@ import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import org.gcube.common.encryption.StringEncrypter; import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint; import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.contentmanager.storageclient.wrapper.BackendType;
import org.gcube.common.resources.gcore.ServiceEndpoint; import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property; import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
@ -34,8 +37,13 @@ public class ISClientConnector {
protected String username; protected String username;
protected String password; protected String password;
protected ServiceEndpoint storageResource; protected ServiceEndpoint storageResource;
public String region;
private static HashMap<String, Object> isCache; private static HashMap<String, Object> isCache;
public ISClientConnector(String backendType){
setBackendType(backendType);
}
public ISClientConnector(){ public ISClientConnector(){
} }
@ -84,39 +92,76 @@ public class ISClientConnector {
private String[] fillConnectionFields(ServiceEndpoint resource) { private String[] fillConnectionFields(ServiceEndpoint resource) {
logger.debug("fillConnectionsFields method ");
if(resource!=null){ if(resource!=null){
String [] server=new String[resource.profile().accessPoints().size()]; logger.debug("checking resource "+resource.id());
int i=0; ArrayList<String> server=new ArrayList<String> ();//new String[resource.profile().accessPoints().size()];
for (AccessPoint ap:resource.profile().accessPoints()) { if(!(getBackendType().equals(BackendType.MongoDB.name()))) {
if (ap.name().equals("server"+(i+1))) { logger.info("searching backend: "+getBackendType());
server[i] = ap.address(); for (AccessPoint ap:resource.profile().accessPoints()) {
// if presents, try to get user and password if( (ap.name().equals(getBackendType()))){
setUsername(ap.username()); //in v1.6.0 we are considering only one accessPoint for a backend except for mongodb.
// set password default value to empty string server.add( ap.address());
setPassword(""); // if presents, try to get user and password
if(getUsername() != null && getUsername().length() > 0){ setUsername(ap.username());
try { decryptPwd(ap);
setPassword(StringEncrypter.getEncrypter().decrypt(ap.password())); setRegion(retrievePropertyValue(ap, "region"));
} catch (Exception e) { break;
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
i++;
} }
}else {
int i=0;
for (AccessPoint ap:resource.profile().accessPoints()) {
if (ap.name().equals("server"+(i+1))) {
server.add(ap.address());
setUsername(ap.username());
setPassword("");
if(getUsername() != null && getUsername().length() > 0){
try {
setPassword(StringEncrypter.getEncrypter().decrypt(ap.password()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
i++;
}
}
logger.info("credentials Mongodb: "+getUsername()+" "+getPassword());
// setBackendType(retrievePropertyValue(resource, "type"));
} }
setBackendType(retrievePropertyValue(resource, "type"));
String [] volatileHost= new String [1]; String [] volatileHost= new String [1];
volatileHost[0]=retrievePropertyValue(resource, "volatile"); volatileHost[0]=retrievePropertyValue(resource, "volatile");
setVolatileHost(volatileHost); setVolatileHost(volatileHost);
logger.info("Type of backend found in RR is "+backendType); logger.info("Type of backend found in RR is "+backendType);
return server; String [] servers= new String[server.size()];
servers=server.toArray(servers);
return servers;
}else{ }else{
throw new IllegalStateException("Runtime Resource found are more than 1 but all without default priority setted"); throw new IllegalStateException("Runtime Resource found are more than 1 but all without default priority setted");
} }
} }
private void decryptPwd(AccessPoint ap) {
// set password default value to empty string
setPassword("");
if(getUsername() != null && getUsername().length() > 0){
try {
setPassword(StringEncrypter.getEncrypter().decrypt(ap.password()));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private ServiceEndpoint getPriorityResource(List<ServiceEndpoint> resources) { private ServiceEndpoint getPriorityResource(List<ServiceEndpoint> resources) {
ServiceEndpoint defaultResource=null; ServiceEndpoint defaultResource=null;
logger.info("search RR with priority "); logger.info("search RR with priority ");
@ -164,6 +209,17 @@ public class ISClientConnector {
} }
return value; return value;
} }
public String retrievePropertyValue(AccessPoint ap, String name) {
String value=null;
Map<String, Property>map= ap.propertyMap();
Property type=map.get(name);
if(type!=null){
value=type.value();
}
return value;
}
private String retrievePropertyValue(ServiceEndpoint res, String name) { private String retrievePropertyValue(ServiceEndpoint res, String name) {
Iterator<AccessPoint> it= res.profile().accessPoints().iterator(); Iterator<AccessPoint> it= res.profile().accessPoints().iterator();
@ -193,7 +249,7 @@ public class ISClientConnector {
return backendType; return backendType;
} }
public void setBackendType(String backendType) { private void setBackendType(String backendType) {
this.backendType = backendType; this.backendType = backendType;
} }
@ -229,6 +285,14 @@ public class ISClientConnector {
this.storageResource = storageResource; this.storageResource = storageResource;
} }
private void setRegion(String region) {
this.region=region;
}
public String getRegion() {
return region;
}
} }

View File

@ -1,9 +1,12 @@
package org.gcube.contentmanager.storageclient.wrapper; package org.gcube.contentmanager.storageclient.wrapper;
//import org.gcube.contentmanagement.blobstorage.resource.AccessType; import org.gcube.contentmanager.storageclient.wrapper.BackendType;
import org.gcube.contentmanager.storageclient.wrapper.AccessType; import org.gcube.contentmanager.storageclient.wrapper.AccessType;
//import org.gcube.contentmanagement.blobstorage.resource.MemoryType; //import org.gcube.contentmanagement.blobstorage.resource.MemoryType;
import org.gcube.contentmanager.storageclient.wrapper.MemoryType; import org.gcube.contentmanager.storageclient.wrapper.MemoryType;
import java.util.Objects;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanagement.blobstorage.service.IClient; import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanagement.blobstorage.service.impl.ServiceEngine; import org.gcube.contentmanagement.blobstorage.service.impl.ServiceEngine;
@ -96,6 +99,34 @@ public class StorageClient {
} }
/**
* New constructor with another optional argument created for gcube infrastructure internal use.
* Available in v1.6.0
* It's possible to specify a specific backend.
* @param ServiceClass
* @param ServiceName
* @param owner
* @param typeAccess
* @param scope
*/
public StorageClient(String serviceClass, String serviceName, String owner, AccessType accessType, BackendType backend){
checkScopeProvider();
String id=owner;
this.owner=owner;
this.scopeString=ScopeProvider.instance.get();
if(accessType!=null)
this.typeAccess=accessType;
else throw new RuntimeException("AccessType parameter must be not null");
this.memoryType=MemoryType.BOTH;
this.serviceClass=serviceClass;
this.serviceName=serviceName;
this.setBackendType(backend.toString());
setClientId(serviceClass, serviceName, id);
}
/** /**
* Constructor created for external use * Constructor created for external use
* @param owner * @param owner
@ -170,6 +201,35 @@ public class StorageClient {
this.serviceName=serviceName; this.serviceName=serviceName;
setClientId(serviceClass, serviceName, id); setClientId(serviceClass, serviceName, id);
} }
/**
* Available in v1.6.0
* It's possible to specify a specific backend.
* @param ServiceClass
* @param ServiceName
* @param owner
* @param typeAccess
* @param backend: specify mongodb (default) or s3
* @param memory defines the kind of memory: VOLATILE or PERSISTENT
* @param scope
*/
public StorageClient(String serviceClass, String serviceName, String owner, AccessType accessType, MemoryType memory, BackendType backend){
checkScopeProvider();
String id=owner;
this.owner=owner;
this.scopeString=ScopeProvider.instance.get();
if(accessType!=null)
this.typeAccess=accessType;
else throw new RuntimeException("AccessType parameter must be not null");
if(memory!=null)
this.memoryType=memory;
else throw new RuntimeException("MemoryType parameter must be not null");
this.serviceClass=serviceClass;
this.serviceName=serviceName;
setClientId(serviceClass, serviceName, id);
this.setBackendType(backend.toString());
}
@ -207,7 +267,11 @@ public class StorageClient {
* @throws IllegalStateException if the resource is not found on the IS * @throws IllegalStateException if the resource is not found on the IS
*/ */
public IClient getClient(){ public IClient getClient(){
Configuration cfg= new Configuration(serviceClass, serviceName, scopeString, owner, clientID, typeAccess.toString(), memoryType.toString()); Configuration cfg=null;
if(Objects.isNull(getBackendType()))
cfg= new Configuration(serviceClass, serviceName, scopeString, owner, clientID, typeAccess.toString(), memoryType.toString());
else
cfg= new Configuration(serviceClass, serviceName, scopeString, owner, clientID, typeAccess.toString(), memoryType.toString(), getBackendType());
cfg.getConfiguration(); cfg.getConfiguration();
ServiceEngine engine= new ServiceEngine(cfg.getPersistentHosts(), cfg.getVolatileHost(), cfg.getEnvironment(), cfg.getTypeAccess(), cfg.getOwner(), cfg.getMemoryType()); ServiceEngine engine= new ServiceEngine(cfg.getPersistentHosts(), cfg.getVolatileHost(), cfg.getEnvironment(), cfg.getTypeAccess(), cfg.getOwner(), cfg.getMemoryType());
// set additional fields for the new engine object // set additional fields for the new engine object
@ -218,8 +282,11 @@ public class StorageClient {
engine.setBackendUser(cfg.getUser()); engine.setBackendUser(cfg.getUser());
engine.setBackendPassword(cfg.getPassword()); engine.setBackendPassword(cfg.getPassword());
engine.setResolverHost(cfg.getResolverHost()); engine.setResolverHost(cfg.getResolverHost());
if(cfg.getPassPhrase()!=null) engine.setRegion(cfg.getRegion());
if(cfg.getPassPhrase()!=null) {
engine.setPassPhrase(cfg.getPassPhrase()); engine.setPassPhrase(cfg.getPassPhrase());
engine.setToken(cfg.getPassPhrase());
}
if(getMemoryType() !=null) if(getMemoryType() !=null)
engine.setGcubeMemoryType(getMemoryType().toString()); engine.setGcubeMemoryType(getMemoryType().toString());
engine.setGcubeScope(ScopeProvider.instance.get()); engine.setGcubeScope(ScopeProvider.instance.get());
@ -370,6 +437,9 @@ public class StorageClient {
public String getBackendType() { public String getBackendType() {
if(Objects.isNull(backendType))
backendType=Configuration.DEFAULT_BACKEND_TYPE;
logger.debug("backend type returned is "+backendType);
return backendType; return backendType;
} }

View File

@ -6,7 +6,7 @@ import java.io.File;
import java.util.List; import java.util.List;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanagement.blobstorage.resource.MyFile; import org.gcube.contentmanagement.blobstorage.resource.RequestObject;
import org.gcube.contentmanagement.blobstorage.resource.StorageObject; import org.gcube.contentmanagement.blobstorage.resource.StorageObject;
import org.gcube.contentmanagement.blobstorage.service.IClient; import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
@ -53,7 +53,7 @@ public class GetMetaFileTest {
@Test @Test
public void getMetaFileByPath() throws RemoteBackendException { public void getMetaFileByPath() throws RemoteBackendException {
MyFile f= client.getMetaFile().RFile(remotePath); RequestObject f= client.getMetaFile().RFile(remotePath);
// System.out.println("mime is: "+f.getMimeType()); // System.out.println("mime is: "+f.getMimeType());
client.put(true, "image/png").LFile("src/test/resources/dog.jpg").RFile(remotePath); client.put(true, "image/png").LFile("src/test/resources/dog.jpg").RFile(remotePath);
f= client.getMetaFile().RFile(remotePath); f= client.getMetaFile().RFile(remotePath);
@ -70,7 +70,7 @@ public class GetMetaFileTest {
@Test @Test
public void getMetaFileById() throws RemoteBackendException { public void getMetaFileById() throws RemoteBackendException {
MyFile f= client.getMetaFile().RFile(id); RequestObject f= client.getMetaFile().RFile(id);
assertNotNull(f); assertNotNull(f);
assertEquals(id, f.getId()); assertEquals(id, f.getId());
print(f); print(f);
@ -89,7 +89,7 @@ public class GetMetaFileTest {
assertTrue(list.isEmpty()); assertTrue(list.isEmpty());
} }
private void print(MyFile f) { private void print(RequestObject f) {
System.out.println("\t name "+f.getName()); System.out.println("\t name "+f.getName());
System.out.println("\t size "+f.getSize()); System.out.println("\t size "+f.getSize());
System.out.println("\t owner "+f.getOwner()); System.out.println("\t owner "+f.getOwner());

View File

@ -6,7 +6,7 @@ import java.io.File;
import java.util.List; import java.util.List;
import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.contentmanagement.blobstorage.resource.MyFile; import org.gcube.contentmanagement.blobstorage.resource.RequestObject;
import org.gcube.contentmanagement.blobstorage.resource.StorageObject; import org.gcube.contentmanagement.blobstorage.resource.StorageObject;
import org.gcube.contentmanagement.blobstorage.service.IClient; import org.gcube.contentmanagement.blobstorage.service.IClient;
import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException; import org.gcube.contentmanagement.blobstorage.transport.backend.RemoteBackendException;
@ -53,12 +53,12 @@ public class PropertiesTest {
public void getPropertyByPath() throws RemoteBackendException { public void getPropertyByPath() throws RemoteBackendException {
String valueFound= client.getMetaInfo(field).RFile(remotePath); String valueFound= client.getMetaInfo(field).RFile(remotePath);
System.out.println("value found for property: "+field+" is "+valueFound); System.out.println("value found for property: "+field+" is "+valueFound);
MyFile f= client.getMetaFile().RFile(remotePath); RequestObject f= client.getMetaFile().RFile(remotePath);
assertNotNull(f); assertNotNull(f);
print(f); print(f);
String result= client.setMetaInfo(field, value).RFile(remotePath); String result= client.setMetaInfo(field, value).RFile(remotePath);
System.out.println("new property set "); System.out.println("new property set ");
MyFile f1= client.getMetaFile().RFile(remotePath); RequestObject f1= client.getMetaFile().RFile(remotePath);
print(f1); print(f1);
assertNotNull(f1); assertNotNull(f1);
assertEquals(f1.getMimeType(), value); assertEquals(f1.getMimeType(), value);
@ -70,12 +70,12 @@ public class PropertiesTest {
public void getPropertyById() throws RemoteBackendException { public void getPropertyById() throws RemoteBackendException {
String valueFound= client.getMetaInfo(field).RFile(id); String valueFound= client.getMetaInfo(field).RFile(id);
System.out.println("value found for property: "+field+" is "+valueFound); System.out.println("value found for property: "+field+" is "+valueFound);
MyFile f= client.getMetaFile().RFile(id); RequestObject f= client.getMetaFile().RFile(id);
assertNotNull(f); assertNotNull(f);
print(f); print(f);
String result= client.setMetaInfo(field, value).RFile(id); String result= client.setMetaInfo(field, value).RFile(id);
System.out.println("new property set "); System.out.println("new property set ");
MyFile f1= client.getMetaFile().RFile(id); RequestObject f1= client.getMetaFile().RFile(id);
print(f1); print(f1);
assertNotNull(f1); assertNotNull(f1);
assertEquals(f1.getMimeType(), value); assertEquals(f1.getMimeType(), value);
@ -85,7 +85,7 @@ public class PropertiesTest {
// @Test // @Test
public void setProperty() throws RemoteBackendException { public void setProperty() throws RemoteBackendException {
String result= client.setMetaInfo(field, value).RFile(id); String result= client.setMetaInfo(field, value).RFile(id);
MyFile f= client.getMetaFile().RFile(remotePath); RequestObject f= client.getMetaFile().RFile(remotePath);
assertNotNull(f); assertNotNull(f);
print(f); print(f);
@ -99,7 +99,7 @@ public class PropertiesTest {
removeLocalFile(); removeLocalFile();
} }
private void print(MyFile f) { private void print(RequestObject f) {
System.out.println("\t name "+f.getName()); System.out.println("\t name "+f.getName());
System.out.println("\t size "+f.getSize()); System.out.println("\t size "+f.getSize());
System.out.println("\t owner "+f.getOwner()); System.out.println("\t owner "+f.getOwner());