2021-11-05 17:57:37 +01:00
|
|
|
package org.gcube.portlets.user.uriresolvermanager.resolvers;
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.net.HttpURLConnection;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import org.gcube.portlets.user.uriresolvermanager.entity.GenericResolver;
|
2022-04-26 17:21:13 +02:00
|
|
|
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil;
|
|
|
|
import org.gcube.portlets.user.urlshortener.UrlShortener;
|
2021-11-05 17:57:37 +01:00
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
2024-04-05 10:14:47 +02:00
|
|
|
* The Class CatalogueResolver.
|
2021-11-05 17:57:37 +01:00
|
|
|
*
|
|
|
|
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
|
|
|
|
*
|
2024-04-05 10:14:47 +02:00
|
|
|
* Apr 4, 2024
|
2021-11-05 17:57:37 +01:00
|
|
|
*/
|
2024-04-05 10:14:47 +02:00
|
|
|
public class CatalogueResolver extends GenericResolver {
|
2021-11-05 17:57:37 +01:00
|
|
|
|
2021-11-08 15:53:31 +01:00
|
|
|
private static final int _60SEC = 60000;
|
2024-04-05 10:14:47 +02:00
|
|
|
public static final Logger LOG = LoggerFactory.getLogger(CatalogueResolver.class);
|
2021-11-05 17:57:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiates a new catalogue resolver wrapper.
|
|
|
|
*
|
|
|
|
* @param resourceName the resource name
|
2021-11-08 15:53:31 +01:00
|
|
|
* @param entryName the entry name
|
2021-11-05 17:57:37 +01:00
|
|
|
*/
|
2024-04-05 10:14:47 +02:00
|
|
|
public CatalogueResolver(String resourceName, String entryName) {
|
2021-11-05 17:57:37 +01:00
|
|
|
super(resourceName, entryName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the link.
|
|
|
|
*
|
2021-11-08 15:53:31 +01:00
|
|
|
* @param baseURI the base URI
|
2021-11-05 17:57:37 +01:00
|
|
|
* @param parameters the parameters
|
|
|
|
* @return the link
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getLink(String baseURI, Map<String, String> parameters) throws Exception {
|
2022-04-26 17:21:13 +02:00
|
|
|
LOG.debug("called getLink: " + baseURI + " parameters: " + parameters);
|
|
|
|
|
2021-11-05 17:57:37 +01:00
|
|
|
HttpURLConnection con = null;
|
|
|
|
String theResponse = null;
|
|
|
|
try {
|
|
|
|
|
|
|
|
URL urlObj = new URL(baseURI);
|
|
|
|
con = (HttpURLConnection) urlObj.openConnection();
|
|
|
|
con.setRequestMethod("POST");
|
|
|
|
con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
|
2021-11-08 15:53:31 +01:00
|
|
|
// con.setRequestProperty("Accept", "application/json");
|
2021-11-05 17:57:37 +01:00
|
|
|
|
|
|
|
con.setDoOutput(true);
|
2021-11-08 15:53:31 +01:00
|
|
|
con.setReadTimeout(_60SEC);
|
|
|
|
con.setConnectTimeout(_60SEC);
|
|
|
|
|
2021-11-05 17:57:37 +01:00
|
|
|
JSONObject jObj = new org.json.JSONObject();
|
|
|
|
for (String key : parameters.keySet()) {
|
|
|
|
jObj.put(key, parameters.get(key));
|
|
|
|
}
|
|
|
|
|
2022-04-26 17:21:13 +02:00
|
|
|
// if (queryStringParameters != null) {
|
|
|
|
// String queryString = UrlEncoderUtil.toQueryString(queryStringParameters);
|
|
|
|
// jObj.put(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER, queryString);
|
|
|
|
// }
|
|
|
|
|
2021-11-05 17:57:37 +01:00
|
|
|
String toJSON = jObj.toString();
|
2021-11-08 15:53:31 +01:00
|
|
|
LOG.info("Submitting JSON: " + toJSON);
|
2021-11-05 17:57:37 +01:00
|
|
|
Integer code = null;
|
|
|
|
try {
|
|
|
|
|
|
|
|
OutputStream os = con.getOutputStream();
|
|
|
|
os.write(toJSON.getBytes("UTF-8"));
|
|
|
|
os.close();
|
|
|
|
|
|
|
|
code = con.getResponseCode();
|
|
|
|
theResponse = readResponse(con.getInputStream());
|
|
|
|
|
|
|
|
if (!((200 <= code) && (code <= 208))) {
|
2024-04-05 10:14:47 +02:00
|
|
|
throw new Exception(CatalogueResolver.class.getSimpleName()+" returned code: " + code + ". Response is: " + theResponse);
|
2021-11-05 17:57:37 +01:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
theResponse = readResponse(con.getInputStream());
|
2024-04-05 10:14:47 +02:00
|
|
|
LOG.error(CatalogueResolver.class.getSimpleName()+" returned code: " + code + ". Response is: " + theResponse);
|
2021-11-05 17:57:37 +01:00
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
2024-04-05 10:14:47 +02:00
|
|
|
LOG.error(CatalogueResolver.class.getSimpleName() + " error: ", e);
|
2021-11-05 17:57:37 +01:00
|
|
|
throw e;
|
|
|
|
} finally {
|
|
|
|
try {
|
|
|
|
if (con != null)
|
|
|
|
con.disconnect();
|
|
|
|
} catch (Exception e) {
|
|
|
|
// silent
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG.info("Got Link: " + theResponse);
|
|
|
|
return theResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-27 16:22:21 +02:00
|
|
|
/**
|
|
|
|
* Short link.
|
|
|
|
*
|
2024-04-05 10:14:47 +02:00
|
|
|
* @param theLink the the link
|
2023-03-27 16:22:21 +02:00
|
|
|
* @param parameters the parameters
|
|
|
|
* @return the string
|
|
|
|
* @throws Exception the exception
|
|
|
|
*/
|
2022-04-26 17:21:13 +02:00
|
|
|
@Override
|
|
|
|
public String shortLink(String theLink, Map<String, String> parameters) throws Exception {
|
|
|
|
LOG.info("specific shortLink called");
|
|
|
|
|
|
|
|
String linkDecoded = theLink;
|
2024-04-05 10:14:47 +02:00
|
|
|
|
2022-04-26 17:21:13 +02:00
|
|
|
String[] queryStringArray = theLink.split("\\?");
|
2024-04-05 10:14:47 +02:00
|
|
|
if (queryStringArray.length > 1) {
|
2022-04-26 17:21:13 +02:00
|
|
|
String queryString = queryStringArray[1];
|
|
|
|
String queryStringEncoded = UrlEncoderUtil.encodeQuery(queryString);
|
|
|
|
theLink = String.format("%s?%s", queryStringArray[0], queryStringEncoded);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
LOG.info("Encoded link is: " + theLink);
|
|
|
|
LOG.info("Shortner starts..");
|
|
|
|
String shortLink = shortTheLink(theLink);
|
|
|
|
LOG.info("Shorted link is: " + shortLink);
|
|
|
|
if (shortLink != null && shortLink.equals(theLink)) {
|
|
|
|
// here the short link and the input link are identical
|
|
|
|
// so the shortening did not work
|
|
|
|
// I'm returning the decoded link because it is directly consumable via browser
|
|
|
|
LOG.debug("Shorted link is equal to input link, returning decoded link: " + linkDecoded);
|
|
|
|
theLink = linkDecoded;
|
|
|
|
} else {
|
|
|
|
// here the link is really shorted
|
|
|
|
LOG.debug("The link is really short, returning it");
|
|
|
|
theLink = shortLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.warn("An error occurred during link shortening: ", e);
|
|
|
|
// here I'm returning the decoded link in case of error on shortening it
|
|
|
|
theLink = linkDecoded;
|
|
|
|
}
|
|
|
|
|
|
|
|
return theLink;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-03-27 16:22:21 +02:00
|
|
|
/**
|
|
|
|
* Short the link.
|
|
|
|
*
|
|
|
|
* @param link the link
|
|
|
|
* @return the string
|
|
|
|
*/
|
2022-04-26 17:21:13 +02:00
|
|
|
private String shortTheLink(String link) {
|
|
|
|
|
|
|
|
String toReturnLink = link;
|
|
|
|
try {
|
|
|
|
UrlShortener shortener = new UrlShortener();
|
|
|
|
String shortedLink = shortener.shorten(link);
|
|
|
|
LOG.info("Shorted link is: " + shortedLink);
|
|
|
|
toReturnLink = shortedLink;
|
|
|
|
} catch (Exception e) {
|
|
|
|
LOG.warn("Returning source link, an error occurred during link shortening: ", e);
|
|
|
|
}
|
|
|
|
return toReturnLink;
|
|
|
|
}
|
|
|
|
|
2021-11-05 17:57:37 +01:00
|
|
|
/**
|
|
|
|
* Read response.
|
|
|
|
*
|
|
|
|
* @param ris the ris
|
|
|
|
* @return the string
|
|
|
|
* @throws IOException Signals that an I/O exception has occurred.
|
|
|
|
*/
|
|
|
|
private String readResponse(InputStream ris) throws IOException {
|
|
|
|
// Receive the response from the server
|
|
|
|
InputStream in = new BufferedInputStream(ris);
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
result.append(line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|