2014-10-13 18:55:53 +02:00
package org.gcube.datatransfer.resolver.applicationprofile ;
import static org.gcube.resources.discovery.icclient.ICFactory.client ;
import java.io.StringReader ;
import java.util.List ;
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
import org.apache.log4j.Logger ;
import org.gcube.common.resources.gcore.utils.XPathHelper ;
2016-09-07 15:54:30 +02:00
import org.gcube.common.scope.api.ScopeProvider ;
2016-12-20 15:27:08 +01:00
import org.gcube.datatransfer.resolver.applicationprofile.GcubeQuery.FIELD_TYPE ;
2018-11-08 15:47:33 +01:00
import org.gcube.datatransfer.resolver.util.ScopeUtil ;
2014-10-13 18:55:53 +02:00
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.w3c.dom.Node ;
import org.xml.sax.InputSource ;
2016-09-07 15:07:46 +02:00
2014-10-13 18:55:53 +02:00
/ * *
2016-09-07 15:07:46 +02:00
* The Class ApplicationProfileReader .
2016-05-12 17:28:18 +02:00
*
2014-10-13 18:55:53 +02:00
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
2016-09-07 15:07:46 +02:00
* Sep 6 , 2016
2014-10-13 18:55:53 +02:00
* /
public class ApplicationProfileReader {
2016-05-12 17:28:18 +02:00
2017-07-03 16:25:14 +02:00
/ * *
*
* /
private static final String RESOURCE_PROFILE_BODY_END_POINT_URL = " /Resource/Profile/Body/EndPoint/URL " ;
protected static final String RESOURCE_PROFILE_BODY_END_POINT_URL_TEXT = RESOURCE_PROFILE_BODY_END_POINT_URL + " /text() " ;
2014-10-13 18:55:53 +02:00
protected static final String RESOURCE_PROFILE_BODY_END_POINT_SCOPE_TEXT = " /Resource/Profile/Body/EndPoint/Scope/text() " ;
protected static final String RESOURCE_PROFILE_BODY_TEXT = " /Resource/Profile/Body/text() " ;
protected static final String RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT = " /Resource/Profile/Body/ThumbnailURL/text() " ;
protected static final String RESOURCE_PROFILE_BODY_APP_ID_TEXT = " /Resource/Profile/Body/AppId/text() " ;
protected static final String RESOURCE_PROFILE_DESCRIPTION_TEXT = " /Resource/Profile/Description/text() " ;
protected static final String RESOURCE_PROFILE_NAME_TEXT = " /Resource/Profile/Name/text() " ;
2016-05-12 17:28:18 +02:00
2014-10-13 18:55:53 +02:00
private Logger logger = Logger . getLogger ( ApplicationProfileReader . class ) ;
private String secondaryType ;
private String appId ;
private String scope ;
private ApplicationProfile applicationProfile ;
2016-12-02 16:59:04 +01:00
private boolean useRootScope = false ;
2016-05-12 17:28:18 +02:00
2014-10-13 18:55:53 +02:00
/ * *
2016-09-07 15:07:46 +02:00
* Instantiates a new application profile reader .
2016-05-12 17:28:18 +02:00
*
2016-12-02 16:59:04 +01:00
* @param scope - the scope where to search the Application Profile
* @param secondaryType the secondary type of Application Profile
* @param portletClassName - the AppId of Generic Resource
* @param useRootScope the use root scope , if true the root scope is used to discovery the Application Profile , otherwise scope is used
2014-10-13 18:55:53 +02:00
* /
2016-12-02 16:59:04 +01:00
public ApplicationProfileReader ( String scope , String secondaryType , String portletClassName , boolean useRootScope ) {
2014-10-13 18:55:53 +02:00
this . scope = scope ;
this . secondaryType = secondaryType ;
this . appId = portletClassName ;
2016-12-02 16:59:04 +01:00
this . useRootScope = useRootScope ;
2016-05-12 17:28:18 +02:00
this . applicationProfile = readProfileFromInfrastrucure ( ) ;
2014-10-13 18:55:53 +02:00
}
2016-09-07 15:07:46 +02:00
/ * *
* Gets the application profile .
*
* @return the application profile
* /
2014-10-13 18:55:53 +02:00
public ApplicationProfile getApplicationProfile ( ) {
return applicationProfile ;
}
/ * *
2016-09-07 15:07:46 +02:00
* this method looks up the applicationProfile profile among the ones available in the infrastructure .
*
2014-10-13 18:55:53 +02:00
* @return the applicationProfile profile
* /
private ApplicationProfile readProfileFromInfrastrucure ( ) {
2017-03-24 17:38:36 +01:00
String originalScope = ScopeProvider . instance . get ( ) ;
2014-10-13 18:55:53 +02:00
ApplicationProfile appProf = new ApplicationProfile ( ) ;
2016-12-20 15:27:08 +01:00
String queryString = GcubeQuery . getGcubeGenericResource ( secondaryType , FIELD_TYPE . APP_ID , appId ) ;
2016-05-12 17:28:18 +02:00
2014-10-13 18:55:53 +02:00
try {
2016-05-12 17:28:18 +02:00
2016-09-07 15:54:30 +02:00
originalScope = ScopeProvider . instance . get ( ) ;
2016-12-02 16:59:04 +01:00
String discoveryScope = useRootScope ? ScopeUtil . getInfrastructureNameFromScope ( scope ) : scope ;
ScopeProvider . instance . set ( discoveryScope ) ;
2017-07-03 16:25:14 +02:00
logger . info ( " Trying to fetch Generic Resource in the scope: " + discoveryScope + " , SecondaryType: " + secondaryType + " , AppId: " + appId ) ;
2014-10-13 18:55:53 +02:00
Query q = new QueryBox ( queryString ) ;
DiscoveryClient < String > client = client ( ) ;
List < String > appProfile = client . submit ( q ) ;
2016-05-12 17:28:18 +02:00
if ( appProfile = = null | | appProfile . size ( ) = = 0 )
2017-07-03 16:25:14 +02:00
throw new ApplicationProfileNotFoundException ( " Generic Resource with SecondaryType: " + secondaryType + " , AppId: " + appId + " is not registered in the scope: " + discoveryScope ) ;
2014-10-13 18:55:53 +02:00
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 ) ;
2016-05-12 17:28:18 +02:00
2017-07-03 16:25:14 +02:00
//set scope
appProf . setScope ( discoveryScope ) ;
2014-10-13 18:55:53 +02:00
List < String > currValue = null ;
currValue = helper . evaluate ( RESOURCE_PROFILE_NAME_TEXT ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
appProf . setName ( currValue . get ( 0 ) ) ;
2016-05-12 17:28:18 +02:00
}
2017-07-03 16:25:14 +02:00
else throw new ApplicationProfileNotFoundException ( " Your ApplicationProfile NAME was not found in the profile " ) ;
2014-10-13 18:55:53 +02:00
currValue = helper . evaluate ( RESOURCE_PROFILE_DESCRIPTION_TEXT ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
appProf . setDescription ( currValue . get ( 0 ) ) ;
2016-05-12 17:28:18 +02:00
}
2014-10-13 18:55:53 +02:00
else logger . warn ( " No Description exists for " + appProf . getName ( ) ) ;
2016-05-12 17:28:18 +02:00
2014-10-13 18:55:53 +02:00
currValue = helper . evaluate ( RESOURCE_PROFILE_BODY_APP_ID_TEXT ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
appProf . setKey ( currValue . get ( 0 ) ) ;
}
2017-07-03 16:25:14 +02:00
else throw new ApplicationProfileNotFoundException ( " Your ApplicationProfile ID was not found in the profile, consider adding <AppId> element in <Body> " ) ;
2016-05-12 17:28:18 +02:00
2014-10-13 18:55:53 +02:00
currValue = helper . evaluate ( RESOURCE_PROFILE_BODY_THUMBNAIL_URL_TEXT ) ;
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
appProf . setImageUrl ( currValue . get ( 0 ) ) ;
}
else {
logger . warn ( " Null or empty <ThumbnailURL> element in <Body> " + appProf . getName ( ) ) ;
}
2016-05-12 17:28:18 +02:00
2017-07-03 16:25:14 +02:00
//currValue = helper.evaluate("/Resource/Profile/Body/EndPoint[Scope='"+scope.toString()+"']/Scope/text()");
2016-05-12 17:28:18 +02:00
2017-07-03 16:25:14 +02:00
//RETRIEVE URL
currValue = helper . evaluate ( RESOURCE_PROFILE_BODY_END_POINT_URL_TEXT ) ;
2016-05-12 17:28:18 +02:00
2017-07-03 16:25:14 +02:00
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
String url = currValue . get ( 0 ) ;
// System.out.println("URL "+url);
if ( url ! = null )
appProf . setUrl ( url ) ;
else
throw new ApplicationProfileNotFoundException ( " Your ApplicationProfile URL was not found in the profile for Scope: " + scope . toString ( ) + " , consider adding <EndPoint><URL> element in <Body> " ) ;
2014-10-13 18:55:53 +02:00
}
2017-07-03 16:25:14 +02:00
else throw new ApplicationProfileNotFoundException ( " ApplicationProfile with SecondaryType: " + secondaryType + " , AppId: " + appId + " in the scope: " + discoveryScope + " does not contain " + RESOURCE_PROFILE_BODY_END_POINT_URL + " property, please add it " ) ;
2016-05-12 17:28:18 +02:00
2014-10-13 18:55:53 +02:00
return appProf ;
}
} catch ( Exception e ) {
2016-05-12 18:29:33 +02:00
logger . error ( " Error while trying to fetch applicationProfile profile from the infrastructure " , e ) ;
2014-10-13 18:55:53 +02:00
return null ;
2016-09-07 15:54:30 +02:00
} finally {
if ( originalScope ! = null & & ! originalScope . isEmpty ( ) ) {
ScopeProvider . instance . set ( originalScope ) ;
2017-03-24 17:38:36 +01:00
logger . info ( " scope provider set to orginal scope: " + originalScope ) ;
2016-09-07 15:54:30 +02:00
} else {
ScopeProvider . instance . reset ( ) ;
logger . info ( " scope provider reset " ) ;
}
2014-10-13 18:55:53 +02:00
}
}
2016-05-12 17:28:18 +02:00
2016-09-07 15:07:46 +02:00
/ * ( non - Javadoc )
* @see java . lang . Object # toString ( )
* /
2014-10-13 18:55:53 +02:00
@Override
public String toString ( ) {
StringBuilder builder = new StringBuilder ( ) ;
builder . append ( " ApplicationProfileReader [secondaryType= " ) ;
builder . append ( secondaryType ) ;
builder . append ( " , appId= " ) ;
builder . append ( appId ) ;
builder . append ( " , scope= " ) ;
builder . append ( scope ) ;
builder . append ( " , applicationProfile= " ) ;
builder . append ( applicationProfile ) ;
builder . append ( " ] " ) ;
return builder . toString ( ) ;
}
2016-05-12 17:28:18 +02:00
2017-07-03 16:25:14 +02:00
// public static void main(String[] args) {
//
// String portletClassName = "org.gcube.portlets.user.gisviewerapp.server.GisViewerAppServiceImpl";
// String scope ="/gcube/devNext/NextNext";
// String secondaryType = "ApplicationProfile";
// ApplicationProfileReader reader = new ApplicationProfileReader(scope, secondaryType, portletClassName, true);
//
// System.out.println(reader);
//
// }
2014-10-13 18:55:53 +02:00
}