Bug #18951 Added business logic to manage the CKAN hostname via property

file
Bug/18951
Francesco Mangiacrapa 4 years ago
parent f6a54fcfcb
commit 09289b4a31

@ -8,15 +8,15 @@
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
@ -29,5 +29,6 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="var" path="GCUBE_DEV_KEYS"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

@ -3,7 +3,9 @@ org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=1.7

@ -23,6 +23,7 @@ public class ItemCatalogueURLs {
private String itemName;
private boolean isPublicItem;
private String privateVRECataloguePortletURL;
private String publicVRECataloguePortletURL;
private String publicGatewayCataloguePortletURL;

@ -0,0 +1,142 @@
package org.gcube.datatransfer.resolver.catalogue.resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class CatalogueStaticConfigurations.
*
* @author Francesco Mangiacrapa at ISTI-CNR Pisa (Italy)
* Apr 6, 2020
*/
public class CatalogueStaticConfigurations{
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(CatalogueStaticConfigurations.class);
/** The Constant PREFIX_FOR_PORTAL_RELATIVE_URL_TO_CATALOGUE. */
static final String PREFIX_FOR_PORTAL_RELATIVE_URL_TO_CATALOGUE = "PREFIX_FOR_PORTAL_RELATIVE_URL_TO_CATALOGUE";
/** The Constant BALANCER_AND_CKAN_PREFIXES_USED_FOR_HOSTNAME. */
static final String BALANCER_AND_CKAN_PREFIXES_USED_FOR_HOSTNAME = "BALANCER_AND_CKAN_PREFIXES_USED_FOR_HOSTNAME";
/** The Constant DATACATALOGUE_URLs_CONFIGURATIONS_PROPERTIES. */
static final String DATACATALOGUE_URLs_CONFIGURATIONS_PROPERTIES = "datacatalogue_urls_configurations.properties";
/** The prefix to be used for catalogue URL. */
private String prefixToBeUsedForCatalogueURL;
/** The prefixes used for ckan hostname. */
private List<String> prefixesUsedForCkanHostname;
/**
* Instantiates a new static configurations.
*/
public CatalogueStaticConfigurations() {
loadCataloguePortalURLsConfigurations();
}
/**
* Load catalogue portal URLs configurations.
*/
private void loadCataloguePortalURLsConfigurations(){
Properties prop = new Properties();
try {
InputStream in = CkanCatalogueConfigurationsReader.class.getResourceAsStream(DATACATALOGUE_URLs_CONFIGURATIONS_PROPERTIES);
// load a properties file
prop.load(in);
// get the property value
String property = prop.getProperty(PREFIX_FOR_PORTAL_RELATIVE_URL_TO_CATALOGUE);
prefixToBeUsedForCatalogueURL = property;
// get the property value
property = prop.getProperty(BALANCER_AND_CKAN_PREFIXES_USED_FOR_HOSTNAME);
if(property.contains(",")) {
String[] values = property.split(",");
prefixesUsedForCkanHostname = Arrays.asList(values);
}else {
prefixesUsedForCkanHostname = Arrays.asList(property);
}
} catch (IOException e) {
LOG.error("An error occurred on read property file: "+DATACATALOGUE_URLs_CONFIGURATIONS_PROPERTIES, e);
}
}
/**
* Extract catalogue name from a ckan or balancer hostname.
*
* @param privateCKANCatalogueURL the private CKAN catalogue URL
* @return the catalogue name
*
* e.g. Returns "-bb" for "ckan-bb"
*/
private String extractCatalogueName(String privateCKANCatalogueURL) {
String toPrivateCKANCatalogueURL = privateCKANCatalogueURL;
LOG.debug("Private Catalogue URL is: "+toPrivateCKANCatalogueURL);
//removing protocol HTTPs or HTTP
toPrivateCKANCatalogueURL = toPrivateCKANCatalogueURL.replaceFirst("https://", "");
toPrivateCKANCatalogueURL = toPrivateCKANCatalogueURL.replaceFirst("http://", "");
LOG.debug("Removed HTTP[s] protocol, now I have: "+toPrivateCKANCatalogueURL);
for (String prefix :prefixesUsedForCkanHostname) {
LOG.trace(prefix+ " found as prefix of: "+privateCKANCatalogueURL);
if(toPrivateCKANCatalogueURL.startsWith(prefix)) {
toPrivateCKANCatalogueURL = toPrivateCKANCatalogueURL.replaceFirst(prefix, ""); //removing prefix
String catalogueName = toPrivateCKANCatalogueURL.substring(0,toPrivateCKANCatalogueURL.indexOf(".d4science"));
LOG.info("Catalogue Name extration returning value: "+catalogueName);
return catalogueName;
}
}
LOG.info("Catalogue Name extration returning null");
return null;
}
/**
* Builds the relative URL to public catalogue gateway.
*
* @param privateCKANCatalogueURL the private CKAN catalogue URL
*
* @return the relative URL that must be used for Public Catalogue Gateway
*/
public String buildRelativeURLToPublicCatalogueGateway(String privateCKANCatalogueURL) {
LOG.info("Trying to build the Catalogue Gateway URL using the prefix: "+prefixToBeUsedForCatalogueURL);
LOG.debug("The prefixes to build the URL for Public Catalogue published at Gateway level are: "+prefixesUsedForCkanHostname);
String catalogueName = extractCatalogueName(privateCKANCatalogueURL);
catalogueName = catalogueName == null? "":catalogueName;
String toNormalizedNameForURL = catalogueName.replaceAll("\\.", "-");
String publicCatalogueGatewayURL = String.format("%s%s", prefixToBeUsedForCatalogueURL,toNormalizedNameForURL);
LOG.info("The Catalogue Gateway URL built is: "+publicCatalogueGatewayURL);
return publicCatalogueGatewayURL;
}
/**
* Gets the prefixes used for ckan hostname.
*
* @return the prefixes used for ckan hostname
*/
public List<String> getPrefixesUsedForCkanHostname() {
return prefixesUsedForCkanHostname;
}
/**
* Gets the prefix to be used for catalogue URL.
*
* @return the prefix to be used for catalogue URL
*/
public String getPrefixToBeUsedForCatalogueURL() {
return prefixToBeUsedForCatalogueURL;
}
}

@ -8,13 +8,10 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.gcube.resources.discovery.icclient.ICFactory.client;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URI;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -38,6 +35,7 @@ import eu.trentorise.opendata.jackan.exceptions.CkanException;
import eu.trentorise.opendata.jackan.model.CkanDataset;
// TODO: Auto-generated Javadoc
/**
* The Class CkanCatalogueConfigurationsReader.
*
@ -46,13 +44,17 @@ import eu.trentorise.opendata.jackan.model.CkanDataset;
*/
public class CkanCatalogueConfigurationsReader {
//private static final String PREFIX_FOR_PORTAL_RELATIVE_URL_TO_CATALOGUE = "PREFIX_FOR_PORTAL_RELATIVE_URL_TO_CATALOGUE";
/** The Constant logger. */
//private static final String BALANCER_AND_CKAN_PREFIXES_USED_FOR_HOSTNAME = "BALANCER_AND_CKAN_PREFIXES_USED_FOR_HOSTNAME";
private static final Logger logger = LoggerFactory.getLogger(CkanCatalogueConfigurationsReader.class);
/** The Constant APPLICATION_PROFILE_NAME. */
public final static String APPLICATION_PROFILE_NAME = "CkanPortlet";
private static final String DATACATALOGUECONFIGURATION_PROPERTIES = "datacatalogueconfiguration.properties";
/**
* Load catalogue end points through the ckan-util-library
* Load catalogue end points through the ckan-util-library.
*
* @return the gateway ckan catalogue reference
* @throws Exception the exception
@ -64,13 +66,13 @@ public class CkanCatalogueConfigurationsReader {
DataCatalogueImpl catalogueImpl = CatalogueServiceEndpointReader.getDataCatalogueImpl();
Map<ACCESS_LEVEL_TO_CATALOGUE_PORTLET, String> accessLevelMap = catalogueImpl.getMapAccessURLToCatalogue();
links.setMapAccessURLToCatalogue(accessLevelMap);
for (ACCESS_LEVEL_TO_CATALOGUE_PORTLET accessLevel : accessLevelMap.keySet()) {
links.setCatalogueURL(accessLevelMap.get(accessLevel), accessLevel);
}
//String publicVREPortletURL = accessLevelMap.get(ACCESS_LEVEL_TO_CATALOGUE_PORTLET.PUBLIC_VRE);
String privatePortletURL = accessLevelMap.get(ACCESS_LEVEL_TO_CATALOGUE_PORTLET.PRIVATE_VRE);
links.setPrivateVREPortletURL(privatePortletURL);
String publicVREPortletURL = accessLevelMap.get(ACCESS_LEVEL_TO_CATALOGUE_PORTLET.PUBLIC_VRE);
links.setPublicVREPortletURL(publicVREPortletURL);
String publicGatewayPortletURL = accessLevelMap.get(ACCESS_LEVEL_TO_CATALOGUE_PORTLET.PUBLIC_GATEWAY);
links.setPublicGatewayPortletURL(publicGatewayPortletURL);
try{
//If a catalogue gateway portlet URL is not provided/read from GR, building it
@ -79,13 +81,14 @@ public class CkanCatalogueConfigurationsReader {
URI toURL = new URI(privatePortletURL);
String publicURL = privatePortletURL.startsWith("https://")?"https://"+toURL.getHost():"http://"+toURL.getHost();
//It returns the string "catalogue"
String prefixToPublicCtlg = getRelativeURLToCatalogue();
//Replacing for example "ckan-bb" with "catalogue-bb"
String publicCatalogueName = extractCatalogueName(catalogueImpl.getCatalogueUrl(), prefixToPublicCtlg);
links.setPublicGatewayPortletURL(publicURL+"/"+publicCatalogueName);
CatalogueStaticConfigurations staticConf = new CatalogueStaticConfigurations();
//Replacing for example "ckan-bb" with "[PREFIXES-TO-CATALOGUE-URL]-bb" (e.g catalogue-bb)
String relativeURLWithCatalogueName = staticConf.buildRelativeURLToPublicCatalogueGateway(catalogueImpl.getCatalogueUrl());
String toGatewayPortletURL = String.format("%s/%s", publicURL, relativeURLWithCatalogueName);
links.setCatalogueURL(toGatewayPortletURL, ACCESS_LEVEL_TO_CATALOGUE_PORTLET.PUBLIC_GATEWAY);
}
}catch(Exception e){
logger.warn("Erron on generating public catalogue URL from private URL: "+privatePortletURL, e);
logger.warn("Erron on creating public catalogue URL from private URL: "+privatePortletURL, e);
}
//Getting the CKAN Portlet URL for current scope
@ -99,22 +102,7 @@ public class CkanCatalogueConfigurationsReader {
return links;
}
/**
* Extract catalogue name.
*
* @param privateCKANCatalogueURL the private ckan catalogue url
* @param replaceCKANWith the replace ckan with
* @return the string
*/
public static String extractCatalogueName(String privateCKANCatalogueURL, String replaceCKANWith){
privateCKANCatalogueURL = privateCKANCatalogueURL.replaceFirst("https://ckan", replaceCKANWith);
privateCKANCatalogueURL = privateCKANCatalogueURL.replaceFirst("http://ckan", replaceCKANWith);
return privateCKANCatalogueURL.substring(0,privateCKANCatalogueURL.indexOf("."));
}
/**
* Retrieve a ckan dataset given its id. The CkanClient is used, without api key. The result is null also when the dataset is private.
*
@ -187,44 +175,20 @@ public class CkanCatalogueConfigurationsReader {
}
/**
* Gets the relative url to catalogue.
* The main method.
*
* @return the relative url to catalogue
* @param args the arguments
*/
private static String getRelativeURLToCatalogue(){
Properties prop = new Properties();
String relativeURLToCatalogue = null;
try {
InputStream in = CkanCatalogueConfigurationsReader.class.getResourceAsStream(DATACATALOGUECONFIGURATION_PROPERTIES);
// load a properties file
prop.load(in);
// get the property value
relativeURLToCatalogue = prop.getProperty("PORTAL_RELATIVE_URL_TO_CATALOGUE");
public static void main(String[] args) {
if(relativeURLToCatalogue==null || relativeURLToCatalogue.isEmpty())
return "catalogue";
} catch (IOException e) {
logger.error("An error occurred on read property file: "+DATACATALOGUECONFIGURATION_PROPERTIES, e);
ScopeProvider.instance.set("/gcube/devsec/devVRE");
try {
GatewayCKANCatalogueReference links = CkanCatalogueConfigurationsReader.loadCatalogueEndPoints();
System.out.println(links);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return relativeURLToCatalogue;
}
// /**
// * The main method.
// *
// * @param args the arguments
// */
// public static void main(String[] args) {
//
// ScopeProvider.instance.set("/gcube/devsec/devVRE");
// try {
// GatewayCKANCatalogueReference links = CkanCatalogueConfigurationsReader.loadCatalogueEndPoints();
// System.out.println(links);
// }
// catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}

@ -4,17 +4,17 @@
package org.gcube.datatransfer.resolver.catalogue.resource;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueRunningCluster.ACCESS_LEVEL_TO_CATALOGUE_PORTLET;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
// TODO: Auto-generated Javadoc
/**
* The Class GatewayCKANCatalogueReference.
*
@ -23,27 +23,65 @@ import lombok.ToString;
* Nov 12, 2019
*/
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class GatewayCKANCatalogueReference implements Serializable{
/**
*
*/
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 1L;
/** The scope. */
private String scope;
/** The ckan URL. */
private String ckanURL;
private String privateVREPortletURL;
private String publicVREPortletURL;
private String publicGatewayPortletURL;
/** The map access URL to catalogue. */
private Map<ACCESS_LEVEL_TO_CATALOGUE_PORTLET, String> mapAccessURLToCatalogue;
/**
* Gets the catalogue URL.
*
* @param accessLevel the access level
* @return the catalogue URL
*/
public String getCatalogueURL(ACCESS_LEVEL_TO_CATALOGUE_PORTLET accessLevel) {
return mapAccessURLToCatalogue.get(accessLevel);
}
/**
* Sets the catalogue URL.
*
* @param catalogueURL the catalogue URL
* @param accessLevel the access level
*/
protected void setCatalogueURL(String catalogueURL, ACCESS_LEVEL_TO_CATALOGUE_PORTLET accessLevel) {
if(mapAccessURLToCatalogue==null)
mapAccessURLToCatalogue = new HashMap<ACCESS_LEVEL_TO_CATALOGUE_PORTLET, String>();
mapAccessURLToCatalogue.put(accessLevel, catalogueURL);
}
public String getScope() {
return scope;
}
public void setScope(String scope) {
this.scope = scope;
}
public String getCkanURL() {
return ckanURL;
}
public void setCkanURL(String ckanURL) {
this.ckanURL = ckanURL;
}
private String ckanURL;
public Map<ACCESS_LEVEL_TO_CATALOGUE_PORTLET, String> mapAccessURLToCatalogue;
}

@ -0,0 +1,14 @@
# Property file
#
# author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
# created 11/2017
# updated 03/2020
#
# BALANCER_AND_CKAN_PREFIXES_USED_FOR_HOSTNAME
# eg. for https://ckan-territoriaperti.d4science.org is ckan
# eg. for https://ckan-imarine.d4science.org is ckan
# eg. for https://catalogue.d4science.org is catalogue
#
PREFIX_FOR_PORTAL_RELATIVE_URL_TO_CATALOGUE = catalogue
BALANCER_AND_CKAN_PREFIXES_USED_FOR_HOSTNAME = ckan,catalogue

@ -1,7 +0,0 @@
# Property files
#
# author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
# created 11/2017
#
PORTAL_RELATIVE_URL_TO_CATALOGUE = catalogue

@ -18,6 +18,7 @@ import javax.ws.rs.core.Response;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.common.scope.impl.ScopeBean;
import org.gcube.common.scope.impl.ScopeBean.Type;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueRunningCluster.ACCESS_LEVEL_TO_CATALOGUE_PORTLET;
import org.gcube.datatransfer.resolver.ConstantsResolver;
import org.gcube.datatransfer.resolver.caches.LoadingMapOfScopeCache;
import org.gcube.datatransfer.resolver.catalogue.CatalogueRequest;
@ -223,13 +224,25 @@ public class CatalogueResolver {
}
}
String publicGatewayPorltetURL = String.format("%s?path=/%s/%s",ckanCatalogueReference.getPublicGatewayPortletURL(),entityContextValue, entityName);
String privateVREPortletURL = String.format("%s?path=/%s/%s",ckanCatalogueReference.getPrivateVREPortletURL(),entityContextValue, entityName);
String publicGatewayPorltetURL = String.format("%s?path=/%s/%s",
ckanCatalogueReference.getCatalogueURL(ACCESS_LEVEL_TO_CATALOGUE_PORTLET.PUBLIC_GATEWAY),
entityContextValue,
entityName);
String privateVREPortletURL = String.format("%s?path=/%s/%s",
ckanCatalogueReference.getCatalogueURL(ACCESS_LEVEL_TO_CATALOGUE_PORTLET.PRIVATE_VRE),
entityContextValue,
entityName);
//Checking if the public VRE portlet URL is available (so it was read from GR)
String publicVREPortletURL = null;
if(ckanCatalogueReference.getPublicVREPortletURL()!=null && !ckanCatalogueReference.getPublicVREPortletURL().isEmpty()) {
String toCheckPublicVREPortletURL = ckanCatalogueReference.getCatalogueURL(ACCESS_LEVEL_TO_CATALOGUE_PORTLET.PUBLIC_VRE);
if(toCheckPublicVREPortletURL!=null && !toCheckPublicVREPortletURL.isEmpty()) {
//here the catalogue is available/deployed as public at VRE level
publicVREPortletURL = String.format("%s?path=/%s/%s",ckanCatalogueReference.getPublicVREPortletURL(),entityContextValue, entityName);
publicVREPortletURL = String.format("%s?path=/%s/%s",
toCheckPublicVREPortletURL,
entityContextValue,
entityName);
}
return new ItemCatalogueURLs(entityName, isPublicItem, privateVREPortletURL, publicVREPortletURL, publicGatewayPorltetURL);
}catch (Exception e) {

@ -0,0 +1,23 @@
import org.gcube.datatransfer.resolver.catalogue.resource.CatalogueStaticConfigurations;
import org.junit.Test;
public class CatalogueNameExtractor {
String[] CKANs = new String[] {
"https://ckan-grsf-admin2.d4science.org",
"https://ckan.pre.d4science.org/",
"https://ckan-aginfra.d4science.org",
"https://catalogue-imarine.d4science.org/" };
@Test
public void extraCatalogueName() {
CatalogueStaticConfigurations staticConf = new CatalogueStaticConfigurations();
for (int i = 0; i < CKANs.length; i++) {
System.out.println(CKANs[i]);
staticConf.buildRelativeURLToPublicCatalogueGateway(CKANs[i]);
System.out.println("\n\n");
}
}
}

@ -10,3 +10,4 @@
/gCubeApps.gcubekey
/gcube.gcubekey
/preprod.gcubekey
/pred4s.gcubekey

Loading…
Cancel
Save