2016-12-20 15:27:08 +01:00
/ *
*
* /
2017-02-28 16:55:31 +01:00
package org.gcube.datatransfer.resolver.catalogue.resource ;
2016-12-20 12:11:35 +01:00
import static org.gcube.resources.discovery.icclient.ICFactory.client ;
import java.io.StringReader ;
2016-12-20 15:27:08 +01:00
import java.util.HashMap ;
2016-12-20 12:11:35 +01:00
import java.util.List ;
2016-12-20 15:27:08 +01:00
import java.util.Map ;
2016-12-20 12:11:35 +01:00
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
import org.apache.log4j.Logger ;
import org.gcube.common.resources.gcore.utils.XPathHelper ;
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.datatransfer.resolver.applicationprofile.ApplicationProfileNotFoundException ;
import org.gcube.datatransfer.resolver.applicationprofile.GcubeQuery ;
2016-12-20 15:27:08 +01:00
import org.gcube.datatransfer.resolver.applicationprofile.GcubeQuery.FIELD_TYPE ;
2016-12-20 12:11:35 +01:00
import org.gcube.datatransfer.resolver.applicationprofile.ScopeUtil ;
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 ;
2017-02-28 16:55:31 +01:00
import org.w3c.dom.Document ;
import org.w3c.dom.Element ;
2016-12-20 12:11:35 +01:00
import org.xml.sax.InputSource ;
/ * *
2016-12-20 15:27:08 +01:00
* The Class ApplicationProfileReaderForCatalogueResolver .
2016-12-20 12:11:35 +01:00
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
2016-12-20 15:27:08 +01:00
* Dec 20 , 2016
2016-12-20 12:11:35 +01:00
* /
public class ApplicationProfileReaderForCatalogueResolver {
2017-02-28 16:55:31 +01:00
public static final String VRE_NAME = " VRE_NAME " ;
public static final String SCOPE = " SCOPE " ;
public static final String END_POINT = " EndPoint " ;
public static final String BODY = " Body " ;
2016-12-20 15:27:08 +01:00
public static final String SECONDARY_TYPE = " ApplicationProfile " ;
public static final String RESOURCE_NAME = " Catalogue-Resolver " ;
2016-12-20 12:11:35 +01:00
protected static final String RESOURCE_PROFILE_NAME_TEXT = " /Resource/Profile/Name/text() " ;
2016-12-20 15:27:08 +01:00
protected static final String RESOURCE_PROFILE_DESCRIPTION_TEXT = " /Resource/Profile/Description/text() " ;
2017-02-28 16:55:31 +01:00
protected static final String RESOURCE_PROFILE_BODY_END_POINT_SCOPE_TEXT = " /Resource/Profile/ " + BODY + " / " + END_POINT + " / " + SCOPE + " /text() " ;
protected static final String RESOURCE_PROFILE_BODY_END_POINT_VRE_NAME_TEXT = " /Resource/Profile/ " + BODY + " / " + END_POINT + " / " + VRE_NAME + " /text() " ;
2016-12-20 12:11:35 +01:00
private Logger logger = Logger . getLogger ( ApplicationProfileReaderForCatalogueResolver . class ) ;
private String secondaryType ;
private String resourceName ;
private String scope ;
private boolean useRootScope = false ;
2017-02-28 16:55:31 +01:00
private Document document ;
2016-12-20 12:11:35 +01:00
2016-12-20 15:27:08 +01:00
/ * * The hash vre name scope .
* Used by Catalogue Resolver for mapping VRE NAME with its SCOPE where to fetch the Data Catalogue Portlet so that resolve correctly URL of
* kind : http : //[CATALOGUE_RESOLVER_SERVLET]/[VRE_NAME]/[entity_context value]/[entity_name value]
* * /
private Map < String , String > hashVreNameScope = new HashMap < String , String > ( ) ;
2017-02-28 16:55:31 +01:00
private Element rootElement ;
2016-12-20 12:11:35 +01:00
/ * *
* Instantiates a new application profile reader for catalogue resolver .
*
* @param scope the scope
* @param useRootScope the use root scope
* /
public ApplicationProfileReaderForCatalogueResolver ( String scope , boolean useRootScope ) {
this . scope = scope ;
this . secondaryType = SECONDARY_TYPE ;
this . resourceName = RESOURCE_NAME ;
this . useRootScope = useRootScope ;
2016-12-20 15:27:08 +01:00
this . readProfileFromInfrastrucure ( ) ;
2016-12-20 12:11:35 +01:00
}
/ * *
2017-02-28 16:55:31 +01:00
* Read profile from infrastrucure and fills the map { @link # hashVreNameScope } .
2016-12-20 12:11:35 +01:00
* /
2016-12-20 15:27:08 +01:00
private void readProfileFromInfrastrucure ( ) {
2016-12-20 12:11:35 +01:00
2018-10-18 18:24:49 +02:00
String queryString = GcubeQuery . getGcubeGenericResource ( secondaryType , FIELD_TYPE . RESOURCE_NAME , resourceName ) ;
try {
logger . info ( " Trying to fetch ApplicationProfile in the scope: " + ScopeProvider . instance . get ( ) + " , SecondaryType: " + secondaryType + " , ResourceName: " + resourceName ) ;
Query q = new QueryBox ( queryString ) ;
DiscoveryClient < String > client = client ( ) ;
List < String > appProfile = client . submit ( q ) ;
if ( appProfile = = null | | appProfile . size ( ) = = 0 )
throw new ApplicationProfileNotFoundException ( " ApplicationProfile with SecondaryType: " + secondaryType + " , ResourceName: " + resourceName + " is not registered in the scope: " + ScopeProvider . instance . get ( ) ) ;
else {
try {
2016-12-20 12:11:35 +01:00
String elem = appProfile . get ( 0 ) ;
2017-02-28 16:55:31 +01:00
DocumentBuilder docBuilder = DocumentBuilderFactory . newInstance ( ) . newDocumentBuilder ( ) ;
document = docBuilder . parse ( new InputSource ( new StringReader ( elem ) ) ) ;
rootElement = document . getDocumentElement ( ) ;
XPathHelper helper = new XPathHelper ( rootElement ) ;
2016-12-20 15:27:08 +01:00
List < String > scopes = helper . evaluate ( RESOURCE_PROFILE_BODY_END_POINT_SCOPE_TEXT ) ;
for ( String scopeFound : scopes ) {
List < String > vreName = helper . evaluate ( " /Resource/Profile/Body/EndPoint[SCOPE=' " + scopeFound . toString ( ) + " ']/VRE_NAME/text() " ) ;
logger . info ( " For scope: " + scopeFound + " , found VRE_NAME " + vreName ) ;
hashVreNameScope . put ( vreName . get ( 0 ) , scopeFound ) ;
2016-12-20 12:11:35 +01:00
}
2018-10-18 18:24:49 +02:00
} catch ( Exception e ) {
throw new ApplicationProfileNotFoundException ( " Error during parsing application profile with resource name: " + resourceName + " in the scope: " + scope . toString ( ) ) ;
}
2016-12-20 12:11:35 +01:00
}
} catch ( Exception e ) {
logger . error ( " Error while trying to fetch applicationProfile profile from the infrastructure " , e ) ;
}
}
2016-12-20 15:27:08 +01:00
2017-02-28 16:55:31 +01:00
/ * *
* Gets the root document .
*
* @return the rootDocument
* /
public Element getRootDocument ( ) {
return rootElement ;
}
/ * *
* Gets the document .
*
* @return the document
* /
public Document getDocument ( ) {
return document ;
}
2016-12-20 15:27:08 +01:00
/ * *
2017-02-28 16:55:31 +01:00
* Gets the hash vre name scope .
*
2016-12-20 15:27:08 +01:00
* @return the hashVreNameScope
* /
public Map < String , String > getHashVreNameScope ( ) {
return hashVreNameScope ;
}
2016-12-20 12:11:35 +01:00
/ * ( non - Javadoc )
* @see java . lang . Object # toString ( )
* /
@Override
public String toString ( ) {
2016-12-20 15:27:08 +01:00
2016-12-20 12:11:35 +01:00
StringBuilder builder = new StringBuilder ( ) ;
2016-12-20 15:27:08 +01:00
builder . append ( " ApplicationProfileReaderForCatalogueResolver [logger= " ) ;
builder . append ( logger ) ;
builder . append ( " , secondaryType= " ) ;
2016-12-20 12:11:35 +01:00
builder . append ( secondaryType ) ;
2016-12-20 15:27:08 +01:00
builder . append ( " , resourceName= " ) ;
2016-12-20 12:11:35 +01:00
builder . append ( resourceName ) ;
builder . append ( " , scope= " ) ;
builder . append ( scope ) ;
2016-12-20 15:27:08 +01:00
builder . append ( " , useRootScope= " ) ;
builder . append ( useRootScope ) ;
builder . append ( " , hashVreNameScope= " ) ;
builder . append ( hashVreNameScope ) ;
2016-12-20 12:11:35 +01:00
builder . append ( " ] " ) ;
return builder . toString ( ) ;
}
2016-12-20 15:27:08 +01:00
/ * public static void main ( String [ ] args ) {
2016-12-20 12:11:35 +01:00
String scope = " /gcube " ;
2016-12-20 15:27:08 +01:00
ApplicationProfileReaderForCatalogueResolver reader = new ApplicationProfileReaderForCatalogueResolver ( scope , true ) ;
2016-12-20 12:11:35 +01:00
System . out . println ( reader ) ;
} * /
}