enhancement

This commit is contained in:
Francesco Mangiacrapa 2022-04-27 14:48:25 +02:00
parent 350d97dd75
commit e2da9ddb2f
3 changed files with 83 additions and 36 deletions

View File

@ -19,6 +19,7 @@ import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapExcept
import org.gcube.portlets.user.uriresolvermanager.readers.RuntimeResourceReader; import org.gcube.portlets.user.uriresolvermanager.readers.RuntimeResourceReader;
import org.gcube.portlets.user.uriresolvermanager.readers.UriResolverMapReader; import org.gcube.portlets.user.uriresolvermanager.readers.UriResolverMapReader;
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil; 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.gcube.portlets.user.urlshortener.UrlShortener;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -206,10 +207,10 @@ public class UriResolverManager {
String baseURI = serviceAccessPoint.getServiceUrl(); String baseURI = serviceAccessPoint.getServiceUrl();
//SPECIALIZED IMPLEMENTATION OF RESOLVER // SPECIALIZED IMPLEMENTATION OF RESOLVER
try { try {
link = resolver.getLink(baseURI, parameters); link = resolver.getLink(baseURI, parameters);
LOG.debug("Read specialized getLink: "+link); LOG.debug("Read specialized getLink: " + link);
if (shortLink) { if (shortLink) {
link = resolver.shortLink(link, parameters); link = resolver.shortLink(link, parameters);
} }
@ -219,33 +220,29 @@ public class UriResolverManager {
LOG.info("Specialized getLink not implemented, going to default implementation"); LOG.info("Specialized getLink not implemented, going to default implementation");
} }
//GENERIC IMPLEMENTATION OF RESOLVER //GENERIC IMPLEMENTATION OF RESOLVER. DEFAULT IMPLEMENTATION APPLYING
String linkDecoded = null; if (link == null) {
String queryString = null;
if (!shortLink) {
// not shortening so returning the link with the query string with only the // not shortening so returning the link with the query string with only the
// parameters encoded // parameters encoded
LOG.info("getLink implemented via GET request and encoded query-string"); LOG.info("Specialized getLink is null, applying DEFAULT implementation via GET request on base URI and encoded query-string");
queryString = UrlEncoderUtil.encodeQuery(parameters); String queryString = UrlEncoderUtil.encodeQuery(parameters);
String toReturn = String.format("%s?%s", baseURI, queryString); link = String.format("%s?%s", baseURI, queryString);
LOG.info("returning link with encoded parameters in the query string: " + toReturn); //LOG.info("returning base URI with encoded parameters in the query string: " + linkEncoded);
return toReturn; //return toReturn;
}else {
LOG.info("Specialized getLink is not null");
} }
// Not specialized getLink has been implemented
// Short link required String linkNotShort = link;
// Going to build the GET request with query string decoded
LOG.info("getLink implemented via GET request and (decoded) query-string");
queryString = UrlEncoderUtil.toQueryString(parameters);
linkDecoded = String.format("%s?%s", baseURI, queryString);
link = linkDecoded;
LOG.info("Created HTTP link: " + link); LOG.info("Created HTTP link: " + link);
// Short link required
if (shortLink) { if (shortLink) {
try { try {
LOG.info("Short link requested, so encoding query string is required..."); LOG.info("Short link requested, so encoding query string is required...");
String queryStringEncoded = UrlEncoderUtil.encodeString(queryString);
link = String.format("%s?%s", baseURI, queryStringEncoded); URI_PART uriParts = UrlEncoderUtil.getURIParts(link);
String queryStringEncoded = UrlEncoderUtil.encodeString(uriParts.getQueryString());
link = String.format("%s?%s", uriParts.getBaseURI(), queryStringEncoded);
LOG.info("Encoded link is: " + link); LOG.info("Encoded link is: " + link);
LOG.info("Shortner starts.."); LOG.info("Shortner starts..");
String shortenedLink = shortTheLink(link); String shortenedLink = shortTheLink(link);
@ -254,18 +251,18 @@ public class UriResolverManager {
// here the short link and the input link are identical // here the short link and the input link are identical
// so the shortening did not work // so the shortening did not work
// I'm returning the decoded link because it is directly consumable via browser // 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); LOG.debug("Shorted link is equal to long link, returning long link: " + linkNotShort);
link = linkDecoded; link = linkNotShort;
} else { } else {
// here the link is really shorted // here the link is really shortened
LOG.debug("The link is really shorted, returning it"); LOG.debug("The link is really short, returning it");
link = shortenedLink; link = shortenedLink;
} }
} catch (Exception e) { } catch (Exception e) {
LOG.warn("An error occurred during link shortening: ", e); LOG.warn("An error occurred during link shortening: ", e);
// here I'm returning the decoded link in case of error on shortening it // here I'm returning the decoded link in case of error on shortening it
link = linkDecoded; link = linkNotShort;
} }
} }
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
@ -275,7 +272,8 @@ public class UriResolverManager {
LOG.error("Uri Resolver Exception: ", e); LOG.error("Uri Resolver Exception: ", e);
throw new UriResolverMapException("Uri Resolver error: " + e.getMessage()); throw new UriResolverMapException("Uri Resolver error: " + e.getMessage());
} }
LOG.info("Returning HTTP(s) link: " + link);
return link; return link;
} }

