Compare commits

...

12 Commits

15 changed files with 1117 additions and 82 deletions

View File

@ -3,6 +3,17 @@
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.7.0]
- Integrated the new Geoportal-Resolver [#24842]
## [v1.6.0] - 2022-07-26
**New**
- [#23157] Enhanced to manage the CatalogueResolver with input query string
- Moved to gcube-bom.2.0.2
## [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.7.0</version>
<packaging>jar</packaging>
<name>uri-resolver-manager</name>
<description>The URI Resolver Manager</description>
@ -46,7 +46,7 @@
<dependency>
<groupId>org.gcube.distribution</groupId>
<artifactId>gcube-bom</artifactId>
<version>2.0.1</version>
<version>2.3.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>

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.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;
@ -151,8 +152,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,62 +207,65 @@ 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");
}
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);
if (uriParts.getQueryString() != null && !uriParts.getQueryString().isEmpty()) {
LOG.info("QueryString part " + uriParts.getQueryString() + " is not null, encoding it");
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 shortedLink = shortTheLink(link);
LOG.info("Shorted link is: " + shortedLink);
if (shortedLink != null && shortedLink.equals(link)) {
String shortenedLink = shortTheLink(link);
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
LOG.debug("Shorted link is equal to input link, returning decoded link: " + linkDecoded);
link = linkDecoded;
LOG.info("Short 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");
link = shortedLink;
// 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) {
@ -270,17 +276,18 @@ public class UriResolverManager {
throw new UriResolverMapException("Uri Resolver error: " + e.getMessage());
}
LOG.info("Returning HTTP(s) link: " + link);
return link;
}
private String shortTheLink(String link) {
private String shortTheLink(String sourceLink) {
String toReturnLink = link;
String toReturnLink = sourceLink;
try {
UrlShortener shortener = new UrlShortener();
String shortedLink = shortener.shorten(link);
LOG.info("Shorted link is: " + shortedLink);
toReturnLink = shortedLink;
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);
}
@ -309,7 +316,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,8 @@ import java.net.URL;
import java.util.Map;
import org.gcube.portlets.user.uriresolvermanager.entity.GenericResolver;
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 +49,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 +70,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 +112,88 @@ public class CatalogueResolverCallBuilder extends GenericResolver {
}
/**
* 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 {
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;
}
/**
* 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,217 @@
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;
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;
/**
* The Class GeoportalResolverCallBuilder.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 27, 2023
*/
public class GeoportalResolverCallBuilder extends GenericResolver {
private static final int _60SEC = 60000;
public static final Logger LOG = LoggerFactory.getLogger(GeoportalResolverCallBuilder.class);
/**
* Instantiates a new geoportal resolver call builder.
*
* @param resourceName the resource name
* @param entryName the entry name
*/
public GeoportalResolverCallBuilder(String resourceName, String entryName) {
super(resourceName, entryName);
}
/**
* Gets the link.
*
* @param baseURI the base URI
* @param parameters the parameters
* @return the link
* @throws Exception the exception
*/
@Override
public String getLink(String baseURI, Map<String, String> parameters) throws Exception {
LOG.debug("called getLink: " + baseURI + " parameters: " + parameters);
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");
// con.setRequestProperty("Accept", "application/json");
con.setDoOutput(true);
con.setReadTimeout(_60SEC);
con.setConnectTimeout(_60SEC);
JSONObject jObj = new org.json.JSONObject();
for (String key : parameters.keySet()) {
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;
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))) {
throw new Exception("CatalogueResolver returned code: " + code + ". Response is: " + theResponse);
}
} catch (IOException e) {
theResponse = readResponse(con.getInputStream());
LOG.error("CatalogueResolver returned code: " + code + ". Response is: " + theResponse);
throw e;
}
} catch (Exception e) {
LOG.error(GeoportalResolverCallBuilder.class.getSimpleName() + " error: ", e);
throw e;
} finally {
try {
if (con != null)
con.disconnect();
} catch (Exception e) {
// silent
}
}
LOG.info("Got Link: " + theResponse);
return theResponse;
}
/**
* 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 {
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;
}
/**
* 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.
*
* @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();
}
}

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,176 @@
/*
*
*/
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 {
// DEFAULT PARAMETERS
public static final String DEFAULT_STATUS = "pending";
public static final String DEFAULT_MODERATION_OP = MODERATION_OP.show.name();
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);
if (crQS.getItemName() == null || crQS.getItemName().isEmpty()) {
throw new IllegalArgumentException("The " + ITEM_NAME_PARAMETER + " cannot be null or empty");
}
Map<String, String> query = new HashMap<String, String>();
query.put(ITEM_NAME_PARAMETER, crQS.getItemName());
if (crQS.getItemId() != null) {
query.put(ITEM_ID_PARAMETER, crQS.getItemId());
}
if (crQS.getItemStatus() != null) {
query.put(STATUS_PARAMETER, crQS.getItemStatus());
} else {
query.put(STATUS_PARAMETER, DEFAULT_STATUS);
}
if (crQS.getModeration() != null) {
query.put(MODERATION_PARAMETER, crQS.getModeration().name());
} else {
query.put(MODERATION_PARAMETER, DEFAULT_MODERATION_OP);
}
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

