[#24842] Integrated the new Geoportal-Resolver

This commit is contained in:
Francesco Mangiacrapa 2023-03-27 16:22:21 +02:00
parent a99e6dce86
commit a6ba441015
8 changed files with 458 additions and 9 deletions

View File

@ -3,6 +3,10 @@
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-SNAPSHOT]
- [#24842] Integrated the new Geoportal-Resolver
## [v1.6.0] - 2022-07-26
**New**

View File

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

View File

@ -112,6 +112,14 @@ 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");
@ -166,6 +174,12 @@ public class CatalogueResolverCallBuilder extends GenericResolver {
}
/**
* Short the link.
*
* @param link the link
* @return the string
*/
private String shortTheLink(String link) {
String toReturnLink = link;

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,73 @@
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;
/**
* 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();
}
/**
* 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;
}
/**
* To string.
*
* @return the string
*/
@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("]");
return builder.toString();
}
}

View File

@ -0,0 +1,109 @@
/*
*
*/
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";
private String itemType;
private String itemId;
private String gcubeScope;
/**
* 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;
}
/**
* 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;
}
/**
* 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());
return query;
}
}

View File

@ -1,6 +1,3 @@
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;
/**

View File

@ -8,9 +8,8 @@ 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.junit.Test;
import org.gcube.portlets.user.uriresolvermanager.resolvers.query.GeoportalResolverQueryStringBuilder;
/**
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
@ -19,7 +18,7 @@ import org.junit.Test;
*/
public class UriResolverManagerTest {
// @Test
//@Test
public void testUriResolverManger() {
UriResolverManager manager;
try {
@ -45,7 +44,7 @@ public class UriResolverManagerTest {
}
}
// @Test
public void testCTLG() {
@ -68,7 +67,7 @@ public class UriResolverManagerTest {
}
}
// @Test
//@Test
public void testCTLGWithQueryString() {
try {
@ -125,6 +124,42 @@ public class UriResolverManagerTest {
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);
//Method 2
GeoportalResolverQueryStringBuilder builder = new GeoportalResolverQueryStringBuilder(itemType,itemId);
builder.scope(gcubeScope);
Map<String, String> params = builder.buildQueryParameters();
String shortLink = resolver.getLink(params, false);
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() {