This commit is contained in:
Fabio Sinibaldi 2016-05-11 17:09:06 +00:00
parent 7f6aaabe61
commit ea9bf13590
14 changed files with 595 additions and 124 deletions

View File

@ -2,6 +2,10 @@ package org.gcube.spatial.data.geonetwork;
public enum LoginLevel {
DEFAULT,SCOPE,PRIVATE
DEFAULT,
SCOPE,
PRIVATE,
CKAN,
ADMIN
}

View File

@ -1,15 +1,21 @@
package org.gcube.spatial.data.geonetwork.configuration;
import java.util.Map;
import org.gcube.spatial.data.geonetwork.LoginLevel;
import org.gcube.spatial.data.geonetwork.model.Account;
import org.gcube.spatial.data.geonetwork.model.ScopeConfiguration;
import org.gcube.spatial.data.geonetwork.model.faults.EncryptionException;
import org.gcube.spatial.data.geonetwork.model.faults.MissingConfigurationException;
public interface Configuration {
public String getGeoNetworkEndpoint();
public Map<LoginLevel,String> getGeoNetworkUsers();
public Map<LoginLevel,String> getGeoNetworkPasswords();
public int getScopeGroup();
/**
* Returns the current configuration. If not found acquire one.
*
* @return
* @throws MissingConfigurationException
* @throws EncryptionException
*/
public ScopeConfiguration getScopeConfiguration() throws MissingConfigurationException, EncryptionException;
public Account getAdminAccount();
public void createScopeConfiguration(ScopeConfiguration toCreate) throws EncryptionException;
}

View File

