2022-11-29 14:42:20 +01:00
|
|
|
package org.gcube.gcat.configuration.isproxies;
|
2022-02-15 09:59:02 +01:00
|
|
|
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
import java.io.IOException;
|
2022-02-22 09:34:07 +01:00
|
|
|
import java.util.HashMap;
|
2022-02-15 09:59:02 +01:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
2022-02-15 16:22:53 +01:00
|
|
|
import java.util.Map;
|
2022-02-15 09:59:02 +01:00
|
|
|
import java.util.Set;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import javax.ws.rs.InternalServerErrorException;
|
2022-02-22 15:39:41 +01:00
|
|
|
import javax.ws.rs.NotFoundException;
|
2022-02-15 09:59:02 +01:00
|
|
|
import javax.ws.rs.WebApplicationException;
|
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
import org.gcube.com.fasterxml.jackson.core.JsonProcessingException;
|
2022-02-15 09:59:02 +01:00
|
|
|
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
import org.gcube.com.fasterxml.jackson.databind.node.ArrayNode;
|
2022-02-22 15:39:41 +01:00
|
|
|
import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
|
2022-02-28 15:43:40 +01:00
|
|
|
import org.gcube.common.authorization.utils.manager.SecretManagerProvider;
|
2022-02-15 09:59:02 +01:00
|
|
|
import org.gcube.common.resources.gcore.GenericResource;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
2022-02-15 16:22:53 +01:00
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Profile;
|
2022-02-15 09:59:02 +01:00
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Property;
|
2022-02-15 16:22:53 +01:00
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.Runtime;
|
|
|
|
import org.gcube.common.resources.gcore.common.Platform;
|
|
|
|
import org.gcube.common.resources.gcore.utils.Group;
|
2022-02-22 15:39:41 +01:00
|
|
|
import org.gcube.gcat.api.GCatConstants;
|
2022-02-15 09:59:02 +01:00
|
|
|
import org.gcube.gcat.api.configuration.CatalogueConfiguration;
|
2022-11-29 14:42:20 +01:00
|
|
|
import org.gcube.gcat.configuration.Version;
|
|
|
|
import org.gcube.gcat.configuration.service.ServiceCKANDB;
|
|
|
|
import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration;
|
2022-02-15 09:59:02 +01:00
|
|
|
import org.gcube.informationsystem.publisher.RegistryPublisher;
|
|
|
|
import org.gcube.informationsystem.publisher.RegistryPublisherFactory;
|
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
|
|
|
import org.gcube.resources.discovery.icclient.ICFactory;
|
2022-02-15 16:22:53 +01:00
|
|
|
import org.gcube.smartgears.ContextProvider;
|
|
|
|
import org.gcube.smartgears.configuration.container.ContainerConfiguration;
|
|
|
|
import org.gcube.smartgears.context.application.ApplicationContext;
|
|
|
|
import org.gcube.smartgears.context.container.ContainerContext;
|
2022-02-15 09:59:02 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2022-02-16 22:34:30 +01:00
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
2022-11-29 12:33:42 +01:00
|
|
|
public class GCoreISConfigurationProxy extends ISConfigurationProxy<ServiceEndpoint> {
|
2022-02-15 09:59:02 +01:00
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(GCoreISConfigurationProxy.class);
|
|
|
|
|
|
|
|
// property to retrieve the master service endpoint into the /root scope
|
2022-02-16 22:34:30 +01:00
|
|
|
public final static String IS_ROOT_MASTER_PROPERTY_KEY = "IS_ROOT_MASTER"; // true, false.. missing means false as
|
|
|
|
|
|
|
|
public final static String DEFAULT_ORGANIZATION_PROPERTY_KEY = "DEFAULT_ORGANIZATION";
|
|
|
|
public final static String SUPPORTED_ORGANIZATION_PROPERTY_KEY = "SUPPORTED_ORGANIZATION";
|
2022-02-22 09:34:07 +01:00
|
|
|
|
2022-02-16 22:34:30 +01:00
|
|
|
public final static String API_KEY_PROPERTY_KEY = "API_KEY";
|
|
|
|
public final static String SOLR_INDEX_ADDRESS_PROPERTY_KEY = "SOLR_INDEX_ADDRESS";
|
|
|
|
public final static String SOCIAL_POST_PROPERTY_KEY = "SOCIAL_POST";
|
|
|
|
public final static String ALERT_USERS_ON_POST_CREATION_PROPERTY_KEY = "ALERT_USERS_ON_POST_CREATION";
|
|
|
|
public final static String MODERATION_ENABLED_KEY_PROPERTY_KEY = "MODERATION_ENABLED";
|
2022-02-15 09:59:02 +01:00
|
|
|
|
2022-02-22 09:34:07 +01:00
|
|
|
public static final Map<String, String> gCoreToConfigurationMapping;
|
|
|
|
public static final Map<String, String> configurationToGCoreMapping;
|
|
|
|
|
|
|
|
static {
|
|
|
|
gCoreToConfigurationMapping = new HashMap<>();
|
|
|
|
configurationToGCoreMapping = new HashMap<>();
|
|
|
|
|
|
|
|
gCoreToConfigurationMapping.put(API_KEY_PROPERTY_KEY, CatalogueConfiguration.SYS_ADMIN_TOKEN_KEY);
|
|
|
|
|
|
|
|
gCoreToConfigurationMapping.put(SOLR_INDEX_ADDRESS_PROPERTY_KEY, CatalogueConfiguration.SOLR_URL_KEY);
|
|
|
|
|
|
|
|
gCoreToConfigurationMapping.put(SOCIAL_POST_PROPERTY_KEY, CatalogueConfiguration.SOCIAL_POST_ENABLED_KEY);
|
|
|
|
gCoreToConfigurationMapping.put(ALERT_USERS_ON_POST_CREATION_PROPERTY_KEY, CatalogueConfiguration.NOTIFICATION_TO_USER_ENABLED_KEY);
|
|
|
|
gCoreToConfigurationMapping.put(MODERATION_ENABLED_KEY_PROPERTY_KEY, CatalogueConfiguration.MODERATION_ENABLED_KEY);
|
|
|
|
|
|
|
|
for(String key : gCoreToConfigurationMapping.keySet()) {
|
|
|
|
configurationToGCoreMapping.put(gCoreToConfigurationMapping.get(key), key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 09:59:02 +01:00
|
|
|
// CKAN Instance info
|
2022-02-22 09:34:07 +01:00
|
|
|
private final static String OLD_CATEGORY = "Application";
|
|
|
|
private final static String OLD_NAME = "CKanDataCatalogue";
|
2022-02-15 09:59:02 +01:00
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
protected ObjectMapper mapper;
|
2022-12-06 18:29:31 +01:00
|
|
|
protected ServiceEndpoint serviceEndpoint;
|
2022-02-15 09:59:02 +01:00
|
|
|
|
|
|
|
public GCoreISConfigurationProxy(String context) {
|
2022-11-29 12:33:42 +01:00
|
|
|
super(context);
|
2022-02-22 15:39:41 +01:00
|
|
|
this.mapper = new ObjectMapper();
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
protected AccessPoint getAccessPoint(Profile profile) {
|
|
|
|
Group<AccessPoint> accessPoints = profile.accessPoints();
|
|
|
|
Iterator<AccessPoint> accessPointIterator = accessPoints.iterator();
|
|
|
|
AccessPoint accessPoint = accessPointIterator.next();
|
|
|
|
return accessPoint;
|
|
|
|
}
|
|
|
|
|
2022-02-15 21:34:50 +01:00
|
|
|
protected String getDefaultSolrURL(String ckanURL) {
|
|
|
|
StringBuffer stringBuffer = new StringBuffer();
|
|
|
|
stringBuffer.append(ckanURL);
|
|
|
|
stringBuffer.append(ckanURL.endsWith("/")?"":"/");
|
|
|
|
stringBuffer.append("solr/");
|
|
|
|
return stringBuffer.toString();
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
protected ObjectNode setValue(ObjectNode node, String key, String value) throws IOException {
|
|
|
|
if(value.toLowerCase().compareTo("true")==0 || value.toLowerCase().compareTo("false")==0) {
|
|
|
|
node.put(key, Boolean.parseBoolean(value));
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(value.startsWith("{") || value.startsWith("[")){
|
|
|
|
JsonNode n = mapper.readTree(value);
|
|
|
|
node.set(key, n);
|
|
|
|
return node;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
node.put(key, value);
|
|
|
|
return node;
|
2022-02-22 09:34:07 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
protected ServiceCatalogueConfiguration getConfiguration(ServiceEndpoint serviceEndpoint) throws IOException {
|
|
|
|
Profile profile = serviceEndpoint.profile();
|
|
|
|
AccessPoint accessPoint = getAccessPoint(profile);
|
|
|
|
Map<String, Property> propertyMap = accessPoint.propertyMap();
|
|
|
|
|
|
|
|
ObjectNode node = mapper.createObjectNode();
|
|
|
|
node.put(CatalogueConfiguration.ID_KEY, serviceEndpoint.id());
|
|
|
|
|
|
|
|
for(String key : propertyMap.keySet()) {
|
|
|
|
String value = propertyMap.get(key).value().trim();
|
|
|
|
setValue(node, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mapper.treeToValue(node, ServiceCatalogueConfiguration.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
private List<ServiceEndpoint> getServiceEndpoints(String category, String name) {
|
|
|
|
SimpleQuery query = queryFor(ServiceEndpoint.class);
|
2022-02-28 15:43:40 +01:00
|
|
|
query.addCondition("$resource/Scopes/Scope/text() eq '" + SecretManagerProvider.instance.get().getContext() + "'");
|
2022-02-22 15:39:41 +01:00
|
|
|
query.addCondition("$resource/Profile/Category/text() eq '" + category + "'");
|
|
|
|
query.addCondition("$resource/Profile/Name/text() eq '" + name + "'");
|
|
|
|
DiscoveryClient<ServiceEndpoint> client = clientFor(ServiceEndpoint.class);
|
|
|
|
List<ServiceEndpoint> serviceEndpoints = client.submit(query);
|
|
|
|
return serviceEndpoints;
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:33:42 +01:00
|
|
|
@Override
|
|
|
|
protected ServiceEndpoint getISResource() {
|
2022-12-06 18:29:31 +01:00
|
|
|
if(serviceEndpoint==null) {
|
|
|
|
List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints(GCatConstants.CONFIGURATION_CATEGORY, GCatConstants.CONFIGURATION_NAME);
|
|
|
|
if (serviceEndpoints==null || serviceEndpoints.size() == 0) {
|
|
|
|
logger.error("There is no {} having Category {} and Name {} in this context.",
|
|
|
|
ServiceEndpoint.class.getSimpleName(), GCatConstants.CONFIGURATION_CATEGORY, GCatConstants.CONFIGURATION_NAME);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
serviceEndpoint = serviceEndpoints.get(0);
|
2022-02-22 15:39:41 +01:00
|
|
|
}
|
|
|
|
return serviceEndpoint;
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:33:42 +01:00
|
|
|
@Override
|
|
|
|
protected ServiceCatalogueConfiguration readFromIS() throws WebApplicationException {
|
|
|
|
ServiceEndpoint serviceEndpoint = getISResource();
|
2022-02-22 15:39:41 +01:00
|
|
|
if(serviceEndpoint==null) {
|
2022-11-29 12:33:42 +01:00
|
|
|
return getOLDCatalogueConfigurationFromGCoreIS();
|
2022-02-22 15:39:41 +01:00
|
|
|
}
|
2022-02-22 15:42:53 +01:00
|
|
|
try {
|
|
|
|
return getConfiguration(serviceEndpoint);
|
|
|
|
}catch (Exception e) {
|
|
|
|
throw new InternalServerErrorException();
|
|
|
|
}
|
2022-02-22 15:39:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
private ServiceEndpoint getOldServiceEndpoint() {
|
|
|
|
List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints(OLD_CATEGORY, OLD_NAME);
|
|
|
|
|
|
|
|
if (serviceEndpoints.size() == 0) {
|
|
|
|
logger.error("There is no {} having Category {} and Name {} in this context.",
|
|
|
|
ServiceEndpoint.class.getSimpleName(), OLD_CATEGORY, OLD_NAME);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceEndpoint serviceEndpoint = null;
|
|
|
|
|
|
|
|
if (serviceEndpoints.size() > 1) {
|
|
|
|
logger.info("Too many {} having Category {} and Name {} in this context. Looking for the one that has the property {}",
|
|
|
|
ServiceEndpoint.class.getSimpleName(), OLD_CATEGORY, OLD_NAME, IS_ROOT_MASTER_PROPERTY_KEY);
|
|
|
|
|
|
|
|
for (ServiceEndpoint se : serviceEndpoints) {
|
|
|
|
Iterator<AccessPoint> accessPointIterator = se.profile().accessPoints().iterator();
|
|
|
|
while (accessPointIterator.hasNext()) {
|
|
|
|
ServiceEndpoint.AccessPoint accessPoint = accessPointIterator.next();
|
|
|
|
|
|
|
|
// get the is master property
|
|
|
|
Property entry = accessPoint.propertyMap().get(IS_ROOT_MASTER_PROPERTY_KEY);
|
|
|
|
String isMaster = entry != null ? entry.value() : null;
|
|
|
|
|
|
|
|
if (isMaster == null || !isMaster.equals("true")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set this variable
|
|
|
|
serviceEndpoint = se;
|
|
|
|
return serviceEndpoint;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if none of them was master, throw an exception
|
|
|
|
if (serviceEndpoint == null) {
|
|
|
|
throw new InternalServerErrorException(
|
|
|
|
"Too many catalogue configuration on IS and no one with MASTER property");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
serviceEndpoint = serviceEndpoints.get(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceEndpoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
2022-11-29 12:33:42 +01:00
|
|
|
protected ServiceCatalogueConfiguration getOLDCatalogueConfigurationFromGCoreIS() {
|
2022-02-17 16:32:54 +01:00
|
|
|
ServiceCatalogueConfiguration catalogueConfiguration = new ServiceCatalogueConfiguration(context);
|
2022-02-15 09:59:02 +01:00
|
|
|
try {
|
2022-02-22 09:34:07 +01:00
|
|
|
// boolean mustBeUpdated = false;
|
2022-02-15 16:22:53 +01:00
|
|
|
|
2022-02-22 09:34:07 +01:00
|
|
|
ServiceEndpoint serviceEndpoint = getOldServiceEndpoint();
|
2022-02-15 09:59:02 +01:00
|
|
|
if (serviceEndpoint == null) {
|
2022-02-22 15:39:41 +01:00
|
|
|
throw new NotFoundException("No configuration found in this context");
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:40:44 +01:00
|
|
|
// catalogueConfiguration.setID(serviceEndpoint.id());
|
2022-11-29 12:33:42 +01:00
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
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
|
2022-02-16 22:34:30 +01:00
|
|
|
String encryptedSysAdminToken = propertyMap.get(API_KEY_PROPERTY_KEY).value();
|
2022-02-17 16:32:54 +01:00
|
|
|
catalogueConfiguration.setEncryptedSysAdminToken(encryptedSysAdminToken);
|
2022-02-15 09:59:02 +01:00
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
|
2022-02-22 09:34:07 +01:00
|
|
|
String defaultOrganization = CatalogueConfiguration.getOrganizationName(context);
|
2022-02-17 16:32:54 +01:00
|
|
|
|
2022-02-15 21:18:02 +01:00
|
|
|
|
|
|
|
String solrURL = null;
|
|
|
|
if (propertyMap.containsKey(SOLR_INDEX_ADDRESS_PROPERTY_KEY)) {
|
|
|
|
solrURL = propertyMap.get(SOLR_INDEX_ADDRESS_PROPERTY_KEY).value();
|
2022-02-15 21:34:50 +01:00
|
|
|
}else {
|
|
|
|
solrURL = getDefaultSolrURL(ckanURL);
|
2022-02-15 21:18:02 +01:00
|
|
|
}
|
2022-02-15 21:34:50 +01:00
|
|
|
catalogueConfiguration.setSolrURL(solrURL);
|
2022-02-15 16:22:53 +01:00
|
|
|
|
|
|
|
// retrieve option to check if the social post has to be made
|
|
|
|
Boolean socialPostEnabled = true;
|
2022-02-15 21:18:02 +01:00
|
|
|
if (propertyMap.containsKey(SOCIAL_POST_PROPERTY_KEY)) {
|
|
|
|
if (propertyMap.get(SOCIAL_POST_PROPERTY_KEY).value().trim().equalsIgnoreCase("false")) {
|
2022-02-15 16:22:53 +01:00
|
|
|
socialPostEnabled = false;
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|
2022-02-15 16:22:53 +01:00
|
|
|
}
|
|
|
|
catalogueConfiguration.setSocialPostEnabled(socialPostEnabled);
|
|
|
|
|
|
|
|
// retrieve option for user alert
|
|
|
|
boolean notificationToUsersEnabled = false; // default is false
|
2022-02-15 21:18:02 +01:00
|
|
|
if (propertyMap.containsKey(ALERT_USERS_ON_POST_CREATION_PROPERTY_KEY)) {
|
|
|
|
if (propertyMap.get(ALERT_USERS_ON_POST_CREATION_PROPERTY_KEY).value().trim()
|
2022-02-15 16:22:53 +01:00
|
|
|
.equalsIgnoreCase("true")) {
|
|
|
|
notificationToUsersEnabled = true;
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-15 16:22:53 +01:00
|
|
|
catalogueConfiguration.setNotificationToUsersEnabled(notificationToUsersEnabled);
|
2022-02-15 09:59:02 +01:00
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
boolean moderationEnabled = false; // default is false
|
2022-02-15 21:18:02 +01:00
|
|
|
if (propertyMap.containsKey(MODERATION_ENABLED_KEY_PROPERTY_KEY)) {
|
|
|
|
if (propertyMap.get(MODERATION_ENABLED_KEY_PROPERTY_KEY).value().trim().equalsIgnoreCase("true")) {
|
2022-02-15 16:22:53 +01:00
|
|
|
moderationEnabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catalogueConfiguration.setModerationEnabled(moderationEnabled);
|
|
|
|
|
2022-02-22 09:34:07 +01:00
|
|
|
Set<String> supportedOrganizations = getSupportedOrganizationsFromGenericResource();
|
2022-02-15 09:59:02 +01:00
|
|
|
if (supportedOrganizations != null) {
|
|
|
|
catalogueConfiguration.setSupportedOrganizations(supportedOrganizations);
|
2022-02-15 21:34:50 +01:00
|
|
|
if(defaultOrganization==null) {
|
|
|
|
defaultOrganization = supportedOrganizations.toArray(new String[supportedOrganizations.size()])[0];
|
|
|
|
catalogueConfiguration.setDefaultOrganization(defaultOrganization);
|
|
|
|
}
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|
2022-02-16 22:34:30 +01:00
|
|
|
|
2022-02-22 09:34:07 +01:00
|
|
|
ServiceCKANDB ckanDB = getCKANDBFromIS();
|
2022-02-16 22:34:30 +01:00
|
|
|
catalogueConfiguration.setCkanDB(ckanDB);
|
|
|
|
|
2022-02-15 09:59:02 +01:00
|
|
|
} catch (WebApplicationException e) {
|
|
|
|
throw e;
|
|
|
|
} catch (Exception e) {
|
|
|
|
throw new InternalServerErrorException("Error while getting configuration on IS", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return catalogueConfiguration;
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:34:30 +01:00
|
|
|
// CKAN Instance info
|
2022-02-22 15:39:41 +01:00
|
|
|
@Deprecated
|
2022-02-16 22:34:30 +01:00
|
|
|
private final static String CKAN_DB_SERVICE_ENDPOINT_CATEGORY= "Database";
|
2022-02-22 15:39:41 +01:00
|
|
|
@Deprecated
|
2022-02-16 22:34:30 +01:00
|
|
|
private final static String CKAN_DB_SERVICE_ENDPOINT_NAME = "CKanDatabase";
|
2022-02-22 15:39:41 +01:00
|
|
|
|
|
|
|
@Deprecated
|
2022-02-17 16:32:54 +01:00
|
|
|
protected ServiceCKANDB getCKANDBFromIS() {
|
2022-02-16 22:34:30 +01:00
|
|
|
try {
|
2022-02-22 09:34:07 +01:00
|
|
|
List<ServiceEndpoint> serviceEndpoints = getServiceEndpoints(CKAN_DB_SERVICE_ENDPOINT_CATEGORY, CKAN_DB_SERVICE_ENDPOINT_NAME);
|
2022-02-22 15:39:41 +01:00
|
|
|
if(serviceEndpoints.size() == 0) {
|
|
|
|
String error = String.format("There is no %s having category '%s' and name '%s' in this context.",
|
|
|
|
ServiceEndpoint.class.getSimpleName(), CKAN_DB_SERVICE_ENDPOINT_CATEGORY, CKAN_DB_SERVICE_ENDPOINT_NAME);
|
|
|
|
logger.error(error);
|
|
|
|
throw new InternalServerErrorException(error);
|
|
|
|
}
|
|
|
|
|
2022-02-16 22:34:30 +01:00
|
|
|
ServiceEndpoint serviceEndpoint = null;
|
|
|
|
|
|
|
|
if(serviceEndpoints.size() > 1) {
|
|
|
|
logger.info("Too many {} having category {} and name {} in this context. Looking for the one that has the property {}",
|
|
|
|
ServiceEndpoint.class.getSimpleName(), CKAN_DB_SERVICE_ENDPOINT_CATEGORY,
|
|
|
|
CKAN_DB_SERVICE_ENDPOINT_NAME);
|
|
|
|
|
|
|
|
for(ServiceEndpoint se : serviceEndpoints) {
|
|
|
|
Iterator<AccessPoint> accessPointIterator = se.profile().accessPoints().iterator();
|
|
|
|
while(accessPointIterator.hasNext()) {
|
|
|
|
ServiceEndpoint.AccessPoint accessPoint = accessPointIterator.next();
|
|
|
|
|
|
|
|
// get the is master property
|
|
|
|
Property entry = accessPoint.propertyMap().get(IS_ROOT_MASTER_PROPERTY_KEY);
|
|
|
|
String isMaster = entry != null ? entry.value() : null;
|
|
|
|
|
|
|
|
if(isMaster == null || !isMaster.equals("true")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set this variable
|
|
|
|
serviceEndpoint = se;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if none of them was master, throw an exception
|
|
|
|
if(serviceEndpoint == null) {
|
|
|
|
throw new InternalServerErrorException(
|
|
|
|
"Too many CKAN configuration on IS and no one with MASTER property");
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
serviceEndpoint = serviceEndpoints.get(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
Iterator<AccessPoint> accessPointIterator = serviceEndpoint.profile().accessPoints().iterator();
|
|
|
|
while(accessPointIterator.hasNext()) {
|
|
|
|
AccessPoint accessPoint = accessPointIterator.next();
|
|
|
|
|
|
|
|
String host = accessPoint.address();
|
|
|
|
String db = accessPoint.name();
|
|
|
|
|
2022-02-17 16:32:54 +01:00
|
|
|
ServiceCKANDB ckanDB = new ServiceCKANDB();
|
2022-02-16 22:34:30 +01:00
|
|
|
String url = String.format("jdbc:postgresql://%s/%s", host, db);
|
|
|
|
ckanDB.setUrl(url);
|
|
|
|
ckanDB.setUsername(accessPoint.username());
|
2022-02-17 16:32:54 +01:00
|
|
|
ckanDB.setEncryptedPassword(accessPoint.password());
|
2022-02-16 22:34:30 +01:00
|
|
|
return ckanDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
} catch(WebApplicationException e) {
|
|
|
|
throw e;
|
|
|
|
} catch(Exception e) {
|
|
|
|
throw new InternalServerErrorException("Error while getting configuration on IS", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-02-15 09:59:02 +01:00
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
@Deprecated
|
2022-02-15 09:59:02 +01:00
|
|
|
public static final String GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS = "ApplicationProfile";
|
2022-02-22 15:39:41 +01:00
|
|
|
@Deprecated
|
2022-02-15 09:59:02 +01:00
|
|
|
public static final String GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS = "Supported CKAN Organizations";
|
2022-02-22 15:39:41 +01:00
|
|
|
@Deprecated
|
2022-02-15 09:59:02 +01:00
|
|
|
public static final String GENERIC_RESOURCE_CKAN_ORGANIZATIONS = "CKANOrganizations";
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
@Deprecated
|
2022-02-15 09:59:02 +01:00
|
|
|
private List<GenericResource> getGenericResources() {
|
|
|
|
SimpleQuery query = ICFactory.queryFor(GenericResource.class);
|
|
|
|
query.addCondition(String.format("$resource/Profile/SecondaryType/text() eq '%s'",
|
|
|
|
GENERIC_RESOURCE_SECONDARY_TYPE_FOR_ORGANIZATIONS));
|
|
|
|
query.addCondition(
|
|
|
|
String.format("$resource/Profile/Name/text() eq '%s'", GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS));
|
|
|
|
|
|
|
|
DiscoveryClient<GenericResource> client = ICFactory.clientFor(GenericResource.class);
|
|
|
|
List<GenericResource> genericResources = client.submit(query);
|
|
|
|
return genericResources;
|
|
|
|
}
|
2022-02-15 16:22:53 +01:00
|
|
|
|
|
|
|
protected String marshallSupportedOrganizations() throws JsonProcessingException {
|
|
|
|
Set<String> supportedOrganizations = catalogueConfiguration.getSupportedOrganizations();
|
|
|
|
return marshallSupportedOrganizations(supportedOrganizations);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected String marshallSupportedOrganizations(Set<String> supportedOrganizations) throws JsonProcessingException {
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
ArrayNode arrayNode = objectMapper.createArrayNode();
|
|
|
|
for(String org : supportedOrganizations) {
|
|
|
|
arrayNode.add(org);
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|
2022-02-15 16:22:53 +01:00
|
|
|
return objectMapper.writeValueAsString(arrayNode);
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
@Deprecated
|
2022-02-15 16:22:53 +01:00
|
|
|
protected Set<String> unmarshallSupportedOrganizations(String supportedOrganizationsJsonArray){
|
2022-02-15 09:59:02 +01:00
|
|
|
try {
|
|
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
2022-02-15 16:22:53 +01:00
|
|
|
JsonNode jsonNode = objectMapper.readTree(supportedOrganizationsJsonArray);
|
2022-02-15 09:59:02 +01:00
|
|
|
ArrayNode array = (ArrayNode) jsonNode.get(GENERIC_RESOURCE_CKAN_ORGANIZATIONS);
|
|
|
|
Set<String> supportedOrganizations = new HashSet<>(array.size());
|
|
|
|
for (int i = 0; i < array.size(); i++) {
|
|
|
|
String o = array.get(i).asText();
|
|
|
|
supportedOrganizations.add(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.debug("Supported CKAN Organization for current Context ({}) are {}", context,
|
|
|
|
supportedOrganizations);
|
|
|
|
|
|
|
|
return supportedOrganizations;
|
|
|
|
} catch (Exception e) {
|
2022-02-15 16:22:53 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
@Deprecated
|
2022-02-15 16:22:53 +01:00
|
|
|
protected Set<String> getSupportedOrganizationsFromGenericResource() {
|
|
|
|
List<GenericResource> genericResources = getGenericResources();
|
2022-02-15 09:59:02 +01:00
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
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,
|
2022-02-17 16:32:54 +01:00
|
|
|
GENERIC_RESOURCE_NAME_FOR_ORGANIZATIONS, ServiceCatalogueConfiguration.getOrganizationName(context));
|
2022-02-15 16:22:53 +01:00
|
|
|
return null;
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
GenericResource genericResource = genericResources.get(0);
|
|
|
|
String supportedOrganizationsJsonArray = genericResource.profile().body().getTextContent();
|
|
|
|
|
|
|
|
Set<String> supportedOrganizatins = unmarshallSupportedOrganizations(supportedOrganizationsJsonArray);
|
|
|
|
|
|
|
|
return supportedOrganizatins;
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
@Deprecated
|
|
|
|
public void deleteOldConfiguration() {
|
2022-02-15 09:59:02 +01:00
|
|
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
2022-02-22 15:39:41 +01:00
|
|
|
deleteOldConfiguration(registryPublisher);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
protected void deleteOldConfiguration(RegistryPublisher registryPublisher) {
|
2022-02-22 09:34:07 +01:00
|
|
|
ServiceEndpoint serviceEndpoint = getOldServiceEndpoint();
|
2022-02-15 09:59:02 +01:00
|
|
|
if(serviceEndpoint!=null) {
|
|
|
|
registryPublisher.remove(serviceEndpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
List<GenericResource> genericResources = getGenericResources();
|
|
|
|
if(genericResources!=null) {
|
|
|
|
for(GenericResource genericResource : genericResources) {
|
|
|
|
registryPublisher.remove(genericResource);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:33:42 +01:00
|
|
|
@Override
|
2022-02-22 15:39:41 +01:00
|
|
|
public void delete() {
|
|
|
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
2022-11-29 12:33:42 +01:00
|
|
|
ServiceEndpoint serviceEndpoint = getISResource();
|
2022-02-22 15:39:41 +01:00
|
|
|
if(serviceEndpoint!=null) {
|
|
|
|
registryPublisher.remove(serviceEndpoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
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);
|
2022-02-22 16:49:05 +01:00
|
|
|
property.encrypted(encrypted);
|
2022-02-15 16:22:53 +01:00
|
|
|
properties.add(property);
|
|
|
|
return property;
|
|
|
|
}
|
2022-02-15 09:59:02 +01:00
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
protected Group<Property> setAccessPointProperties(AccessPoint accessPoint, String address, boolean update) throws JsonProcessingException {
|
2022-02-15 16:22:53 +01:00
|
|
|
accessPoint.description(String.format("Access Point %s by gcat %s", update ? "updated" : "created", getGcatVersion().toString()));
|
2022-02-22 15:39:41 +01:00
|
|
|
accessPoint.address(address);
|
|
|
|
accessPoint.name(GCatConstants.CONFIGURATION_NAME);
|
2022-02-15 09:59:02 +01:00
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
Group<Property> properties = accessPoint.properties();
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
JsonNode jsonNode = mapper.valueToTree(catalogueConfiguration);
|
|
|
|
Iterator<String> iterator = jsonNode.fieldNames();
|
|
|
|
while (iterator.hasNext()) {
|
|
|
|
String key = iterator.next();
|
2022-02-22 16:49:05 +01:00
|
|
|
|
|
|
|
if(key.compareTo(CatalogueConfiguration.ID_KEY)==0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(key.compareTo(CatalogueConfiguration.SYS_ADMIN_TOKEN_KEY)==0) {
|
|
|
|
addProperty(properties, key, catalogueConfiguration.getEncryptedSysAdminToken(), true);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
JsonNode valueJsonNode = jsonNode.get(key);
|
|
|
|
|
|
|
|
String value = valueJsonNode.toString();
|
2022-02-22 16:49:05 +01:00
|
|
|
if(valueJsonNode.isTextual()) {
|
|
|
|
value = valueJsonNode.asText();
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
addProperty(properties, key, value);
|
2022-02-22 16:49:05 +01:00
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
}
|
2022-02-15 21:18:02 +01:00
|
|
|
return properties;
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
/**
|
|
|
|
* Set the version of gcat so that in future implementation
|
|
|
|
* we can understand if the configuration must be updated.
|
|
|
|
* @param platform
|
2022-03-31 11:44:57 +02:00
|
|
|
* @return the platform
|
2022-02-15 16:22:53 +01:00
|
|
|
*/
|
|
|
|
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>
|
2022-02-22 15:39:41 +01:00
|
|
|
* <Name>gcat</Name>
|
|
|
|
* <!-- The version of gcat -->
|
2022-02-15 16:22:53 +01:00
|
|
|
* <Version>2</Version>
|
|
|
|
* <MinorVersion>2</MinorVersion>
|
|
|
|
* <RevisionVersion>0</RevisionVersion>
|
|
|
|
* <BuildVersion>0</BuildVersion>
|
|
|
|
* </Platform>
|
|
|
|
*/
|
2022-02-22 15:39:41 +01:00
|
|
|
platform.name(GCatConstants.SERVICE_NAME);
|
2022-02-15 16:22:53 +01:00
|
|
|
platform = setVersion(platform);
|
|
|
|
return platform;
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getRunningOn(ContainerConfiguration containerConfiguration) {
|
|
|
|
return String.format("%s:%s", containerConfiguration.hostname(), containerConfiguration.port());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected Runtime setRuntimeProperties(Runtime runtime) {
|
2022-02-22 15:39:41 +01:00
|
|
|
try {
|
|
|
|
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());
|
|
|
|
}catch (Exception e) {
|
|
|
|
runtime.hostedOn("localhost");
|
|
|
|
runtime.ghnId("");
|
|
|
|
runtime.status("READY");
|
|
|
|
}
|
2022-02-15 16:22:53 +01:00
|
|
|
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>
|
|
|
|
*/
|
2022-02-22 15:39:41 +01:00
|
|
|
profile.category(GCatConstants.CONFIGURATION_CATEGORY);
|
|
|
|
profile.name(GCatConstants.CONFIGURATION_NAME);
|
2022-02-15 16:22:53 +01:00
|
|
|
profile.description(String.format("gCat configuration %s by the service via REST", update ? "updated" : "created"));
|
|
|
|
return profile;
|
|
|
|
}
|
|
|
|
|
2022-02-22 15:39:41 +01:00
|
|
|
// @Deprecated
|
|
|
|
// protected boolean isRootMaster(ServiceEndpoint serviceEndpoint) {
|
|
|
|
// Profile profile = serviceEndpoint.profile();
|
|
|
|
// AccessPoint accessPoint = getAccessPoint(profile);
|
|
|
|
// Map<String, Property> propertyMap = accessPoint.propertyMap();
|
|
|
|
// if (propertyMap.containsKey(IS_ROOT_MASTER_PROPERTY_KEY)) {
|
|
|
|
// if (propertyMap.get(IS_ROOT_MASTER_PROPERTY_KEY).value().trim().equalsIgnoreCase("true")) {
|
|
|
|
// return true;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// return false;
|
|
|
|
// }
|
2022-02-15 21:18:02 +01:00
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
protected ServiceEndpoint createServiceEndpoint(ServiceEndpoint serviceEndpoint) throws Exception {
|
2022-02-23 17:49:27 +01:00
|
|
|
boolean update = true;
|
|
|
|
if(serviceEndpoint==null) {
|
|
|
|
serviceEndpoint = new ServiceEndpoint();
|
|
|
|
serviceEndpoint.setId(catalogueConfiguration.getID());
|
|
|
|
update = false;
|
|
|
|
}
|
2022-02-15 16:22:53 +01:00
|
|
|
|
|
|
|
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();
|
2022-02-22 15:39:41 +01:00
|
|
|
setAccessPointProperties(accessPoint, runtime.hostedOn(), update);
|
|
|
|
|
2022-02-15 16:22:53 +01:00
|
|
|
return serviceEndpoint;
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:33:42 +01:00
|
|
|
@Override
|
|
|
|
protected ServiceCatalogueConfiguration createOnIS() throws Exception {
|
2022-02-15 16:22:53 +01:00
|
|
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
|
|
|
String id = catalogueConfiguration.getID();
|
|
|
|
if(id==null || id.compareTo("")==0) {
|
|
|
|
id = UUID.randomUUID().toString();
|
|
|
|
catalogueConfiguration.setID(id);
|
|
|
|
}
|
2022-02-23 17:49:27 +01:00
|
|
|
ServiceEndpoint serviceEndpoint = createServiceEndpoint(null);
|
2022-02-15 16:22:53 +01:00
|
|
|
registryPublisher.create(serviceEndpoint);
|
|
|
|
return catalogueConfiguration;
|
|
|
|
}
|
|
|
|
|
2022-11-29 12:33:42 +01:00
|
|
|
@Override
|
2022-12-06 18:29:31 +01:00
|
|
|
protected ServiceCatalogueConfiguration updateOnIS() throws Exception {
|
2022-02-15 09:59:02 +01:00
|
|
|
RegistryPublisher registryPublisher = RegistryPublisherFactory.create();
|
2022-12-06 18:29:31 +01:00
|
|
|
ServiceEndpoint serviceEndpoint = getISResource();
|
2022-02-15 16:22:53 +01:00
|
|
|
String id = serviceEndpoint.id();
|
2022-02-23 17:35:21 +01:00
|
|
|
catalogueConfiguration.setID(id);
|
2022-02-15 16:22:53 +01:00
|
|
|
serviceEndpoint = createServiceEndpoint(serviceEndpoint);
|
|
|
|
registryPublisher.update(serviceEndpoint);
|
|
|
|
return catalogueConfiguration;
|
|
|
|
}
|
|
|
|
|
2022-02-15 09:59:02 +01:00
|
|
|
}
|