From c99c9ea7ac48e209b738b404a8887886a0ce1f93 Mon Sep 17 00:00:00 2001 From: Francesco Mangiacrapa Date: Thu, 11 Jul 2013 09:55:56 +0000 Subject: [PATCH] public links completed git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@78961 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../client/view/tree/ContextMenuTree.java | 1 - .../server/GWTWorkspaceServiceImpl.java | 13 ++- .../server/util/HttpRequestUtil.java | 84 +++++++++++++++++++ 3 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 src/main/java/org/gcube/portlets/user/workspace/server/util/HttpRequestUtil.java diff --git a/src/main/java/org/gcube/portlets/user/workspace/client/view/tree/ContextMenuTree.java b/src/main/java/org/gcube/portlets/user/workspace/client/view/tree/ContextMenuTree.java index e8f9fb9..e54b1da 100644 --- a/src/main/java/org/gcube/portlets/user/workspace/client/view/tree/ContextMenuTree.java +++ b/src/main/java/org/gcube/portlets/user/workspace/client/view/tree/ContextMenuTree.java @@ -185,7 +185,6 @@ public class ContextMenuTree { }); contextMenu.add(mnGetLink); - contextMenu.add(new SeparatorMenuItem()); //PUBLIC LINK MenuItem mnPublicLink = new MenuItem(); 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 71b6647..e104424 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 @@ -52,6 +52,7 @@ import org.gcube.portlets.user.workspace.server.notifications.NotificationsProdu import org.gcube.portlets.user.workspace.server.notifications.NotificationsUtil; import org.gcube.portlets.user.workspace.server.resolver.UriResolverReaderParameter; import org.gcube.portlets.user.workspace.server.shortener.UrlShortener; +import org.gcube.portlets.user.workspace.server.util.HttpRequestUtil; import org.gcube.portlets.user.workspace.server.util.StringUtil; import org.gcube.portlets.user.workspace.server.util.UserUtil; import org.gcube.portlets.user.workspace.server.util.WsUtil; @@ -2259,11 +2260,17 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT 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) - return getShortUrl(uriRequest); + uriRequest = getShortUrl(uriRequest); return uriRequest; + } else throw new Exception("Sorry, The Uri resolver service is temporarily unavailable. Please try again later"); @@ -2274,8 +2281,8 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT return ""; }catch (Exception e) { - workspaceLogger.error("Error get short url for ", e); - return ""; + workspaceLogger.error("Error getPublicLinkForFolderItemId for item: "+itemId); + throw new Exception(e.getMessage()); } } diff --git a/src/main/java/org/gcube/portlets/user/workspace/server/util/HttpRequestUtil.java b/src/main/java/org/gcube/portlets/user/workspace/server/util/HttpRequestUtil.java new file mode 100644 index 0000000..82fbe4c --- /dev/null +++ b/src/main/java/org/gcube/portlets/user/workspace/server/util/HttpRequestUtil.java @@ -0,0 +1,84 @@ +package org.gcube.portlets.user.workspace.server.util; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.SocketTimeoutException; +import java.net.URL; +import java.net.URLConnection; + +import org.apache.log4j.Logger; + + +/** + * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it + * @Apr 26, 2013 + * + */ +public class HttpRequestUtil { + + private static final int CONNECTION_TIMEOUT = 1000; + public static Logger logger = Logger.getLogger(HttpRequestUtil.class); + + public static boolean urlExists(String urlConn) throws Exception { + + + if(urlConn==null || urlConn.isEmpty()) + return false; + + URL url; + + try { + url = new URL(urlConn); + + URLConnection connection = url.openConnection(); + connection.setConnectTimeout(CONNECTION_TIMEOUT); + connection.setReadTimeout(CONNECTION_TIMEOUT+CONNECTION_TIMEOUT); + + logger.trace("open connection on: " + url); + + // Cast to a HttpURLConnection + if (connection instanceof HttpURLConnection) { + HttpURLConnection httpConnection = (HttpURLConnection) connection; + + httpConnection.setRequestMethod("GET"); + + int code = httpConnection.getResponseCode(); + + httpConnection.disconnect(); + + if (code == 200) { + logger.trace("status code is "+code+" - on url connetction: "+urlConn); + return true; + }else + logger.warn("status code is "+code+" - on url connetction: "+urlConn); + +// logger.trace("result: "+result); + + } else { + logger.error("error - not a http request!"); + } + + return false; + + } catch (SocketTimeoutException e) { + logger.error("Error SocketTimeoutException with url " +urlConn); + return true; + } catch (MalformedURLException e) { + logger.error("Error MalformedURLException with url " +urlConn); + throw new Exception("Error MalformedURLException"); + } catch (IOException e) { + logger.error("Error IOException with url " +urlConn); + throw new Exception("Error IOException"); + }catch (Exception e) { + logger.error("Error Exception with url " +urlConn); + throw new Exception("Error Exception"); + } + } + + + + public static void main(String[] args) throws Exception { + System.out.println(HttpRequestUtil.urlExists("http://geoserver2.d4science.research-infrastructures.eu/geoserver/wms")); + } +}