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
This commit is contained in:
Francesco Mangiacrapa 2013-07-11 09:55:56 +00:00
parent f7e6efb2bb
commit c99c9ea7ac
3 changed files with 94 additions and 4 deletions

View File

@ -185,7 +185,6 @@ public class ContextMenuTree {
}); });
contextMenu.add(mnGetLink); contextMenu.add(mnGetLink);
contextMenu.add(new SeparatorMenuItem());
//PUBLIC LINK //PUBLIC LINK
MenuItem mnPublicLink = new MenuItem(); MenuItem mnPublicLink = new MenuItem();

View File

@ -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.notifications.NotificationsUtil;
import org.gcube.portlets.user.workspace.server.resolver.UriResolverReaderParameter; 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.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.StringUtil;
import org.gcube.portlets.user.workspace.server.util.UserUtil; import org.gcube.portlets.user.workspace.server.util.UserUtil;
import org.gcube.portlets.user.workspace.server.util.WsUtil; import org.gcube.portlets.user.workspace.server.util.WsUtil;
@ -2260,10 +2261,16 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
itemName = StringUtil.replaceAllWhiteSpace(itemName, "_"); itemName = StringUtil.replaceAllWhiteSpace(itemName, "_");
uriRequest = uriResolver.resolveAsUriRequest(smpUri, itemName, folderItem.getMimeType(), true); 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) if(shortenUrl)
return getShortUrl(uriRequest); uriRequest = getShortUrl(uriRequest);
return uriRequest; return uriRequest;
} }
else else
throw new Exception("Sorry, The Uri resolver service is temporarily unavailable. Please try again later"); 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 ""; return "";
}catch (Exception e) { }catch (Exception e) {
workspaceLogger.error("Error get short url for ", e); workspaceLogger.error("Error getPublicLinkForFolderItemId for item: "+itemId);
return ""; throw new Exception(e.getMessage());
} }
} }

View File

@ -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"));
}
}