Created and used the URLShortenerUtil class

This commit is contained in:
Francesco Mangiacrapa 2024-05-07 10:13:35 +02:00
parent 3fd0589afa
commit 8604632f48
4 changed files with 72 additions and 49 deletions

View File

@ -18,9 +18,9 @@ import org.gcube.portlets.user.uriresolvermanager.exception.NotImplementedExcept
import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException;
import org.gcube.portlets.user.uriresolvermanager.readers.RuntimeResourceReader;
import org.gcube.portlets.user.uriresolvermanager.readers.UriResolverMapReader;
import org.gcube.portlets.user.uriresolvermanager.util.URLShortenerUtil;
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil;
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil.URI_PART;
import org.gcube.portlets.user.urlshortener.UrlShortener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -250,7 +250,7 @@ public class UriResolverManager {
}
LOG.info("Encoded link is: " + link);
LOG.info("Shortner starts..");
String shortenedLink = shortTheLink(link);
String shortenedLink = URLShortenerUtil.shortTheLink(link);
if (shortenedLink != null && shortenedLink.equals(link)) {
// here the short link and the input link are identical
// so the shortening did not work
@ -281,20 +281,6 @@ public class UriResolverManager {
return link;
}
private String shortTheLink(String sourceLink) {
String toReturnLink = sourceLink;
try {
UrlShortener shortener = new UrlShortener();
String shortLink = shortener.shorten(sourceLink);
LOG.info("Short link is: " + shortLink);
toReturnLink = shortLink;
} catch (Exception e) {
LOG.warn("Returning source link, an error occurred during link shortening: ", e);
}
return toReturnLink;
}
/**
* Gets the application types.
*

View File

@ -4,23 +4,25 @@ import java.net.MalformedURLException;
import java.net.URL;
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceAccessPoint;
import org.gcube.portlets.user.uriresolvermanager.util.URLShortenerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class GeoportalExporterAPI.
*
* The endpoints provided by GeoportalExporter service in the URI-Resolver service
* The endpoints provided by GeoportalExporter service in the URI-Resolver
* service
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Apr 23, 2024
* Apr 23, 2024
*/
public class GeoportalExporterAPI {
private GeoportalExporterEndpoint endpoint;
private ServiceAccessPoint serviceAccessPoint;
private final String QUERY_PARAMETER_AS_URL = "as-url";
public static final Logger LOG = LoggerFactory.getLogger(GeoportalExporterAPI.class);
@ -40,25 +42,50 @@ public class GeoportalExporterAPI {
/**
* Export project.
*
* @param type the type
* @param ucdID the ucd ID
* @param projectID the project ID
* @param asURL the as URL
* @param type the type
* @param ucdID the ucd ID
* @param projectID the project ID
* @param asDirectURLToPDFFile the as direct URL to PDF file
* @return the url
* @throws MalformedURLException the malformed URL exception
*/
public URL exportProject(String type, String ucdID, String projectID, boolean asURL) throws MalformedURLException {
public URL exportProject(String type, String ucdID, String projectID, boolean asDirectURLToPDFFile)
throws MalformedURLException {
if (type == null || type.isEmpty())
type = DEFAULT_TYPE;
String api = String.format("%s/export/%s/%s/%s", serviceAccessPoint.getServiceUrl(), type, ucdID, projectID);
if(asURL)
api+="?"+QUERY_PARAMETER_AS_URL+"="+asURL;
if (asDirectURLToPDFFile)
api += "?" + QUERY_PARAMETER_AS_URL + "=" + asDirectURLToPDFFile;
LOG.info("returning exportProject API: " + api);
return new URL(api);
}
/**
* Export project.
*
* @param type the type
* @param ucdID the ucd ID
* @param projectID the project ID
* @param asDirectURLToPDFFile the as direct URL to PDF file
* @param shortLink the short link
* @return the string
* @throws MalformedURLException the malformed URL exception
*/
public String exportProject(String type, String ucdID, String projectID, boolean asDirectURLToPDFFile,
boolean shortLink) throws MalformedURLException {
URL theURL = exportProject(type, ucdID, projectID, asDirectURLToPDFFile);
String theShortLink = null;
if (theURL != null) {
theShortLink = URLShortenerUtil.shortTheLink(theURL.toString());
}
return theShortLink;
}
/**
* View job.
*

View File

@ -11,8 +11,8 @@ import java.net.URL;
import java.util.Map;
import org.gcube.portlets.user.uriresolvermanager.entity.GenericResolver;
import org.gcube.portlets.user.uriresolvermanager.util.URLShortenerUtil;
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil;
import org.gcube.portlets.user.urlshortener.UrlShortener;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -136,7 +136,7 @@ public class CatalogueResolver extends GenericResolver {
try {
LOG.info("Encoded link is: " + theLink);
LOG.info("Shortner starts..");
String shortLink = shortTheLink(theLink);
String shortLink = URLShortenerUtil.shortTheLink(theLink);
LOG.info("Shorted link is: " + shortLink);
if (shortLink != null && shortLink.equals(theLink)) {
// here the short link and the input link are identical
@ -160,26 +160,6 @@ public class CatalogueResolver extends GenericResolver {
}
/**
* Short the link.
*
* @param link the link
* @return the string
*/
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;
}
/**
* Read response.
*

View File

@ -0,0 +1,30 @@
package org.gcube.portlets.user.uriresolvermanager.util;
import org.gcube.portlets.user.urlshortener.UrlShortener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class URLShortenerUtil {
private static Logger LOG = LoggerFactory.getLogger(URLShortenerUtil.class);
/**
* Short the link.
*
* @param link the link
* @return the string
*/
public static String shortTheLink(String link) {
String toReturnLink = link;
try {
UrlShortener shortener = new UrlShortener();
String shortedLink = shortener.shorten(link);
LOG.info("Short link is: " + shortedLink);
toReturnLink = shortedLink;
} catch (Exception e) {
LOG.warn("Returning source link, an error occurred during link shortening: ", e);
}
return toReturnLink;
}
}