The parameters hierarchicalMode and includeContextsInHeader are now

instance based
This commit is contained in:
Luca Frosini 2022-07-21 16:55:22 +02:00
parent 3b06356a26
commit c63dc4c4c8
3 changed files with 40 additions and 113 deletions

View File

@ -28,6 +28,15 @@ import org.gcube.informationsystem.types.reference.Type;
*/
public interface ResourceRegistryClient {
public boolean isHierarchicalMode();
public void setHierarchicalMode(boolean hierarchicalMode);
public boolean isIncludeContextsInHeader();
public void setIncludeContextsInHeader(boolean includeContextsInHeader);
public List<Context> getAllContext() throws ResourceRegistryException;
public boolean existContext(String uuid) throws ContextNotFoundException, ResourceRegistryException;

View File

@ -1,17 +1,10 @@
package org.gcube.informationsystem.resourceregistry.client;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.resources.gcore.ServiceEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.informationsystem.resourceregistry.api.Constants;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.gcube.resources.discovery.icclient.ICFactory;
import org.gcube.informationsystem.resourceregistry.api.rest.ServiceInstance;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -22,58 +15,6 @@ public class ResourceRegistryClientFactory {
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryClientFactory.class);
protected static boolean HIERARCHICAL_MODE;
protected static boolean isHierarchicalMode() {
return ResourceRegistryClientFactory.HIERARCHICAL_MODE;
}
/**
* The affected methods are {@link ResourceRegistryClient} instances safe methods i.e. read* and exists*
* @param hierarchicalMode
*/
public static void setHierarchicalMode(boolean hierarchicalMode) {
ResourceRegistryClientFactory.HIERARCHICAL_MODE = hierarchicalMode;
}
protected static boolean INCLUDE_CONTEXTS_IN_INSTANCES_HEADER;
protected static boolean includeContextsInInstanceHeader() {
return ResourceRegistryClientFactory.INCLUDE_CONTEXTS_IN_INSTANCES_HEADER;
}
/**
* The affected methods are {@link ResourceRegistryClient} instances safe methods i.e. read* and exists*
* @param includeContextsInInstancesHeader
*/
public static void includeContextsInInstanceHeader(boolean includeContextsInInstancesHeader) {
ResourceRegistryClientFactory.INCLUDE_CONTEXTS_IN_INSTANCES_HEADER = includeContextsInInstancesHeader;
}
protected static List<String> addresses;
static {
addresses = new ArrayList<>();
}
private static String FORCED_URL = null;
protected static void forceToURL(String url){
FORCED_URL = url;
HIERARCHICAL_MODE = false;
}
private static String classFormat = "$resource/Profile/ServiceClass/text() eq '%1s'";
private static String nameFormat = "$resource/Profile/ServiceName/text() eq '%1s'";
private static String statusFormat = "$resource/Profile/DeploymentData/Status/text() eq 'ready'";
private static String containsFormat = "$entry/@EntryName eq '%1s'";
private static String serviceEndpointCategoryFormat = "$resource/Profile/Category/text() eq '%1s'";
private static String serviceEndpointNameFormat = "$resource/Profile/Name/text() eq '%1s'";
private static String serviceEndpointstatusFormat = "$resource/Profile/RunTime/Status/text() eq 'READY'";
public static String getCurrentContextFullName() {
String token = SecurityTokenProvider.instance.get();
AuthorizationEntry authorizationEntry = null;
@ -85,43 +26,10 @@ public class ResourceRegistryClientFactory {
return authorizationEntry.getContext();
}
private static SimpleQuery queryForService(){
return ICFactory.queryFor(GCoreEndpoint.class)
.addCondition(String.format(classFormat, Constants.SERVICE_CLASS))
.addCondition(String.format(nameFormat, Constants.SERVICE_NAME))
.addCondition(String.format(statusFormat))
.addVariable("$entry","$resource/Profile/AccessPoint/RunningInstanceInterfaces/Endpoint")
.addCondition(String.format(containsFormat, Constants.SERVICE_ENTRY_NAME))
.setResult("$entry/text()");
}
private static String FORCED_URL = null;
private static SimpleQuery queryForProxy(){
return ICFactory.queryFor(ServiceEndpoint.class)
.addCondition(String.format(serviceEndpointCategoryFormat, Constants.SERVICE_CLASS))
.addCondition(String.format(serviceEndpointNameFormat, Constants.SERVICE_NAME))
.addCondition(String.format(serviceEndpointstatusFormat))
.addVariable("$entry","$resource/Profile/AccessPoint/Interface/Endpoint")
.addCondition(String.format(containsFormat, Constants.SERVICE_ENTRY_NAME))
.setResult("$entry/text()");
}
protected static List<String> getAddresses(){
List<String> addresses = new ArrayList<>();
try {
SimpleQuery proxyQuery = queryForProxy();
addresses = ICFactory.client().submit(proxyQuery);
if(addresses==null || addresses.isEmpty()){
throw new Exception("No ResourceRegistry Proxy Found");
}
} catch (Exception e) {
logger.debug("{}. Looking for RunningInstance.", e.getMessage());
SimpleQuery serviceQuery = queryForService();
addresses = ICFactory.client().submit(serviceQuery);
}
return addresses;
protected static void forceToURL(String url){
FORCED_URL = url;
}
public static ResourceRegistryClient create() {
@ -130,21 +38,10 @@ public class ResourceRegistryClientFactory {
if(FORCED_URL!=null){
address = FORCED_URL;
}else {
if(addresses==null || addresses.isEmpty()) {
addresses = getAddresses();
}
if(addresses==null || addresses.isEmpty()){
String error = String.format("No %s:%s found in the current context %s", Constants.SERVICE_CLASS, Constants.SERVICE_NAME, getCurrentContextFullName());
throw new RuntimeException(error);
}
Random random = new Random();
int index = random.nextInt(addresses.size());
address = addresses.get(index);
address = String.format("%s/%s", ServiceInstance.getServiceURL(),Constants.SERVICE_NAME);
}
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
return new ResourceRegistryClientImpl(address);
}

View File

@ -55,6 +55,25 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
protected final String address;
protected boolean hierarchicalMode;
protected boolean includeContextsInHeader;
public boolean isHierarchicalMode() {
return hierarchicalMode;
}
public void setHierarchicalMode(boolean hierarchicalMode) {
this.hierarchicalMode = hierarchicalMode;
}
public boolean isIncludeContextsInHeader() {
return includeContextsInHeader;
}
public void setIncludeContextsInHeader(boolean includeContextsInHeader) {
this.includeContextsInHeader = includeContextsInHeader;
}
private GXHTTPStringRequest includeAdditionalQueryParameters(GXHTTPStringRequest gxHTTPStringRequest) throws UnsupportedEncodingException{
return includeAdditionalQueryParameters(gxHTTPStringRequest, null);
}
@ -65,21 +84,21 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
}
private GXHTTPStringRequest checkHierarchicalMode(GXHTTPStringRequest gxHTTPStringRequest, Map<String,String> queryParams) throws UnsupportedEncodingException{
if(ResourceRegistryClientFactory.isHierarchicalMode()) {
if(hierarchicalMode) {
if(queryParams==null) {
queryParams = new HashMap<>();
}
queryParams.put(AccessPath.HIERARCHICAL_MODE_QUERY_PARAMETER, Boolean.toString(true));
queryParams.put(AccessPath.HIERARCHICAL_MODE_QUERY_PARAMETER, Boolean.toString(hierarchicalMode));
}
return gxHTTPStringRequest.queryParams(queryParams);
}
private GXHTTPStringRequest checkIncludeContextsInInstanceHeader(GXHTTPStringRequest gxHTTPStringRequest, Map<String,String> queryParams) throws UnsupportedEncodingException{
if(ResourceRegistryClientFactory.includeContextsInInstanceHeader()) {
if(includeContextsInHeader) {
if(queryParams==null) {
queryParams = new HashMap<>();
}
queryParams.put(AccessPath.INCLUDE_CONTEXTS_IN_HEADER_QUERY_PARAMETER, Boolean.toString(true));
queryParams.put(AccessPath.INCLUDE_CONTEXTS_IN_HEADER_QUERY_PARAMETER, Boolean.toString(includeContextsInHeader));
}
return gxHTTPStringRequest.queryParams(queryParams);
}
@ -96,6 +115,8 @@ public class ResourceRegistryClientImpl implements ResourceRegistryClient {
public ResourceRegistryClientImpl(String address) {
this.address = address;
this.hierarchicalMode = false;
this.includeContextsInHeader = false;
ContextCache contextCache = ContextCache.getInstance();
contextCache.setContextCacheRenewal(contextCacheRenewal);
}