2016-04-21 10:37:31 +02:00
package org.gcube.portlets.user.workspaceexplorerapp.server ;
2020-04-09 17:58:03 +02:00
import java.net.URL ;
2016-04-21 10:37:31 +02:00
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.List ;
import java.util.concurrent.TimeUnit ;
2020-04-09 17:58:03 +02:00
import javax.servlet.http.HttpSession ;
2016-09-19 12:19:49 +02:00
import org.gcube.common.encryption.StringEncrypter ;
import org.gcube.common.scope.api.ScopeProvider ;
2020-04-08 18:17:48 +02:00
import org.gcube.common.storagehubwrapper.server.tohl.Workspace ;
2020-04-09 17:58:03 +02:00
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceFolder ;
2020-04-09 15:33:21 +02:00
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItem ;
import org.gcube.common.storagehubwrapper.shared.tohl.WorkspaceItemType ;
import org.gcube.common.storagehubwrapper.shared.tohl.items.FileItem ;
2016-04-21 10:37:31 +02:00
import org.gcube.portlets.user.workspaceexplorerapp.client.WorkspaceExplorerAppConstants ;
import org.gcube.portlets.user.workspaceexplorerapp.client.rpc.WorkspaceExplorerAppService ;
2020-04-09 17:58:03 +02:00
import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.ItemBuilder ;
import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.WorkspaceInstance ;
import org.gcube.portlets.user.workspaceexplorerapp.server.workspace.WsUtil ;
2016-04-21 10:37:31 +02:00
import org.gcube.portlets.user.workspaceexplorerapp.shared.FilterCriteria ;
import org.gcube.portlets.user.workspaceexplorerapp.shared.Item ;
import org.gcube.portlets.user.workspaceexplorerapp.shared.ItemType ;
import org.gcube.portlets.user.workspaceexplorerapp.shared.WorkspaceNavigatorServiceException ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
import com.google.gwt.user.server.rpc.RemoteServiceServlet ;
2017-07-03 17:25:05 +02:00
2020-04-09 15:33:21 +02:00
// TODO: Auto-generated Javadoc
2016-04-21 10:37:31 +02:00
/ * *
2017-07-03 17:25:05 +02:00
* The Class WorkspaceExplorerAppServiceImpl .
2016-04-21 10:37:31 +02:00
*
* @author Francesco Mangiacrapa francesco . mangiacrapa @isti.cnr.it
2017-07-03 17:25:05 +02:00
* Jul 3 , 2017
2016-04-21 10:37:31 +02:00
* /
@SuppressWarnings ( " serial " )
public class WorkspaceExplorerAppServiceImpl extends RemoteServiceServlet implements WorkspaceExplorerAppService {
2020-04-09 17:58:03 +02:00
public static final String THE_WORKSPACE_SHUB_INSTANCIED = " THE_WORKSPACE_SHUB_INSTANCIED " ;
2016-04-21 10:37:31 +02:00
/ * *
*
* /
public static final Logger logger = LoggerFactory . getLogger ( WorkspaceExplorerAppServiceImpl . class ) ;
2016-09-19 12:19:49 +02:00
public static final String UTF_8 = " UTF-8 " ;
2016-04-21 10:37:31 +02:00
2020-04-09 17:58:03 +02:00
/ * *
* Gets the workspace .
*
* @return the workspace
* @throws Exception
* /
private Workspace getWorkspace ( ) throws Exception {
HttpSession httpSession = this . getThreadLocalRequest ( ) . getSession ( ) ;
Workspace workspace = ( Workspace ) httpSession . getAttribute ( THE_WORKSPACE_SHUB_INSTANCIED ) ;
if ( workspace = = null ) {
WorkspaceInstance workspaceInstance = new WorkspaceInstance ( httpSession ) ;
workspace = workspaceInstance . get ( ) ;
httpSession . setAttribute ( THE_WORKSPACE_SHUB_INSTANCIED , workspace ) ;
}
return workspace ;
}
2016-04-21 10:37:31 +02:00
/ * *
* { @inheritDoc }
* /
@Override
public Item getRoot ( List < ItemType > showableTypes , boolean purgeEmpyFolders , FilterCriteria filterCriteria ) throws WorkspaceNavigatorServiceException {
2020-04-15 11:57:46 +02:00
logger . debug ( " getRoot showableTypes: " + showableTypes + " purgeEmpyFolders: " + purgeEmpyFolders + " filterCriteria: " + filterCriteria ) ;
2016-04-21 10:37:31 +02:00
try {
2020-04-09 17:58:03 +02:00
Workspace workspace = getWorkspace ( ) ;
2016-04-21 10:37:31 +02:00
logger . trace ( " Start getRoot... " ) ;
2020-04-08 18:17:48 +02:00
org . gcube . common . storagehubwrapper . shared . tohl . WorkspaceFolder root = workspace . getRoot ( ) ;
2016-04-21 10:37:31 +02:00
logger . trace ( " GetRoot - Replyiing root " ) ;
long startTime = System . currentTimeMillis ( ) ;
logger . trace ( " start time - " + startTime ) ;
2020-04-09 17:58:03 +02:00
Item rootItem = ItemBuilder . getItem ( null , root , root . getPath ( ) , showableTypes , filterCriteria ) ;
//adding the children
List < ? extends WorkspaceItem > children = workspace . getChildren ( root . getId ( ) ) ;
rootItem = ItemBuilder . addChildren ( rootItem , " " , children , showableTypes , filterCriteria ) ;
2016-04-21 10:37:31 +02:00
rootItem . setName ( WorkspaceExplorerAppConstants . HOME_LABEL ) ;
rootItem . setIsRoot ( true ) ;
/ * SPECIAL FOLDERS
Item specialFolders = ItemBuilder . getItem ( null , specials , showableTypes , filterCriteria , 2 ) ;
specialFolders . setShared ( true ) ;
rootItem . addChild ( specialFolders ) ;
* /
if ( purgeEmpyFolders ) {
rootItem = ItemBuilder . purgeEmptyFolders ( rootItem ) ;
}
logger . trace ( " Returning: " ) ;
Long endTime = System . currentTimeMillis ( ) - startTime ;
String time = String . format ( " %d msc %d sec " , endTime , TimeUnit . MILLISECONDS . toSeconds ( endTime ) ) ;
logger . info ( " end time - " + time ) ;
Collections . sort ( rootItem . getChildren ( ) , new ItemComparator ( ) ) ;
logger . info ( " Returning children size: " + rootItem . getChildren ( ) . size ( ) ) ;
return rootItem ;
} catch ( Exception e ) {
logger . error ( " Error during root retrieving " , e ) ;
throw new WorkspaceNavigatorServiceException ( " Sorry, an error occurred when performing get root " ) ;
}
}
2020-04-09 15:33:21 +02:00
2016-04-21 10:37:31 +02:00
/ * *
2020-04-09 15:33:21 +02:00
* Gets the folder .
*
* @param item the item
* @param showableTypes the showable types
* @param purgeEmpyFolders the purge empy folders
* @param filterCriteria the filter criteria
* @return the folder
2020-04-09 17:58:03 +02:00
* @throws WorkspaceNavigatorServiceException the workspace navigator service exception
2016-04-21 10:37:31 +02:00
* /
@Override
public Item getFolder ( Item item , List < ItemType > showableTypes , boolean purgeEmpyFolders , FilterCriteria filterCriteria ) throws WorkspaceNavigatorServiceException {
2020-04-15 11:57:46 +02:00
logger . debug ( " getFolder item: " + item + " showableTypes: " + showableTypes + " purgeEmpyFolders: " + purgeEmpyFolders + " filterCriteria: " + filterCriteria ) ;
2016-04-21 10:37:31 +02:00
2020-04-09 15:33:21 +02:00
WorkspaceItem workspaceItem = null ;
2020-04-09 17:58:03 +02:00
Workspace workspace = null ;
2016-04-21 10:37:31 +02:00
try {
if ( item = = null | | item . getId ( ) = = null )
throw new Exception ( " Item id is null " ) ;
2020-04-09 17:58:03 +02:00
workspace = getWorkspace ( ) ;
2020-04-09 15:33:21 +02:00
workspaceItem = workspace . getItem ( item . getId ( ) ) ;
} catch ( Exception e ) {
logger . error ( " Error during item retrieving " , e ) ;
throw new WorkspaceNavigatorServiceException ( " Sorry, an error occurred when performing get folder. Does it still exist? " ) ;
}
if ( workspaceItem ! = null & & workspaceItem . isFolder ( ) ) {
try {
WorkspaceItem folder = workspaceItem ;
logger . trace ( " GetFolder - Replyiing folder " ) ;
long startTime = System . currentTimeMillis ( ) ;
logger . trace ( " start time - " + startTime ) ;
String path = item . getPath ( ) ! = null & & ! item . getPath ( ) . isEmpty ( ) ? item . getPath ( ) : folder . getPath ( ) ;
2020-04-09 17:58:03 +02:00
Item itemFolder = ItemBuilder . getItem ( null , folder , path , showableTypes , filterCriteria ) ;
//adding the children
List < ? extends WorkspaceItem > children = workspace . getChildren ( itemFolder . getId ( ) ) ;
itemFolder = ItemBuilder . addChildren ( itemFolder , path , children , showableTypes , filterCriteria ) ;
2020-04-09 15:33:21 +02:00
// _log.trace("Only showable types:");
if ( purgeEmpyFolders ) {
itemFolder = ItemBuilder . purgeEmptyFolders ( itemFolder ) ;
}
logger . trace ( " Returning: " ) ;
Long endTime = System . currentTimeMillis ( ) - startTime ;
String time = String . format ( " %d msc %d sec " , endTime , TimeUnit . MILLISECONDS . toSeconds ( endTime ) ) ;
logger . info ( " end time - " + time ) ;
Collections . sort ( itemFolder . getChildren ( ) , new ItemComparator ( ) ) ;
return itemFolder ;
} catch ( Exception e ) {
throw new WorkspaceNavigatorServiceException ( e . getMessage ( ) ) ;
2016-04-21 10:37:31 +02:00
}
2020-04-09 15:33:21 +02:00
} else {
logger . error ( " The item requested is not a folder " ) ;
throw new WorkspaceNavigatorServiceException ( " Error, the item requested is not a folder " ) ;
}
2016-04-21 10:37:31 +02:00
}
2020-04-09 17:58:03 +02:00
// /**
// * Gets the item by category.
// *
// * @param category the category
// * @return the item by category
// * @throws WorkspaceNavigatorServiceException the workspace navigator service exception
// */
// /* (non-Javadoc)
// * @see org.gcube.portlets.widgets.wsexplorer.client.rpc.WorkspaceExplorerService#getItemByCategory(org.gcube.portlets.widgets.wsexplorer.shared.ItemCategory)
// */
// @Override
// public Item getItemByCategory(ItemCategory category) throws WorkspaceNavigatorServiceException{
// logger.trace("GetItemByCategory category: "+category);
// throw new WorkspaceNavigatorServiceException("The method getItemByCategory is not implemented");
// }
2016-04-21 10:37:31 +02:00
/ * *
* { @inheritDoc }
* /
@Override
public Item getMySpecialFolder ( List < ItemType > showableTypes , boolean purgeEmpyFolders , FilterCriteria filterCriteria ) throws WorkspaceNavigatorServiceException {
logger . trace ( " GetMySpecialFolder showableTypes: " + showableTypes + " purgeEmpyFolders: " + purgeEmpyFolders + " filterCriteria: " + filterCriteria ) ;
try {
2020-04-09 17:58:03 +02:00
Workspace workspace = getWorkspace ( ) ;
2016-04-21 10:37:31 +02:00
WorkspaceItem folder = workspace . getMySpecialFolders ( ) ;
long startTime = System . currentTimeMillis ( ) ;
2020-04-09 15:33:21 +02:00
2016-04-21 10:37:31 +02:00
logger . trace ( " start time - " + startTime ) ;
2020-04-09 17:58:03 +02:00
Item itemFolder = ItemBuilder . getItem ( null , folder , folder . getPath ( ) , showableTypes , filterCriteria ) ;
//adding the children
List < ? extends WorkspaceItem > children = workspace . getChildren ( itemFolder . getId ( ) ) ;
itemFolder = ItemBuilder . addChildren ( itemFolder , " " , children , showableTypes , filterCriteria ) ;
2016-04-21 10:37:31 +02:00
//OVERRIDING VRE FOLDERS NAME - SET SPECIAL FOLDER /Workspace/MySpecialFolders
itemFolder . setName ( WorkspaceExplorerAppConstants . VRE_FOLDERS_LABEL ) ;
itemFolder . setSpecialFolder ( true ) ;
logger . trace ( " Builded MySpecialFolder: " + itemFolder ) ;
logger . trace ( " Only showable types: " ) ;
if ( purgeEmpyFolders ) {
itemFolder = ItemBuilder . purgeEmptyFolders ( itemFolder ) ;
}
2020-04-09 15:33:21 +02:00
2016-04-21 10:37:31 +02:00
logger . trace ( " Returning: " ) ;
Long endTime = System . currentTimeMillis ( ) - startTime ;
String time = String . format ( " %d msc %d sec " , endTime , TimeUnit . MILLISECONDS . toSeconds ( endTime ) ) ;
logger . trace ( " end time - " + time ) ;
Collections . sort ( itemFolder . getChildren ( ) , new ItemComparator ( ) ) ;
return itemFolder ;
} catch ( Exception e ) {
logger . error ( " Error during special folders retrieving " , e ) ;
throw new WorkspaceNavigatorServiceException ( " Sorry, an error occurred when performing get My Special Folder " ) ;
}
}
2020-04-09 17:58:03 +02:00
// /**
// * {@inheritDoc}
// */
// @Override
// public boolean checkName(String name) throws WorkspaceNavigatorServiceException {
// logger.trace("checkName name: "+name);
// throw new WorkspaceNavigatorServiceException("The method checkName is not implemented");
//// try {
//// Workspace workspace = WsUtil.getWorkspace(this.getThreadLocalRequest().getSession());
//// return workspace.isValidName(name);
//// } catch (Exception e) {
//// logger.error("Error during folder retrieving", e);
//// throw new WorkspaceNavigatorServiceException(e.getMessage());
//// }
// }
2016-04-21 10:37:31 +02:00
/ * protected void printName ( String indentation , Item item )
{
if ( item ! = null ) {
_log . trace ( indentation + item . getName ( ) ) ;
for ( Item child : item . getChildren ( ) ) printName ( indentation + " \ t " , child ) ;
}
} * /
/ * *
* Gets Breadcrumbs ( the list of parents ) by item identifier .
*
* @param itemIdentifier the item identifier
* @param includeItemAsParent - if parameter is true and item passed in input is a folder , the folder is included in path returned as last parent
* @return the list parents by item identifier
* @throws Exception the exception
* /
@Override
public List < Item > getBreadcrumbsByItemIdentifier ( String itemIdentifier , boolean includeItemAsParent ) throws Exception {
logger . trace ( " ListParents By Item Identifier " + itemIdentifier ) ;
try {
2020-04-09 17:58:03 +02:00
Workspace workspace = getWorkspace ( ) ;
2016-04-21 10:37:31 +02:00
WorkspaceItem wsItem = workspace . getItem ( itemIdentifier ) ;
logger . trace ( " workspace retrieve item name: " + wsItem . getName ( ) ) ;
2020-04-09 15:33:21 +02:00
List < ? extends WorkspaceItem > parents = workspace . getParentsById ( itemIdentifier ) ;
2016-04-21 10:37:31 +02:00
logger . trace ( " parents size: " + parents . size ( ) ) ;
Item [ ] arrayParents ;
2016-09-14 16:46:22 +02:00
switch ( parents . size ( ) ) {
case 0 : // itemIdentifier is ROOT
logger . trace ( " itemIdentifier isRoot... " ) ;
2020-04-09 15:33:21 +02:00
if ( includeItemAsParent ) { //ADDING ROOT
org . gcube . common . storagehubwrapper . shared . tohl . WorkspaceFolder wsFolder =
( org . gcube . common . storagehubwrapper . shared . tohl . WorkspaceFolder ) wsItem ;
2016-09-14 16:46:22 +02:00
Item root = ItemBuilder . buildFolderForBreadcrumbs ( wsFolder , null ) ;
List < Item > listParents = new ArrayList < Item > ( 1 ) ;
listParents . add ( root ) ;
2020-04-09 15:33:21 +02:00
//workspaceLogger.trace("returning: "+listParents.toString());
2016-09-14 16:46:22 +02:00
return listParents ;
}
else {
logger . trace ( " returning empty list " ) ;
return new ArrayList < Item > ( ) ; // empty list
}
case 1 : //itemIdentifier is first level from root
logger . trace ( " itemIdentifier is first level... " ) ;
List < Item > listParents = new ArrayList < Item > ( ) ;
2020-04-09 15:33:21 +02:00
org . gcube . common . storagehubwrapper . shared . tohl . WorkspaceFolder wsRootFolder = ( org . gcube . common . storagehubwrapper . shared . tohl . WorkspaceFolder ) parents . get ( 0 ) ; //isRoot
2016-09-14 16:46:22 +02:00
Item root = ItemBuilder . buildFolderForBreadcrumbs ( wsRootFolder , null ) ;
Item parent = null ;
if ( includeItemAsParent ) {
2020-04-09 15:33:21 +02:00
org . gcube . common . storagehubwrapper . shared . tohl . WorkspaceFolder wsFolder1 =
( org . gcube . common . storagehubwrapper . shared . tohl . WorkspaceFolder ) workspace . getItem ( itemIdentifier ) ; //root
2016-09-14 16:46:22 +02:00
parent = ItemBuilder . buildFolderForBreadcrumbs ( wsFolder1 , null ) ;
}
listParents . add ( root ) ;
if ( parent ! = null )
listParents . add ( parent ) ;
// workspaceLogger.trace("returning: "+listParents.toString());
return listParents ;
default :
break ;
2016-04-21 10:37:31 +02:00
}
2016-09-14 16:46:22 +02:00
WorkspaceItem lastItem = parents . get ( parents . size ( ) - 1 ) ;
//CONVERTING LAST ELEMENT IF NECESSARY
logger . trace ( " converting last element.. " ) ;
if ( includeItemAsParent & & lastItem . isFolder ( ) ) { //FIX BUG #298
arrayParents = new Item [ parents . size ( ) ] ;
logger . debug ( " including last item into path: " + lastItem . getName ( ) ) ;
arrayParents [ parents . size ( ) - 1 ] = ItemBuilder . buildFolderForBreadcrumbs ( ( WorkspaceFolder ) lastItem , null ) ;
} else
2016-04-21 10:37:31 +02:00
arrayParents = new Item [ parents . size ( ) - 1 ] ;
/ * * HANDLE MY_SPECIAL_FOLDER TO AVOID COMPLETE PATH WORKSPACE / MY_SPECIAL_FOLDER
* BUT RETURNING ONLY / MY_SPECIAL_FOLDER
* /
if ( wsItem . isFolder ( ) ) {
2020-04-14 17:43:25 +02:00
if ( ItemBuilder . isSpecialFolder ( ( WorkspaceFolder ) wsItem , lastItem . isRoot ( ) ) ) {
2016-04-21 10:37:31 +02:00
return new ArrayList < Item > ( Arrays . asList ( arrayParents ) ) ;
}
}
//CONVERTING PATH
logger . trace ( " converting path from second-last.. " ) ;
for ( int i = parents . size ( ) - 2 ; i > = 0 ; i - - ) {
WorkspaceFolder wsParentFolder = ( WorkspaceFolder ) parents . get ( i ) ;
arrayParents [ i ] = ItemBuilder . buildFolderForBreadcrumbs ( wsParentFolder , null ) ;
if ( arrayParents [ i ] . isSpecialFolder ( ) ) { //SKIP HOME PARENT FOR MY_SPECIAL_FOLDER
logger . info ( " arrayParents index " + i + " is special folder, exit " ) ;
break ;
}
}
//SET PARENTS
logger . trace ( " setting parents.. " ) ;
for ( int i = 0 ; i < arrayParents . length - 1 ; i + + ) {
Item parent = arrayParents [ i ] ;
Item fileModel = arrayParents [ i + 1 ] ;
fileModel . setParent ( parent ) ;
}
logger . trace ( " ListParents return size: " + arrayParents . length ) ;
if ( arrayParents [ 0 ] = = null ) { //EXIT BY BREAK IN CASE OF SPECIAL FOLDER
List < Item > breadcrumbs = new ArrayList < Item > ( arrayParents . length - 1 ) ;
for ( int i = 1 ; i < arrayParents . length ; i + + ) {
breadcrumbs . add ( arrayParents [ i ] ) ;
}
return breadcrumbs ;
}
else {
return new ArrayList < Item > ( Arrays . asList ( arrayParents ) ) ;
}
} catch ( Exception e ) {
logger . error ( " Error in get List Parents By Item Identifier " , e ) ;
throw new Exception ( " Sorry, an error occurred during path retrieving! " ) ;
}
}
/ * *
* Gets the parents by item identifier to limit .
2016-09-14 16:46:22 +02:00
* when limit is reached parents are null
2016-04-21 10:37:31 +02:00
* @param itemIdentifier the item identifier
* @param parentLimit the parent limit
* @param includeItemAsParent the include item as parent
* @return the parents by item identifier to limit
* @throws Exception the exception
* /
@Override
public List < Item > getBreadcrumbsByItemIdentifierToParentLimit ( String itemIdentifier , String parentLimit , boolean includeItemAsParent ) throws Exception {
2020-04-14 17:43:25 +02:00
logger . trace ( " getBreadcrumbsByItemIdentifierToParentLimit by Item Identifier " + itemIdentifier + " and limit: " + parentLimit + " and includeItemAsParent: " + includeItemAsParent ) ;
2016-04-21 10:37:31 +02:00
try {
2020-04-09 17:58:03 +02:00
Workspace workspace = getWorkspace ( ) ;
2016-04-21 10:37:31 +02:00
WorkspaceItem wsItem = workspace . getItem ( itemIdentifier ) ;
logger . trace ( " workspace retrieve item name: " + wsItem . getName ( ) ) ;
2020-04-09 15:33:21 +02:00
List < ? extends WorkspaceItem > parents = null ;
2016-09-20 11:39:53 +02:00
try {
parents = workspace . getParentsById ( itemIdentifier ) ;
} catch ( Exception e ) {
logger . error ( " Error on getting list of parents from HL for id: " + itemIdentifier , e ) ;
parents = new ArrayList < WorkspaceItem > ( ) ;
}
if ( parents = = null ) //avoiding null
parents = new ArrayList < WorkspaceItem > ( ) ;
2020-04-14 17:43:25 +02:00
if ( logger . isDebugEnabled ( ) ) {
for ( WorkspaceItem workspaceItem : parents ) {
logger . debug ( " the parent is: " + workspaceItem . getName ( ) ) ;
}
}
List < Item > arrayParents = new ArrayList < Item > ( ) ;
2016-09-14 16:46:22 +02:00
parentLimit = parentLimit ! = null ? parentLimit : " " ;
switch ( parents . size ( ) ) {
2016-11-10 12:31:25 +01:00
case 0 : // itemIdentifier is ROOT
logger . trace ( " itemIdentifier isRoot... " ) ;
if ( includeItemAsParent ) { //ADDIND ROOT
WorkspaceFolder wsFolder = ( WorkspaceFolder ) wsItem ;
2016-09-14 16:46:22 +02:00
Item root = ItemBuilder . buildFolderForBreadcrumbs ( wsFolder , null ) ;
List < Item > listParents = new ArrayList < Item > ( 1 ) ;
listParents . add ( root ) ;
2016-11-10 12:31:25 +01:00
// workspaceLogger.trace("returning: "+listParents.toString());
2016-09-14 16:46:22 +02:00
return listParents ;
}
else {
2016-11-10 12:31:25 +01:00
logger . trace ( " returning empty list " ) ;
2016-09-14 16:46:22 +02:00
return new ArrayList < Item > ( ) ; // empty list
}
2016-04-21 10:37:31 +02:00
}
2020-04-14 17:43:25 +02:00
//WorkspaceItem lastItem = parents.get(parents.size()-1);
//Adding the item passed as last first parent
if ( includeItemAsParent & & wsItem . isFolder ( ) ) {
WorkspaceFolder wsItemAsFolder = ( WorkspaceFolder ) wsItem ;
Item theItem = ItemBuilder . buildFolderForBreadcrumbs ( wsItemAsFolder , null ) ;
arrayParents . add ( theItem ) ;
}
2016-04-21 10:37:31 +02:00
/ * * HANDLE MY_SPECIAL_FOLDER TO AVOID COMPLETE PATH WORKSPACE / MY_SPECIAL_FOLDER
* BUT RETURNING ONLY / MY_SPECIAL_FOLDER
* /
2020-04-14 17:43:25 +02:00
WorkspaceItem theFirstParent = parents . get ( 0 ) ;
2016-04-21 10:37:31 +02:00
if ( wsItem . isFolder ( ) ) {
2020-04-14 17:43:25 +02:00
if ( ItemBuilder . isSpecialFolder ( ( WorkspaceFolder ) wsItem , theFirstParent . isRoot ( ) ) ) {
2016-04-21 10:37:31 +02:00
logger . debug ( " item id is special folder, returning " ) ;
2020-04-14 17:43:25 +02:00
return new ArrayList < Item > ( arrayParents ) ;
2016-04-21 10:37:31 +02:00
}
if ( itemIdentifier . compareTo ( parentLimit ) = = 0 ) {
logger . debug ( " item and parent limit are identical element, returning " ) ;
2020-04-14 17:43:25 +02:00
return new ArrayList < Item > ( arrayParents ) ;
2016-04-21 10:37:31 +02:00
}
}
//CONVERTING PATH
2020-04-14 17:43:25 +02:00
logger . debug ( " converting parents... " ) ;
//for (int i = parents.size()-2; i >= 0; i--) {
for ( int i = 0 ; i < parents . size ( ) ; i + + ) {
logger . debug ( " converting index: " + i ) ;
2016-04-21 10:37:31 +02:00
WorkspaceFolder wsParentFolder = ( WorkspaceFolder ) parents . get ( i ) ;
2020-04-14 17:43:25 +02:00
logger . debug ( " the folder at index: " + i + " is " + wsParentFolder . getName ( ) ) ;
Item itemParent = ItemBuilder . buildFolderForBreadcrumbs ( wsParentFolder , null ) ;
arrayParents . add ( itemParent ) ;
if ( itemParent . isSpecialFolder ( ) ) { //SKIP HOME PARENT FOR MY_SPECIAL_FOLDER
2016-04-21 10:37:31 +02:00
logger . info ( " arrayParents index " + i + " is special folder, break " ) ;
break ;
2020-04-14 17:43:25 +02:00
} else if ( parentLimit . compareTo ( itemParent . getId ( ) ) = = 0 ) {
2016-04-21 10:37:31 +02:00
logger . info ( " reached parent limit " + parentLimit + " , break " ) ;
break ;
}
}
//SET PARENTS
2016-09-19 18:30:12 +02:00
logger . debug ( " setting parents.. " ) ;
2020-04-14 17:43:25 +02:00
for ( int i = 0 ; i < arrayParents . size ( ) - 1 ; i + + ) {
2016-04-21 10:37:31 +02:00
2020-04-14 17:43:25 +02:00
Item parent = arrayParents . get ( i + 1 ) ;
Item fileModel = arrayParents . get ( i ) ;
2016-04-21 10:37:31 +02:00
if ( fileModel ! = null ) {
fileModel . setParent ( parent ) ;
}
}
2020-04-14 17:43:25 +02:00
// if(arrayParents[0]==null){ //EXIT BY BREAK IN CASE OF SPECIAL FOLDER OR REACHED PARENT LIMIT
// List<Item> breadcrumbs = new ArrayList<Item>();
// for (int i=1; i<arrayParents.length; i++) {
// if(arrayParents[i]!=null) {
// breadcrumbs.add(arrayParents[i]);
// }
// }
// logger.debug("Returning the breadcrumb: "+breadcrumbs);
// return breadcrumbs;
// }
// else {
// return new ArrayList<Item>(Arrays.asList(arrayParents));
// }
Collections . reverse ( arrayParents ) ;
logger . info ( " Reversed the parent list " ) ;
if ( logger . isDebugEnabled ( ) ) {
for ( Item item : arrayParents ) {
logger . info ( " Returning parent: " + item . getName ( ) ) ;
2016-04-21 10:37:31 +02:00
}
}
2020-04-14 17:43:25 +02:00
// logger.info("Limited list of parents returning size: "+arrayParents.length);
return arrayParents ;
//new ArrayList<Item>(Arrays.asList(arrayParents));
2016-04-21 10:37:31 +02:00
} catch ( Exception e ) {
logger . error ( " Error in get List Parents By Item Identifier " , e ) ;
throw new Exception ( " Sorry, an error occurred during path retrieving! " ) ;
}
}
2020-04-09 17:58:03 +02:00
// /**
// * Gets the size by item id.
// *
// * @param itemId the item id
// * @return the size by item id
// * @throws Exception the exception
// */
// /* (non-Javadoc)
// * @see org.gcube.portlets.widgets.wsexplorer.client.rpc.WorkspaceExplorerService#loadSizeByItemId(java.lang.String)
// */
// @Override
// public Long getSizeByItemId(String itemId) throws Exception {
//
// logger.info("get Size By ItemId "+ itemId);
// try {
//
// Workspace workspace = getWorkspace();
// WorkspaceItem wsItem = workspace.getItem(itemId);
// Long size = new Long(-1);
//
// if(wsItem instanceof FileItem){ //ITEM
// FileItem fileItem = (FileItem) wsItem;
// size = new Long(fileItem.getSize());
// } else if (wsItem instanceof WorkspaceFolder ){ //FOLDER
// WorkspaceFolder theFolder = (WorkspaceFolder) wsItem;
// //size = theFolder.getSize(); NOT SUPPORTED BY SHUB
// } else if (wsItem instanceof WorkspaceSharedFolder){ //SHARED FOLDER
// WorkspaceSharedFolder theFolder = (WorkspaceSharedFolder) wsItem;
// //size = theFolder.getSize(); NOT SUPPORTED BY SHUB
// }
// logger.info("returning size: " +size);
// return size;
//
// } catch (Exception e) {
// logger.error("get Size By ItemId ", e);
// throw new Exception(e.getMessage());
// }
// }
// /**
// * Gets the mime type.
// *
// * @param itemId the item id
// * @return the mime type
// * @throws Exception the exception
// */
// /* (non-Javadoc)
// * @see org.gcube.portlets.widgets.wsexplorer.client.rpc.WorkspaceExplorerService#getMimeType(java.lang.String)
// */
// @Override
// public String getMimeType(String itemId) throws Exception {
//
// logger.info("get MimeType By ItemId "+ itemId);
// try {
//
// Workspace workspace = getWorkspace();
// WorkspaceItem wsItem = workspace.getItem(itemId);
//
// if(wsItem instanceof FileItem) {
// FileItem fileItem = (FileItem) wsItem;
// return fileItem.getMimeType();
// }else
// return null;
//
// } catch (Exception e) {
// logger.error("get MimeType By ItemId ", e);
// throw new Exception(e.getMessage());
// }
// }
// /**
// * Gets the user acl for folder id.
// *
// * @param folderId the folder id
// * @return the user acl for folder id
// * @throws Exception the exception
// */
// @Override
// public String getUserACLForFolderId(String folderId) throws Exception{
// try {
// logger.info("Get user ACL to FOLDER id: "+folderId);
// Workspace workspace = getWorkspace();
// WorkspaceItem wsItem = workspace.getItem(folderId);
//
// if(!isASharedFolder(wsItem, false)) {
// return "OWNER";
// }
// else {
// return wsItem.getACLUser().toString();
// }
// } catch (Exception e) {
// logger.error("Error in server get UserACLForFolderId", e);
// String error = "An error occurred when getting ACL rules for selected folder. "+e.getMessage();
// throw new Exception(error);
// }
// }
2016-04-21 10:37:31 +02:00
/ * *
* Checks if is a shared folder .
*
* @param item the item
* @param asRoot the as root
* @return true , if is a shared folder
* /
2020-04-09 15:33:21 +02:00
private boolean isASharedFolder ( WorkspaceItem item , boolean asRoot ) {
2016-04-21 10:37:31 +02:00
try {
2020-04-09 15:33:21 +02:00
if ( item ! = null & & item . isFolder ( ) & & item . isShared ( ) ) { // IS A SHARED SUB-FOLDER
if ( asRoot ) {
return item . getType ( ) . equals ( WorkspaceItemType . SHARED_FOLDER ) ; // IS ROOT?
2016-04-21 10:37:31 +02:00
}
return true ;
}
return false ;
2020-04-09 15:33:21 +02:00
} catch ( Exception e ) {
2016-04-21 10:37:31 +02:00
logger . error ( " Error in server isASharedFolder " , e ) ;
return false ;
}
}
2020-04-09 17:58:03 +02:00
// /**
// * Gets the readable size by item id.
// *
// * @param itemId the item id
// * @return the readable size by item id
// * @throws Exception the exception
// */
// /* (non-Javadoc)
// * @see org.gcube.portlets.widgets.wsexplorer.client.rpc.WorkspaceExplorerService#getFormattedSizeByItemId(java.lang.String, org.gcube.portlets.widgets.wsexplorer.shared.SizeFormatter)
// */
// @Override
// public String getReadableSizeByItemId(String itemId) throws Exception {
//
// try{
// logger.info("getFormattedSize ByItemId "+ itemId);
// long size = getSizeByItemId(itemId);
// return StringUtil.readableFileSize(size);
// } catch (Exception e) {
// logger.error("getFormattedSize By ItemId ", e);
// throw new Exception(e.getMessage());
// }
// }
// /**
// * Creates the folder.
// *
// * @param nameFolder the name folder
// * @param description the description
// * @param parentId the parent id
// * @return the item
// * @throws Exception the exception
// */
// /* (non-Javadoc)
// * @see org.gcube.portlets.user.workspaceexplorerapp.client.rpc.WorkspaceExplorerAppService#createFolder(java.lang.String, java.lang.String, java.lang.String)
// */
// @Override
// public Item createFolder(String nameFolder, String description, String parentId) throws Exception {
//
// logger.debug("creating folder: "+nameFolder +", parent id: "+parentId);
// throw new WorkspaceNavigatorServiceException("The method 'createFolder' is not implemented");
//
// /*try {
//
// if(parentId==null || parentId.isEmpty())
// throw new Exception("Parent id is null or empty");
//
// if(nameFolder == null)
// nameFolder = "Empty Folder";
//
// Workspace workspace = getWorkspace();
// WorkspaceFolder wsFolder = workspace.createFolder(nameFolder, description, parentId);
// List<ItemType> allTypes = Arrays.asList(ItemType.values());
//
// Item parent = null;
// try{
// String parentPath = wsFolder.getParent()!=null?wsFolder.getParent().getPath():"";
// parent = ItemBuilder.getItem(null, wsFolder.getParent(), parentPath, allTypes, null, false);
// }catch(Exception e){
// logger.error("Get parent thown an exception, is it the root id? "+parentId);
// }
//
// //TODO PATCH TO AVOID PROBLEM ON GETPATH. FOR EXAMPLE WHEN PARENT IS ROOT
// String itemPath = null;
// try{
// itemPath = wsFolder.getPath();
// }catch(Exception e){
// logger.error("Get path thown an exception, for id: "+wsFolder.getId());
// itemPath= wsFolder.isFolder()?workspace.getRoot().getPath()+"/"+wsFolder.getName():workspace.getRoot().getPath();
// }
//
// return ItemBuilder.getItem(parent, wsFolder, itemPath, allTypes, null, false);
//
// } catch(InsufficientPrivilegesException e){
// String error = "Insufficient Privileges to create the folder";
// logger.error(error, e);
// throw new Exception(error);
// } catch (ItemAlreadyExistException e) {
// String error = "An error occurred on creating folder, " +e.getMessage();
// logger.error(error, e);
// throw new Exception(error);
// } catch (Exception e) {
// String error = "An error occurred on the sever during creating folder. Try again";
// logger.error(error, e);
// throw new Exception(error);
// }*/
//
// }
2016-04-21 10:37:31 +02:00
/ * *
* Gets the public link for item id .
*
* @param itemId the item id
* @return the public link for item id
* @throws Exception the exception
* /
@Override
public String getPublicLinkForItemId ( String itemId ) throws Exception {
logger . trace ( " get Public Link For ItemId: " + itemId ) ;
try {
if ( itemId = = null )
2020-04-09 17:58:03 +02:00
throw new Exception ( " Sorry, the itemId is null. The public link for empty item is unavailable " ) ;
2016-04-21 10:37:31 +02:00
2020-04-09 17:58:03 +02:00
Workspace workspace = getWorkspace ( ) ;
2016-04-21 10:37:31 +02:00
WorkspaceItem wsItem = workspace . getItem ( itemId ) ;
if ( wsItem = = null )
2020-04-09 17:58:03 +02:00
throw new Exception ( " Sorry, the workspace item is null. The public link for empty item is unavailable " ) ;
2016-04-21 10:37:31 +02:00
2020-04-09 17:58:03 +02:00
if ( wsItem instanceof FileItem ) {
2016-04-21 10:37:31 +02:00
2020-04-09 17:58:03 +02:00
URL theURL = workspace . getPublicLinkForFile ( wsItem . getId ( ) ) ;
2016-04-21 10:37:31 +02:00
2020-04-09 17:58:03 +02:00
if ( theURL = = null )
throw new Exception ( " Sorry, the public link for " + wsItem . getName ( ) + " is not available " ) ;
return theURL . toString ( ) ;
2016-04-21 10:37:31 +02:00
} else {
logger . warn ( " ItemId: " + itemId + " is not available, sent exception Public Link unavailable " ) ;
2020-04-09 17:58:03 +02:00
throw new Exception ( " Sorry, the Public Link for selected item is unavailable " ) ;
2016-04-21 10:37:31 +02:00
}
} catch ( Exception e ) {
2020-04-09 17:58:03 +02:00
logger . error ( " Error on getting public link for item: " + itemId , e ) ;
2016-04-21 10:37:31 +02:00
throw new Exception ( e . getMessage ( ) ) ;
}
}
2016-09-19 12:19:49 +02:00
/ * *
* Gets the valid id from encrypted .
*
* @param encodedFolderId the encrypted folder id
* @return the valid id from encrypted
2017-07-03 17:25:05 +02:00
* @throws Exception the exception
2016-09-19 12:19:49 +02:00
* /
@Override
public String getFolderIdFromEncrypted ( String encodedFolderId ) throws Exception {
try {
String scope = WsUtil . getScope ( this . getThreadLocalRequest ( ) . getSession ( ) ) ;
ScopeProvider . instance . set ( scope ) ;
2020-04-14 17:43:25 +02:00
logger . info ( " Trying to decode encoded folder Id: " + encodedFolderId + " in the scope: " + scope ) ;
2016-09-19 12:19:49 +02:00
String base64DecodedId = StringUtil . base64DecodeString ( encodedFolderId ) ;
// String useThis = "P+IpJ6F6cTaGENfKMQWmStGUE79gbri5bVGRnzOvb8YUNIsJqFrdhceBrF+/u00j";
logger . info ( " Base 64 decoded folder Id: " + base64DecodedId + " , now decrypting... " ) ;
String decryptedFId = StringEncrypter . getEncrypter ( ) . decrypt ( base64DecodedId ) ;
logger . info ( " Decrypted folder Id: " + decryptedFId , " returning " ) ;
return decryptedFId ;
} catch ( Exception e ) {
logger . error ( " Error during decrypting folder Id: " + encodedFolderId , e ) ;
throw new Exception ( " Sorry, an error occurred when decrypting the folder id. Try again or contact the support " ) ;
}
}
2016-04-21 10:37:31 +02:00
}