@ -5,7 +5,7 @@ public class ConfigurationManager {
private static Class<? extends Configuration> configClass=DefaultConfiguration.class;
public static synchronized Configuration get() throws Exception{
public static synchronized Configuration get() throws InstantiationException, IllegalAccessException{
return configClass.newInstance();
}
public static void setConfiguration(Class<? extends Configuration> configuration){configClass=configuration;}

View File

@ -4,124 +4,280 @@ import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.spatial.data.geonetwork.LoginLevel;
import org.gcube.spatial.data.geonetwork.model.Account;
import org.gcube.spatial.data.geonetwork.model.Account.Type;
import org.gcube.spatial.data.geonetwork.model.ScopeConfiguration;
import org.gcube.spatial.data.geonetwork.model.faults.EncryptionException;
import org.gcube.spatial.data.geonetwork.model.faults.MissingConfigurationException;
import org.gcube.spatial.data.geonetwork.utils.EncryptionUtils;
import org.gcube.spatial.data.geonetwork.utils.RuntimeParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.gcube.spatial.data.geonetwork.utils.ScopeUtils;
@Slf4j
public class DefaultConfiguration implements Configuration {
private static final Logger logger = LoggerFactory.getLogger(DefaultConfiguration.class);
private String url;
private Map<LoginLevel, String> users=new HashMap<LoginLevel, String>();
private Map<LoginLevel, String> pwds=new HashMap<LoginLevel, String>();
private int group;
public DefaultConfiguration() throws Exception{
Properties props=new RuntimeParameters().getProps();
SimpleQuery query = queryFor(ServiceEndpoint.class);
static{
log.debug("Loading properties");
try{
Properties props=new RuntimeParameters().getProps();
log.trace("LOADED PROPERTIES : "+props);
// per scope
assignedScopePrefix=props.getProperty("assignedScopePrefix");
scopeUserPrefix=props.getProperty("scopeUserPrefix");
scopePasswordPrefix=props.getProperty("scopePasswordPrefix");
ckanUserPrefix=props.getProperty("ckanUserPrefix");
ckanPasswordPrefix=props.getProperty("ckanPasswordPrefix");
defaultGroupPrefix=props.getProperty("defaultGroupPrefix");
privateGroupPrefix=props.getProperty("privateGroupPrefix");
publicGroupPrefix=props.getProperty("publicGroupPrefix");
query.addCondition("$resource/Profile/Category/text() eq '"+props.getProperty("geonetwork.category")+"'")
.addCondition("$resource/Profile/Platform/Name/text() eq '"+props.getProperty("geonetwork.platform.name")+"'")
.setResult("$resource/Profile/AccessPoint");
// global
availableGroupSuffixList=props.getProperty("availableGroupSuffixList");
adminUser=props.getProperty("adminUser");
adminPassword=props.getProperty("adminPassword");
geonetworkCategory=props.getProperty("geonetwork.category");
geonetworkPlatformName=props.getProperty("geonetwork.platform.name");
endpointName=props.getProperty("geonetwork.endpoint.name");
masterProperty=props.getProperty("geonetwork.master");
}catch(Exception e){
log.error("Unable to load properties",e);
}
}
DiscoveryClient<AccessPoint> client = clientFor(AccessPoint.class);
//***************************** Properties are loaded at startup
// per scope
private static String assignedScopePrefix=null;
private static String scopeUserPrefix=null;
private static String scopePasswordPrefix=null;
private static String ckanUserPrefix=null;
private static String ckanPasswordPrefix=null;
private static String defaultGroupPrefix=null;
private static String privateGroupPrefix=null;
private static String publicGroupPrefix=null;
List<AccessPoint> accesspoints = client.submit(query);
// global
private static String availableGroupSuffixList=null;
private static String adminUser=null;
private static String adminPassword=null;
private static String geonetworkCategory=null;
private static String geonetworkPlatformName=null;
private static String endpointName=null;
private static String masterProperty=null;
String endpointName=props.getProperty("geonetwork.endpoint.name");
String masterProperty=props.getProperty("geonetwork.master");
String scopeName=ScopeProvider.instance.get().substring(ScopeProvider.instance.get().lastIndexOf('/')+1);
String scopeUser=scopeName+props.getProperty("geonetwork.scopeuser.password.suffix");
String privateUser=scopeName+props.getProperty("geonetwork.privateuser.password.suffix");
String scopeGroup=scopeName+props.getProperty("geonetwork.scopegroup.id");
//***************************** INSTANCE VALUES
private String geonetworkUrl=null;
private String adminUserValue=null;
private String adminPasswordValue=null;
private ScopeConfiguration configuration=null;
private boolean updated=false;
public DefaultConfiguration() throws EncryptionException, MissingConfigurationException{
}
/**
* loads configuration from Is
*
* @throws EncryptionException
* @throws MissingConfigurationException
*/
protected Map<String,Property> loadFromIs()throws EncryptionException, MissingConfigurationException{
log.debug("Querying IS for Geonetwork information..");
boolean found=false;
List<AccessPoint> accessPoints = doTheQuery(geonetworkCategory, geonetworkPlatformName);
for (AccessPoint point : accesspoints) {
log.debug("Found "+accessPoints.size()+" access points");
for (AccessPoint point : accessPoints) {
if(point.name().equals(endpointName)){
Map<String, Property> properties=point.propertyMap();
if(properties.containsKey(masterProperty)&&Boolean.parseBoolean(properties.get(masterProperty).value())){
url=point.address();
users.put(LoginLevel.DEFAULT, point.username());
pwds.put(LoginLevel.DEFAULT,decrypt(point.password()));
if(properties.containsKey(privateUser)){
users.put(LoginLevel.PRIVATE, privateUser);
pwds.put(LoginLevel.PRIVATE, decrypt(properties.get(privateUser).value()));
}
if(properties.containsKey(scopeUser)){
users.put(LoginLevel.SCOPE, scopeUser);
pwds.put(LoginLevel.SCOPE, decrypt(properties.get(scopeUser).value()));
}
if(properties.containsKey(scopeGroup))
group=Integer.parseInt(properties.get(scopeGroup).value());
found=true;
break;
geonetworkUrl=point.address();
log.debug("Found master endpoint @ "+point.address()+", loading configuration map");
adminUserValue=properties.get(adminUser).value();
adminPasswordValue=EncryptionUtils.decrypt(properties.get(adminPassword).value());
configuration=getByScope(fromMap(properties), ScopeUtils.getCurrentScopeName());
log.debug("Loaded config : "+configuration);
updated=true;
return properties;
// users=point.username();
// pwds=StringEncrypter.getEncrypter().decrypt(point.password());
}
}
}
if(!found) throw new Exception("No Resource found under current scope "+ScopeProvider.instance.get());
throw new MissingConfigurationException("No Resource found under current scope "+ScopeProvider.instance.get());
}
/**
* UPDATE existing values and store new ones
*
* @param updatedMap
* @return
* @throws MissingConfigurationException
* @throws EncryptionException
*/
protected Map<String,Property> storeAndReload(Map<String,Property> updatedMap) throws MissingConfigurationException,EncryptionException{
DO THE STORE
return loadFromIs();
}
protected List<AccessPoint> doTheQuery(String geonetworkCategory, String geonetworkPlatformName){
SimpleQuery query = queryFor(ServiceEndpoint.class);
query.addCondition("$resource/Profile/Category/text() eq '"+geonetworkCategory+"'")
.addCondition("$resource/Profile/Platform/Name/text() eq '"+geonetworkPlatformName+"'")
.setResult("$resource/Profile/AccessPoint");
DiscoveryClient<AccessPoint> client = clientFor(AccessPoint.class);
return client.submit(query);
}
protected static Set<ScopeConfiguration> fromMap(Map<String,Property> theMap) throws EncryptionException{
HashSet<ScopeConfiguration> toReturn=new HashSet<>();
String[] suffixes=theMap.get(availableGroupSuffixList).value().split(",");
System.out.println("Suffixes to use are "+suffixes);
for(String suff:suffixes)
try{
toReturn.add(getConfigurationBySuffix(theMap, suff));
}catch(Exception e){
log.warn("Invalid configuration "+suff,e);
}
return toReturn;
}
protected static ScopeConfiguration getConfigurationBySuffix(Map<String,Property> theMap,String suffix) throws EncryptionException{
HashMap<Account.Type,Account> accounts=new HashMap<>();
String scopeAccountName=theMap.get(scopeUserPrefix+suffix).value();
String scopeAccountPassword=EncryptionUtils.decrypt(theMap.get(scopePasswordPrefix+suffix).value());
accounts.put(Account.Type.SCOPE,new Account(scopeAccountName,scopeAccountPassword,Type.SCOPE));
String ckanAccountName=theMap.get(ckanUserPrefix+suffix).value();
String ckanPassword=EncryptionUtils.decrypt(theMap.get(ckanPasswordPrefix+suffix).value());
accounts.put(Type.CKAN, new Account(ckanAccountName,ckanPassword,Type.CKAN));
Integer publicGroup=Integer.parseInt(theMap.get(publicGroupPrefix+suffix).value());
Integer privateGroup=Integer.parseInt(theMap.get(privateGroupPrefix+suffix).value());
Integer defaultGroup=Integer.parseInt(theMap.get(defaultGroupPrefix+suffix).value());
String assignedScope=theMap.get(assignedScopePrefix+suffix).value();
return new ScopeConfiguration(assignedScope, publicGroup, privateGroup, accounts, defaultGroup);
}
protected static ScopeConfiguration getByScope(Set<ScopeConfiguration> existing, String scope) throws MissingConfigurationException{
for(ScopeConfiguration configuration : existing)
if(configuration.getAssignedScope().equals(scope)) return configuration;
throw new MissingConfigurationException("Scope "+scope+" has no configuration");
}
/**
* Looks for default user name among properties to determine used suffix
*
* @param toLookFor
* @param map
* @return
* @throws MissingConfigurationException
*/
public static String getSuffixByConfiguration(ScopeConfiguration toLookFor,Map<String,Property> map) throws MissingConfigurationException{
String defaultUser=toLookFor.getAccounts().get(Type.SCOPE).getUser();
for(Entry<String,Property> entry:map.entrySet())
if(entry.getValue().value().equals(defaultUser)) return entry.getKey().substring(scopeUserPrefix.length());
throw new MissingConfigurationException("Unable to find suffix for configuration "+toLookFor);
}
public static void acquireConfigurationInMap(ScopeConfiguration toAcquire, Map<String,Property> map, String scope) throws MissingConfigurationException{
String propertyName=assignedScopePrefix+getSuffixByConfiguration(toAcquire, map);
map.get(propertyName).nameAndValue(propertyName,scope);
}
//
// public static Map<String,Property> asMap(Set<ScopeConfiguration> configs,String toUseSuffix) throws Exception{
// Map<String,Property> toReturn=new HashMap<>();
// String suffixes="";
// int i=0;
// for(ScopeConfiguration config:configs){
//
// toReturn.put(assignedScopePrefix+i, new Property().nameAndValue(assignedScopePrefix+i, config.getAssignedScope()));
//
// Account scopeAccount=config.getAccounts().get(Account.Type.SCOPE);
// toReturn.put(scopeUserPrefix+i, new Property().nameAndValue(scopeUserPrefix+i, scopeAccount.getUser()));
// toReturn.put(scopePasswordPrefix+i, new Property().nameAndValue(scopePasswordPrefix+i, EncryptionUtils.encrypt(scopeAccount.getPassword())));
//
// Account ckanAccount=config.getAccounts().get(Account.Type.CKAN);
// toReturn.put(ckanUserPrefix+i,new Property().nameAndValue(ckanUserPrefix+i, ckanAccount.getUser()));
// toReturn.put(ckanPasswordPrefix+i,new Property().nameAndValue(ckanPasswordPrefix+i, EncryptionUtils.encrypt(ckanAccount.getPassword())));
//
// toReturn.put(defaultGroupPrefix+i,new Property().nameAndValue(defaultGroupPrefix+i, config.getDefaultGroup().toString()));
// toReturn.put(publicGroupPrefix+i,new Property().nameAndValue(publicGroupPrefix+i, config.getPublicGroup().toString()));
// toReturn.put(privateGroupPrefix+i,new Property().nameAndValue(privateGroupPrefix+i, config.getPrivateGroup().toString()));
//
// suffixes=suffixes+i+",";
// i++;
// }
// suffixes=suffixes.substring(0, suffixes.length()-1);
// toReturn.put(availableGroupSuffixList, new Property().nameAndValue(availableGroupSuffixList,suffixes));
// return toReturn;
// }
//**************************** INTERFACE IMPLEMENTATION
@Override
public Account getAdminAccount() {
return new Account(adminUserValue,adminPasswordValue,Type.SCOPE);
}
@Override
public String getGeoNetworkEndpoint() {
return url;
return geonetworkUrl;
}
@Override
public Map<LoginLevel, String> getGeoNetworkPasswords() {
return pwds;
}
@Override
public Map<LoginLevel, String> getGeoNetworkUsers() {
return users;
public ScopeConfiguration getScopeConfiguration()throws MissingConfigurationException, EncryptionException {
Map<String,Property> scopeProperties=null;
try{
if(!updated) scopeProperties=loadFromIs();
return configuration;
}catch(MissingConfigurationException e){
log.trace("Configuration not found, acquiring it.. ");
ScopeConfiguration available=getByScope(fromMap(scopeProperties),ScopeConfiguration.NOT_ASSIGNED);
acquireConfigurationInMap(available, scopeProperties, ScopeUtils.getCurrentScopeName());
storeAndReload(scopeProperties); // throws Exception if can't load configuration
}
return configuration;
}
@Override
public int getScopeGroup() {
return group;
public void createScopeConfiguration(ScopeConfiguration toCreate)
throws EncryptionException {
NB IDENTIFY SUFFIX
storeAndReload(asMap(toCreate));
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("DefaultConfiguration [url=");
builder.append(url);
builder.append(", users=");
builder.append(users);
builder.append(", pwds=");
builder.append(pwds);
builder.append("]");
return builder.toString();
}
private static final String decrypt(String toDecrypt) throws Exception{
return StringEncrypter.getEncrypter().decrypt(toDecrypt);
}
}

View File

@ -11,6 +11,11 @@ import lombok.Setter;
@AllArgsConstructor
public class Account {
public static enum Type{
CKAN,SCOPE
}
private String user;
private String password;
private Type type;
}

View File

@ -2,20 +2,29 @@ package org.gcube.spatial.data.geonetwork.model;
import java.util.Map;
import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.gcube.spatial.data.geonetwork.LoginLevel;
@Data
@Getter
@Setter
@EqualsAndHashCode
@AllArgsConstructor
@ToString
public class ScopeConfiguration {
public static String NOT_ASSIGNED;
public static String NOT_ASSIGNED="N/A";
private String assignedScope;
private Integer publicGroup;
private Integer privateGroup;
private Map<LoginLevel,Account> accounts;
private Map<Account.Type,Account> accounts;
private Integer defaultGroup;
public void addAccount(Account toAdd){
accounts.put(toAdd.getType(),toAdd);
}
}

View File

@ -0,0 +1,35 @@
package org.gcube.spatial.data.geonetwork.model.faults;
public class ConfigurationException extends GeoNetworkException {
/**
*
*/
private static final long serialVersionUID = -8652824274753741239L;
public ConfigurationException() {
// TODO Auto-generated constructor stub
}
public ConfigurationException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public ConfigurationException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public ConfigurationException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public ConfigurationException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -0,0 +1,35 @@
package org.gcube.spatial.data.geonetwork.model.faults;
public class EncryptionException extends GeoNetworkException {
/**
*
*/
private static final long serialVersionUID = 8029412117818590297L;
public EncryptionException() {
// TODO Auto-generated constructor stub
}
public EncryptionException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public EncryptionException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public EncryptionException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public EncryptionException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -0,0 +1,35 @@
package org.gcube.spatial.data.geonetwork.model.faults;
public class GeoNetworkException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1198171132587190575L;
public GeoNetworkException() {
// TODO Auto-generated constructor stub
}
public GeoNetworkException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public GeoNetworkException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public GeoNetworkException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public GeoNetworkException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -0,0 +1,35 @@
package org.gcube.spatial.data.geonetwork.model.faults;
public class MissingConfigurationException extends ConfigurationException {
/**
*
*/
private static final long serialVersionUID = 2645967308563239255L;
public MissingConfigurationException() {
// TODO Auto-generated constructor stub
}
public MissingConfigurationException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
public MissingConfigurationException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
}
public MissingConfigurationException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
}
public MissingConfigurationException(String message, Throwable cause,
boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
}
}

View File

@ -0,0 +1,25 @@
package org.gcube.spatial.data.geonetwork.utils;
import org.gcube.common.encryption.StringEncrypter;
import org.gcube.spatial.data.geonetwork.model.faults.EncryptionException;
public class EncryptionUtils {
public static final String decrypt(String toDecrypt) throws EncryptionException{
try{
return StringEncrypter.getEncrypter().decrypt(toDecrypt);
}catch(Exception e){
throw new EncryptionException(e);
}
}
public static final String encrypt(String toEncrypt) throws EncryptionException{
try{
return StringEncrypter.getEncrypter().encrypt(toEncrypt);
}catch(Exception e){
throw new EncryptionException(e);
}
}
}

View File

@ -0,0 +1,14 @@
package org.gcube.spatial.data.geonetwork.utils;
import org.gcube.common.scope.api.ScopeProvider;
public class ScopeUtils {
public static String getCurrentScope(){
return ScopeProvider.instance.get();
}
public static String getCurrentScopeName(){
return ScopeProvider.instance.get().substring(ScopeProvider.instance.get().lastIndexOf('/')+1);
}
}

View File

@ -1,10 +1,22 @@
#Query and Service Instance properties
geonetwork.category=Gis
geonetwork.platform.name=geonetwork
geonetwork.endpoint.name=geonetwork
geonetwork.master=master
geonetwork.scopeuser.password.suffix=_Public
geonetwork.privateuser.password.suffix=_Private
geonetwork.scopegroup.id=_group
availableGroupSuffixList=suffixes
adminUser=admin
adminPassword=adminPwd
# Per scope parameters
assignedScopePrefix=scope
scopeUserPrefix=scopeUser
scopePasswordPrefix=scopePwd
ckanUserPrefix=ckanUser
ckanPasswordPrefix=ckanPwd
defaultGroupPrefix=default
privateGroupPrefix=private
publicGroupPrefix=public
#ISO Metadata Constants

View File

@ -4,48 +4,148 @@ import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.spatial.data.geonetwork.GeoNetwork;
import org.gcube.spatial.data.geonetwork.GeoNetworkAdministration;
import org.gcube.spatial.data.geonetwork.LoginLevel;
import org.gcube.spatial.data.geonetwork.model.Account;
import org.gcube.spatial.data.geonetwork.model.Account.Type;
import org.gcube.spatial.data.geonetwork.model.ScopeConfiguration;
import org.gcube.spatial.data.geonetwork.utils.EncryptionUtils;
public class AdministrationTests {
private static final String defaultScope="/gcube/devsec";
//******************** CONFIGURATION
// per scope
private static final String assignedScopePrefix="assignedScope";
private static final String scopeUserPrefix="scopeUser";
private static final String scopePasswordPrefix="scopeUserPassword";
private static final String ckanUserPrefix="ckanUser";
private static final String ckanPasswordPrefix="ckanUserPassword";
private static final String defaultGroupPrefix="defaultGroup";
private static final String privateGroupPrefix="privateGroup";
private static final String publicGroupPrefix="publicGroup";
// global
private static final String availableGroupSuffixList="availableGroups";
private static final String adminUser="adminUser";
private static final String adminPassword="adminPassword";
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
ScopeProvider.instance.set(defaultScope);
System.out.println("Checking scope : "+defaultScope);
ScopeProvider.instance.set(defaultScope);
// System.out.println("Checking scope : "+defaultScope);
//
//
// GeoNetworkAdministration reader=GeoNetwork.get();
// reader.login(LoginLevel.DEFAULT);
// System.out.println(reader.getGroups());
GeoNetworkAdministration reader=GeoNetwork.get();
reader.login(LoginLevel.DEFAULT);
System.out.println(reader.getGroups());
// String path="/tmp/groups3727046726110219425.txt";
// System.out.println("Reading from path "+path);
// String content=readFile(path,Charset.defaultCharset());
// System.out.println("Content is ");
// System.out.println(content);
// System.out.println("************************************");
// System.out.println(GroupsUtils.parseGroupPage(content));
// String path="/tmp/groups3727046726110219425.txt";
// System.out.println("Reading from path "+path);
// String content=readFile(path,Charset.defaultCharset());
// System.out.println("Content is ");
// System.out.println(content);
// System.out.println("************************************");
// System.out.println(GroupsUtils.parseGroupPage(content));
Set<ScopeConfiguration> myConfigs=dummyConfigs();
Map<String,Property> propMap=asMap(myConfigs);
System.out.println(propMap);
System.out.println("Reforming..");
Set<ScopeConfiguration> parsed=fromMap(propMap);
System.out.println(asMap(parsed));
System.out.println("Equals : "+myConfigs.equals(parsed));
System.out.println("Acquiring new ..");
ScopeConfiguration unused=getUnused(myConfigs);
System.out.println("selected is : "+unused);
acquireConfigurationInMap(unused, propMap, "MY SCOPE");
System.out.println("New configuration set : ");
myConfigs=fromMap(propMap);
System.out.println(asMap(myConfigs));
System.out.println("Acquired : "+getByScope(myConfigs, "MY SCOPE"));
}
static String readFile(String path, Charset encoding)
throws IOException
{
private static Set<ScopeConfiguration> dummyConfigs(){
HashSet<ScopeConfiguration> toReturn=new HashSet<>();
for(int i=0;i<2;i++){
ScopeConfiguration config=new ScopeConfiguration(ScopeConfiguration.NOT_ASSIGNED,1+2*i,2*i,new HashMap<Account.Type,Account>(),1+2*i);
config.addAccount( new Account("CKAN_USR_"+i,"ckanPwd",Account.Type.CKAN));
config.addAccount( new Account("Scope_USR_"+i,"scopePwd",Account.Type.SCOPE));
toReturn.add(config);
}
ScopeConfiguration usedConfig=new ScopeConfiguration(ScopeConfiguration.NOT_ASSIGNED,115,112,new HashMap<Account.Type,Account>(),112);
usedConfig.addAccount( new Account("CKAN_USR_"+115,"ckanPwd",Account.Type.CKAN));
usedConfig.addAccount( new Account("Scope_USR_"+112,"scopePwd",Account.Type.SCOPE));
toReturn.add(usedConfig);
return toReturn;
}
public static ScopeConfiguration getUnused(Set<ScopeConfiguration> existing){
return getByScope(existing, ScopeConfiguration.NOT_ASSIGNED);
}
public static ScopeConfiguration getByScope(Set<ScopeConfiguration> existing, String scope){
for(ScopeConfiguration configuration : existing)
if(configuration.getAssignedScope().equals(scope)) return configuration;
return null;
}
static String readFile(String path, Charset encoding) throws IOException{
byte[] encoded = Files.readAllBytes(Paths.get(path));
return new String(encoded, encoding);
}
}
/******************************************
* String endpoint
* String adminUser
* String adminPwd
* *...scopeConfiguration
* -assignedScope
* -defaultUser
* -defaultUserPwd
* -ckanUser
* -ckanpwd
* -defaultGroup
* -privateGroup
* -publicGroup
*
*
*
*
*
* @param configs
* @return
* @throws Exception
*/
}