@ -0,0 +1,81 @@
package org.gcube.portlets.user.uriresolvermanager.resolvers.query;
/**
* The Class GeoportalResolverQueryString.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 27, 2023
*/
public class GeoportalResolverQueryString {
private final String itemType;
private String itemId;
private String gcubeScope;
private String resolveAs;
/**
* Instantiates a new geoportal resolver query string.
*
* @param builder the builder
*/
public GeoportalResolverQueryString(GeoportalResolverQueryStringBuilder builder) {
this.itemType = builder.getItemType();
this.itemId = builder.getItemId();
this.gcubeScope = builder.getGcubeScope();
this.resolveAs = builder.getResolveAs() != null ? builder.getResolveAs().getParamValue() : null;
}
/**
* Gets the item type.
*
* @return the item type
*/
public String getItemType() {
return itemType;
}
/**
* Gets the item id.
*
* @return the item id
*/
public String getItemId() {
return itemId;
}
/**
* Gets the gcube scope.
*
* @return the gcube scope
*/
public String getGcubeScope() {
return gcubeScope;
}
/**
* Gets the resolver as.
*
* @return the resolver as
*/
public String getResolveAs() {
return resolveAs;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("GeoportalResolverQueryString [itemType=");
builder.append(itemType);
builder.append(", itemId=");
builder.append(itemId);
builder.append(", gcubeScope=");
builder.append(gcubeScope);
builder.append(", resolveAs=");
builder.append(resolveAs);
builder.append("]");
return builder.toString();
}
}

View File

@ -0,0 +1,160 @@
/*
*
*/
package org.gcube.portlets.user.uriresolvermanager.resolvers.query;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class GeoportalResolverQueryStringBuilder.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 27, 2023
*/
public final class GeoportalResolverQueryStringBuilder {
public static final Logger LOG = LoggerFactory.getLogger(GeoportalResolverQueryStringBuilder.class);
public static final String ITEM_TYPE_PARAMETER = "item_type";
public static final String ITEM_ID_PARAMETER = "item_id";
public static final String GCUBE_SCOPE_PARAMETER = "gcube_scope";
public static final String RESOLVE_AS_PARAMETER = "res";
private String itemType;
private String itemId;
private String gcubeScope;
private RESOLVE_AS resolveAs;
/**
* The Enum RESOLVE_AS_PARAMETER.
*
* @author Francesco Mangiacrapa at ISTI-CNR francesco.mangiacrapa@isti.cnr.it
*
* Mar 28, 2023
*/
public static enum RESOLVE_AS {
PUBLIC("public"), PRIVATE("private");
String paramValue;
/**
* Instantiates a new resolve as.
*
* @param paramValue the param value
*/
RESOLVE_AS(String paramValue) {
this.paramValue = paramValue;
}
/**
* Gets the param value.
*
* @return the param value
*/
public String getParamValue() {
return paramValue;
}
}
/**
* Instantiates a new geoportal resolver query string builder.
*
* @param itemType the item type
* @param itemId the item id
*/
public GeoportalResolverQueryStringBuilder(String itemType, String itemId) {
this.itemType = itemType;
this.itemId = itemId;
}
/**
* Scope.
*
* @param gcubeScope the gcube scope
* @return the geoportal resolver query string builder
*/
public GeoportalResolverQueryStringBuilder scope(String gcubeScope) {
this.gcubeScope = gcubeScope;
return this;
}
/**
* Resolver as.
*
* @param resolveAs the resolve as
* @return the geoportal resolver query string builder
*/
public GeoportalResolverQueryStringBuilder resolverAs(RESOLVE_AS resolveAs) {
this.resolveAs = resolveAs;
return this;
}
/**
* Gets the item type.
*
* @return the item type
*/
public String getItemType() {
return itemType;
}
/**
* Gets the item id.
*
* @return the item id
*/
public String getItemId() {
return itemId;
}
/**
* Gets the gcube scope.
*
* @return the gcube scope
*/
public String getGcubeScope() {
return gcubeScope;
}
public RESOLVE_AS getResolveAs() {
return resolveAs;
}
/**
* Builds the query parameters.
*
* @return the map
*/
public Map<String, String> buildQueryParameters() {
GeoportalResolverQueryString crQS = new GeoportalResolverQueryString(this);
if (crQS.getItemType() == null || crQS.getItemType().isEmpty()) {
throw new IllegalArgumentException("The " + ITEM_TYPE_PARAMETER + " cannot be null or empty");
}
if (crQS.getItemId() == null || crQS.getItemId().isEmpty()) {
throw new IllegalArgumentException("The " + ITEM_ID_PARAMETER + " cannot be null or empty");
}
if (crQS.getGcubeScope() == null || crQS.getGcubeScope().isEmpty()) {
throw new IllegalArgumentException("The " + GCUBE_SCOPE_PARAMETER + " cannot be null or empty");
}
Map<String, String> query = new HashMap<String, String>();
query.put(ITEM_TYPE_PARAMETER, crQS.getItemType());
query.put(ITEM_ID_PARAMETER, crQS.getItemId());
query.put(GCUBE_SCOPE_PARAMETER, crQS.getGcubeScope());
if (crQS.getResolveAs() != null) {
query.put(RESOLVE_AS_PARAMETER, crQS.getResolveAs());
}
return query;
}
}

View File

@ -5,13 +5,11 @@ 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,22 +151,52 @@ public class UrlEncoderUtil {
return string;
}
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
public static URI_PART getURIParts(String uri) {
// System.out.println(UrlEncoderUtil.encodeQuery("request=GetStyles", "layers=test Name", "service=WMS", "version=1.1.1"));
if (uri == null || uri.isEmpty())
return null;
HashMap<String, String> parameters = new HashMap<String, String>();
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]);
}
parameters.put("request", "GetStyles");
parameters.put("layers", "test Name");
parameters.put("version", "1.1.1");
return null;
System.out.println(UrlEncoderUtil.encodeQuery(parameters));
}
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();
}
}

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

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

View File

@ -1,6 +1,4 @@
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 +13,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.CatalogueResolverQueryStringBuilder;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder.RESOLVE_AS;
import org.junit.Test;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
@ -13,24 +20,33 @@ import org.gcube.portlets.user.uriresolvermanager.exception.UriResolverMapExcept
*/
public class UriResolverManagerTest {
// @Test
//@Test
public void testUriResolverManger() {
UriResolverManager manager;
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
public void testCTLG() {
@ -53,7 +69,104 @@ 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");
params.put("query_string", "param1=value1&parm2=value2");
// 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 testCTLGGenerateLinkForModeration() {
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", "organization");
params.put("entity_name", "devvre");
CatalogueResolverQueryStringBuilder builder = new CatalogueResolverQueryStringBuilder(
"test-moderation-1649068829317");
//builder.itemStatus("approved").moderation(MODERATION_OP.show);
String queryString = builder.buildQueryParametersToQueryString();
params.put(CatalogueResolverQueryStringBuilder.QUERY_STRING_PARAMETER, queryString);
String shortLink = resolver.getLink(params, true);
System.out.println(shortLink);
} catch (UriResolverMapException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//@Test
public void testGeoportalCreateLink() {
try {
ScopeProvider.instance.set("/gcube");
UriResolverManager resolver;
String gcubeScope = "/gcube/devsec/devVRE";
String itemId = "63d011c4dcac4551b9a6b930";
String itemType = "profiledConcessioni";
resolver = new UriResolverManager("GEO");
//Method 1
// Map<String, String> params = new HashMap<String, String>();
// params.put(GeoportalResolverQueryStringBuilder.GCUBE_SCOPE_PARAMETER, gcubeScope);
// params.put(GeoportalResolverQueryStringBuilder.ITEM_ID_PARAMETER, itemId);
// params.put(GeoportalResolverQueryStringBuilder.ITEM_TYPE_PARAMETER, itemType);
// params.put(GeoportalResolverQueryStringBuilder.RESOLVE_AS_PARAMETER, GeoportalResolverQueryStringBuilder.RESOLVE_AS.PUBLIC.getParamValue());
//Method 2
GeoportalResolverQueryStringBuilder builder = new GeoportalResolverQueryStringBuilder(itemType,itemId);
builder.scope(gcubeScope);
builder.resolverAs(RESOLVE_AS.PUBLIC);
//builder.resolverAs(RESOLVE_AS.PRIVATE);
Map<String, String> params = builder.buildQueryParameters();
String shortLink = resolver.getLink(params, true);
System.out.println(shortLink);
} catch (UriResolverMapException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
// @Test IS OK
public void testSHUB() {
try {
@ -78,12 +191,14 @@ public class UriResolverManagerTest {
public void testGIS() {
try {
ScopeProvider.instance.set("/pred4s/preprod/preVRE");
String scope = "/pred4s/preprod/preVRE";
String UUID = "da165110-88fd-11da-a88f-000d939bc5d8";
ScopeProvider.instance.set(scope);
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("gis-UUID", UUID);
params.put("scope", scope);
String shortLink = resolver.getLink(params, true);
System.out.println(shortLink);
} catch (UriResolverMapException e) {
e.printStackTrace();