diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java index 5403f65..f525187 100644 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/UriResolverManager.java @@ -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.UriResolverMapReader; 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; @@ -206,10 +207,10 @@ public class UriResolverManager { String baseURI = serviceAccessPoint.getServiceUrl(); - //SPECIALIZED IMPLEMENTATION OF RESOLVER + // SPECIALIZED IMPLEMENTATION OF RESOLVER try { link = resolver.getLink(baseURI, parameters); - LOG.debug("Read specialized getLink: "+link); + LOG.debug("Read specialized getLink: " + link); if (shortLink) { link = resolver.shortLink(link, parameters); } @@ -219,33 +220,29 @@ public class UriResolverManager { LOG.info("Specialized getLink not implemented, going to default implementation"); } - //GENERIC IMPLEMENTATION OF RESOLVER - String linkDecoded = null; - String queryString = null; - - if (!shortLink) { + //GENERIC IMPLEMENTATION OF RESOLVER. DEFAULT IMPLEMENTATION APPLYING + if (link == null) { // not shortening so returning the link with the query string with only the // parameters encoded - LOG.info("getLink implemented via GET request and encoded query-string"); - queryString = UrlEncoderUtil.encodeQuery(parameters); - String toReturn = String.format("%s?%s", baseURI, queryString); - LOG.info("returning link with encoded parameters in the query string: " + toReturn); - return toReturn; + LOG.info("Specialized getLink is null, applying DEFAULT implementation via GET request on base URI and encoded query-string"); + String queryString = UrlEncoderUtil.encodeQuery(parameters); + link = String.format("%s?%s", baseURI, queryString); + //LOG.info("returning base URI with encoded parameters in the query string: " + linkEncoded); + //return toReturn; + }else { + LOG.info("Specialized getLink is not null"); } - // Not specialized getLink has been implemented - // Short link required - // 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; - + + String linkNotShort = link; LOG.info("Created HTTP link: " + link); + // Short link required if (shortLink) { try { 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("Shortner starts.."); String shortenedLink = shortTheLink(link); @@ -254,18 +251,18 @@ public class UriResolverManager { // 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); - link = linkDecoded; + LOG.debug("Shorted link is equal to long link, returning long link: " + linkNotShort); + link = linkNotShort; } else { - // here the link is really shorted - LOG.debug("The link is really shorted, returning it"); + // here the link is really shortened + LOG.debug("The link is really short, returning it"); link = shortenedLink; } } 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 - link = linkDecoded; + link = linkNotShort; } } } catch (IllegalArgumentException e) { @@ -275,7 +272,8 @@ public class UriResolverManager { LOG.error("Uri Resolver Exception: ", e); throw new UriResolverMapException("Uri Resolver error: " + e.getMessage()); } - + + LOG.info("Returning HTTP(s) link: " + link); return link; } diff --git a/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/UrlEncoderUtil.java b/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/UrlEncoderUtil.java index 3634c81..c88eb14 100644 --- a/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/UrlEncoderUtil.java +++ b/src/main/java/org/gcube/portlets/user/uriresolvermanager/util/UrlEncoderUtil.java @@ -10,7 +10,6 @@ import java.util.Map; import org.slf4j.Logger; import org.slf4j.LoggerFactory; - /** * The Class UrlEncoderUtil. * @@ -152,5 +151,53 @@ public class UrlEncoderUtil { 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(); + } + + } } diff --git a/src/test/java/UriResolverManagerTest.java b/src/test/java/UriResolverManagerTest.java index 2dcd182..32d44a0 100644 --- a/src/test/java/UriResolverManagerTest.java +++ b/src/test/java/UriResolverManagerTest.java @@ -47,7 +47,7 @@ public class UriResolverManagerTest { } - //@Test + // @Test public void testCTLG() { try { @@ -69,7 +69,7 @@ public class UriResolverManagerTest { } } - //@Test + // @Test public void testCTLGWithQueryString() { try { @@ -101,7 +101,7 @@ public class UriResolverManagerTest { } } - @Test + // @Test IS OK public void testSHUB() { try { @@ -122,15 +122,17 @@ public class UriResolverManagerTest { } } - //@Test + @Test public void testGIS() { 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"); Map params = new HashMap(); - params.put("gis-UUID", "1a657005-29c6-4528-a115-69640c4c2900"); - params.put("scope", "/gcube/devsec/devVRE"); + params.put("gis-UUID", UUID); + params.put("scope", scope); String shortLink = resolver.getLink(params, true); System.out.println(shortLink); } catch (UriResolverMapException e) {