@ -7,18 +7,28 @@ import java.util.List;
import javax.portlet.ResourceRequest ;
import javax.servlet.http.HttpServletRequest ;
import org.gcube.common.authorization.library.provider. Security TokenProvider;
import org.gcube.common.authorization.library.provider. Access TokenProvider;
import org.gcube.common.portal.GCubePortalConstants ;
import org.gcube.common.portal.PortalContext ;
import org.gcube.common.scope.api.ScopeProvider ;
import org.gcube.common.storagehub.client.plugins.AbstractPlugin ;
import org.gcube.common.storagehub.client.proxies.ItemManagerClient ;
import org.gcube.common.storagehub.client.proxies.WorkspaceManagerClient ;
import org.gcube.common.storagehub.model.items.Item ;
import org.gcube.oidc.rest.JWTToken ;
import org.gcube.portal.oidc.lr62.InvalidTokenException ;
import org.gcube.portal.oidc.lr62.JWTTokenUtil ;
import org.gcube.portal.oidc.lr62.MissingTokenException ;
import org.gcube.portal.oidc.lr62.NotAuthorizedException ;
import org.gcube.portal.oidc.lr62.OIDCUmaUtil ;
import org.gcube.portal.oidc.lr62.RefreshException ;
import org.gcube.portal.oidc.lr62.UMAException ;
import org.gcube.portlets.user.wswidget.shared.AuthorizedUser ;
import org.gcube.portlets.user.wswidget.shared.WSItem ;
import com.liferay.portal.kernel.log.Log ;
import com.liferay.portal.kernel.log.LogFactoryUtil ;
import com.liferay.portal.util.PortalUtil ;
public class StorageHubServiceUtil {
private static Log _log = LogFactoryUtil . getLog ( StorageHubServiceUtil . class ) ;
@ -29,13 +39,12 @@ public class StorageHubServiceUtil {
* @param itemName
* @param from
* @param offset
* @return
* @return the list of items
* /
public static List < WSItem > getItemChildren ( AuthorizedUser authUser , String itemId , String itemName , int from , int offset ) {
WSItem toReturn = new WSItem ( itemId , itemName , true ) ;
_log . debug ( "getItemChildren folder/item = " + itemId ) ;
ArrayList < WSItem > children = new ArrayList < > ( ) ;
SecurityTokenProvider . instance . set ( authUser . getSecurityToken ( ) ) ;
ItemManagerClient client = AbstractPlugin . item ( ) . build ( ) ;
List < ? extends Item > theChildren = null ;
try {
@ -61,21 +70,17 @@ public class StorageHubServiceUtil {
}
toReturn . setChildren ( children ) ;
Collections . sort ( toReturn . getChildren ( ) , new ItemComparator ( ) ) ;
_log . debug ( " *** Returning children size: "+ toReturn . getChildren ( ) . size ( ) ) ;
_log . debug ( " Returning children size: "+ toReturn . getChildren ( ) . size ( ) ) ;
return children ;
}
/ * *
*
* /
public static int getItemChildrenCount ( ResourceRequest request , String itemId ) {
String userName = Utils . getCurrentUser ( request ) . getUsername ( ) ;
String scope = Utils . getCurrentContext ( request ) ;
String authorizationToken = Utils . getCurrentUserToken ( scope , userName ) ;
SecurityTokenProvider . instance . set ( authorizationToken ) ;
ItemManagerClient client = AbstractPlugin . item ( ) . build ( ) ;
int toReturn = - 1 ;
try {
client . childrenCount ( itemId , false ) ;
toReturn = client . childrenCount ( itemId , false ) ;
}
catch ( Exception e ) {
e . printStackTrace ( ) ;
@ -91,7 +96,6 @@ public class StorageHubServiceUtil {
* /
public static List < WSItem > getRootChildren ( AuthorizedUser authUser , int from , int offset ) {
try {
SecurityTokenProvider . instance . set ( authUser . getSecurityToken ( ) ) ;
WorkspaceManagerClient client = AbstractPlugin . workspace ( ) . build ( ) ;
Item itemRoot = client . getWorkspace ( "hl:accounting" ) ;
WSItem root = new WSItem ( itemRoot . getId ( ) , Utils . HOME_LABEL , true ) ;
@ -112,7 +116,6 @@ public class StorageHubServiceUtil {
WSItem toReturn = new WSItem ( "recents" , "Recent Documents" , true ) ;
_log . debug ( "getRecentItems " ) ;
ArrayList < WSItem > children = new ArrayList < > ( ) ;
SecurityTokenProvider . instance . set ( authUser . getSecurityToken ( ) ) ;
WorkspaceManagerClient wsclient = AbstractPlugin . workspace ( ) . build ( ) ;
List < ? extends Item > theChildren = wsclient . getRecentModifiedFilePerVre ( ) ;
@ -136,14 +139,19 @@ public class StorageHubServiceUtil {
* @return the id of the VRE Folder associated to the given context
* /
public static String getWorkspaceFolderURL ( HttpServletRequest request ) {
String userName = Utils . getCurrentUser ( request ) . getUsername ( ) ;
String scope = Utils . getCurrentContext ( request ) ;
String authorizationToken = Utils . getCurrentUserToken ( scope , userName ) ;
SecurityTokenProvider . instance . set ( authorizationToken ) ;
String siteLandingPagePath = PortalContext . getConfiguration ( ) . getSiteLandingPagePath ( request ) ;
String toReturn = siteLandingPagePath ;
try {
String currentContext = Utils . getCurrentContext ( request ) ;
try {
long userId = PortalUtil . getUser ( request ) . getUserId ( ) ;
JWTToken umaToken = OIDCUmaUtil . getUMAToken ( request , userId , currentContext ) ;
AccessTokenProvider . instance . set ( JWTTokenUtil . getAccessTokenString ( umaToken ) ) ;
} catch ( Exception e1 ) {
e1 . printStackTrace ( ) ;
}
ScopeProvider . instance . set ( currentContext ) ;
WorkspaceManagerClient wsclient = AbstractPlugin . workspace ( ) . build ( ) ;
String itemId = wsclient . getVreFolder ( "hl:accounting" ) . getId ( ) ;
toReturn = new String ( new StringBuffer ( siteLandingPagePath )
@ -160,23 +168,31 @@ public class StorageHubServiceUtil {
* @param request
* @return the VRE Folders Id
* /
public static String getVREFoldersId ( HttpServletRequest request ) {
String userName = Utils . getCurrentUser ( request ) . getUsername ( ) ;
String scope = Utils . getCurrentContext ( request ) ;
String authorizationToken = Utils . getCurrentUserToken ( scope , userName ) ;
SecurityTokenProvider . instance . set ( authorizationToken ) ;
public static String getVREFoldersId ( long groupId , HttpServletRequest request , String currentUsername ) {
String toReturn = "" ;
String currentContext = PortalContext . getConfiguration ( ) . getCurrentScope ( "" + groupId ) ;
String username = currentUsername ;
try {
JWTToken umaToken = OIDCUmaUtil . getUMAToken ( request , username , currentContext ) ;
AccessTokenProvider . instance . set ( JWTTokenUtil . getAccessTokenString ( umaToken ) ) ;
} catch ( InvalidTokenException | MissingTokenException | RefreshException | NotAuthorizedException
| UMAException e1 ) {
_log . warn ( "could not get UMA Token for context=" + currentContext ) ;
e1 . printStackTrace ( ) ;
}
_log . debug ( "getVREFoldersId, context=" + currentContext ) ;
ScopeProvider . instance . set ( currentContext ) ;
try {
WorkspaceManagerClient wsclient = AbstractPlugin . workspace ( ) . build ( ) ;
try {
List < ? extends Item > list = wsclient . getVreFolders ( "hl:accounting" ) ;
toReturn = list . iterator ( ) . next ( ) . getParentId ( ) ;
} catch ( Exception e ) {
_log . info ( "This user has no VRE Folders" , e ) ;
} catch ( java. util . NoSuchElement Exception e x ) {
_log . info ( "This user has no VRE Folders" );
return null ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
_log. warn ( "Could not load VREFolders id" + e . getMessage ( ) ) ;
}
return toReturn ;
}