diff --git a/src/main/java/org/gcube/portlets/user/workspace/server/DownloadServlet.java b/src/main/java/org/gcube/portlets/user/workspace/server/DownloadServlet.java index f123c55..eb16e58 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/server/DownloadServlet.java +++ b/src/main/java/org/gcube/portlets/user/workspace/server/DownloadServlet.java @@ -44,7 +44,7 @@ import org.gcube.portlets.user.workspace.shared.SessionExpiredException; /** * @author Federico De Faveri defaveri@isti.cnr.it - * + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it */ public class DownloadServlet extends HttpServlet{ @@ -61,26 +61,20 @@ public class DownloadServlet extends HttpServlet{ logger.trace("Workspace DownloadServlet ready."); } - - - public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { String itemId = req.getParameter("id"); boolean viewContent = (req.getParameter("viewContent")==null)?false:req.getParameter("viewContent").equals("true"); boolean isValidItem = (req.getParameter(ConstantsExplorer.VALIDATEITEM)==null)?false:req.getParameter(ConstantsExplorer.VALIDATEITEM).equals("true"); boolean urlRedirectOnError = (req.getParameter(ConstantsExplorer.REDIRECTONERROR)==null)?false:req.getParameter(ConstantsExplorer.REDIRECTONERROR).equals("true"); - - + logger.trace("Input Params [id: "+itemId + ", viewContent: "+viewContent+", "+ConstantsExplorer.VALIDATEITEM +": " +isValidItem+", urlRedirectOnError:" +urlRedirectOnError+"]"); - if(itemId==null || itemId.isEmpty()){ sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Item id is null"); return; } logger.trace("FILE DOWNLOAD REQUEST "+itemId); - Workspace wa = null; try { //ADDED 13-01-2014 SESSION VALIDATION @@ -97,13 +91,11 @@ public class DownloadServlet extends HttpServlet{ } handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during workspace retrieving"); -// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error during workspace retrieving"); return; } if (wa == null) { handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no workspace in session"); -// sendError(resp,HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no workspace in session"); return; } @@ -111,7 +103,6 @@ public class DownloadServlet extends HttpServlet{ try { item = wa.getItem(itemId); - if(isValidItem){ //ADDED 25/06/2013 - THIS CODE RETURN A SC_ACCEPT IS ITEM EXIST String message = HttpServletResponse.SC_ACCEPTED+ ": The resource is available"; sendMessageResourceAvailable(resp, message); @@ -121,18 +112,14 @@ public class DownloadServlet extends HttpServlet{ try{ //ACCOUNTING - item.markAsRead(true); - + item.markAsRead(true); } catch (InternalErrorException e) { logger.error("Requested item "+itemId+" has thrown an internal error exception",e); } } catch (ItemNotFoundException e) { logger.error("Requested item "+itemId+" not found",e); -// sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": Error, no items found"); handleError(urlRedirectOnError, req, resp, itemId, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": The file has been deleted by another user."); -// sendError(resp, HttpServletResponse.SC_INTERNAL_SERVER_ERROR +": "+ConstantsExplorer.ERROR_ITEM_DOES_NOT_EXIST); - return; } diff --git a/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceBuilder.java b/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceBuilder.java index a616b2c..39821cc 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceBuilder.java +++ b/src/main/java/org/gcube/portlets/user/workspace/server/GWTWorkspaceBuilder.java @@ -717,7 +717,7 @@ public class GWTWorkspaceBuilder { * * @param listSearchItems the list search items * @return the list - * @throws InternalErrorException the internal error exception + * @throws Exception the exception */ public List buildGXTListFileGridModelItemForSearch(List listSearchItems) throws Exception { List listFileGridModel = new ArrayList(); @@ -733,7 +733,7 @@ public class GWTWorkspaceBuilder { * @param item the item * @param parentFileModel the parent file model * @return the file grid model - * @throws InternalErrorException the internal error exception + * @throws Exception the exception */ private FileGridModel buildGXTFileGridModelItemForSearch(SearchItem item, FileModel parentFileModel) throws Exception{ @@ -1073,7 +1073,7 @@ public class GWTWorkspaceBuilder { } /** - * Gets the publi link for folder item. + * Gets the public link for folder item. * * @param worspaceFolderItem the worspace folder item * @return the publi link for folder item @@ -1124,6 +1124,61 @@ public class GWTWorkspaceBuilder { return ""; } + + + + /** + * Gets the storage id for folder item. + * + * @param worspaceFolderItem the worspace folder item + * @return the storage id for folder item + * @throws InternalErrorException the internal error exception + */ + public String getStorageIDForFolderItem(FolderItem worspaceFolderItem) throws InternalErrorException{ + + if(worspaceFolderItem==null) + return ""; + + try{ + switch(worspaceFolderItem.getFolderItemType()){ + case EXTERNAL_IMAGE: + return ((ExternalImage) worspaceFolderItem).getStorageID(); + case EXTERNAL_FILE: + return ((ExternalFile) worspaceFolderItem).getStorageID(); + case EXTERNAL_PDF_FILE: + return ((ExternalPDFFile) worspaceFolderItem).getStorageID(); + case EXTERNAL_URL: + break; + case REPORT_TEMPLATE: + break; + case REPORT: + break; + case QUERY: + break; + case TIME_SERIES: + break; + case PDF_DOCUMENT: + break; + case IMAGE_DOCUMENT: + GCubeItem imgDoc = (GCubeItem) worspaceFolderItem; //Cast GCubeItem + return imgDoc.getStorageID(); + case DOCUMENT: + break; + case URL_DOCUMENT: + break; + case METADATA: + break; + default: + return ""; + } + + }catch (Exception e) { + logger.error("an error occurred when get public link for item: "+worspaceFolderItem.getName()); + return ""; + } + + return ""; + } /** * Builds the gxt folder model item. @@ -1297,7 +1352,7 @@ public class GWTWorkspaceBuilder { * @param listSearchItem the list search item * @param category the category * @return the list - * @throws InternalErrorException the internal error exception + * @throws Exception the exception */ public List filterListFileGridModelItemByCategory(List listSearchItem, String category) throws Exception { @@ -1317,7 +1372,7 @@ public class GWTWorkspaceBuilder { * * @param listWorkspaceSmartFolder the list workspace smart folder * @return the list - * @throws InternalErrorException the internal error exception + * @throws Exception the exception */ public List buildGXTListSmartFolderModel(List listWorkspaceSmartFolder) throws Exception { List listSmartFolder = new ArrayList(); @@ -1335,7 +1390,7 @@ public class GWTWorkspaceBuilder { * @param wsFolder the ws folder * @param query the query * @return the smart folder model - * @throws InternalErrorException the internal error exception + * @throws Exception the exception */ public SmartFolderModel buildGXTSmartFolderModel(WorkspaceSmartFolder wsFolder, String query) throws Exception { 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 3c8593b..63b57aa 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 @@ -2579,35 +2579,30 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT public String getPublicLinkForFolderItemId(String itemId, boolean shortenUrl) throws Exception{ workspaceLogger.trace("get Public Link For ItemId: "+ itemId); - GWTWorkspaceBuilder builder = getGWTWorkspaceBuilder(); try{ Workspace workspace = getWorkspace(); - WorkspaceItem wsItem = workspace.getItem(itemId); if(wsItem.getType().equals(WorkspaceItemType.FOLDER_ITEM)){ FolderItem folderItem = (FolderItem) wsItem; - String smpUri = builder.getPublicLinkForFolderItem(folderItem); + String storageID = builder.getStorageIDForFolderItem(folderItem); - if(smpUri==null || smpUri.isEmpty()) + if(storageID==null || storageID.isEmpty()) throw new Exception("Sorry, public link on "+wsItem.getName() +" is not available"); UriResolverReaderParameter uriResolver = getUriResolver(); - 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); + uriRequest = uriResolver.resolveAsUriRequest(storageID, itemName, folderItem.getMimeType(), true); - //VALIDATE CONNECTION if(!HttpRequestUtil.urlExists(uriRequest+"&validation=true")) throw new Exception("Sorry, The Public Link for selected file is unavailable"); @@ -2616,13 +2611,12 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT uriRequest = getShortUrl(uriRequest); return uriRequest; - } else throw new Exception("Sorry, The Uri resolver service is temporarily unavailable. Please try again later"); }else{ - workspaceLogger.warn("ItemId: "+ itemId +" is not a folder item, sent exception Public Link unavailable"); + workspaceLogger.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"); } diff --git a/src/main/java/org/gcube/portlets/user/workspace/server/resolver/UriResolverReaderParameter.java b/src/main/java/org/gcube/portlets/user/workspace/server/resolver/UriResolverReaderParameter.java index 547c690..36285b8 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/server/resolver/UriResolverReaderParameter.java +++ b/src/main/java/org/gcube/portlets/user/workspace/server/resolver/UriResolverReaderParameter.java @@ -35,16 +35,12 @@ public class UriResolverReaderParameter { private String baseUri = ""; //Query URL parameter - private String smpParameter = ""; + private String storageIDParameter = ""; private String fileNameParameter = ""; private String contentTypeParameter = ""; - private String query = ""; - private boolean isAvailable = false; - - public static Logger logger = Logger.getLogger(UriResolverReaderParameter.class); @@ -72,15 +68,16 @@ public class UriResolverReaderParameter { throw new Exception("No Resolver available"); //Base Address -// System.out.println(endpoints.get(0).address()); + + //THE FIRST ACCESS POINT (endpoints.get(0)) IS SMP-URI, THE SECOND (endpoints.get(1)) IS SMP-ID + AccessPoint ap2 = endpoints.get(1); + baseUri = ap2!=null?endpoints.get(1).address():""; - baseUri = endpoints.get(0)!=null?endpoints.get(0).address():""; - - if(endpoints.get(0)!=null){ + if(ap2!=null){ - smpParameter = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get("SMP_URI_parameter").value():""; - fileNameParameter = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get("fileName_parameter").value():""; - contentTypeParameter = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get("contentType_parameter").value():""; + storageIDParameter = ap2.propertyMap()!=null?ap2.propertyMap().get("SMP_ID_parameter").value():""; + fileNameParameter = ap2.propertyMap()!=null?ap2.propertyMap().get("fileName_parameter").value():""; + contentTypeParameter = ap2.propertyMap()!=null?ap2.propertyMap().get("contentType_parameter").value():""; } // uriRequest = uri+"?"+smpParameter; @@ -116,7 +113,7 @@ public class UriResolverReaderParameter { /** * - * @param smp + * @param storageID * @param fileName * @param contentType * @param encodeParams - if true, translates a string into application/x-www-form-urlencoded @@ -124,15 +121,13 @@ public class UriResolverReaderParameter { * @return the http url to get file * @throws Exception */ - public String resolveAsUriRequest(String smp, String fileName, String contentType, boolean encodeParams) throws Exception{ - - if(smp==null || smp.isEmpty()) - throw new Exception("smp url is null or empty"); + public String resolveAsUriRequest(String storageID, String fileName, String contentType, boolean encodeParams) throws Exception{ + if(storageID==null || storageID.isEmpty()) + throw new Exception("storage ID is null or empty"); - if(!encodeParams){ //ENCODE URI - query = smpParameter+"="+smp; + query = storageIDParameter+"="+storageID; if(fileName!=null && !fileName.isEmpty()) query+="&"+fileNameParameter+"="+fileName; @@ -141,7 +136,7 @@ public class UriResolverReaderParameter { query+="&"+contentTypeParameter+"="+contentType; } else{ - Map hashParameters = getHashParemeters(smp, fileName, contentType); + Map hashParameters = getHashParemeters(storageID, fileName, contentType); query = UrlEncoderUtil.encodeQuery(hashParameters); } @@ -152,25 +147,23 @@ public class UriResolverReaderParameter { return uriRequest; } - - - + /** * - * @param smp + * @param storageID * @param fileName * @param contentType * @return * @throws Exception */ - public Map getHashParemeters(String smp, String fileName, String contentType) throws Exception{ + public Map getHashParemeters(String storageID, String fileName, String contentType) throws Exception{ Map hashParameters = new HashMap(); - if(smp==null || smp.isEmpty()) + if(storageID==null || storageID.isEmpty()) throw new Exception("smp url is null or empty"); - hashParameters.put(smpParameter, smp); + hashParameters.put(storageIDParameter, storageID); if(fileName!=null && !fileName.isEmpty()) hashParameters.put(fileNameParameter, fileName); @@ -193,8 +186,8 @@ public class UriResolverReaderParameter { StringBuilder builder = new StringBuilder(); builder.append("UriResolverReaderParameter [baseUri="); builder.append(baseUri); - builder.append(", smpParameter="); - builder.append(smpParameter); + builder.append(", storageIDParameter="); + builder.append(storageIDParameter); builder.append(", fileNameParameter="); builder.append(fileNameParameter); builder.append(", contentTypeParameter="); @@ -208,9 +201,11 @@ public class UriResolverReaderParameter { public String getBaseUri() { return baseUri; } - - public String getSmpParameter() { - return smpParameter; + /** + * @return the storageIDParameter + */ + public String getStorageIDParameter() { + return storageIDParameter; } public String getFileNameParameter() { @@ -225,8 +220,9 @@ public class UriResolverReaderParameter { public static void main(String[] args) throws Exception { String scope = "/gcube/devsec"; - logger.trace(new UriResolverReaderParameter(scope)); + System.out.println(new UriResolverReaderParameter(scope).toString()); + /* UriResolverReaderParameter uriResolver = new UriResolverReaderParameter(scope); String fileName = "Wikipedia_logo_silver.png"; @@ -235,6 +231,7 @@ public class UriResolverReaderParameter { String uriRequest = uriResolver.resolveAsUriRequest("smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV+gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y", fileName, "image/png", true); System.out.println("uriRequest "+uriRequest); + */ // // InputStream is = uriResolver.resolve("smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV+gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y", fileName, "image/png"); // File file = new File(fileName); diff --git a/src/test/java/org/gcube/portlets/user/workspace/SinglePublicLinkTest.java b/src/test/java/org/gcube/portlets/user/workspace/SinglePublicLinkTest.java index 54ea502..ca85af3 100644 --- a/src/test/java/org/gcube/portlets/user/workspace/SinglePublicLinkTest.java +++ b/src/test/java/org/gcube/portlets/user/workspace/SinglePublicLinkTest.java @@ -31,8 +31,8 @@ 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 String DEFAULT_SCOPE = "/d4science.research-infrastructures.eu/gCubeApps"; //PRODUCTION + public static String DEFAULT_SCOPE = "/gcube/devsec"; //DEV public static void main(String[] args) { @@ -44,15 +44,27 @@ public class SinglePublicLinkTest { Workspace ws = HomeLibrary .getHomeManagerFactory() .getHomeManager() - .getHome("pasquale.pagano") + .getHome("francesco.mangiacrapa") .getWorkspace(); // System.out.println("start get root"); -// WorkspaceItem root = ws.getRoot(); + WorkspaceItem root = ws.getRoot(); - getPublicLinkForFolderItemId("7e9740c5-d1ea-4c7a-b727-81817301cb5e", true, ws); + for (WorkspaceItem wsi : root.getChildren()) { + if(wsi.getType().equals(WorkspaceItemType.FOLDER_ITEM)){ + try{ + String publicLink = getPublicLinkForFolderItemId(wsi.getId(), true, ws); + System.out.println("\nITEM: "+wsi.getName()); + System.out.println(publicLink); +// break; + }catch(Exception e){ + e.printStackTrace(); + } + } + } + // System.out.println("start get children"); // List children = root.getChildren(); @@ -97,16 +109,15 @@ public class SinglePublicLinkTest { GWTWorkspaceBuilder builder = new GWTWorkspaceBuilder(); try{ - WorkspaceItem wsItem = workspace.getItem(itemId); if(wsItem.getType().equals(WorkspaceItemType.FOLDER_ITEM)){ FolderItem folderItem = (FolderItem) wsItem; - String smpUri = builder.getPublicLinkForFolderItem(folderItem); + String storageID = builder.getStorageIDForFolderItem(folderItem); - if(smpUri==null || smpUri.isEmpty()) + if(storageID==null || storageID.isEmpty()) throw new Exception("Sorry, public link on "+wsItem.getName() +" is not available"); UriResolverReaderParameter uriResolver = new UriResolverReaderParameter(DEFAULT_SCOPE); @@ -116,20 +127,17 @@ public class SinglePublicLinkTest { if(uriResolver!=null && uriResolver.isAvailable()){ String itemName = StringUtil.removeSpecialCharacters(folderItem.getName()); - itemName = StringUtil.replaceAllWhiteSpace(itemName, "_"); - uriRequest = uriResolver.resolveAsUriRequest(smpUri, itemName, folderItem.getMimeType(), true); + uriRequest = uriResolver.resolveAsUriRequest(storageID, itemName, folderItem.getMimeType(), true); - //VALIDATE CONNECTION - if(!HttpRequestUtil.urlExists(uriRequest)) + if(!HttpRequestUtil.urlExists(uriRequest+"&validation=true")) 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");