106 lines
3.0 KiB
Java
106 lines
3.0 KiB
Java
/**
|
|
*
|
|
*/
|
|
package org.gcube.portlets.user.workspace.server.shortner;
|
|
|
|
/**
|
|
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
|
|
* @Jun 28, 2013
|
|
*
|
|
*/
|
|
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 org.apache.log4j.Logger;
|
|
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
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param longUrl
|
|
* @return a shorten url
|
|
*/
|
|
public static String shorten(String longUrl) throws Exception{
|
|
|
|
if (longUrl == null) {
|
|
return longUrl;
|
|
}
|
|
|
|
try {
|
|
|
|
URL url = new URL(shortnerServerUrl);
|
|
|
|
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);
|
|
// jsonObj.put(authenticationKeyParam, authenticationKeyValue);
|
|
|
|
OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream());
|
|
wr.write(jsonObj.toJSONString());
|
|
wr.flush();
|
|
|
|
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
JSONParser parser = new JSONParser();
|
|
JSONObject jsonObject = (JSONObject) parser.parse(rd);
|
|
|
|
|
|
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 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");
|
|
|
|
System.out.println("Shorted: "+shorten);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
} |