View File

@ -10,7 +10,6 @@ import java.util.Map;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
/** /**
* The Class UrlEncoderUtil. * The Class UrlEncoderUtil.
* *
@ -152,5 +151,53 @@ public class UrlEncoderUtil {
return string; return string;
} }
public static URI_PART getURIParts(String uri) {
if (uri == null || uri.isEmpty())
return null;
String[] uriSections = uri.split("\\?");
if (uriSections.length == 1) {
return new URI_PART(uriSections[0], null);
} else if (uriSections.length == 2) {
return new URI_PART(uriSections[0], uriSections[1]);
}
return null;
}
public static class URI_PART {
String baseURI;
String queryString;
public URI_PART() {
}
public URI_PART(String baseURI, String queryString) {
this.baseURI = baseURI;
this.queryString = queryString;
}
public String getBaseURI() {
return baseURI;
}
public String getQueryString() {
return queryString;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("URI_PART [baseURI=");
builder.append(baseURI);
builder.append(", queryString=");
builder.append(queryString);
builder.append("]");
return builder.toString();
}
}
} }

View File

@ -47,7 +47,7 @@ public class UriResolverManagerTest {
} }
//@Test // @Test
public void testCTLG() { public void testCTLG() {
try { try {
@ -69,7 +69,7 @@ public class UriResolverManagerTest {
} }
} }
//@Test // @Test
public void testCTLGWithQueryString() { public void testCTLGWithQueryString() {
try { try {
@ -101,7 +101,7 @@ public class UriResolverManagerTest {
} }
} }
@Test // @Test IS OK
public void testSHUB() { public void testSHUB() {
try { try {
@ -122,15 +122,17 @@ public class UriResolverManagerTest {
} }
} }
//@Test @Test
public void testGIS() { public void testGIS() {
try { try {
ScopeProvider.instance.set("/gcube/devsec/devVRE"); String scope = "/pred4s/preprod/preVRE";
String UUID = "da165110-88fd-11da-a88f-000d939bc5d8";
ScopeProvider.instance.set(scope);
UriResolverManager resolver = new UriResolverManager("GIS"); UriResolverManager resolver = new UriResolverManager("GIS");
Map<String, String> params = new HashMap<String, String>(); Map<String, String> params = new HashMap<String, String>();
params.put("gis-UUID", "1a657005-29c6-4528-a115-69640c4c2900"); params.put("gis-UUID", UUID);
params.put("scope", "/gcube/devsec/devVRE"); params.put("scope", scope);
String shortLink = resolver.getLink(params, true); String shortLink = resolver.getLink(params, true);
System.out.println(shortLink); System.out.println(shortLink);
} catch (UriResolverMapException e) { } catch (UriResolverMapException e) {