Added support to discover DataMiner service by user token
git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/nlphub@178538 82a268e6-3cf1-43bd-a215-b396298e98cfmaster
parent
98c4e2e87c
commit
b847f5df4d
@ -0,0 +1,73 @@
|
||||
package org.gcube.data.analysis.nlphub.is;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.common.authorization.library.AuthorizationEntry;
|
||||
import org.gcube.data.analysis.nlphub.shared.Constants;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.gcube.common.authorization.client.Constants.authorizationService;
|
||||
|
||||
public class DiscoverDataMinerService {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(DiscoverDataMinerService.class);
|
||||
|
||||
public String retrieveServiceUrl(String token) throws Exception {
|
||||
logger.debug("Retrieve DataMiner Service by token: " + token);
|
||||
String scope = retrieveScope(token);
|
||||
logger.debug("Retrieve DataMiner Service in scope: "+scope);
|
||||
String url= retrieveServiceInScope(scope);
|
||||
logger.debug("DataMiner url: "+url);
|
||||
return url;
|
||||
}
|
||||
|
||||
private String retrieveServiceInScope(String scope) throws Exception {
|
||||
List<String> serviceAddress = InformationSystemUtils
|
||||
.retrieveServiceAddress(Constants.DATAMINER_SERVICE_CATEGORY, Constants.DATA_MINER_SERVICE_NAME, scope);
|
||||
logger.debug("Service Address retrieved:" + serviceAddress);
|
||||
if (serviceAddress == null || serviceAddress.size() < 1) {
|
||||
logger.error("No DataMiner service address available!");
|
||||
throw new Exception("No DataMiner service address available!");
|
||||
} else {
|
||||
logger.info("DataMiner service address found: " + serviceAddress.get(0));
|
||||
return serviceAddress.get(0);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private String retrieveScope(String token) throws Exception {
|
||||
|
||||
String userName = null;
|
||||
String scope = null;
|
||||
|
||||
if (Constants.DEBUG) {
|
||||
logger.debug("Debug Mode");
|
||||
userName = Constants.DEFAULT_USER;
|
||||
scope = Constants.DEFAULT_SCOPE;
|
||||
token = Constants.DEFAULT_TOKEN;
|
||||
} else {
|
||||
logger.debug("Production Mode");
|
||||
if (token == null || token.isEmpty()) {
|
||||
logger.error("Error retrieving user credentials: token=" + token);
|
||||
throw new Exception("Error retrieving user credentials");
|
||||
}
|
||||
|
||||
try {
|
||||
logger.debug("Retrieving user credentials");
|
||||
AuthorizationEntry entry = authorizationService().get(token);
|
||||
userName = entry.getClientInfo().getId();
|
||||
scope = entry.getContext();
|
||||
} catch (Exception e) {
|
||||
logger.error("Error retrieving user credentials: " + e.getLocalizedMessage());
|
||||
throw new Exception(e.getLocalizedMessage(), e);
|
||||
}
|
||||
|
||||
}
|
||||
logger.debug("UserName: " + userName);
|
||||
logger.debug("Scope: " + scope);
|
||||
return scope;
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package org.gcube.data.analysis.nlphub.is;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.gcube.common.resources.gcore.ServiceEndpoint;
|
||||
import org.gcube.common.scope.api.ScopeProvider;
|
||||
import org.gcube.resources.discovery.client.api.DiscoveryClient;
|
||||
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
|
||||
import org.gcube.resources.discovery.icclient.ICFactory;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class InformationSystemUtils {
|
||||
private static Logger logger = LoggerFactory.getLogger(InformationSystemUtils.class);
|
||||
|
||||
public static List<String> retrieveServiceAddress(String category, String name, String scope) throws Exception {
|
||||
try {
|
||||
|
||||
if (scope == null || scope.length() == 0)
|
||||
return new ArrayList<String>();
|
||||
|
||||
ScopeProvider.instance.set(scope);
|
||||
|
||||
SimpleQuery query = ICFactory.queryFor(ServiceEndpoint.class);
|
||||
query.addCondition("$resource/Profile/Category/text() eq '" + category + "'")
|
||||
.addCondition("$resource/Profile/Name/text() eq '" + name + "'")
|
||||
.setResult("$resource/Profile/AccessPoint/Interface/Endpoint/text()");
|
||||
DiscoveryClient<String> client = ICFactory.client();
|
||||
List<String> addresses = client.submit(query);
|
||||
|
||||
return addresses;
|
||||
|
||||
} catch (Throwable e) {
|
||||
logger.error("Error in discovery DataMiner Service Endpoint in scope: " + scope);
|
||||
logger.error("Error: " + e.getLocalizedMessage());
|
||||
e.printStackTrace();
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue