Added RR for Google Shortener

git-svn-id: http://svn.d4science-ii.research-infrastructures.eu/gcube/trunk/portlets/user/workspace-tree-widget@77434 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2013-07-01 13:54:54 +00:00
parent f2e13c1339
commit 2ef13ff2d7
5 changed files with 276 additions and 23 deletions

View File

@ -106,6 +106,10 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
return WsUtil.withoutPortal;
}
protected UrlShortener getUrlShortener() {
return WsUtil.getUrlShortener(this.getThreadLocalRequest().getSession());
}
@Override
public FolderModel getRootForTree() throws Exception {
@ -2200,8 +2204,13 @@ public class GWTWorkspaceServiceImpl extends RemoteServiceServlet implements GWT
workspaceLogger.trace("get short url for "+ longUrl);
UrlShortener shortener = getUrlShortener();
try{
return UrlShortener.shorten(longUrl);
if(shortener!=null && shortener.isAvailable())
return shortener.shorten(longUrl);
return longUrl;
}catch (Exception e) {
workspaceLogger.error("Error get short url for ", e);

View File

@ -8,6 +8,9 @@ package org.gcube.portlets.user.workspace.server.shortner;
* @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;
@ -15,30 +18,92 @@ 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.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.impl.XQuery;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public final class UrlShortener {
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 authenticationKeyValue ="AIzaSyDfMO0VY3o8GjRUqnTfqScjm_EsFEuBa3g";
protected static String authenticationKeyParam = "key";
protected static Logger logger = Logger.getLogger(UrlShortener.class);
/**
*
*/
public UrlShortener() {
// TODO Auto-generated constructor stub
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() throws Exception {
try{
logger.trace("Tentative reading HTTP-URL-Shortener RR");
ScopeProvider.instance.set("/gcube");
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():"";
}
uriRequest = shortnerServerUrl+"?"+KEY+"="+paramKeyValue;
logger.trace("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;
}
/**
@ -46,7 +111,7 @@ public final class UrlShortener {
* @param longUrl
* @return a shorten url
*/
public static String shorten(String longUrl) throws Exception{
public String shorten(String longUrl) throws Exception{
if (longUrl == null) {
return longUrl;
@ -64,7 +129,10 @@ public final class UrlShortener {
JSONObject jsonObj = new JSONObject();
jsonObj.put(LONG_URL, longUrl);
// jsonObj.put(authenticationKeyParam, authenticationKeyValue);
//ACTIVE THIS AFTER RELEASE
if(paramKeyValue!=null && !paramKeyValue.isEmpty())
jsonObj.put(KEY, paramKeyValue);
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
wr.write(jsonObj.toJSONString());
@ -91,11 +159,33 @@ public final class UrlShortener {
}
}
public static String getShortnerServerUrl() {
return shortnerServerUrl;
}
public static String getKey() {
return KEY;
}
public static String getParamKeyValue() {
return paramKeyValue;
}
public String getUriRequest() {
return uriRequest;
}
public static void main(String[] args) {
String shorten;
try {
shorten = UrlShortener.shorten("https://dev.d4science.org/group/data-e-infrastructure-gateway/workspace?itemid=062c558c-c7ce-4de3-a4c7-e1411816cc12&operation=gotofolder");
UrlShortener urlSh = new UrlShortener();
System.out.println("UrlShortener: "+urlSh);
shorten = urlSh.shorten("https://dev.d4science.org/group/data-e-infrastructure-gateway/workspace?itemid=062c558c-c7ce-4de3-a4c7-e1411816cc12&operation=gotofolder");
System.out.println("Shorted: "+shorten);
} catch (Exception e) {
@ -103,4 +193,15 @@ 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();
}
}

View File

@ -20,6 +20,7 @@ import org.gcube.portlets.user.homelibrary.home.workspace.Workspace;
import org.gcube.portlets.user.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
import org.gcube.portlets.user.workspace.server.GWTWorkspaceBuilder;
import org.gcube.portlets.user.workspace.server.notifications.NotificationsProducer;
import org.gcube.portlets.user.workspace.server.shortner.UrlShortener;
import org.gcube.portlets.user.workspace.server.util.scope.ScopeUtilFilter;
@ -38,6 +39,7 @@ public class WsUtil {
public static final String NOTIFICATION_PRODUCER = "NOTIFICATIONMANAGER";
public static final String WS_RUN_IN_TEST_MODE = "WS_RUN_IN_TEST_MODE";
public static final String WORKSPACE_SCOPE_UTIL = "WORKSPACE_SCOPE_UTIL";
public static final String URL_SHORTENER_SERVICE = "URL_SHORTENER_SERVICE";
public static final String NOTIFICATION_PORTLET_CLASS_ID = "org.gcube.portlets.user.workspace.server.GWTWorkspaceServiceImpl"; //IN DEV
// public static final String TEST_SCOPE = "/gcube/devsec";
@ -266,4 +268,29 @@ public class WsUtil {
return scopeUtil;
}
/**
* @param session
* @return
*/
public static UrlShortener getUrlShortener(HttpSession httpSession) {
ASLSession session = getAslSession(httpSession);
UrlShortener shortener = null;
try{
shortener = (UrlShortener) session.getAttribute(WsUtil.URL_SHORTENER_SERVICE);
if(shortener==null){
shortener = new UrlShortener();
session.setAttribute(URL_SHORTENER_SERVICE, shortener);
return shortener;
}
}catch (Exception e) {
defaultLogger.error("an error occurred in instancing url shortener ",e);
}
return null;
}
}

View File

@ -26,7 +26,7 @@ import org.gcube.resources.discovery.client.queries.impl.XQuery;
* @Jun 26, 2013
*
*/
public class UriResolverReaderParameter {
public class UriResolverReaderRR {
//Base Address
String uri = "";
@ -34,14 +34,14 @@ public class UriResolverReaderParameter {
String parameter = "";
private String uriRequest = "";
public static Logger log = Logger.getLogger(UriResolverReaderParameter.class);
public static Logger log = Logger.getLogger(UriResolverReaderRR.class);
/**
* @throws Exception
*
*/
public UriResolverReaderParameter() throws Exception {
public UriResolverReaderRR() throws Exception {
ScopeProvider.instance.set("/gcube/devsec");
@ -113,9 +113,9 @@ public class UriResolverReaderParameter {
public static void main(String[] args) throws Exception {
log.trace(new UriResolverReaderParameter());
log.trace(new UriResolverReaderRR());
UriResolverReaderParameter uriResolver = new UriResolverReaderParameter();
UriResolverReaderRR uriResolver = new UriResolverReaderRR();
InputStream is = uriResolver.resolve("smp://Wikipedia_logo_silver.png?5ezvFfBOLqaqBlwCEtAvz4ch5BUu1ag3yftpCvV+gayz9bAtSsnO1/sX6pemTKbDe0qbchLexXeWgGcJlskYE8td9QSDXSZj5VSl9kdN9SN0/LRYaWUZuP4Q1J7lEiwkU4GKPsiD6PDRVcT4QAqTEy5hSIbr6o4Y");
File file = new File("test.png");

View File

@ -0,0 +1,116 @@
/**
*
*/
package org.gcube.portlets.user.workspace;
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
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.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.impl.XQuery;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* @Jun 26, 2013
*
*/
public class UrlShortenerReaderRR {
/**
*
*/
protected static final String KEY = "key";
//Base Address
protected String uri = "";
//Query URL parameter
protected String keyValue = "";
private String uriRequest = "";
public static Logger log = Logger.getLogger(UrlShortenerReaderRR.class);
/**
* @throws Exception
*
*/
public UrlShortenerReaderRR() throws Exception {
ScopeProvider.instance.set("/gcube");
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)
throw new Exception("No Shortener available");
//Base Address
// System.out.println(endpoints.get(0).address());
uri = endpoints.get(0)!=null?endpoints.get(0).address():"";
if(endpoints.get(0)!=null){
keyValue = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get(KEY).value():"";
}
uriRequest = uri+"?"+KEY+"="+keyValue;
//Query URL parameter
// System.out.println(endpoints.get(0).propertyMap().get("parameter").value());
}
/**
*
* @return Base Address of Uri Resolver
*/
public String getUri() {
return uri;
}
/**
*
* @return Query URL parameter of Uri Resolver
*/
public String getParameter() {
return keyValue;
}
public static void main(String[] args) throws Exception {
log.trace(new UrlShortenerReaderRR());
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("UrlShortenerReaderRR [uri=");
builder.append(uri);
builder.append(", keyValue=");
builder.append(keyValue);
builder.append(", uriRequest=");
builder.append(uriRequest);
builder.append("]");
return builder.toString();
}
}