#23157 CatalogueResolver enhancement

This commit is contained in:
Francesco Mangiacrapa 2022-04-26 17:21:13 +02:00
parent f124c6ea36
commit 350d97dd75
12 changed files with 493 additions and 55 deletions

View File

@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v1.6.0-SNAPSHOT] - 2022-04-20
**New**
- [#23157] Enhanced to manage the CatalogueResolver with input query string
## [v1.5.0] - 2021-11-05
#### Enhancement

View File

@ -11,7 +11,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.gcube.portlets.user</groupId>
<artifactId>uri-resolver-manager</artifactId>
<version>1.5.0</version>
<version>1.6.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>uri-resolver-manager</name>
<description>The URI Resolver Manager</description>

View File

@ -151,8 +151,10 @@ public class UriResolverManager {
/**
* Gets the link.
*
* @param parameters the map of the parameters sent as HTTP query string
* @param shortLink if true the link is shorted otherwise none
* @param parameters the map of the parameters sent as HTTP query
* string
* @param queryStringParameters the query string parameters
* @param shortLink if true the link is shortened otherwise none
* @return the link
* @throws IllegalArgumentException the illegal argument exception
* @throws UriResolverMapException the uri resolver map exception
@ -204,16 +206,20 @@ public class UriResolverManager {
String baseURI = serviceAccessPoint.getServiceUrl();
//SPECIALIZED IMPLEMENTATION OF RESOLVER
try {
link = resolver.getLink(baseURI, parameters);
if (shortLink)
return shortTheLink(link);
LOG.debug("Read specialized getLink: "+link);
if (shortLink) {
link = resolver.shortLink(link, parameters);
}
return link;
} catch (NotImplementedException e) {
LOG.info("Specialized getLink not implemented, going to default implementation");
}
//GENERIC IMPLEMENTATION OF RESOLVER
String linkDecoded = null;
String queryString = null;
@ -242,9 +248,9 @@ public class UriResolverManager {
link = String.format("%s?%s", baseURI, queryStringEncoded);
LOG.info("Encoded link is: " + link);
LOG.info("Shortner starts..");
String shortedLink = shortTheLink(link);
LOG.info("Shorted link is: " + shortedLink);
if (shortedLink != null && shortedLink.equals(link)) {
String shortenedLink = shortTheLink(link);
LOG.info("Short link is: " + shortenedLink);
if (shortenedLink != null && shortenedLink.equals(link)) {
// 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
@ -253,7 +259,7 @@ public class UriResolverManager {
} else {
// here the link is really shorted
LOG.debug("The link is really shorted, returning it");
link = shortedLink;
link = shortenedLink;
}
} catch (Exception e) {
@ -309,7 +315,7 @@ public class UriResolverManager {
try {
String scope = ScopeProvider.instance.get();
LOG.info("SiscoveryServiceParameters is using scope: " + scope + ", read from ScopeProvider");
LOG.info("DiscoveryServiceParameters is using scope: " + scope + ", read from ScopeProvider");
if (scope == null)
throw new UriResolverMapException("Scope is null, set scope into ScopeProvider!");

View File

@ -64,18 +64,31 @@ public class GenericResolver implements Resolver {
public void setEntryName(String entryName) {
this.entryName = entryName;
}
/**
* Gets the link.
*
* @param baseURI the base URI
* @param baseURI the base URI
* @param parameters the parameters
* @return the link
* @throws Exception the exception
*/
public String getLink(String baseURI, Map<String, String> parameters) throws Exception{
throw new NotImplementedException("getLink Method not Implement");
@Override
public String getLink(String baseURI, Map<String, String> parameters) throws Exception {
throw new NotImplementedException("getLink method not implemented");
}
/**
* Short link.
*
* @param theLink the the link
* @param parameters the parameters
* @return the string
* @throws Exception the exception
*/
@Override
public String shortLink(String theLink, Map<String, String> parameters) throws Exception {
throw new NotImplementedException("shortLink method not implemented");
}
/**
@ -98,4 +111,5 @@ public class GenericResolver implements Resolver {
builder.append("]");
return builder.toString();
}
}

View File

@ -28,11 +28,21 @@ public interface Resolver {
/**
* Gets the link.
*
* @param baseURI the base URI
* @param baseURI the base URI
* @param parameters the parameters
* @return the link
* @throws Exception the exception
*/
public String getLink(String baseURI, Map<String, String> parameters) throws Exception;
/**
* Short link.
*
* @param theLink the the link
* @param parameters the parameters
* @return the string
* @throws Exception the exception
*/
public String shortLink(String theLink, Map<String, String> parameters) throws Exception;
}

View File

@ -11,6 +11,9 @@ import java.net.URL;
import java.util.Map;
import org.gcube.portlets.user.uriresolvermanager.entity.GenericResolver;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryStringBuilder;
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;
@ -47,8 +50,8 @@ public class CatalogueResolverCallBuilder extends GenericResolver {
*/
@Override
public String getLink(String baseURI, Map<String, String> parameters) throws Exception {
LOG.debug("called getLink: " + baseURI + " parameters: "+parameters);
LOG.debug("called getLink: " + baseURI + " parameters: " + parameters);
HttpURLConnection con = null;
String theResponse = null;
try {
@ -68,6 +71,11 @@ public class CatalogueResolverCallBuilder extends GenericResolver {
jObj.put(key, parameters.get(key));
}
// if (queryStringParameters != null) {
// String queryString = UrlEncoderUtil.toQueryString(queryStringParameters);
// jObj.put(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER, queryString);
// }
String toJSON = jObj.toString();
LOG.info("Submitting JSON: " + toJSON);
Integer code = null;
@ -105,6 +113,74 @@ public class CatalogueResolverCallBuilder extends GenericResolver {
}
@Override
public String shortLink(String theLink, Map<String, String> parameters) throws Exception {
LOG.info("specific shortLink called");
String linkDecoded = theLink;
String[] queryStringArray = theLink.split("\\?");
if(queryStringArray.length>1) {
String queryString = queryStringArray[1];
String queryStringEncoded = UrlEncoderUtil.encodeQuery(queryString);
theLink = String.format("%s?%s", queryStringArray[0], queryStringEncoded);
}
// if (parameters != null) {
// LOG.debug("Trying to read the parameter: " + CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER);
// String queryStringParmeters = parameters.get(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER);
// if (queryStringParmeters != null) {
// LOG.debug(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER + " found");
// queryString = UrlEncoderUtil.encodeQuery(queryStringParmeters);
// }
// }
try {
// LOG.debug(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER + " encoded is: " + queryString);
// if (queryString != null) {
// String queryStringEncoded = UrlEncoderUtil.encodeString(queryString);
// theLink = String.format("%s?%s", theLink, queryStringEncoded);
// }
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;
}
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,97 @@
package org.gcube.portlets.user.uriresolvermanager.resolvers.query;
/**
* The Class CatalogueResolverQueryString.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Apr 26, 2022
*/
public class CatalogueResolverQueryString {
/**
* The Enum MODERATION_OP.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Apr 26, 2022
*/
public static enum MODERATION_OP {
show
}
private final String itemName;
private String itemId;
private String itemStatus;
private MODERATION_OP moderation;
/**
* Instantiates a new catalogue resolver query string.
*
* @param builder the builder
*/
CatalogueResolverQueryString(CatalogueResolverQueryStringBuilder builder) {
this.itemName = builder.getItemName();
this.itemId = builder.getItemId();
this.itemStatus = builder.getItemStatus();
this.moderation = builder.getModeration();
}
/**
* Gets the item id.
*
* @return the item id
*/
public String getItemId() {
return itemId;
}
/**
* Gets the moderation.
*
* @return the moderation
*/
public MODERATION_OP getModeration() {
return moderation;
}
/**
* Gets the item name.
*
* @return the item name
*/
public String getItemName() {
return itemName;
}
/**
* Gets the item status.
*
* @return the item status
*/
public String getItemStatus() {
return itemStatus;
}
/**
* To string.
*
* @return the string
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("CatalogueResolverQueryString [itemName=");
builder.append(itemName);
builder.append(", itemId=");
builder.append(itemId);
builder.append(", itemStatus=");
builder.append(itemStatus);
builder.append(", moderation=");
builder.append(moderation);
builder.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,167 @@
/*
*
*/
package org.gcube.portlets.user.uriresolvermanager.resolvers.query;
import java.util.HashMap;
import java.util.Map;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP;
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class CatalogueResolverQueryStringBuilder.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Apr 26, 2022
*/
public final class CatalogueResolverQueryStringBuilder {
public static final Logger LOG = LoggerFactory.getLogger(CatalogueResolverQueryStringBuilder.class);
public static final String MODERATION_PARAMETER = "moderation";
public static final String ITEM_NAME_PARAMETER = "item_name";
public static final String ITEM_ID_PARAMETER = "item_id";
public static final String STATUS_PARAMETER = "status";
public static final String QUERY_STRING_PARAMETER = "query_string";
private final String itemName;
private String itemId;
private String itemStatus;
private MODERATION_OP moderation;
/**
* Instantiates a new catalogue resolver query string builder.
*
* @param itemName the item name
*/
public CatalogueResolverQueryStringBuilder(String itemName) {
this.itemName = itemName;
}
/**
* Item id.
*
* @param itemId the item id
* @return the catalogue resolver query string builder
*/
public CatalogueResolverQueryStringBuilder itemId(String itemId) {
this.itemId = itemId;
return this;
}
/**
* Moderation.
*
* @param moderation the moderation
* @return the catalogue resolver query string builder
*/
public CatalogueResolverQueryStringBuilder moderation(MODERATION_OP moderation) {
this.moderation = moderation;
return this;
}
/**
* Item status.
*
* @param itemStatus the item status
* @return the catalogue resolver query string builder
*/
public CatalogueResolverQueryStringBuilder itemStatus(String itemStatus) {
this.itemStatus = itemStatus;
return this;
}
/**
* Gets the item name.
*
* @return the item name
*/
public String getItemName() {
return itemName;
}
/**
* Gets the item id.
*
* @return the item id
*/
public String getItemId() {
return itemId;
}
/**
* Gets the item status.
*
* @return the item status
*/
public String getItemStatus() {
return itemStatus;
}
/**
* Gets the moderation.
*
* @return the moderation
*/
public MODERATION_OP getModeration() {
return moderation;
}
/**
* Builds the query parameters.
*
* @return the map
*/
public Map<String, String> buildQueryParameters() {
CatalogueResolverQueryString crQS = new CatalogueResolverQueryString(this);
Map<String, String> query = new HashMap<String, String>();
if (crQS.getItemId() != null) {
query.put(ITEM_ID_PARAMETER, crQS.getItemId());
}
if (crQS.getItemName() != null) {
query.put(ITEM_NAME_PARAMETER, crQS.getItemName());
}
if (crQS.getItemStatus() != null) {
query.put(STATUS_PARAMETER, crQS.getItemStatus());
}
if (crQS.getModeration() != null) {
query.put(MODERATION_PARAMETER, crQS.getModeration().name());
}
return query;
}
/**
* Builds the query parameters to query string.
*
* @return the string
*/
public String buildQueryParametersToQueryString() {
Map<String, String> mapParameters = buildQueryParameters();
return UrlEncoderUtil.toQueryString(mapParameters);
}
/**
* Builds the query obj.
*
* @return the catalogue resolver query string
*/
public CatalogueResolverQueryString buildQueryObj() {
return new CatalogueResolverQueryString(this);
}
}

View File

@ -5,13 +5,12 @@ package org.gcube.portlets.user.uriresolvermanager.util;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// TODO: Auto-generated Javadoc
/**
* The Class UrlEncoderUtil.
*
@ -153,23 +152,5 @@ public class UrlEncoderUtil {
return string;
}
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
// System.out.println(UrlEncoderUtil.encodeQuery("request=GetStyles", "layers=test Name", "service=WMS", "version=1.1.1"));
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("request", "GetStyles");
parameters.put("layers", "test Name");
parameters.put("version", "1.1.1");
System.out.println(UrlEncoderUtil.encodeQuery(parameters));
}
}

1
src/test/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/resources/

View File

@ -1,6 +1,7 @@
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException;
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil;
/**
*
@ -15,23 +16,54 @@ public class UriResolverManagerMain {
public static void main(String[] args) {
try {
ScopeProvider.instance.set("/gcube");
UriResolverManager resolver = new UriResolverManager("GIS");
System.out.println(resolver.getCapabilities());
System.out.println(resolver.getApplicationTypes());
String theLink = "https://data.dev.d4science.org/ctlg/devVRE/sarda-sarda";
String[] queryStringArray = theLink.split("\\?");
if(queryStringArray.length>1) {
String queryString = queryStringArray[1];
String queryStringEncoded = UrlEncoderUtil.encodeQuery(queryString);
theLink = String.format("%s?%s", queryStringArray[0], queryStringEncoded);
}
System.out.println(theLink);
// ScopeProvider.instance.set("/gcube");
// UriResolverManager resolver = new UriResolverManager("GIS");
// System.out.println(resolver.getCapabilities());
// System.out.println(resolver.getApplicationTypes());
// System.out.println(resolver.discoveryServiceParameters(resolver.getResolver("SMP-ID")));
// Map<String, String> params = new HashMap<String, String>();
// params.put("gis-UUID", "5ac49f44-999f-4efe-a32b-af71da2b39ac");
// params.put("scope", "/gcube/devsec/devVRE");
// String shortLink = resolver.getLink(params, true);
// System.out.println(shortLink); //true, link is shorted otherwise none
} catch (UriResolverMapException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}catch (Exception e) {
//// System.out.println(shortLink); //true, link is shorted otherwise none
// } catch (UriResolverMapException e) {
// e.printStackTrace();
// } catch (IllegalArgumentException e) {
// e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The main method.
*
* @param args the arguments
*/
// public static void main(String[] args) {
//
//// System.out.println(UrlEncoderUtil.encodeQuery("request=GetStyles", "layers=test Name", "service=WMS", "version=1.1.1"));
//
// HashMap<String, String> parameters = new HashMap<String, String>();
//
// parameters.put("request", "GetStyles");
// parameters.put("layers", "test Name");
// parameters.put("version", "1.1.1");
//
// System.out.println(UrlEncoderUtil.encodeQuery(parameters));
//
// }
}

View File

@ -1,10 +1,17 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
import org.gcube.portlets.user.uriresolvermanager.entity.Resolver;
import org.gcube.portlets.user.uriresolvermanager.entity.ServiceParameter;
import org.gcube.portlets.user.uriresolvermanager.exception.IllegalArgumentException;
import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapException;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryString.MODERATION_OP;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.CatalogueResolverQueryStringBuilder;
import org.gcube.portlets.user.uriresolvermanager.util.UrlEncoderUtil;
import org.junit.Test;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
@ -19,19 +26,28 @@ public class UriResolverManagerTest {
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
manager = new UriResolverManager();
System.out.println(manager.getCapabilities());
System.out.println(manager.getApplicationTypes());
System.out.println("Capabiities: " + manager.getCapabilities());
System.out.println("ApplicationTypes: " + manager.getApplicationTypes());
for (String applicationType : manager.getApplicationTypes()) {
Resolver resolver = manager.getResolver(applicationType);
System.out.println("ApplicationType: " + applicationType + " has: " + resolver);
List<ServiceParameter> serviceParameters = manager.discoveryServiceParameters(resolver);
System.out.println("Parameters: " + serviceParameters);
}
} catch (UriResolverMapException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// @Test
//@Test
public void testCTLG() {
try {
@ -53,7 +69,39 @@ public class UriResolverManagerTest {
}
}
// @Test
//@Test
public void testCTLGWithQueryString() {
try {
ScopeProvider.instance.set("/gcube/devsec/devVRE");
UriResolverManager resolver;
resolver = new UriResolverManager("CTLG");
Map<String, String> params = new HashMap<String, String>();
params.put("gcube_scope", "/gcube/devsec/devVRE");
params.put("entity_context", "dataset");
params.put("entity_name", "sarda-sarda");
CatalogueResolverQueryStringBuilder builder = new CatalogueResolverQueryStringBuilder("sarda-sarda");
builder.itemStatus("pending").moderation(MODERATION_OP.show);
String queryString = builder.buildQueryParametersToQueryString();
params.put(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER, queryString);
// METHOD 1 - Query String as parameter of the getLink method
String shortLink = resolver.getLink(params, true);
// METHOD 2 - Query String as parameter passed in the params
// String queryString = QueryStringUtil.toQueryString(queryStringParameters);
// params.put(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER, queryString);
System.out.println(shortLink);
} catch (UriResolverMapException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testSHUB() {
try {
@ -74,16 +122,16 @@ public class UriResolverManagerTest {
}
}
// @Test
//@Test
public void testGIS() {
try {
ScopeProvider.instance.set("/pred4s/preprod/preVRE");
ScopeProvider.instance.set("/gcube/devsec/devVRE");
UriResolverManager resolver = new UriResolverManager("GIS");
Map<String, String> params = new HashMap<String, String>();
params.put("gis-UUID", "1a657005-29c6-4528-a115-69640c4c2900");
params.put("scope", "/pred4s/preprod/preVRE");
String shortLink = resolver.getLink(params, false);
params.put("scope", "/gcube/devsec/devVRE");
String shortLink = resolver.getLink(params, true);
System.out.println(shortLink);
} catch (UriResolverMapException e) {
e.printStackTrace();