2017-04-25 15:41:27 +02:00
package org.gcube.portlets.widgets.ckandatapublisherwidget.server.utils ;
import static org.gcube.common.authorization.client.Constants.authorizationService ;
2019-06-12 12:26:30 +02:00
import java.net.MalformedURLException ;
import java.net.URL ;
2017-04-25 15:41:27 +02:00
import java.util.ArrayList ;
import java.util.List ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpSession ;
import org.gcube.common.authorization.client.exceptions.ObjectNotFound ;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider ;
import org.gcube.common.authorization.library.provider.UserInfo ;
import org.gcube.common.portal.PortalContext ;
import org.gcube.common.scope.api.ScopeProvider ;
2021-04-09 14:51:55 +02:00
import org.gcube.datacatalogue.utillibrary.server.ApplicationProfileScopePerUrlReader ;
2017-04-25 15:41:27 +02:00
import org.gcube.vomanagement.usermanagement.GroupManager ;
import org.gcube.vomanagement.usermanagement.exception.GroupRetrievalFault ;
import org.gcube.vomanagement.usermanagement.exception.UserManagementSystemException ;
import org.gcube.vomanagement.usermanagement.impl.LiferayGroupManager ;
import org.gcube.vomanagement.usermanagement.model.GCubeGroup ;
import org.gcube.vomanagement.usermanagement.model.GCubeUser ;
2021-04-09 14:51:55 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2017-04-25 15:41:27 +02:00
/ * *
2019-06-12 12:26:30 +02:00
* Util class with static methods .
*
2017-04-25 15:41:27 +02:00
* @author Costantino Perciante at ISTI - CNR ( costantino . perciante @isti.cnr.it )
* /
public class GenericUtils {
// Logger
//private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Utils.class);
2021-04-09 14:51:55 +02:00
private static final Logger logger = LoggerFactory . getLogger ( GenericUtils . class ) ;
2017-04-25 15:41:27 +02:00
public static final String GCUBE_REQUEST_URL = " gcube-request-url " ;
/ * *
2019-06-12 12:26:30 +02:00
* Given a ckan organization name retrieve the infrastructure scope .
*
2017-04-25 15:41:27 +02:00
* @param organizationName ( prevre , devvre , . . . )
* @return the scope of the infrastructure
2019-06-12 12:26:30 +02:00
* @throws Exception the exception
2017-04-25 15:41:27 +02:00
* /
public static String retrieveScopeFromOrganizationName ( String organizationName ) throws Exception {
logger . debug ( " Organization name is " + organizationName ) ;
GroupManager gm = new LiferayGroupManager ( ) ;
List < GCubeGroup > groups = gm . listGroups ( ) ;
for ( GCubeGroup gCubeGroup : groups ) {
2021-04-09 14:51:55 +02:00
if ( gCubeGroup . getGroupName ( ) . equalsIgnoreCase ( organizationName ) ) {
String theScope = gm . getInfrastructureScope ( gCubeGroup . getGroupId ( ) ) ;
logger . info ( " For organizationName: " + organizationName + " by using " + LiferayGroupManager . class . getSimpleName ( ) + " got the scope: " + theScope + " , returning it " ) ;
return theScope ;
}
2017-04-25 15:41:27 +02:00
}
2021-04-09 14:51:55 +02:00
logger . info ( " No scope detected for organizationName: " + organizationName + " by using " + LiferayGroupManager . class . getSimpleName ( ) ) ;
2017-04-25 15:41:27 +02:00
return null ;
}
2021-04-09 14:51:55 +02:00
2017-04-25 15:41:27 +02:00
/ * *
2019-06-12 12:26:30 +02:00
* First check to retrieve the token , else create it .
*
* @param username the username
* @param context the context
2017-04-25 15:41:27 +02:00
* @return the user token for the context
* /
public static String tryGetElseCreateToken ( String username , String context ) {
String token = null ;
try {
try {
logger . debug ( " Checking if token for user " + username + " in context " + context + " already exists... " ) ;
token = authorizationService ( ) . resolveTokenByUserAndContext ( username , context ) ;
logger . debug ( " It exists! " ) ;
} catch ( ObjectNotFound e ) {
logger . info ( " Creating token for user " + username + " and context " + context ) ;
token = authorizationService ( ) . generateUserToken ( new UserInfo ( username , new ArrayList < String > ( ) ) , context ) ;
logger . debug ( " received token: " + token . substring ( 0 , 5 ) + " *********************** " ) ;
}
} catch ( Exception e ) {
logger . error ( " Failed both token retrieval and creation " , e ) ;
}
return token ;
}
/ * *
2019-06-12 12:26:30 +02:00
* Get the scope in which ckan information needs to be discovered from the url .
*
* @param httpServletRequest the http servlet request
* @return the scope from client url
2017-04-25 15:41:27 +02:00
* /
public static String getScopeFromClientUrl ( HttpServletRequest httpServletRequest ) {
if ( httpServletRequest = = null )
throw new IllegalArgumentException ( " HttpServletRequest is null! " ) ;
String scopeToReturn = null ;
try {
String clientUrl = getCurrentClientUrl ( httpServletRequest ) . split ( " \\ ? " ) [ 0 ] ;
logger . debug ( " Client url is " + clientUrl ) ;
// check if this information is in session, otherwise set it and return
HttpSession session = httpServletRequest . getSession ( ) ;
if ( ( scopeToReturn = ( String ) session . getAttribute ( clientUrl ) ) ! = null ) {
logger . debug ( " Scope to return is " + scopeToReturn ) ;
} else {
// ask to the ckan library and set it
scopeToReturn = ApplicationProfileScopePerUrlReader . getScopePerUrl ( clientUrl ) ;
logger . debug ( " Scope to return is " + scopeToReturn ) ;
session . setAttribute ( clientUrl , scopeToReturn ) ;
}
} catch ( Exception e ) {
scopeToReturn = getCurrentContext ( httpServletRequest , false ) ;
logger . warn ( " Failed to determine the scope from the client url, returning the current one: " + scopeToReturn ) ;
}
return scopeToReturn ;
}
/ * *
2019-06-12 12:26:30 +02:00
* Needed to get the url of the client .
*
2017-04-25 15:41:27 +02:00
* @param httpServletRequest the httpServletRequest object
2019-02-22 15:15:25 +01:00
* @return the instance of the user
2017-04-25 15:41:27 +02:00
* @see the url at client side
* /
public static String getCurrentClientUrl ( HttpServletRequest httpServletRequest ) {
return httpServletRequest . getHeader ( GCUBE_REQUEST_URL ) ;
}
2019-06-12 12:26:30 +02:00
2017-04-25 15:41:27 +02:00
/ * *
2019-06-12 12:26:30 +02:00
* Gets the gateway client hostname .
*
* @param httpServletRequest the http servlet request
* @return the gateway client hostname
* /
public static String getGatewayClientHostname ( HttpServletRequest httpServletRequest ) {
String clientURL = getCurrentClientUrl ( httpServletRequest ) ;
try {
URL gURL = new URL ( clientURL ) ;
StringBuilder theURL = new StringBuilder ( ) ;
2019-06-12 15:03:08 +02:00
// theURL.append(gURL.getProtocol());
// theURL.append("://");
2019-06-12 12:26:30 +02:00
theURL . append ( gURL . getHost ( ) ) ;
return theURL . toString ( ) ;
} catch ( MalformedURLException e ) {
logger . warn ( " Failed to determine the gateway from the client url: " + clientURL ) ;
return null ;
}
}
/ * *
* Retrieve the current user by using the portal manager .
*
* @param request the request
2017-04-25 15:41:27 +02:00
* @return a GcubeUser object
* /
public static GCubeUser getCurrentUser ( HttpServletRequest request ) {
if ( request = = null )
throw new IllegalArgumentException ( " HttpServletRequest is null! " ) ;
PortalContext pContext = PortalContext . getConfiguration ( ) ;
GCubeUser user = pContext . getCurrentUser ( request ) ;
logger . debug ( " Returning user " + user ) ;
return user ;
}
/ * *
2019-06-12 12:26:30 +02:00
* Retrieve the current scope by using the portal manager .
*
* @param request the request
* @param setInThread the set in thread
2017-04-25 15:41:27 +02:00
* @return a GcubeUser object
* /
public static String getCurrentContext ( HttpServletRequest request , boolean setInThread ) {
if ( request = = null )
throw new IllegalArgumentException ( " HttpServletRequest is null! " ) ;
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String context = pContext . getCurrentScope ( request ) ;
logger . debug ( " Returning context " + context ) ;
if ( context ! = null & & setInThread )
ScopeProvider . instance . set ( context ) ;
return context ;
}
2019-02-22 15:15:25 +01:00
2017-04-25 15:41:27 +02:00
/ * *
2019-06-12 12:26:30 +02:00
* Retrieve the current token by using the portal manager .
*
* @param request the request
* @param setInThread the set in thread
2017-04-25 15:41:27 +02:00
* @return a GcubeUser object
* /
public static String getCurrentToken ( HttpServletRequest request , boolean setInThread ) {
if ( request = = null )
throw new IllegalArgumentException ( " HttpServletRequest is null! " ) ;
PortalContext pContext = PortalContext . getConfiguration ( ) ;
String token = pContext . getCurrentUserToken ( getCurrentContext ( request , false ) , getCurrentUser ( request ) . getUsername ( ) ) ;
2019-02-22 15:15:25 +01:00
logger . debug ( " Returning token " + token . substring ( 0 , token . length ( ) - 7 ) + " XXXXX " ) ;
2017-04-25 15:41:27 +02:00
if ( token ! = null & & setInThread )
SecurityTokenProvider . instance . set ( token ) ;
return token ;
}
/ * *
2019-06-12 12:26:30 +02:00
* Retrieve the group given the scope .
*
* @param scope the scope
* @return the group from scope
* @throws UserManagementSystemException the user management system exception
* @throws GroupRetrievalFault the group retrieval fault
2017-04-25 15:41:27 +02:00
* /
public static GCubeGroup getGroupFromScope ( String scope ) throws UserManagementSystemException , GroupRetrievalFault {
if ( scope = = null | | scope . isEmpty ( ) )
throw new IllegalArgumentException ( " Scope is missing here!! " ) ;
GroupManager gm = new LiferayGroupManager ( ) ;
long groupId = gm . getGroupIdFromInfrastructureScope ( scope ) ;
return gm . getGroup ( groupId ) ;
}
}