Merge branch 'master' of gitea@code-repo.d4science.org:gCubeSystem/resource-registry-context-client.git
This commit is contained in:
commit
d6bb89079b
|
@ -1,17 +1,10 @@
|
||||||
package org.gcube.informationsystem.resourceregistry.contexts;
|
package org.gcube.informationsystem.resourceregistry.contexts;
|
||||||
|
|
||||||
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.AuthorizationEntry;
|
||||||
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
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.common.scope.api.ScopeProvider;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.Constants;
|
import org.gcube.informationsystem.resourceregistry.api.Constants;
|
||||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
import org.gcube.informationsystem.resourceregistry.api.rest.ServiceInstance;
|
||||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -22,28 +15,6 @@ public class ResourceRegistryContextClientFactory {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientFactory.class);
|
private static final Logger logger = LoggerFactory.getLogger(ResourceRegistryContextClientFactory.class);
|
||||||
|
|
||||||
protected static List<String> addresses;
|
|
||||||
|
|
||||||
static {
|
|
||||||
addresses = new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String FORCED_URL = null;
|
|
||||||
|
|
||||||
protected static void forceToURL(String url){
|
|
||||||
FORCED_URL = url;
|
|
||||||
}
|
|
||||||
|
|
||||||
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() {
|
public static String getCurrentContextFullName() {
|
||||||
String token = SecurityTokenProvider.instance.get();
|
String token = SecurityTokenProvider.instance.get();
|
||||||
AuthorizationEntry authorizationEntry = null;
|
AuthorizationEntry authorizationEntry = null;
|
||||||
|
@ -55,43 +26,10 @@ public class ResourceRegistryContextClientFactory {
|
||||||
return authorizationEntry.getContext();
|
return authorizationEntry.getContext();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static SimpleQuery queryForService(){
|
private static String FORCED_URL = null;
|
||||||
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 SimpleQuery queryForProxy(){
|
protected static void forceToURL(String url){
|
||||||
return ICFactory.queryFor(ServiceEndpoint.class)
|
FORCED_URL = url;
|
||||||
.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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ResourceRegistryContextClient create() {
|
public static ResourceRegistryContextClient create() {
|
||||||
|
@ -100,21 +38,10 @@ public class ResourceRegistryContextClientFactory {
|
||||||
if(FORCED_URL!=null){
|
if(FORCED_URL!=null){
|
||||||
address = FORCED_URL;
|
address = FORCED_URL;
|
||||||
}else {
|
}else {
|
||||||
|
address = String.format("%s/%s", ServiceInstance.getServiceURL(),Constants.SERVICE_NAME);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
logger.trace("The {} will be contacted at {}", Constants.SERVICE_NAME, address);
|
||||||
return new ResourceRegistryContextClientImpl(address);
|
return new ResourceRegistryContextClientImpl(address);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,7 +17,6 @@ import org.gcube.informationsystem.resourceregistry.api.exceptions.NotFoundExcep
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextAlreadyPresentException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.contexts.ContextNotFoundException;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.rest.AccessPath;
|
|
||||||
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
|
import org.gcube.informationsystem.resourceregistry.api.rest.ContextPath;
|
||||||
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
|
import org.gcube.informationsystem.resourceregistry.api.rest.httputils.HTTPUtility;
|
||||||
import org.gcube.informationsystem.utils.ElementMapper;
|
import org.gcube.informationsystem.utils.ElementMapper;
|
||||||
|
|
|
@ -6,3 +6,4 @@
|
||||||
/registry.properties.empty
|
/registry.properties.empty
|
||||||
/registry.properties.redirect-test
|
/registry.properties.redirect-test
|
||||||
/registry.properties.test
|
/registry.properties.test
|
||||||
|
/config.ini
|
||||||
|
|
Loading…
Reference in New Issue