Added configuration on IS management
This commit is contained in:
parent
2edc222f6e
commit
c8e383ea3c
4
pom.xml
4
pom.xml
|
@ -51,6 +51,10 @@
|
||||||
<groupId>org.gcube.core</groupId>
|
<groupId>org.gcube.core</groupId>
|
||||||
<artifactId>common-scope</artifactId>
|
<artifactId>common-scope</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.gcube.core</groupId>
|
||||||
|
<artifactId>common-smartgears-app</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.gcube.data-catalogue</groupId>
|
<groupId>org.gcube.data-catalogue</groupId>
|
||||||
<artifactId>gcat-api</artifactId>
|
<artifactId>gcat-api</artifactId>
|
||||||
|
|
|
@ -6,12 +6,14 @@ import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import javax.ws.rs.InternalServerErrorException;
|
import javax.ws.rs.InternalServerErrorException;
|
||||||
import javax.ws.rs.WebApplicationException;
|
import javax.ws.rs.WebApplicationException;
|
||||||
|
|
||||||
|
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
|
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
|
||||||
|
@ -19,13 +21,22 @@ import org.gcube.common.encryption.encrypter.StringEncrypter;
|
||||||
import org.gcube.common.resources.gcore.GenericResource;
|
import org.gcube.common.resources.gcore.GenericResource;
|
||||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||||
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
||||||
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Profile;
|
||||||
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
||||||
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
|
||||||
|
import org.gcube.common.resources.gcore.common.Platform;
|
||||||
|
import org.gcube.common.resources.gcore.utils.Group;
|
||||||
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
|
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
|
||||||
import org.gcube.informationsystem.publisher.RegistryPublisher;
|
import org.gcube.informationsystem.publisher.RegistryPublisher;
|
||||||
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
||||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||||
|
import org.gcube.smartgears.ContextProvider;
|
||||||
|
import org.gcube.smartgears.configuration.application.ApplicationConfiguration;
|
||||||
|
import org.gcube.smartgears.configuration.container.ContainerConfiguration;
|
||||||
|
import org.gcube.smartgears.context.application.ApplicationContext;
|
||||||
|
import org.gcube.smartgears.context.container.ContainerContext;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -36,14 +47,16 @@ public class GCoreISConfigurationProxy {
|
||||||
// property to retrieve the master service endpoint into the /root scope
|
// property to retrieve the master service endpoint into the /root scope
|
||||||
private final static String IS_MASTER_ROOT_KEY_PROPERTY = "IS_ROOT_MASTER"; // true, false.. missing means false as
|
private final static String IS_MASTER_ROOT_KEY_PROPERTY = "IS_ROOT_MASTER"; // true, false.. missing means false as
|
||||||
|
|
||||||
|
private final static String DEFAULT_ORGANIZATION_PROPERTY = "DEFAULT_ORGANIZATION";
|
||||||
|
private final static String SUPPORTED_ORGANIZATION_PROPERTY = "SUPPORTED_ORGANIZATION";
|
||||||
private final static String API_KEY_PROPERTY = "API_KEY";
|
private final static String API_KEY_PROPERTY = "API_KEY";
|
||||||
private final static String SOCIAL_POST = "SOCIAL_POST";
|
private final static String SOCIAL_POST_PROPERTY = "SOCIAL_POST";
|
||||||
private final static String ALERT_USERS_ON_POST_CREATION = "ALERT_USERS_ON_POST_CREATION";
|
private final static String ALERT_USERS_ON_POST_CREATION_PROPERTY = "ALERT_USERS_ON_POST_CREATION";
|
||||||
private final static String MODERATION_ENABLED_KEY = "MODERATION_ENABLED";
|
private final static String MODERATION_ENABLED_KEY_PROPERTY = "MODERATION_ENABLED";
|
||||||
|
|
||||||
// CKAN Instance info
|
// CKAN Instance info
|
||||||
private final static String RUNTIME_CATALOGUE_RESOURCE_NAME = "CKanDataCatalogue";
|
private final static String CATEGORY = "Application";
|
||||||
private final static String PLATFORM_CATALOGUE_NAME = "Tomcat";
|
private final static String NAME = "CKanDataCatalogue";
|
||||||
|
|
||||||
protected final String context;
|
protected final String context;
|
||||||
protected CatalogueConfiguration catalogueConfiguration;
|
protected CatalogueConfiguration catalogueConfiguration;
|
||||||
|
@ -60,13 +73,22 @@ public class GCoreISConfigurationProxy {
|
||||||
|
|
||||||
public CatalogueConfiguration getCatalogueConfiguration() {
|
public CatalogueConfiguration getCatalogueConfiguration() {
|
||||||
if (catalogueConfiguration == null) {
|
if (catalogueConfiguration == null) {
|
||||||
loadFromIS();
|
getCatalogueConfigurationFromIS();
|
||||||
}
|
}
|
||||||
return catalogueConfiguration;
|
return catalogueConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected CatalogueConfiguration loadFromIS() {
|
protected AccessPoint getAccessPoint(Profile profile) {
|
||||||
|
Group<AccessPoint> accessPoints = profile.accessPoints();
|
||||||
|
Iterator<AccessPoint> accessPointIterator = accessPoints.iterator();
|
||||||
|
AccessPoint accessPoint = accessPointIterator.next();
|
||||||
|
return accessPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected CatalogueConfiguration getCatalogueConfigurationFromIS() {
|
||||||
try {
|
try {
|
||||||
|
boolean mustBeUpdated = false;
|
||||||
|
|
||||||
catalogueConfiguration = new CatalogueConfiguration(context);
|
catalogueConfiguration = new CatalogueConfiguration(context);
|
||||||
|
|
||||||
ServiceEndpoint serviceEndpoint = getServiceEndpoint();
|
ServiceEndpoint serviceEndpoint = getServiceEndpoint();
|
||||||
|
@ -76,53 +98,87 @@ public class GCoreISConfigurationProxy {
|
||||||
|
|
||||||
catalogueConfiguration.setID(serviceEndpoint.id());
|
catalogueConfiguration.setID(serviceEndpoint.id());
|
||||||
|
|
||||||
Iterator<AccessPoint> accessPointIterator = serviceEndpoint.profile().accessPoints().iterator();
|
Profile profile = serviceEndpoint.profile();
|
||||||
while (accessPointIterator.hasNext()) {
|
AccessPoint accessPoint = getAccessPoint(profile);
|
||||||
AccessPoint accessPoint = accessPointIterator.next();
|
|
||||||
|
// add this host
|
||||||
|
String ckanURL = accessPoint.address();
|
||||||
|
catalogueConfiguration.setCkanURL(ckanURL);
|
||||||
|
|
||||||
|
Map<String, Property> propertyMap = accessPoint.propertyMap();
|
||||||
|
|
||||||
|
// retrieve sys admin token
|
||||||
|
String sysAdminToken = propertyMap.get(API_KEY_PROPERTY).value();
|
||||||
|
sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken);
|
||||||
|
catalogueConfiguration.setSysAdminToken(sysAdminToken);
|
||||||
|
|
||||||
// add this host
|
|
||||||
String ckanURL = accessPoint.address();
|
String organization = CatalogueConfiguration.getOrganizationName(context);
|
||||||
catalogueConfiguration.setCkanURL(ckanURL);
|
if (propertyMap.containsKey(DEFAULT_ORGANIZATION_PROPERTY)) {
|
||||||
|
String org = propertyMap.get(DEFAULT_ORGANIZATION_PROPERTY).value().trim();
|
||||||
// retrieve sys admin token
|
if(org!=null && org.compareTo("")==0) {
|
||||||
String sysAdminToken = accessPoint.propertyMap().get(API_KEY_PROPERTY).value();
|
mustBeUpdated = true;
|
||||||
sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken);
|
}else {
|
||||||
catalogueConfiguration.setSysAdminToken(sysAdminToken);
|
organization = org;
|
||||||
|
|
||||||
// retrieve option to check if the social post has to be made
|
|
||||||
Boolean socialPostEnabled = true;
|
|
||||||
if (accessPoint.propertyMap().containsKey(SOCIAL_POST)) {
|
|
||||||
if (accessPoint.propertyMap().get(SOCIAL_POST).value().trim().equalsIgnoreCase("false")) {
|
|
||||||
socialPostEnabled = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catalogueConfiguration.setSocialPostEnabled(socialPostEnabled);
|
}else {
|
||||||
|
mustBeUpdated = true;
|
||||||
// retrieve option for user alert
|
|
||||||
boolean notificationToUsersEnabled = false; // default is false
|
|
||||||
if (accessPoint.propertyMap().containsKey(ALERT_USERS_ON_POST_CREATION)) {
|
|
||||||
if (accessPoint.propertyMap().get(ALERT_USERS_ON_POST_CREATION).value().trim()
|
|
||||||
.equalsIgnoreCase("true")) {
|
|
||||||
notificationToUsersEnabled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catalogueConfiguration.setNotificationToUsersEnabled(notificationToUsersEnabled);
|
|
||||||
|
|
||||||
boolean moderationEnabled = false; // default is false
|
|
||||||
if (accessPoint.propertyMap().containsKey(MODERATION_ENABLED_KEY)) {
|
|
||||||
if (accessPoint.propertyMap().get(MODERATION_ENABLED_KEY).value().trim().equalsIgnoreCase("true")) {
|
|
||||||
moderationEnabled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catalogueConfiguration.setModerationEnabled(moderationEnabled);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
catalogueConfiguration.setOrganization(organization);
|
||||||
|
|
||||||
|
|
||||||
|
// retrieve option to check if the social post has to be made
|
||||||
|
Boolean socialPostEnabled = true;
|
||||||
|
if (propertyMap.containsKey(SOCIAL_POST_PROPERTY)) {
|
||||||
|
if (propertyMap.get(SOCIAL_POST_PROPERTY).value().trim().equalsIgnoreCase("false")) {
|
||||||
|
socialPostEnabled = false;
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
mustBeUpdated = true;
|
||||||
|
}
|
||||||
|
catalogueConfiguration.setSocialPostEnabled(socialPostEnabled);
|
||||||
|
|
||||||
Set<String> supportedOrganizations = getSupportedOrganizationsFromIS();
|
// retrieve option for user alert
|
||||||
|
boolean notificationToUsersEnabled = false; // default is false
|
||||||
|
if (propertyMap.containsKey(ALERT_USERS_ON_POST_CREATION_PROPERTY)) {
|
||||||
|
if (propertyMap.get(ALERT_USERS_ON_POST_CREATION_PROPERTY).value().trim()
|
||||||
|
.equalsIgnoreCase("true")) {
|
||||||
|
notificationToUsersEnabled = true;
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
mustBeUpdated = true;
|
||||||
|
}
|
||||||
|
catalogueConfiguration.setNotificationToUsersEnabled(notificationToUsersEnabled);
|
||||||
|
|
||||||
|
boolean moderationEnabled = false; // default is false
|
||||||
|
if (propertyMap.containsKey(MODERATION_ENABLED_KEY_PROPERTY)) {
|
||||||
|
if (propertyMap.get(MODERATION_ENABLED_KEY_PROPERTY).value().trim().equalsIgnoreCase("true")) {
|
||||||
|
moderationEnabled = true;
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
mustBeUpdated = true;
|
||||||
|
}
|
||||||
|
catalogueConfiguration.setModerationEnabled(moderationEnabled);
|
||||||
|
|
||||||
|
Set<String> supportedOrganizations = null;
|
||||||
|
if (propertyMap.containsKey(SUPPORTED_ORGANIZATION_PROPERTY)) {
|
||||||
|
String jsonArray = propertyMap.get(SUPPORTED_ORGANIZATION_PROPERTY).value();
|
||||||
|
supportedOrganizations = unmarshallSupportedOrganizations(jsonArray);
|
||||||
|
removeAllGenericResources();
|
||||||
|
}else {
|
||||||
|
supportedOrganizations = getSupportedOrganizationsFromGenericResource();
|
||||||
|
mustBeUpdated = true;
|
||||||
|
}
|
||||||
if (supportedOrganizations != null) {
|
if (supportedOrganizations != null) {
|
||||||
catalogueConfiguration.setSupportedOrganizations(supportedOrganizations);
|
catalogueConfiguration.setSupportedOrganizations(supportedOrganizations);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(mustBeUpdated) {
|
||||||
|
// updateOnIS(serviceEndpoint);
|
||||||
|
logger.warn("The ServiceEndpoint with ID {} in context {} should be updated", serviceEndpoint.id(), context);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (WebApplicationException e) {
|
} catch (WebApplicationException e) {
|
||||||
throw e;
|
throw e;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -140,8 +196,9 @@ public class GCoreISConfigurationProxy {
|
||||||
*/
|
*/
|
||||||
private List<ServiceEndpoint> getServiceEndpoints() {
|
private List<ServiceEndpoint> getServiceEndpoints() {
|
||||||
SimpleQuery query = queryFor(ServiceEndpoint.class);
|
SimpleQuery query = queryFor(ServiceEndpoint.class);
|
||||||
query.addCondition("$resource/Profile/Name/text() eq '" + RUNTIME_CATALOGUE_RESOURCE_NAME + "'");
|
query.addCondition("$resource/Profile/Category/Name/text() eq '" + CATEGORY + "'");
|
||||||
query.addCondition("$resource/Profile/Platform/Name/text() eq '" + PLATFORM_CATALOGUE_NAME + "'");
|
query.addCondition("$resource/Profile/Name/text() eq '" + NAME + "'");
|
||||||
|
|
||||||
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
||||||
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
|
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
|
||||||
return serviceEndpoints;
|
return serviceEndpoints;
|
||||||
|
@ -151,17 +208,16 @@ public class GCoreISConfigurationProxy {
|
||||||
List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints();
|
List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints();
|
||||||
|
|
||||||
if (serviceEndpoints.size() == 0) {
|
if (serviceEndpoints.size() == 0) {
|
||||||
logger.error("There is no {} having name {} and Platform {} in this context.",
|
logger.error("There is no {} having Category {} and Name {} in this context.",
|
||||||
ServiceEndpoint.class.getSimpleName(), RUNTIME_CATALOGUE_RESOURCE_NAME, PLATFORM_CATALOGUE_NAME);
|
ServiceEndpoint.class.getSimpleName(), CATEGORY, NAME);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ServiceEndpoint serviceEndpoint = null;
|
ServiceEndpoint serviceEndpoint = null;
|
||||||
|
|
||||||
if (serviceEndpoints.size() > 1) {
|
if (serviceEndpoints.size() > 1) {
|
||||||
logger.info("Too many {} having name {} in this context. Looking for the one that has the property {}",
|
logger.info("Too many {} having Category {} and Name {} in this context. Looking for the one that has the property {}",
|
||||||
ServiceEndpoint.class.getSimpleName(), RUNTIME_CATALOGUE_RESOURCE_NAME,
|
ServiceEndpoint.class.getSimpleName(), CATEGORY, NAME, IS_MASTER_ROOT_KEY_PROPERTY);
|
||||||
IS_MASTER_ROOT_KEY_PROPERTY);
|
|
||||||
|
|
||||||
for (ServiceEndpoint se : serviceEndpoints) {
|
for (ServiceEndpoint se : serviceEndpoints) {
|
||||||
Iterator<AccessPoint> accessPointIterator = se.profile().accessPoints().iterator();
|
Iterator<AccessPoint> accessPointIterator = se.profile().accessPoints().iterator();
|
||||||
|
@ -178,7 +234,7 @@ public class GCoreISConfigurationProxy {
|
||||||
|
|
||||||
// set this variable
|
// set this variable
|
||||||
serviceEndpoint = se;
|
serviceEndpoint = se;
|
||||||
break;
|
return serviceEndpoint;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,29 +266,25 @@ public class GCoreISConfigurationProxy {
|
||||||
List<GenericResource> genericResources = client.submit(query);
|
List<GenericResource> genericResources = client.submit(query);
|
||||||
return genericResources;
|
return genericResources;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Set<String> getSupportedOrganizationsFromIS() {
|
protected String marshallSupportedOrganizations() throws JsonProcessingException {
|
||||||
List<GenericResource> genericResources = getGenericResources();
|
Set<String> supportedOrganizations = catalogueConfiguration.getSupportedOrganizations();
|
||||||
|
return marshallSupportedOrganizations(supportedOrganizations);
|
||||||
if (genericResources == null || genericResources.size() == 0) {
|
}
|
||||||
logger.trace(
|
|
||||||
"{} with SecondaryType {} and Name %s not found. Item will be only be created in {} CKAN organization",
|
protected String marshallSupportedOrganizations(Set<String> supportedOrganizations) throws JsonProcessingException {
|
||||||
GenericResource.class.getSimpleName(), GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS,
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, CatalogueConfiguration.getOrganizationName(context));
|
ArrayNode arrayNode = objectMapper.createArrayNode();
|
||||||
return null;
|
for(String org : supportedOrganizations) {
|
||||||
|
arrayNode.add(org);
|
||||||
}
|
}
|
||||||
|
return objectMapper.writeValueAsString(arrayNode);
|
||||||
if (genericResources.size() > 1) {
|
}
|
||||||
throw new InternalServerErrorException(
|
|
||||||
"To many generic resources to get supported organizations in context " + context);
|
protected Set<String> unmarshallSupportedOrganizations(String supportedOrganizationsJsonArray){
|
||||||
}
|
|
||||||
|
|
||||||
GenericResource genericResource = genericResources.get(0);
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String body = genericResource.profile().body().getTextContent();
|
|
||||||
ObjectMapper objectMapper = new ObjectMapper();
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
JsonNode jsonNode = objectMapper.readTree(body);
|
JsonNode jsonNode = objectMapper.readTree(supportedOrganizationsJsonArray);
|
||||||
ArrayNode array = (ArrayNode) jsonNode.get(GENERIC_RESOURCE_CKAN_ORGANIZATIONS);
|
ArrayNode array = (ArrayNode) jsonNode.get(GENERIC_RESOURCE_CKAN_ORGANIZATIONS);
|
||||||
Set<String> supportedOrganizations = new HashSet<>(array.size());
|
Set<String> supportedOrganizations = new HashSet<>(array.size());
|
||||||
for (int i = 0; i < array.size(); i++) {
|
for (int i = 0; i < array.size(); i++) {
|
||||||
|
@ -245,10 +297,41 @@ public class GCoreISConfigurationProxy {
|
||||||
|
|
||||||
return supportedOrganizations;
|
return supportedOrganizations;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void removeAllGenericResources() {
|
||||||
|
List<GenericResource> genericResources = getGenericResources();
|
||||||
|
removeAllGenericResources(genericResources);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void removeAllGenericResources(List<GenericResource> genericResources) {
|
||||||
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
||||||
|
for(GenericResource genericResource : genericResources) {
|
||||||
|
registryPublisher.remove(genericResource);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Set<String> getSupportedOrganizationsFromGenericResource() {
|
||||||
|
List<GenericResource> genericResources = getGenericResources();
|
||||||
|
|
||||||
|
if (genericResources == null || genericResources.size() == 0) {
|
||||||
|
logger.trace(
|
||||||
|
"{} with SecondaryType {} and Name %s not found. Item will be only be created in {} CKAN organization",
|
||||||
|
GenericResource.class.getSimpleName(), GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS,
|
||||||
|
GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, CatalogueConfiguration.getOrganizationName(context));
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
GenericResource genericResource = genericResources.get(0);
|
||||||
|
String supportedOrganizationsJsonArray = genericResource.profile().body().getTextContent();
|
||||||
|
|
||||||
|
Set<String> supportedOrganizatins = unmarshallSupportedOrganizations(supportedOrganizationsJsonArray);
|
||||||
|
|
||||||
|
removeAllGenericResources(genericResources);
|
||||||
|
|
||||||
|
return supportedOrganizatins;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -268,39 +351,184 @@ public class GCoreISConfigurationProxy {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected Property addProperty(Group<Property> properties, String name, String value) {
|
||||||
|
return addProperty(properties, name, value, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Property addProperty(Group<Property> properties, String name, String value, boolean encrypted) {
|
||||||
|
Property property = new Property();
|
||||||
|
property.nameAndValue(name, value);
|
||||||
|
property.encrypted(encrypted);
|
||||||
|
properties.add(property);
|
||||||
|
return property;
|
||||||
|
}
|
||||||
|
|
||||||
protected ServiceEndpoint manageServiceEndpoint(ServiceEndpoint serviceEndpoint) {
|
protected AccessPoint setAccessPointProperties(AccessPoint accessPoint, boolean update) throws JsonProcessingException {
|
||||||
|
accessPoint.description(String.format("Access Point %s by gcat %s", update ? "updated" : "created", getGcatVersion().toString()));
|
||||||
|
accessPoint.address(catalogueConfiguration.getCkanURL());
|
||||||
|
|
||||||
|
// TODO Change ASAP
|
||||||
|
//accessPoint.name("gcat Configuration");
|
||||||
|
accessPoint.name("CKan Data Catalogue");
|
||||||
|
|
||||||
|
Group<Property> properties = accessPoint.properties();
|
||||||
|
addProperty(properties, DEFAULT_ORGANIZATION_PROPERTY, catalogueConfiguration.getOrganization());
|
||||||
|
addProperty(properties, API_KEY_PROPERTY, catalogueConfiguration.getSysAdminToken(), true);
|
||||||
|
addProperty(properties, SOCIAL_POST_PROPERTY, Boolean.toString(catalogueConfiguration.isSocialPostEnabled()));
|
||||||
|
addProperty(properties, ALERT_USERS_ON_POST_CREATION_PROPERTY, Boolean.toString(catalogueConfiguration.isNotificationToUsersEnabled()));
|
||||||
|
addProperty(properties, MODERATION_ENABLED_KEY_PROPERTY, Boolean.toString(catalogueConfiguration.isModerationEnabled()));
|
||||||
|
addProperty(properties, SUPPORTED_ORGANIZATION_PROPERTY, marshallSupportedOrganizations());
|
||||||
|
|
||||||
|
return accessPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final static String PLATFORM_NAME = "Tomcat";
|
||||||
|
|
||||||
|
protected Version getGcatVersion() {
|
||||||
|
ApplicationContext applicationContext = ContextProvider.get();
|
||||||
|
ApplicationConfiguration applicationConfiguration = applicationContext.configuration();
|
||||||
|
Version version = new Version(applicationConfiguration.version());
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the version of gcat so that in future implementation
|
||||||
|
* we can understand if the configuration must be updated.
|
||||||
|
* @param platform
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
protected Platform setVersion(Platform platform) {
|
||||||
|
Version version = getGcatVersion();
|
||||||
|
platform.version((short) version.getMajor());
|
||||||
|
platform.minorVersion((short) version.getMinor());
|
||||||
|
platform.revisionVersion((short) version.getRevision());
|
||||||
|
platform.buildVersion((short) 0);
|
||||||
|
return platform;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected Platform setPlatformProperty(Platform platform) {
|
||||||
|
/*
|
||||||
|
* <Platform>
|
||||||
|
* <Name>Tomcat</Name>
|
||||||
|
*
|
||||||
|
* The version of gcat
|
||||||
|
* <Version>2</Version>
|
||||||
|
* <MinorVersion>2</MinorVersion>
|
||||||
|
* <RevisionVersion>0</RevisionVersion>
|
||||||
|
* <BuildVersion>0</BuildVersion>
|
||||||
|
* </Platform>
|
||||||
|
*/
|
||||||
|
|
||||||
|
// TODO change to gcat ASAP
|
||||||
|
// platform.name("gcat");
|
||||||
|
platform.name(PLATFORM_NAME);
|
||||||
|
platform = setVersion(platform);
|
||||||
|
return platform;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getRunningOn(ContainerConfiguration containerConfiguration) {
|
||||||
|
return String.format("%s:%s", containerConfiguration.hostname(), containerConfiguration.port());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Runtime setRuntimeProperties(Runtime runtime) {
|
||||||
|
ApplicationContext applicationContext = ContextProvider.get();
|
||||||
|
ContainerContext containerContext = applicationContext.container();
|
||||||
|
ContainerConfiguration containerConfiguration = containerContext.configuration();
|
||||||
|
String runningOn = getRunningOn(containerConfiguration);
|
||||||
|
runtime.hostedOn(runningOn);
|
||||||
|
runtime.ghnId(containerContext.id());
|
||||||
|
runtime.status(applicationContext.configuration().mode().toString());
|
||||||
|
return runtime;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Profile setProfileProperties(Profile profile, boolean update) {
|
||||||
|
/*
|
||||||
|
* <Profile>
|
||||||
|
* <Category>Application</Category>
|
||||||
|
* <Name>CKanDataCatalogue</Name>
|
||||||
|
* <Description>gCat Configuration created/updated by the service via REST</Description>
|
||||||
|
*/
|
||||||
|
profile.category(CATEGORY);
|
||||||
|
profile.name(NAME);
|
||||||
|
profile.description(String.format("gCat configuration %s by the service via REST", update ? "updated" : "created"));
|
||||||
|
return profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected ServiceEndpoint createServiceEndpoint(ServiceEndpoint serviceEndpoint) throws Exception {
|
||||||
|
boolean update = serviceEndpoint != null;
|
||||||
|
|
||||||
|
if(update) {
|
||||||
|
if(catalogueConfiguration.getSysAdminToken()==null) {
|
||||||
|
Profile profile = serviceEndpoint.profile();
|
||||||
|
AccessPoint accessPoint = getAccessPoint(profile);
|
||||||
|
|
||||||
|
// add this host
|
||||||
|
String ckanURL = accessPoint.address();
|
||||||
|
catalogueConfiguration.setCkanURL(ckanURL);
|
||||||
|
|
||||||
|
Map<String, Property> propertyMap = accessPoint.propertyMap();
|
||||||
|
|
||||||
|
// retrieve sys admin token
|
||||||
|
String sysAdminToken = propertyMap.get(API_KEY_PROPERTY).value();
|
||||||
|
sysAdminToken = StringEncrypter.getEncrypter().decrypt(sysAdminToken);
|
||||||
|
catalogueConfiguration.setSysAdminToken(sysAdminToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
serviceEndpoint = new ServiceEndpoint();
|
||||||
|
serviceEndpoint.setId(catalogueConfiguration.getID());
|
||||||
|
|
||||||
|
Profile profile = serviceEndpoint.newProfile();
|
||||||
|
profile = setProfileProperties(profile, update);
|
||||||
|
|
||||||
|
Platform platform = profile.newPlatform();
|
||||||
|
setPlatformProperty(platform);
|
||||||
|
|
||||||
|
Runtime runtime = profile.newRuntime();
|
||||||
|
runtime = setRuntimeProperties(runtime);
|
||||||
|
|
||||||
|
Group<AccessPoint> accessPoints = profile.accessPoints();
|
||||||
|
AccessPoint accessPoint = accessPoints.add();
|
||||||
|
setAccessPointProperties(accessPoint, update);
|
||||||
|
|
||||||
return serviceEndpoint;
|
return serviceEndpoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CatalogueConfiguration createOrUpdate() {
|
public CatalogueConfiguration createOnIS(ServiceEndpoint serviceEndpoint) throws Exception {
|
||||||
ServiceEndpoint serviceEndpoint = getServiceEndpoint();
|
|
||||||
|
|
||||||
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
||||||
|
String id = catalogueConfiguration.getID();
|
||||||
if(serviceEndpoint!=null) {
|
if(id==null || id.compareTo("")==0) {
|
||||||
// It's an update
|
id = UUID.randomUUID().toString();
|
||||||
String id = serviceEndpoint.id();
|
catalogueConfiguration.setID(id);
|
||||||
if(catalogueConfiguration.getID().compareTo(id)!=0) {
|
}
|
||||||
catalogueConfiguration.setID(id);
|
serviceEndpoint = createServiceEndpoint(serviceEndpoint);
|
||||||
}
|
registryPublisher.create(serviceEndpoint);
|
||||||
serviceEndpoint = manageServiceEndpoint(serviceEndpoint);
|
return catalogueConfiguration;
|
||||||
registryPublisher.update(serviceEndpoint);
|
}
|
||||||
}else {
|
|
||||||
// It's a create
|
public CatalogueConfiguration updateOnIS(ServiceEndpoint serviceEndpoint) throws Exception {
|
||||||
String id = catalogueConfiguration.getID();
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
||||||
if(id==null || id.compareTo("")==0) {
|
String id = serviceEndpoint.id();
|
||||||
id = UUID.randomUUID().toString();
|
if(catalogueConfiguration.getID().compareTo(id)!=0) {
|
||||||
}
|
catalogueConfiguration.setID(id);
|
||||||
|
|
||||||
serviceEndpoint = new ServiceEndpoint();
|
|
||||||
serviceEndpoint = manageServiceEndpoint(serviceEndpoint);
|
|
||||||
serviceEndpoint.setId(id);
|
|
||||||
registryPublisher.create(serviceEndpoint);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
serviceEndpoint = createServiceEndpoint(serviceEndpoint);
|
||||||
|
registryPublisher.update(serviceEndpoint);
|
||||||
|
return catalogueConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CatalogueConfiguration createOrUpdateOnIS() throws Exception {
|
||||||
|
ServiceEndpoint serviceEndpoint = getServiceEndpoint();
|
||||||
|
if(serviceEndpoint!=null) {
|
||||||
|
// It's an update
|
||||||
|
updateOnIS(serviceEndpoint);
|
||||||
|
}else {
|
||||||
|
// It's a create
|
||||||
|
createOnIS(serviceEndpoint);
|
||||||
|
}
|
||||||
return catalogueConfiguration;
|
return catalogueConfiguration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,123 @@
|
||||||
|
package org.gcube.gcat.configuration;
|
||||||
|
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class Version implements Comparable<Version> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regex validating the version
|
||||||
|
*/
|
||||||
|
public final static String VERSION_REGEX = "^[1-9][0-9]{0,}\\.(0|([1-9][0-9]{0,}))\\.(0|([1-9][0-9]{0,}))$";
|
||||||
|
|
||||||
|
private final static Pattern VERSION_PATTERN;
|
||||||
|
|
||||||
|
static {
|
||||||
|
VERSION_PATTERN = Pattern.compile(VERSION_REGEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected int major;
|
||||||
|
protected int minor;
|
||||||
|
protected int revision;
|
||||||
|
|
||||||
|
protected Version(){}
|
||||||
|
|
||||||
|
public Version(String version) {
|
||||||
|
setVersion(version);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Version(int major, int minor, int revision) {
|
||||||
|
this.major = major;
|
||||||
|
this.minor = minor;
|
||||||
|
this.revision = revision;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVersion(String version) {
|
||||||
|
if(!VERSION_PATTERN.matcher(version).find()) {
|
||||||
|
throw new RuntimeException("The provided version (i.e. " + version + ") MUST comply with the regex " + VERSION_REGEX);
|
||||||
|
}
|
||||||
|
String[] parts = version.split("\\.");
|
||||||
|
this.major = Integer.valueOf(parts[0]);
|
||||||
|
this.minor = Integer.valueOf(parts[1]);
|
||||||
|
this.revision = Integer.valueOf(parts[2]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMajor() {
|
||||||
|
return major;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setMajor(int major) {
|
||||||
|
this.major = major;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMinor() {
|
||||||
|
return minor;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setMinor(int minor) {
|
||||||
|
this.minor = minor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRevision() {
|
||||||
|
return revision;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setRevision(int revision) {
|
||||||
|
this.revision = revision;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return major + "." + minor + "." + revision;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public int compareTo(Version other) {
|
||||||
|
if(other == null) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int compare = Integer.compare(major, other.major);
|
||||||
|
if(compare!=0) {
|
||||||
|
return compare;
|
||||||
|
}
|
||||||
|
|
||||||
|
compare = Integer.compare(minor, other.minor);
|
||||||
|
if(compare!=0) {
|
||||||
|
return compare;
|
||||||
|
}
|
||||||
|
|
||||||
|
compare = Integer.compare(revision, other.revision);
|
||||||
|
return compare;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + major;
|
||||||
|
result = prime * result + minor;
|
||||||
|
result = prime * result + revision;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (getClass() != obj.getClass())
|
||||||
|
return false;
|
||||||
|
Version other = (Version) obj;
|
||||||
|
if (major != other.major)
|
||||||
|
return false;
|
||||||
|
if (minor != other.minor)
|
||||||
|
return false;
|
||||||
|
if (revision != other.revision)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -48,11 +48,11 @@ public class CatalogueConfigurationFactory {
|
||||||
gCoreISConfigurationProxy.delete();
|
gCoreISConfigurationProxy.delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized static CatalogueConfiguration createOrUpdate(CatalogueConfiguration catalogueConfiguration) {
|
public synchronized static CatalogueConfiguration createOrUpdate(CatalogueConfiguration catalogueConfiguration) throws Exception {
|
||||||
String context = SecretManager.instance.get().getContext();
|
String context = SecretManager.instance.get().getContext();
|
||||||
catalogueConfigurations.remove(context);
|
catalogueConfigurations.remove(context);
|
||||||
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context, catalogueConfiguration);
|
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context, catalogueConfiguration);
|
||||||
catalogueConfiguration = gCoreISConfigurationProxy.createOrUpdate();
|
catalogueConfiguration = gCoreISConfigurationProxy.createOrUpdateOnIS();
|
||||||
catalogueConfigurations.put(context, catalogueConfiguration);
|
catalogueConfigurations.put(context, catalogueConfiguration);
|
||||||
return catalogueConfiguration;
|
return catalogueConfiguration;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ public class GCoreISConfigurationProxyTest extends ContextTest {
|
||||||
ContextTest.setContextByName("/gcube/devNext/NextNext");
|
ContextTest.setContextByName("/gcube/devNext/NextNext");
|
||||||
String context = SecretManager.instance.get().getContext();
|
String context = SecretManager.instance.get().getContext();
|
||||||
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
|
GCoreISConfigurationProxy gCoreISConfigurationProxy = new GCoreISConfigurationProxy(context);
|
||||||
Set<String> organizations = gCoreISConfigurationProxy.getSupportedOrganizationsFromIS();
|
Set<String> organizations = gCoreISConfigurationProxy.getSupportedOrganizationsFromGenericResource();
|
||||||
Assert.assertTrue(organizations.size()>0);
|
Assert.assertTrue(organizations.size()>0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue