updated tests
git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@124381 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
1c0f14b386
commit
2fe12b9bbe
|
@ -2339,38 +2339,6 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
|
|||
|
||||
if(wsItem.getType().equals(WorkspaceItemType.FOLDER_ITEM)){
|
||||
|
||||
/*FolderItem folderItem = (FolderItem) wsItem;
|
||||
String storageID = builder.getStorageIDForFolderItem(folderItem);
|
||||
|
||||
if(storageID==null || storageID.isEmpty())
|
||||
throw new Exception("Sorry, public link on "+wsItem.getName() +" is not available");
|
||||
|
||||
UriResolverReaderParameterForResolverIndex uriResolver = getUriResolver();
|
||||
String uriRequest = "";
|
||||
|
||||
if(uriResolver!=null && uriResolver.isAvailable()){
|
||||
|
||||
// String itemName = StringUtil.removeSpecialCharacters(folderItem.getName());
|
||||
// itemName = StringUtil.replaceAllWhiteSpace(itemName, "_");
|
||||
// uriRequest = uriResolver.resolveAsUriRequest(storageID, itemName, folderItem.getMimeType(), true);
|
||||
|
||||
uriRequest = uriResolver.resolveAsStorageIdRequest(storageID, true);
|
||||
|
||||
|
||||
//VALIDATE CONNECTION
|
||||
// if(!HttpRequestUtil.urlExists(uriRequest+"&validation=true"))
|
||||
// throw new Exception("Sorry, The Public Link for selected file is unavailable");
|
||||
|
||||
String shortURL = null;
|
||||
|
||||
if(shortenUrl)
|
||||
shortURL = getShortUrl(uriRequest);
|
||||
|
||||
|
||||
// }
|
||||
// else
|
||||
// throw new Exception("Sorry, The Uri resolver service is temporarily unavailable. Please try again later");*/
|
||||
|
||||
FolderItem folderItem = (FolderItem) wsItem;
|
||||
String uriRequest = folderItem.getPublicLink(false);
|
||||
|
||||
|
|
|
@ -95,6 +95,7 @@ public final class UrlShortener {
|
|||
}
|
||||
|
||||
String getParam = "";
|
||||
logger.trace("paramKeyValue: "+paramKeyValue);
|
||||
|
||||
//ACTIVE THIS AFTER RELEASE
|
||||
if(paramKeyValue!=null && !paramKeyValue.isEmpty()){
|
||||
|
@ -197,6 +198,17 @@ public final class UrlShortener {
|
|||
return uriRequest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("UrlShortener [uriRequest=");
|
||||
builder.append(uriRequest);
|
||||
builder.append(", isAvailable=");
|
||||
builder.append(isAvailable);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@ -215,15 +227,4 @@ public final class UrlShortener {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("UrlShortener [uriRequest=");
|
||||
builder.append(uriRequest);
|
||||
builder.append(", isAvailable=");
|
||||
builder.append(isAvailable);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -16,6 +16,8 @@ import java.net.HttpURLConnection;
|
|||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
|
||||
import org.gcube.common.encryption.StringEncrypter;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
|
||||
|
@ -31,7 +33,10 @@ public final class GoogleUrlShortener {
|
|||
static String shortnerServerUrl = "https://www.googleapis.com/urlshortener/v1/url";
|
||||
|
||||
static String urlMethod = "";
|
||||
static String authenticationKeyValue ="AIzaSyDfMO0VY3o8GjRUqnTfqScjm_EsFEuBa3g";
|
||||
// static String authenticationKeyValue ="AIzaSyDfMO0VY3o8GjRUqnTfqScjm_EsFEuBa3g";
|
||||
static String authenticationKeyValue ="AIzaSyCQSY7UU3xiBqqlzU5ovr-efs6EXKz5e7Y";
|
||||
|
||||
|
||||
static String authenticationKeyParam = "key";
|
||||
|
||||
// static String shortUrl = "https://dev.d4science.org/group/data-e-infrastructure-gateway/workspace?itemid=062c558c-c7ce-4de3-a4c7-e1411816cc12&operation=gotofolder";
|
||||
|
@ -55,6 +60,9 @@ public final class GoogleUrlShortener {
|
|||
|
||||
JSONObject jsonObj = new JSONObject();
|
||||
|
||||
String decrypt = StringEncrypter.getEncrypter().decrypt(authenticationKeyValue);
|
||||
System.out.println(decrypt);
|
||||
|
||||
jsonObj.put(LONG_URL, longUrl);
|
||||
jsonObj.put(authenticationKeyParam, authenticationKeyValue);
|
||||
|
||||
|
@ -98,6 +106,12 @@ public final class GoogleUrlShortener {
|
|||
JSONObject jsonObj = new JSONObject();
|
||||
|
||||
jsonObj.put(LONG_URL, longUrl);
|
||||
|
||||
ScopeProvider.instance.set("/gcube");
|
||||
|
||||
String decrypt = StringEncrypter.getEncrypter().decrypt(authenticationKeyValue);
|
||||
System.out.println(decrypt);
|
||||
|
||||
jsonObj.put(authenticationKeyParam, authenticationKeyValue);
|
||||
String body = jsonObj.toString();
|
||||
|
||||
|
|
|
@ -0,0 +1,230 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.portlets.user.workspace;
|
||||
|
||||
/**
|
||||
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
||||
* @Jun 28, 2013
|
||||
*
|
||||
*/
|
||||
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
||||
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.portlets.user.workspace.server.util.scope.ScopeUtilFilter;
|
||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||
import org.gcube.resources.discovery.client.queries.impl.XQuery;
|
||||
import org.json.JSONObject;
|
||||
import org.json.JSONTokener;
|
||||
|
||||
|
||||
|
||||
public final class UrlShortener {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected static final String NO_HTTP_URL_SHORTENER_AVAILABLE = "No HTTP URL Shortener available";
|
||||
protected static final String APPLICATION_JSON = "application/json";
|
||||
protected static final String LONG_URL = "longUrl";
|
||||
|
||||
// protected static String shortnerServerUrl = "https://www.googleapis.com/urlshortener/v1/url";
|
||||
// protected static String urlMethod = "";
|
||||
// protected static String authenticationKeyParam = "key";
|
||||
// protected static String authenticationKeyValue ="AIzaSyDfMO0VY3o8GjRUqnTfqScjm_EsFEuBa3g";
|
||||
|
||||
private static String shortnerServerUrl = "";
|
||||
protected static final String KEY = "key";
|
||||
private static String paramKeyValue ="";
|
||||
|
||||
protected static Logger logger = Logger.getLogger(UrlShortener.class);
|
||||
private String uriRequest;
|
||||
private boolean isAvailable = false;
|
||||
|
||||
/*
|
||||
* @throws Exception
|
||||
* @throws InvalidResultException
|
||||
* @throws DiscoveryException
|
||||
* @throws IllegalStateException *
|
||||
*
|
||||
*/
|
||||
public UrlShortener(String scope) throws Exception {
|
||||
|
||||
try{
|
||||
logger.trace("Tentative reading HTTP-URL-Shortener RR");
|
||||
|
||||
ScopeUtilFilter scopeUtil = new ScopeUtilFilter(scope,true);
|
||||
|
||||
ScopeProvider.instance.set(scopeUtil.getScopeRoot());
|
||||
|
||||
XQuery query = queryFor(ServiceEndpoint.class);
|
||||
|
||||
query.addCondition("$resource/Profile/Name/text() eq 'HTTP-URL-Shortener'").setResult("$resource/Profile/AccessPoint");
|
||||
|
||||
DiscoveryClient<AccessPoint> client = clientFor(AccessPoint.class);
|
||||
|
||||
List<AccessPoint> endpoints = client.submit(query);
|
||||
|
||||
if (endpoints.size() == 0){
|
||||
isAvailable = false;
|
||||
logger.trace("HTTP-URL-Shortener not found");
|
||||
throw new Exception(NO_HTTP_URL_SHORTENER_AVAILABLE);
|
||||
}
|
||||
|
||||
//Base Address
|
||||
// System.out.println(endpoints.get(0).address());
|
||||
|
||||
shortnerServerUrl = endpoints.get(0)!=null?endpoints.get(0).address():"";
|
||||
|
||||
if(endpoints.get(0)!=null){
|
||||
|
||||
paramKeyValue = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get(KEY).value():"";
|
||||
}
|
||||
|
||||
String getParam = "";
|
||||
logger.trace("paramKeyValue: "+paramKeyValue);
|
||||
|
||||
//ACTIVE THIS AFTER RELEASE
|
||||
if(paramKeyValue!=null && !paramKeyValue.isEmpty()){
|
||||
getParam = "?"+KEY +"="+paramKeyValue;
|
||||
}
|
||||
|
||||
uriRequest = shortnerServerUrl+getParam;
|
||||
|
||||
logger.trace("HTTP-URL-Shortener found, uriRequest: "+uriRequest);
|
||||
|
||||
//DEBUG
|
||||
// System.out.println("HTTP-URL-Shortener found, uriRequest: "+uriRequest);
|
||||
|
||||
isAvailable = true;
|
||||
|
||||
}catch (Exception e) {
|
||||
isAvailable = false;
|
||||
logger.error("an error occurred in reading RR: ", e);
|
||||
throw new Exception(NO_HTTP_URL_SHORTENER_AVAILABLE);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAvailable() {
|
||||
return isAvailable;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param longUrl
|
||||
* @return a shorten url
|
||||
*/
|
||||
public String shorten(String longUrl) throws Exception{
|
||||
|
||||
if (longUrl == null) {
|
||||
return longUrl;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
logger.trace("shorten longUrl: "+longUrl);
|
||||
|
||||
URL url = new URL(uriRequest);
|
||||
|
||||
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||
connection.setDoOutput(true);
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", APPLICATION_JSON);
|
||||
|
||||
JSONObject jsonObj = new JSONObject();
|
||||
jsonObj.put(LONG_URL, longUrl);
|
||||
|
||||
// //ACTIVE THIS AFTER RELEASE
|
||||
// if(paramKeyValue!=null && !paramKeyValue.isEmpty()){
|
||||
// jsonObj.put(KEY, paramKeyValue);
|
||||
// }
|
||||
|
||||
logger.trace("request json : "+jsonObj.toString());
|
||||
|
||||
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
||||
wr.write(jsonObj.toString());
|
||||
wr.flush();
|
||||
|
||||
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
|
||||
JSONTokener tokener = new JSONTokener(rd);
|
||||
JSONObject jsonObject = new JSONObject(tokener);
|
||||
// JSONParser parser = new JSONParser();
|
||||
// JSONObject jsonObject = (JSONObject) parser.parse(rd);
|
||||
logger.trace("response received json : "+jsonObject.toString());
|
||||
|
||||
wr.close();
|
||||
rd.close();
|
||||
return (String) jsonObject.get("id"); //is shorted url
|
||||
|
||||
} catch (MalformedURLException e) {
|
||||
|
||||
logger.error("MalformedURLException error in UrlShortener", e);
|
||||
return longUrl;
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
logger.error("IOException error in UrlShortener", e);
|
||||
return longUrl;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getShortnerServerUrl() {
|
||||
return shortnerServerUrl;
|
||||
}
|
||||
|
||||
public static String getKey() {
|
||||
return KEY;
|
||||
}
|
||||
|
||||
public static String getParamKeyValue() {
|
||||
return paramKeyValue;
|
||||
}
|
||||
|
||||
public String getUriRequest() {
|
||||
return uriRequest;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
builder.append("UrlShortener [uriRequest=");
|
||||
builder.append(uriRequest);
|
||||
builder.append(", isAvailable=");
|
||||
builder.append(isAvailable);
|
||||
builder.append("]");
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
String shorten;
|
||||
|
||||
try {
|
||||
|
||||
UrlShortener urlSh = new UrlShortener("/d4science.research-infrastructures.eu");
|
||||
System.out.println("UrlShortener: "+urlSh);
|
||||
|
||||
shorten = urlSh.shorten("https://dev.d4science.org/group/data-e-infrastructure-gateway/workspace?itemid=056648-c7ce-4de3-a4c7-e1411816cc12&operation=gotofolder");
|
||||
|
||||
System.out.println("Shorted: "+shorten);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -69,6 +69,7 @@ public class UrlShortenerReaderRR {
|
|||
|
||||
uriRequest = uri+"?"+KEY+"="+keyValue;
|
||||
|
||||
System.out.println(uriRequest);
|
||||
|
||||
//Query URL parameter
|
||||
// System.out.println(endpoints.get(0).propertyMap().get("parameter").value());
|
||||
|
|
Loading…
Reference in New Issue