2013-06-28 18:42:10 +02:00
|
|
|
/**
|
2016-02-19 15:59:23 +01:00
|
|
|
*
|
2013-06-28 18:42:10 +02:00
|
|
|
*/
|
2013-07-02 10:30:40 +02:00
|
|
|
package org.gcube.portlets.user.workspace.server.shortener;
|
2013-06-28 18:42:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
|
|
* @Jun 28, 2013
|
|
|
|
*
|
|
|
|
*/
|
2013-07-01 15:54:54 +02:00
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.clientFor;
|
|
|
|
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
|
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
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;
|
2013-07-01 15:54:54 +02:00
|
|
|
import java.util.List;
|
2013-06-28 18:42:10 +02:00
|
|
|
|
|
|
|
import org.apache.log4j.Logger;
|
2013-07-01 15:54:54 +02:00
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
|
|
|
import org.gcube.common.resources.gcore.ServiceEndpoint.AccessPoint;
|
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2013-10-22 16:43:50 +02:00
|
|
|
import org.gcube.portlets.user.workspace.server.util.scope.ScopeUtilFilter;
|
2013-07-01 15:54:54 +02:00
|
|
|
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
|
|
|
import org.gcube.resources.discovery.client.queries.impl.XQuery;
|
2014-05-23 12:16:37 +02:00
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONTokener;
|
|
|
|
|
|
|
|
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
public final class UrlShortener {
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
/**
|
2016-02-19 15:59:23 +01:00
|
|
|
*
|
2013-07-01 15:54:54 +02:00
|
|
|
*/
|
|
|
|
protected static final String NO_HTTP_URL_SHORTENER_AVAILABLE = "No HTTP URL Shortener available";
|
2013-06-28 18:42:10 +02:00
|
|
|
protected static final String APPLICATION_JSON = "application/json";
|
|
|
|
protected static final String LONG_URL = "longUrl";
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
// 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";
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
private static String shortnerServerUrl = "";
|
|
|
|
protected static final String KEY = "key";
|
|
|
|
private static String paramKeyValue ="";
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
protected static Logger logger = Logger.getLogger(UrlShortener.class);
|
2013-07-01 15:54:54 +02:00
|
|
|
private String uriRequest;
|
|
|
|
private boolean isAvailable = false;
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
/*
|
2016-02-19 15:59:23 +01:00
|
|
|
* @throws Exception
|
|
|
|
* @throws InvalidResultException
|
|
|
|
* @throws DiscoveryException
|
2013-07-01 15:54:54 +02:00
|
|
|
* @throws IllegalStateException *
|
2016-02-19 15:59:23 +01:00
|
|
|
*
|
2013-06-28 18:42:10 +02:00
|
|
|
*/
|
2013-10-22 16:43:50 +02:00
|
|
|
public UrlShortener(String scope) throws Exception {
|
2013-07-01 15:54:54 +02:00
|
|
|
|
|
|
|
try{
|
|
|
|
logger.trace("Tentative reading HTTP-URL-Shortener RR");
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2014-05-09 17:48:30 +02:00
|
|
|
ScopeUtilFilter scopeUtil = new ScopeUtilFilter(scope,true);
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-10-22 16:43:50 +02:00
|
|
|
ScopeProvider.instance.set(scopeUtil.getScopeRoot());
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
XQuery query = queryFor(ServiceEndpoint.class);
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
query.addCondition("$resource/Profile/Name/text() eq 'HTTP-URL-Shortener'").setResult("$resource/Profile/AccessPoint");
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
DiscoveryClient<AccessPoint> client = clientFor(AccessPoint.class);
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
List<AccessPoint> endpoints = client.submit(query);
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
if (endpoints.size() == 0){
|
|
|
|
isAvailable = false;
|
|
|
|
logger.trace("HTTP-URL-Shortener not found");
|
|
|
|
throw new Exception(NO_HTTP_URL_SHORTENER_AVAILABLE);
|
|
|
|
}
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
//Base Address
|
|
|
|
// System.out.println(endpoints.get(0).address());
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
shortnerServerUrl = endpoints.get(0)!=null?endpoints.get(0).address():"";
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
if(endpoints.get(0)!=null){
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
paramKeyValue = endpoints.get(0).propertyMap()!=null?endpoints.get(0).propertyMap().get(KEY).value():"";
|
|
|
|
}
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-02 10:30:40 +02:00
|
|
|
String getParam = "";
|
2016-02-19 15:59:23 +01:00
|
|
|
logger.trace("paramKeyValue: "+paramKeyValue);
|
|
|
|
|
2013-07-02 10:30:40 +02:00
|
|
|
//ACTIVE THIS AFTER RELEASE
|
|
|
|
if(paramKeyValue!=null && !paramKeyValue.isEmpty()){
|
2016-02-19 15:59:23 +01:00
|
|
|
getParam = "?"+KEY +"="+paramKeyValue;
|
2013-07-02 10:30:40 +02:00
|
|
|
}
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-02 10:30:40 +02:00
|
|
|
uriRequest = shortnerServerUrl+getParam;
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
logger.trace("HTTP-URL-Shortener found, uriRequest: "+uriRequest);
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-02 10:30:40 +02:00
|
|
|
//DEBUG
|
2013-07-02 14:59:25 +02:00
|
|
|
// System.out.println("HTTP-URL-Shortener found, uriRequest: "+uriRequest);
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
isAvailable = true;
|
2016-02-19 15:59:23 +01:00
|
|
|
|
|
|
|
}catch (Exception e) {
|
2013-07-01 15:54:54 +02:00
|
|
|
isAvailable = false;
|
|
|
|
logger.error("an error occurred in reading RR: ", e);
|
|
|
|
throw new Exception(NO_HTTP_URL_SHORTENER_AVAILABLE);
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
public boolean isAvailable() {
|
|
|
|
return isAvailable;
|
2013-06-28 18:42:10 +02:00
|
|
|
}
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
/**
|
2016-02-19 15:59:23 +01:00
|
|
|
*
|
2013-06-28 18:42:10 +02:00
|
|
|
* @param longUrl
|
|
|
|
* @return a shorten url
|
|
|
|
*/
|
2013-07-01 15:54:54 +02:00
|
|
|
public String shorten(String longUrl) throws Exception{
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
if (longUrl == null) {
|
|
|
|
return longUrl;
|
|
|
|
}
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
try {
|
|
|
|
|
2013-07-02 10:30:40 +02:00
|
|
|
logger.trace("shorten longUrl: "+longUrl);
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-02 10:30:40 +02:00
|
|
|
URL url = new URL(uriRequest);
|
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
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);
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-02 10:30:40 +02:00
|
|
|
// //ACTIVE THIS AFTER RELEASE
|
|
|
|
// if(paramKeyValue!=null && !paramKeyValue.isEmpty()){
|
|
|
|
// jsonObj.put(KEY, paramKeyValue);
|
|
|
|
// }
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2014-05-23 12:16:37 +02:00
|
|
|
logger.trace("request json : "+jsonObj.toString());
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
2014-05-23 12:16:37 +02:00
|
|
|
wr.write(jsonObj.toString());
|
2013-06-28 18:42:10 +02:00
|
|
|
wr.flush();
|
|
|
|
|
|
|
|
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2014-05-23 12:16:37 +02:00
|
|
|
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());
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
wr.close();
|
|
|
|
rd.close();
|
|
|
|
return (String) jsonObject.get("id"); //is shorted url
|
|
|
|
|
|
|
|
} catch (MalformedURLException e) {
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
logger.error("MalformedURLException error in UrlShortener", e);
|
|
|
|
return longUrl;
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
logger.error("IOException error in UrlShortener", e);
|
|
|
|
return longUrl;
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-01 15:54:54 +02:00
|
|
|
public static String getShortnerServerUrl() {
|
|
|
|
return shortnerServerUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getKey() {
|
|
|
|
return KEY;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String getParamKeyValue() {
|
|
|
|
return paramKeyValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getUriRequest() {
|
|
|
|
return uriRequest;
|
|
|
|
}
|
2016-02-19 15:59:23 +01:00
|
|
|
|
|
|
|
@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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
public static void main(String[] args) {
|
|
|
|
String shorten;
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
try {
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-10-22 16:43:50 +02:00
|
|
|
UrlShortener urlSh = new UrlShortener("/d4science.research-infrastructures.eu");
|
2013-07-01 15:54:54 +02:00
|
|
|
System.out.println("UrlShortener: "+urlSh);
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-07-02 10:30:40 +02:00
|
|
|
shorten = urlSh.shorten("https://dev.d4science.org/group/data-e-infrastructure-gateway/workspace?itemid=056648-c7ce-4de3-a4c7-e1411816cc12&operation=gotofolder");
|
2016-02-19 15:59:23 +01:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
System.out.println("Shorted: "+shorten);
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2013-07-01 15:54:54 +02:00
|
|
|
|
2013-06-28 18:42:10 +02:00
|
|
|
}
|