Speedup URIResolver

This commit is contained in:
luca.frosini 2023-06-28 17:23:23 +02:00
parent b189ad0e72
commit 0f1d3e544b
3 changed files with 51 additions and 7 deletions

View File

@ -775,7 +775,8 @@ public class CKANPackage extends CKAN implements Moderated {
protected String addItemURLViaResolver(JsonNode jsonNode) {
// Adding Item URL via Resolver
itemURL = URIResolver.getCatalogueItemURL(name);
URIResolver uriResolver = URIResolver.getInstance();
itemURL = uriResolver.getCatalogueItemURL(name);
addExtraField(jsonNode, EXTRAS_ITEM_URL_KEY, itemURL);
return itemURL;
}

View File

@ -1,5 +1,6 @@
package org.gcube.gcat.utils;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
@ -11,7 +12,7 @@ import org.gcube.portlets.user.uriresolvermanager.UriResolverManager;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public abstract class URIResolver {
public class URIResolver {
private static final String CATALOGUE_CONTEXT = "gcube_scope";
private static final String ENTITY_TYPE = "entity_context";
@ -19,11 +20,36 @@ public abstract class URIResolver {
private static final String DATASET = "dataset";
protected static URIResolver uriResolver;
protected UriResolverManager uriResolverManager;
protected Calendar expireTime;
public static String getCatalogueItemURL(String name) {
public static URIResolver getInstance() {
if(uriResolver == null) {
uriResolver = new URIResolver();
}else {
Calendar now = Calendar.getInstance();
if(now.after(uriResolver.expireTime)) {
uriResolver = new URIResolver();
}
}
return uriResolver;
}
private URIResolver() {
try {
uriResolverManager = new UriResolverManager("CTLG");
expireTime = Calendar.getInstance();
expireTime.add(Calendar.MINUTE, 30);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String getCatalogueItemURL(String name) {
try {
String context = SecretManagerProvider.instance.get().getContext();
UriResolverManager uriResolverManager = new UriResolverManager("CTLG");
Map<String, String> params = new HashMap<>();
params.put(CATALOGUE_CONTEXT, context);
params.put(ENTITY_TYPE, DATASET);
@ -37,4 +63,4 @@ public abstract class URIResolver {
}
}
}
}

View File

@ -1,5 +1,7 @@
package org.gcube.gcat.utils;
import java.util.Calendar;
import org.gcube.common.encryption.encrypter.StringEncrypter;
import org.gcube.gcat.ContextTest;
import org.junit.Test;
@ -11,9 +13,24 @@ public class URIResolverTest extends ContextTest {
private static final Logger logger = LoggerFactory.getLogger(URIResolverTest.class);
@Test
public void getURL() {
String catalogueItemURL = URIResolver.getCatalogueItemURL("my_first_restful_transaction_model");
public void getInstanceTest() throws Exception {
URIResolver uriResolver = URIResolver.getInstance();
String catalogueItemURL = uriResolver.getCatalogueItemURL("my_first_restful_transaction_model");
logger.debug("Item URL is {}", catalogueItemURL);
URIResolver.uriResolver = null;
uriResolver = URIResolver.getInstance();
catalogueItemURL = uriResolver.getCatalogueItemURL("my_first_restful_transaction_model");
logger.debug("Item URL is {}", catalogueItemURL);
uriResolver.expireTime = Calendar.getInstance();
uriResolver.expireTime.add(Calendar.MINUTE, -1);
uriResolver = URIResolver.getInstance();
catalogueItemURL = uriResolver.getCatalogueItemURL("my_first_restful_transaction_model");
logger.debug("Item URL is {}", catalogueItemURL);
}
@Test