diff --git a/src/main/java/org/gcube/grsf/publisher/ckan/record/Record.java b/src/main/java/org/gcube/grsf/publisher/ckan/record/Record.java index cd932dc..f173dc6 100644 --- a/src/main/java/org/gcube/grsf/publisher/ckan/record/Record.java +++ b/src/main/java/org/gcube/grsf/publisher/ckan/record/Record.java @@ -33,7 +33,6 @@ import org.gcube.common.storagehub.client.dsl.FileContainer; import org.gcube.common.storagehub.model.exceptions.StorageHubException; import org.gcube.gcat.api.GCatConstants; import org.gcube.gcat.api.roles.Role; -import org.gcube.gcat.configuration.CatalogueConfigurationFactory; import org.gcube.gcat.persistence.ckan.CKAN; import org.gcube.gcat.persistence.ckan.CKANPackage; import org.gcube.gcat.persistence.ckan.CKANResource; @@ -42,6 +41,7 @@ import org.gcube.gcat.utils.Constants; import org.gcube.gcat.utils.URIResolver; import org.gcube.grsf.publisher.ckan.others.GRSFResource; import org.gcube.grsf.publisher.configuration.GRSFCatalogueConfiguration; +import org.gcube.grsf.publisher.configuration.GRSFCatalogueConfigurationFactory; import org.gcube.grsf.publisher.freemarker.FreeMarker; import org.gcube.grsf.publisher.utils.TypeUtils; import org.gcube.grsf.publisher.workspace.GRSFStorageHubManagement; @@ -241,7 +241,7 @@ public abstract class Record extends CKANPackage { String recordURL = uriResolver.getCatalogueItemURL(grsfUUID); map.put(RECORD_URL_TEMPLATE_PROPERTY_KEY, recordURL); - GRSFCatalogueConfiguration grsfCC = (GRSFCatalogueConfiguration) CatalogueConfigurationFactory.getInstance(); + GRSFCatalogueConfiguration grsfCC = GRSFCatalogueConfigurationFactory.getInstance(); map.put(INCLUDE_SENSITIVE_TEMPLATE_PROPERTY_KEY, grsfCC.isIncludeSensitive()); map.put(IS_PATCH_TEMPLATE_PROPERTY_KEY, patch); diff --git a/src/main/java/org/gcube/grsf/publisher/configuration/GRSFCatalogueConfigurationFactory.java b/src/main/java/org/gcube/grsf/publisher/configuration/GRSFCatalogueConfigurationFactory.java new file mode 100644 index 0000000..259ed4a --- /dev/null +++ b/src/main/java/org/gcube/grsf/publisher/configuration/GRSFCatalogueConfigurationFactory.java @@ -0,0 +1,150 @@ +package org.gcube.grsf.publisher.configuration; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.gcube.common.authorization.utils.manager.SecretManager; +import org.gcube.common.authorization.utils.manager.SecretManagerProvider; +import org.gcube.common.authorization.utils.secret.Secret; +import org.gcube.gcat.configuration.isproxies.ISConfigurationProxy; +import org.gcube.gcat.configuration.isproxies.ISConfigurationProxyFactory; +import org.gcube.gcat.configuration.service.ServiceCatalogueConfiguration; +import org.gcube.gcat.persistence.ckan.cache.CKANUserCache; +import org.gcube.gcat.utils.Constants; +import org.gcube.grsf.publisher.configuration.isproxies.impl.GRSFFacetBasedISConfigurationProxyFactory; +import org.gcube.grsf.publisher.configuration.isproxies.impl.GRSFGCoreISConfigurationProxyFactory; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class GRSFCatalogueConfigurationFactory { + + private static final Logger logger = LoggerFactory.getLogger(GRSFCatalogueConfigurationFactory.class); + + private static final Map grsfCatalogueConfigurations; + + private static List> factories; + + static { + grsfCatalogueConfigurations = new HashMap<>(); + factories = new ArrayList<>(); + } + + private static List> getFactories(){ + if(factories.size()==0) { + factories.add(new GRSFFacetBasedISConfigurationProxyFactory()); + factories.add(new GRSFGCoreISConfigurationProxyFactory()); + } + return factories; + } + + public static void addISConfigurationProxyFactory(ISConfigurationProxyFactory icpf) { + factories.add(icpf); + } + + private static GRSFCatalogueConfiguration load(String context) { + GRSFCatalogueConfiguration grsfCatalogueConfiguration = null; + SecretManager secretManager = SecretManagerProvider.instance.get(); + try { + Secret secret = Constants.getCatalogueSecret(); + secretManager.startSession(secret); + + for(ISConfigurationProxyFactory icpf : getFactories()) { + try { + ISConfigurationProxy icp = icpf.getInstance(context); + + grsfCatalogueConfiguration = (GRSFCatalogueConfiguration) icp.getCatalogueConfiguration(); + logger.trace("The configuration has been read using {}.", icp.getClass().getSimpleName()); + }catch(Exception e){ + logger.warn("{} cannot be used to read {}. Reason is {}", icpf.getClass().getSimpleName(), ServiceCatalogueConfiguration.class.getSimpleName(), e.getMessage()); + } + } + + } catch(Exception e) { + logger.error("Unable to start session. Reason is " + e.getMessage()); + } finally { + secretManager.endSession(); + } + + if(grsfCatalogueConfiguration==null) { + throw new RuntimeException("Unable to load " + ServiceCatalogueConfiguration.class.getSimpleName() + " by using configured " + ISConfigurationProxyFactory.class.getSimpleName() + " i.e. " + getFactories()); + } + return grsfCatalogueConfiguration; + } + + private static void purgeFromIS(String context) { + SecretManager secretManager = SecretManagerProvider.instance.get(); + try { + Secret secret = Constants.getCatalogueSecret(); + secretManager.startSession(secret); + for(ISConfigurationProxyFactory icpf : getFactories()) { + ISConfigurationProxy icp = icpf.getInstance(context); + icp.delete(); + } + } catch(Exception e) { + logger.error("Unable to start session. Reason is " + e.getMessage()); + } finally { + secretManager.endSession(); + } + } + + private static void createOrUpdateOnIS(String context, ServiceCatalogueConfiguration catalogueConfiguration) throws Exception { + SecretManager secretManager = SecretManagerProvider.instance.get(); + try { + Secret secret = Constants.getCatalogueSecret(); + secretManager.startSession(secret); + + for(ISConfigurationProxyFactory icpf : getFactories()) { + ISConfigurationProxy icp = icpf.getInstance(context); + icp.setCatalogueConfiguration(catalogueConfiguration); + icp.createOrUpdateOnIS(); + } + + } finally { + secretManager.endSession(); + } + } + + public synchronized static GRSFCatalogueConfiguration getInstance() { + String context = SecretManagerProvider.instance.get().getContext(); + GRSFCatalogueConfiguration grsfCatalogueConfiguration = grsfCatalogueConfigurations.get(context); + if(grsfCatalogueConfiguration == null) { + grsfCatalogueConfiguration = load(context); + grsfCatalogueConfigurations.put(context, grsfCatalogueConfiguration); + } + return grsfCatalogueConfiguration; + } + + public synchronized static void renew() { + String context = SecretManagerProvider.instance.get().getContext(); + grsfCatalogueConfigurations.remove(context); + GRSFCatalogueConfiguration grsfCatalogueConfiguration = load(context); + grsfCatalogueConfigurations.put(context, grsfCatalogueConfiguration); + } + + public synchronized static void purge() { + // Remove the resource from IS + String context = SecretManagerProvider.instance.get().getContext(); + grsfCatalogueConfigurations.remove(context); + purgeFromIS(context); + } + + public synchronized static GRSFCatalogueConfiguration createOrUpdate(GRSFCatalogueConfiguration grsfCatalogueConfiguration) throws Exception { + String context = SecretManagerProvider.instance.get().getContext(); + grsfCatalogueConfigurations.remove(context); + + createOrUpdateOnIS(context, grsfCatalogueConfiguration); + grsfCatalogueConfigurations.put(context, grsfCatalogueConfiguration); + + // The supported organizations could be changed we need to empty the user cache for the context + // to avoid to miss to add an user in an organization which has been added. + CKANUserCache.emptyUserCache(); + + return grsfCatalogueConfiguration; + } + +} diff --git a/src/main/java/org/gcube/grsf/publisher/rest/FisheryRESTAPIs.java b/src/main/java/org/gcube/grsf/publisher/rest/FisheryRESTAPIs.java index 199e905..7f51239 100644 --- a/src/main/java/org/gcube/grsf/publisher/rest/FisheryRESTAPIs.java +++ b/src/main/java/org/gcube/grsf/publisher/rest/FisheryRESTAPIs.java @@ -25,9 +25,6 @@ import com.webcohesion.enunciate.metadata.rs.StatusCodes; */ @Path(FisheryRESTAPIs.COLLECTION_PATH) public class FisheryRESTAPIs extends BaseRESTAPIs { - - @PathParam("source") - protected String source; public static final String COLLECTION_PATH = "fishery"; public static final String RECORD_ID_PARAMETER = "fishery_record_id"; diff --git a/src/main/java/org/gcube/grsf/publisher/rest/StockRESTAPIs.java b/src/main/java/org/gcube/grsf/publisher/rest/StockRESTAPIs.java index 103dae7..86ea66a 100644 --- a/src/main/java/org/gcube/grsf/publisher/rest/StockRESTAPIs.java +++ b/src/main/java/org/gcube/grsf/publisher/rest/StockRESTAPIs.java @@ -25,9 +25,6 @@ import com.webcohesion.enunciate.metadata.rs.StatusCodes; */ @Path(StockRESTAPIs.COLLECTION_PATH) public class StockRESTAPIs extends BaseRESTAPIs { - - @PathParam("source") - protected String source; public static final String COLLECTION_PATH = "stock"; public static final String RECORD_ID_PARAMETER = "stock_record_id";