diff --git a/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceServiceImpl.java b/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceServiceImpl.java index 11d7565..75d4239 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceServiceImpl.java +++ b/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceServiceImpl.java @@ -2254,7 +2254,7 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT } }catch (Exception e) { - workspaceLogger.error("Error getPublicLinkForFolderItemId for item: "+itemId); + workspaceLogger.error("Error getPublicLinkForFolderItemId for item: "+itemId, e); throw new Exception(e.getMessage()); } diff --git a/src/main/java/org/gcube/portlets/user/workspace/server/util/WsUtil.java b/src/main/java/org/gcube/portlets/user/workspace/server/util/WsUtil.java index dac3ead..1d80e86 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/server/util/WsUtil.java +++ b/src/main/java/org/gcube/portlets/user/workspace/server/util/WsUtil.java @@ -74,10 +74,10 @@ public class WsUtil { /*USE ANOTHER ACCOUNT (OTHERWHISE BY TEST_USER) FOR RUNNING * COMMENT THIS IN DEVELOP ENVIROMENT (UNCOMMENT IN PRODUCTION)*/ -// user=TEST_USER; + user=TEST_USER; //UNCOMMENT THIS IN DEVELOP ENVIROMENT - user = "francesco.mangiacrapa"; +// user = "francesco.mangiacrapa"; logger.warn("WORKSPACE PORTLET STARTING IN TEST MODE - NO USER FOUND - PORTLETS STARTING WITH FOLLOWING SETTINGS:"); logger.warn("session id: "+sessionID); diff --git a/src/test/java/org/gcube/portlets/user/workspace/PublicLinkTest.java b/src/test/java/org/gcube/portlets/user/workspace/PublicLinkTest.java index 389dcb0..fc99025 100644 --- a/src/test/java/org/gcube/portlets/user/workspace/PublicLinkTest.java +++ b/src/test/java/org/gcube/portlets/user/workspace/PublicLinkTest.java @@ -19,6 +19,7 @@ import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalPDFFile; import org.gcube.common.homelibrary.home.workspace.folder.items.gcube.ImageDocument; import org.gcube.common.scope.api.ScopeProvider; import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.portlets.user.workspace.server.GWTWorkspaceServiceImpl; /** * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it @@ -30,23 +31,26 @@ public class PublicLinkTest { protected static Logger logger = Logger.getLogger(PublicLinkTest.class); + public static String DEFAULT_SCOPE = "/d4science.research-infrastructures.eu/gCubeApps/EcologicalModelling"; //PRODUCTION +// public static String DEFAULT_SCOPE = "/gcube/devsec"; //DEV + public static void main(String[] args) { try { - ScopeBean scope = new ScopeBean("/gcube/devsec"); + ScopeBean scope = new ScopeBean(DEFAULT_SCOPE); ScopeProvider.instance.set(scope.toString()); Workspace ws = HomeLibrary .getHomeManagerFactory() .getHomeManager() - .getHome("francesco.mangiacrapa") + .getHome("leonardo.candela") .getWorkspace(); // System.out.println("start get root"); WorkspaceItem root = ws.getRoot(); - + System.out.println("start get children"); List children = root.getChildren(); diff --git a/src/test/java/org/gcube/portlets/user/workspace/SinglePublicLinkTest.java b/src/test/java/org/gcube/portlets/user/workspace/SinglePublicLinkTest.java new file mode 100644 index 0000000..bf0b09a --- /dev/null +++ b/src/test/java/org/gcube/portlets/user/workspace/SinglePublicLinkTest.java @@ -0,0 +1,207 @@ +/** + * + */ +package org.gcube.portlets.user.workspace; + +import java.util.List; + +import org.apache.log4j.Logger; +import org.gcube.common.homelibrary.home.HomeLibrary; +import org.gcube.common.homelibrary.home.exceptions.InternalErrorException; +import org.gcube.common.homelibrary.home.workspace.Workspace; +import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder; +import org.gcube.common.homelibrary.home.workspace.WorkspaceItem; +import org.gcube.common.homelibrary.home.workspace.WorkspaceItemType; +import org.gcube.common.homelibrary.home.workspace.folder.FolderItem; +import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalFile; +import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalImage; +import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalPDFFile; +import org.gcube.common.homelibrary.home.workspace.folder.items.gcube.ImageDocument; +import org.gcube.common.scope.api.ScopeProvider; +import org.gcube.common.scope.impl.ScopeBean; +import org.gcube.portlets.user.workspace.server.GWTWorkspaceBuilder; +import org.gcube.portlets.user.workspace.server.GWTWorkspaceServiceImpl; +import org.gcube.portlets.user.workspace.server.resolver.UriResolverReaderParameter; +import org.gcube.portlets.user.workspace.server.util.HttpRequestUtil; +import org.gcube.portlets.user.workspace.server.util.StringUtil; + +/** + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Jul 3, 2013 + * + */ +public class SinglePublicLinkTest { + + + protected static Logger logger = Logger.getLogger(SinglePublicLinkTest.class); + + public static String DEFAULT_SCOPE = "/d4science.research-infrastructures.eu/gCubeApps"; //PRODUCTION +// public static String DEFAULT_SCOPE = "/gcube/devsec"; //DEV + + public static void main(String[] args) { + + try { + + ScopeBean scope = new ScopeBean(DEFAULT_SCOPE); + ScopeProvider.instance.set(scope.toString()); + + Workspace ws = HomeLibrary + .getHomeManagerFactory() + .getHomeManager() + .getHome("leonardo.candela") + .getWorkspace(); + // + + System.out.println("start get root"); +// WorkspaceItem root = ws.getRoot(); + + + getPublicLinkForFolderItemId("0e875263-f6bc-4945-9435-bfb8774027ca", true, ws); + +// System.out.println("start get children"); +// List children = root.getChildren(); +// +// +// System.out.println("children size: "+children.size()); +// +// int i=0; +// for (WorkspaceItem workspaceItem : children) { +// +// +// if(workspaceItem.getType().equals(WorkspaceItemType.FOLDER) || workspaceItem.getType().equals(WorkspaceItemType.SHARED_FOLDER)){ +// +// WorkspaceFolder folder = (WorkspaceFolder) workspaceItem; +// +// System.out.println(++i+") folder name: "+folder.getName() + " is shared: "+folder.isShared()); +// }else{ +// +// +// if(workspaceItem.getType().equals(WorkspaceItemType.FOLDER_ITEM)){ +// +// FolderItem folderItem = (FolderItem) workspaceItem; +// +// String publicLink = getPubliLinkForFolderItem(folderItem); +// +// System.out.println(++i+") folderItem id: "+folderItem.getId() +", name: "+folderItem.getName() + ", ### Public link: "+publicLink); +// } +// +// } +// } + System.out.println("end"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + + public static String getPublicLinkForFolderItemId(String itemId, boolean shortenUrl, Workspace workspace) throws Exception{ + + logger.trace("get Public Link For ItemId: "+ itemId); + + GWTWorkspaceBuilder builder = new GWTWorkspaceBuilder(); + + try{ + + + WorkspaceItem wsItem = workspace.getItem(itemId); + + if(wsItem.getType().equals(WorkspaceItemType.FOLDER_ITEM)){ + + FolderItem folderItem = (FolderItem) wsItem; + String smpUri = builder.getPubliLinkForFolderItem(folderItem); + + if(smpUri==null || smpUri.isEmpty()) + throw new Exception("Sorry, public link on "+wsItem.getName() +" is not available"); + + UriResolverReaderParameter uriResolver = new UriResolverReaderParameter(DEFAULT_SCOPE); + + String uriRequest = ""; + + if(uriResolver!=null && uriResolver.isAvailable()){ + + String itemName = StringUtil.removeSpecialCharacters(folderItem.getName()); + + itemName = StringUtil.replaceAllWhiteSpace(itemName, "_"); + uriRequest = uriResolver.resolveAsUriRequest(smpUri, itemName, folderItem.getMimeType(), true); + + + //VALIDATE CONNECTION + if(!HttpRequestUtil.urlExists(uriRequest)) + throw new Exception("Sorry, The Public Link for selected file is unavailable"); + +// if(shortenUrl) +// uriRequest = getShortUrl(uriRequest); + + return uriRequest; + + } + else + throw new Exception("Sorry, The Uri resolver service is temporarily unavailable. Please try again later"); + + }else{ + logger.warn("ItemId: "+ itemId +" is not a folder item, sent exception Public Link unavailable"); + throw new Exception("Sorry, The Public Link for selected file is unavailable"); + } + + }catch (Exception e) { + logger.error("Error getPublicLinkForFolderItemId for item: "+itemId, e); + throw new Exception(e.getMessage()); + } + + } + protected static String getPubliLinkForFolderItem(FolderItem worspaceFolderItem) throws InternalErrorException{ + + if(worspaceFolderItem==null) + return ""; + + try{ + + switch(worspaceFolderItem.getFolderItemType()) + { + case EXTERNAL_IMAGE: + return ((ExternalImage) worspaceFolderItem).getPublicLink(); + case EXTERNAL_FILE: + return ((ExternalFile) worspaceFolderItem).getPublicLink(); + case EXTERNAL_PDF_FILE: + return ((ExternalPDFFile) worspaceFolderItem).getPublicLink(); + case EXTERNAL_URL: + break; + case REPORT_TEMPLATE: + break; + case REPORT: + break; + case QUERY: + break; + case TIME_SERIES: + break; + // case AQUAMAPS_ITEM: + // break; + case PDF_DOCUMENT: + break; + case IMAGE_DOCUMENT: + return ((ImageDocument) worspaceFolderItem).getPublicLink(); + case DOCUMENT: + break; + case URL_DOCUMENT: + break; + case METADATA: + break; + case WORKFLOW_REPORT: + break; + case WORKFLOW_TEMPLATE: + break; + case EXTERNAL_RESOURCE_LINK: + break; + default: + return ""; + } + + }catch (Exception e) { + logger.error("an error occurred when get public link for item: "+worspaceFolderItem.getName()); + return ""; + } + + return ""; + } + +}