2018-07-20 14:38:09 +02:00
/ * *
*
* /
package org.gcube.datatransfer.resolver.catalogue.resource ;
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.StringReader ;
import java.net.URI ;
import java.util.List ;
2019-11-12 18:18:22 +01:00
import java.util.Map ;
2018-07-20 14:38:09 +02:00
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
import org.gcube.common.resources.gcore.utils.XPathHelper ;
import org.gcube.common.scope.api.ScopeProvider ;
2018-11-09 12:05:53 +01:00
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueImpl ;
2019-11-12 18:18:22 +01:00
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueRunningCluster.ACCESS_LEVEL_TO_CATALOGUE_PORTLET ;
2018-07-20 14:38:09 +02:00
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileNotFoundException ;
import org.gcube.datatransfer.resolver.catalogue.endpoint.CatalogueServiceEndpointReader ;
import org.gcube.resources.discovery.client.api.DiscoveryClient ;
import org.gcube.resources.discovery.client.queries.api.Query ;
import org.gcube.resources.discovery.client.queries.impl.QueryBox ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import org.w3c.dom.Node ;
import org.xml.sax.InputSource ;
import eu.trentorise.opendata.jackan.CkanClient ;
import eu.trentorise.opendata.jackan.exceptions.CkanException ;
import eu.trentorise.opendata.jackan.model.CkanDataset ;
2020-04-06 16:11:55 +02:00
// TODO: Auto-generated Javadoc
2018-07-20 14:38:09 +02:00
/ * *
* The Class CkanCatalogueConfigurationsReader .
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
* Apr 20 , 2018
* /
public class CkanCatalogueConfigurationsReader {
2020-04-06 16:11:55 +02:00
//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";
2018-07-20 14:38:09 +02:00
private static final Logger logger = LoggerFactory . getLogger ( CkanCatalogueConfigurationsReader . class ) ;
2020-04-06 16:11:55 +02:00
/** The Constant APPLICATION_PROFILE_NAME. */
2018-07-20 14:38:09 +02:00
public final static String APPLICATION_PROFILE_NAME = " CkanPortlet " ;
2020-04-06 16:11:55 +02:00
2019-11-13 11:27:17 +01:00
2018-07-20 14:38:09 +02:00
/ * *
2020-04-06 16:11:55 +02:00
* Load catalogue end points through the ckan - util - library .
2018-07-20 14:38:09 +02:00
*
* @return the gateway ckan catalogue reference
* @throws Exception the exception
* /
public static GatewayCKANCatalogueReference loadCatalogueEndPoints ( ) throws Exception {
GatewayCKANCatalogueReference links = new GatewayCKANCatalogueReference ( ) ;
links . setScope ( ScopeProvider . instance . get ( ) ) ;
2018-11-09 12:05:53 +01:00
DataCatalogueImpl catalogueImpl = CatalogueServiceEndpointReader . getDataCatalogueImpl ( ) ;
2019-11-12 18:18:22 +01:00
Map < ACCESS_LEVEL_TO_CATALOGUE_PORTLET , String > accessLevelMap = catalogueImpl . getMapAccessURLToCatalogue ( ) ;
2020-04-06 16:11:55 +02:00
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);
2019-11-12 18:18:22 +01:00
String privatePortletURL = accessLevelMap . get ( ACCESS_LEVEL_TO_CATALOGUE_PORTLET . PRIVATE_VRE ) ;
2019-11-13 11:27:17 +01:00
String publicGatewayPortletURL = accessLevelMap . get ( ACCESS_LEVEL_TO_CATALOGUE_PORTLET . PUBLIC_GATEWAY ) ;
2018-11-09 12:05:53 +01:00
try {
2019-11-14 10:41:08 +01:00
//If a catalogue gateway portlet URL is not provided/read from GR, building it
2019-11-13 11:27:17 +01:00
if ( publicGatewayPortletURL = = null | | publicGatewayPortletURL . isEmpty ( ) ) {
//Building the gateway catalogue public URL from private VRE Portlet URL
URI toURL = new URI ( privatePortletURL ) ;
String publicURL = privatePortletURL . startsWith ( " https:// " ) ? " https:// " + toURL . getHost ( ) : " http:// " + toURL . getHost ( ) ;
//It returns the string "catalogue"
2020-04-06 16:11:55 +02:00
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 ) ;
2019-11-13 11:27:17 +01:00
}
2018-11-09 12:05:53 +01:00
} catch ( Exception e ) {
2020-04-06 16:11:55 +02:00
logger . warn ( " Erron on creating public catalogue URL from private URL: " + privatePortletURL , e ) ;
2018-11-09 12:05:53 +01:00
}
//Getting the CKAN Portlet URL for current scope
try {
String ckanPortletURL = catalogueImpl . getCatalogueUrl ( ) ;
links . setCkanURL ( ckanPortletURL ) ;
} catch ( Exception e ) {
logger . warn ( " Erron on getting CKAN Porlet URL for scope: " + ScopeProvider . instance . get ( ) , e ) ;
}
return links ;
2018-12-12 15:38:44 +01:00
}
2018-11-09 12:05:53 +01:00
2020-04-06 16:11:55 +02:00
2018-07-20 14:38:09 +02:00
/ * *
* Retrieve a ckan dataset given its id . The CkanClient is used , without api key . The result is null also when the dataset is private .
2018-12-12 15:38:44 +01:00
*
* @param datasetIdorName the dataset idor name
* @param catalogueURL the catalogue url
* @return the dataset
* @throws Exception the exception
2018-07-20 14:38:09 +02:00
* /
public static CkanDataset getDataset ( String datasetIdorName , String catalogueURL ) throws Exception {
logger . info ( " Performing request GET CKAN dataset with id: " + datasetIdorName ) ;
// checks
checkNotNull ( datasetIdorName ) ;
checkArgument ( ! datasetIdorName . isEmpty ( ) ) ;
try {
CkanClient client = new CkanClient ( catalogueURL ) ;
return client . getDataset ( datasetIdorName ) ;
} catch ( CkanException e ) {
logger . info ( " Getting dataset " + datasetIdorName + " thrown a CkanException, returning null " ) ;
return null ;
}
}
/ * *
* Gets the portlet url for scope from is .
*
* @return the portlet url for scope from is
* @throws Exception the exception
* /
2018-11-09 12:05:53 +01:00
protected static String getPortletUrlForScopeFromIS ( ) throws Exception {
2018-07-20 14:38:09 +02:00
String scope = ScopeProvider . instance . get ( ) ;
logger . debug ( " Trying to fetch applicationProfile profile from the infrastructure for " +
APPLICATION_PROFILE_NAME + " scope: " + scope ) ;
try {
Query q =
new QueryBox (
" for $profile in collection('/db/Profiles/GenericResource')//Resource " +
" where $profile/Profile/SecondaryType/string() eq 'ApplicationProfile' and $profile/Profile/Name/string() " +
" eq ' " +
APPLICATION_PROFILE_NAME +
" ' " +
" return $profile " ) ;
DiscoveryClient < String > client = client ( ) ;
List < String > appProfile = client . submit ( q ) ;
if ( appProfile = = null | | appProfile . size ( ) = = 0 )
throw new ApplicationProfileNotFoundException (
" Your applicationProfile is not registered in the infrastructure " ) ;
else {
String elem = appProfile . get ( 0 ) ;
DocumentBuilder docBuilder =
DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) ;
Node node = docBuilder . parse ( new InputSource ( new StringReader ( elem ) ) ) . getDocumentElement ( ) ;
XPathHelper helper = new XPathHelper ( node ) ;
List < String > currValue = null ;
currValue =
helper . evaluate ( " /Resource/Profile/Body/url/text() " ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
logger . debug ( " CKAN Portlet url found is " + currValue . get ( 0 ) ) ;
return currValue . get ( 0 ) ;
}
}
}
catch ( Exception e ) {
throw new Exception ( " Error while trying to fetch applicationProfile profile for name " + APPLICATION_PROFILE_NAME + " from the infrastructure, using scope: " + scope ) ;
}
return null ;
}
/ * *
2020-04-06 16:11:55 +02:00
* The main method .
2018-07-20 14:38:09 +02:00
*
2020-04-06 16:11:55 +02:00
* @param args the arguments
2018-07-20 14:38:09 +02:00
* /
2020-04-06 16:11:55 +02:00
public static void main ( String [ ] args ) {
2018-07-20 14:38:09 +02:00
2020-04-06 16:11:55 +02:00
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 ( ) ;
2018-07-20 14:38:09 +02:00
}
}
}