2012-12-08 19:49:21 +01:00
package org.gcube.applicationsupportlayer.social ;
2013-09-25 15:51:00 +02:00
import static org.gcube.resources.discovery.icclient.ICFactory.client ;
import java.io.StringReader ;
2013-01-03 16:28:36 +01:00
import java.util.List ;
2013-09-25 15:51:00 +02:00
import javax.xml.parsers.DocumentBuilder ;
import javax.xml.parsers.DocumentBuilderFactory ;
2013-01-03 16:28:36 +01:00
import org.gcube.applicationsupportlayer.social.ex.ApplicationProfileNotFoundException ;
2016-02-25 18:57:25 +01:00
import org.gcube.applicationsupportlayer.social.shared.SocialNetworkingUser ;
2013-09-25 15:51:00 +02:00
import org.gcube.common.resources.gcore.utils.XPathHelper ;
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.common.scope.impl.ScopeBean ;
2012-12-08 19:49:21 +01:00
import org.gcube.portal.databook.server.DBCassandraAstyanaxImpl ;
import org.gcube.portal.databook.server.DatabookStore ;
2013-01-03 16:28:36 +01:00
import org.gcube.portal.databook.shared.ApplicationProfile ;
2013-09-25 15:51:00 +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 ;
2013-09-30 15:43:29 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2013-09-25 15:51:00 +02:00
import org.w3c.dom.Node ;
import org.xml.sax.InputSource ;
2012-12-08 19:49:21 +01:00
/ * *
*
* @author Massimiliano Assante , ISTI - CNR
*
2013-01-03 16:28:36 +01:00
* superclass for notifications , posting news and so on
2012-12-08 19:49:21 +01:00
* /
2013-01-03 16:28:36 +01:00
public class SocialPortalBridge {
2012-12-09 17:11:26 +01:00
2013-09-30 15:43:29 +02:00
private static final Logger _log = LoggerFactory . getLogger ( SocialPortalBridge . class ) ;
2016-02-25 18:57:25 +01:00
protected String currScope ;
protected String currGroupName ;
protected SocialNetworkingUser currUser ;
2013-01-03 16:28:36 +01:00
protected ApplicationProfile applicationProfile ;
2012-12-09 17:11:26 +01:00
//unique instance
private static DatabookStore store ;
2013-01-09 17:47:22 +01:00
/ * *
*
2016-02-25 18:57:25 +01:00
* @param scope the current scope
* @param currUser an instance of { @link SocialNetworkingUser } filled with the required user data
2013-01-09 17:47:22 +01:00
* /
2016-02-25 18:57:25 +01:00
public SocialPortalBridge ( String scope , SocialNetworkingUser currUser ) {
this . currScope = scope ;
this . currGroupName = extractGroupNameFromScope ( currScope ) ;
this . currUser = currUser ;
2013-01-09 17:47:22 +01:00
this . applicationProfile = null ;
2016-02-25 18:57:25 +01:00
_log . debug ( " ASLSocial instanciated with current scope= " + currScope + " currentGroup name= " + currGroupName ) ;
}
/ * *
*
* @param scope the current scope
* @param currUser an instance of { @link SocialNetworkingUser } filled with the required user data
* @param portletClassName your application unique identifier registered in the infrastructure
* /
public SocialPortalBridge ( String scope , SocialNetworkingUser currUser , String portletClassName ) {
this ( scope , currUser ) ;
this . applicationProfile = getProfileFromInfrastrucure ( portletClassName ) ;
2016-09-26 12:14:27 +02:00
_log . debug ( " ASLSocial called on " + portletClassName ) ;
2013-01-09 17:47:22 +01:00
}
2016-02-25 18:57:25 +01:00
2012-12-12 17:16:21 +01:00
/ * *
*
* @return the unique instance of the store
* /
2012-12-09 17:11:26 +01:00
public static synchronized DatabookStore getStoreInstance ( ) {
if ( store = = null ) {
store = new DBCassandraAstyanaxImpl ( ) ;
}
return store ;
2012-12-08 19:49:21 +01:00
}
/ * *
* Escape an html string . Escaping data received from the client helps to
* prevent cross - site script vulnerabilities .
*
* @param html the html string to escape
* @return the escaped string
* /
2012-12-12 17:16:21 +01:00
protected String escapeHtml ( String html ) {
2012-12-08 19:49:21 +01:00
if ( html = = null ) {
return null ;
}
return html . replaceAll ( " & " , " & " ) . replaceAll ( " < " , " < " )
. replaceAll ( " > " , " > " ) ;
}
2013-01-03 16:28:36 +01:00
/ * *
* @return the applicationProfile profile among the ones available in the infrastructure
* /
public ApplicationProfile getApplicationProfile ( ) {
return applicationProfile ;
}
/ * *
* this method looks up the applicationProfile profile among the ones available in the infrastructure
* @param portletClassName your servlet class name will be used ad unique identifier for your applicationProfile
* @return the applicationProfile profile
* /
private ApplicationProfile getProfileFromInfrastrucure ( String portletClassName ) {
2016-02-25 18:57:25 +01:00
ScopeBean scope = new ScopeBean ( currScope ) ;
2013-06-10 18:31:20 +02:00
_log . debug ( " Trying to fetch applicationProfile profile from the infrastructure for " + portletClassName + " scope: " + scope ) ;
2013-01-03 16:28:36 +01:00
try {
ApplicationProfile toReturn = new ApplicationProfile ( ) ;
2013-09-25 15:51:00 +02:00
Query q = new QueryBox ( " for $profile in collection('/db/Profiles/GenericResource')//Resource " +
2013-01-03 16:28:36 +01:00
" where $profile/Profile/SecondaryType/string() eq 'ApplicationProfile' and $profile/Profile/Body/AppId/string() " +
" eq ' " + portletClassName + " ' " +
" return $profile " ) ;
2013-06-10 18:31:20 +02:00
2013-01-03 16:28:36 +01:00
2013-09-25 15:51:00 +02:00
String currScope = ScopeProvider . instance . get ( ) ;
2017-01-16 18:10:09 +01:00
ScopeBeanExt sbe = new ScopeBeanExt ( this . currScope ) ;
String scopeToQuery = sbe . getInfrastructureScope ( ) ;
ScopeProvider . instance . set ( scopeToQuery ) ;
2013-09-25 15:51:00 +02:00
DiscoveryClient < String > client = client ( ) ;
List < String > appProfile = client . submit ( q ) ;
2013-01-03 16:28:36 +01:00
if ( appProfile = = null | | appProfile . size ( ) = = 0 )
throw new ApplicationProfileNotFoundException ( " Your applicationProfile is not registered in the infrastructure " ) ;
else {
2013-09-25 15:51:00 +02:00
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 ) ;
2013-01-03 16:28:36 +01:00
List < String > currValue = null ;
2013-09-25 15:51:00 +02:00
currValue = helper . evaluate ( " /Resource/Profile/Name/text() " ) ;
2013-01-03 16:28:36 +01:00
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
toReturn . setName ( currValue . get ( 0 ) ) ;
}
else throw new ApplicationProfileNotFoundException ( " Your applicationProfile NAME was not found in the profile " ) ;
2013-09-25 15:51:00 +02:00
currValue = helper . evaluate ( " /Resource/Profile/Description/text() " ) ;
2013-01-03 16:28:36 +01:00
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
toReturn . setDescription ( currValue . get ( 0 ) ) ;
}
else _log . warn ( " No Description exists for " + toReturn . getName ( ) ) ;
2013-09-25 15:51:00 +02:00
currValue = helper . evaluate ( " /Resource/Profile/Body/AppId/text() " ) ;
2013-01-03 16:28:36 +01:00
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
toReturn . setKey ( currValue . get ( 0 ) ) ;
}
else throw new ApplicationProfileNotFoundException ( " Your applicationProfile ID n was not found in the profile, consider adding <AppId> element in <Body> " ) ;
2013-09-25 15:51:00 +02:00
currValue = helper . evaluate ( " /Resource/Profile/Body/ThumbnailURL/text() " ) ;
2013-01-03 16:28:36 +01:00
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
toReturn . setImageUrl ( currValue . get ( 0 ) ) ;
}
else throw new ApplicationProfileNotFoundException ( " Your applicationProfile Image Url was not found in the profile, consider adding <ThumbnailURL> element in <Body> " ) ;
2013-09-25 15:51:00 +02:00
currValue = helper . evaluate ( " /Resource/Profile/Body/EndPoint/Scope/text() " ) ;
2013-01-03 16:28:36 +01:00
if ( currValue ! = null & & currValue . size ( ) > 0 ) {
List < String > scopes = currValue ;
boolean foundUrl = false ;
for ( int i = 0 ; i < scopes . size ( ) ; i + + ) {
if ( currValue . get ( i ) . trim ( ) . compareTo ( scope . toString ( ) ) = = 0 ) {
2013-09-25 15:51:00 +02:00
toReturn . setUrl ( helper . evaluate ( " /Resource/Profile/Body/EndPoint/URL/text() " ) . get ( i ) ) ;
2013-01-03 16:28:36 +01:00
toReturn . setScope ( scope . toString ( ) ) ;
foundUrl = true ;
break ;
}
}
if ( ! foundUrl )
throw new ApplicationProfileNotFoundException ( " Your applicationProfile URL was not found in the profile for Scope: " + scope . toString ( ) ) ;
}
else throw new ApplicationProfileNotFoundException ( " Your applicationProfile EndPoint was not found in the profile, consider adding <EndPoint><Scope> element in <Body> " ) ;
2013-06-10 18:31:20 +02:00
_log . debug ( " Returning " + toReturn ) ;
2013-09-25 15:51:00 +02:00
ScopeProvider . instance . set ( currScope ) ;
2013-01-03 16:28:36 +01:00
return toReturn ;
}
} catch ( Exception e ) {
_log . error ( " Error while trying to fetch applicationProfile profile from the infrastructure " ) ;
e . printStackTrace ( ) ;
return null ;
}
}
2016-02-25 18:57:25 +01:00
private String extractGroupNameFromScope ( String scope ) throws IllegalArgumentException {
if ( scope . indexOf ( '/' ) > - 1 ) {
String [ ] splits = scope . split ( " / " ) ;
String groupName = splits [ splits . length - 1 ] ;
return groupName ;
}
else {
throw new IllegalArgumentException ( " Scope " + scope + " is not a vaild scope, it should start with '/' " ) ;
}
}
2012-12-08 19:49:21 +01:00
}