diff --git a/pom.xml b/pom.xml index badd041..a4f3f4d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,6 +1,6 @@ - + maven-parent org.gcube.tools @@ -8,13 +8,13 @@ - + 4.0.0 org.gcube.data.analysis nlphub 1.0.0-SNAPSHOT war - + 7.0.70 1.1 @@ -91,17 +91,25 @@ common-configuration-scanner provided + org.gcube.resources registry-publisher provided + org.gcube.core common-scope provided + + + org.gcube.resources.discovery + ic-client + + org.apache.tomcat @@ -159,7 +167,7 @@ --> - + maven-compiler-plugin 3.5.1 diff --git a/src/main/java/org/gcube/data/analysis/nlphub/is/DiscoverDataMinerService.java b/src/main/java/org/gcube/data/analysis/nlphub/is/DiscoverDataMinerService.java new file mode 100644 index 0000000..c3a35a9 --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/nlphub/is/DiscoverDataMinerService.java @@ -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 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; + + } + +} diff --git a/src/main/java/org/gcube/data/analysis/nlphub/is/InformationSystemUtils.java b/src/main/java/org/gcube/data/analysis/nlphub/is/InformationSystemUtils.java new file mode 100644 index 0000000..61c88fa --- /dev/null +++ b/src/main/java/org/gcube/data/analysis/nlphub/is/InformationSystemUtils.java @@ -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 retrieveServiceAddress(String category, String name, String scope) throws Exception { + try { + + if (scope == null || scope.length() == 0) + return new ArrayList(); + + 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 client = ICFactory.client(); + List 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; + } + } + +} diff --git a/src/main/java/org/gcube/data/analysis/nlphub/nlp/NLpLanguageRecognizer.java b/src/main/java/org/gcube/data/analysis/nlphub/nlp/NLpLanguageRecognizer.java index 635c7d0..9599268 100644 --- a/src/main/java/org/gcube/data/analysis/nlphub/nlp/NLpLanguageRecognizer.java +++ b/src/main/java/org/gcube/data/analysis/nlphub/nlp/NLpLanguageRecognizer.java @@ -9,6 +9,7 @@ import java.net.URLEncoder; import javax.servlet.http.HttpServletResponse; import javax.xml.parsers.DocumentBuilderFactory; +import org.gcube.data.analysis.nlphub.is.DiscoverDataMinerService; import org.gcube.data.analysis.nlphub.legacy.JsonManager; import org.gcube.data.analysis.nlphub.legacy.NlpHubException; import org.gcube.data.analysis.nlphub.shared.Constants; @@ -47,8 +48,14 @@ public class NLpLanguageRecognizer { public static void run(String dataMiner, String sentence, String token, String publicLink, HttpServletResponse response) throws NlpHubException { try { - if (dataMiner == null) - dataMiner = Constants.DEFAULT_DATAMINER_URL; + try { + DiscoverDataMinerService discoverDataMinerService=new DiscoverDataMinerService(); + dataMiner=discoverDataMinerService.retrieveServiceUrl(token); + } catch (Exception e) { + logger.error("Error retrieving DataMiner service:" + e.getMessage(),e); + throw new Exception("Error retrieving DataMiner service:" + e.getMessage()); + } + //else // dataMiner = "http://" + dataMiner; String urlService = dataMiner + "?request=Execute&service=WPS&Version=1.0.0"; diff --git a/src/main/java/org/gcube/data/analysis/nlphub/nlp/NlpAsyncNerRunner.java b/src/main/java/org/gcube/data/analysis/nlphub/nlp/NlpAsyncNerRunner.java index 922fb49..ec784d4 100644 --- a/src/main/java/org/gcube/data/analysis/nlphub/nlp/NlpAsyncNerRunner.java +++ b/src/main/java/org/gcube/data/analysis/nlphub/nlp/NlpAsyncNerRunner.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import javax.xml.parsers.DocumentBuilderFactory; +import org.gcube.data.analysis.nlphub.is.DiscoverDataMinerService; import org.gcube.data.analysis.nlphub.legacy.AsyncHttpRequest; import org.gcube.data.analysis.nlphub.shared.Constants; import org.slf4j.Logger; @@ -24,21 +25,29 @@ public class NlpAsyncNerRunner extends AsyncHttpRequest { private static final Logger logger = LoggerFactory.getLogger(NlpAsyncNerRunner.class); public static final String WPS_EXECUTE_URL = "?request=Execute&service=WPS&Version=1.0.0"; public static final String WPS_DESCRIBE_PROCESS_URL = "?request=DescribeProcess&service=WPS&Version=1.0.0"; - public static final String DATAMINER_DEFAULT="http://dataminer-prototypes.d4science.org/wps/WebProcessingService"; // private String identifier, token, httpMethod, annotations, publicLink, // dataMiner; private String identifier; private RunnerCommander commander; public NlpAsyncNerRunner(String dataMiner, String identifier, String token, String publicLink, String annotations, - String language, RunnerCommander commander) { + String language, RunnerCommander commander) throws Exception { super(); this.commander=commander; this.identifier=identifier; String httpMethod = "GET"; - if (dataMiner == null) - dataMiner = Constants.DEFAULT_DATAMINER_URL; + + try { + DiscoverDataMinerService discoverDataMinerService=new DiscoverDataMinerService(); + dataMiner=discoverDataMinerService.retrieveServiceUrl(token); + } catch (Exception e) { + logger.error("Error retrieving DataMiner service:" + e.getMessage(),e); + throw new Exception("Error retrieving DataMiner service:" + e.getMessage()); + } + + + // else // this.dataMiner = "http://" + dataMiner; logger.debug("NlpAsyncNerRunner: Create Excution Request"); diff --git a/src/main/java/org/gcube/data/analysis/nlphub/nlp/RunnerCommander.java b/src/main/java/org/gcube/data/analysis/nlphub/nlp/RunnerCommander.java index 7a46256..da20345 100644 --- a/src/main/java/org/gcube/data/analysis/nlphub/nlp/RunnerCommander.java +++ b/src/main/java/org/gcube/data/analysis/nlphub/nlp/RunnerCommander.java @@ -84,7 +84,7 @@ public class RunnerCommander extends Thread { } } - private void runAlgorithms() { + private void runAlgorithms() throws Exception { for (String id : identifiers) { NlpAsyncNerRunner n = new NlpAsyncNerRunner(dataMiner, id, token, link, annotationList, null, this); logger.debug("Run algorithm: "+id); @@ -93,7 +93,12 @@ public class RunnerCommander extends Thread { } public void run() { - runAlgorithms(); + try { + runAlgorithms(); + } catch (Exception e1) { + logger.error("Error in run algorithms: "+e1.getLocalizedMessage(),e1); + return; + } long counter = 0; try { while (counter <= maxWaitingTime) { diff --git a/src/main/java/org/gcube/data/analysis/nlphub/shared/Constants.java b/src/main/java/org/gcube/data/analysis/nlphub/shared/Constants.java index 8017025..9990cc5 100644 --- a/src/main/java/org/gcube/data/analysis/nlphub/shared/Constants.java +++ b/src/main/java/org/gcube/data/analysis/nlphub/shared/Constants.java @@ -1,12 +1,17 @@ package org.gcube.data.analysis.nlphub.shared; + public class Constants { public static final boolean DEBUG = false; public static final boolean TEST_ENABLE = false; public static final String DEFAULT_USER = "giancarlo.panichi"; public static final String DEFAULT_SCOPE = "/gcube/devNext/NextNext"; + public static final String DEFAULT_TOKEN = ""; + public static final String DATA_MINER_SERVICE_NAME = "DataMiner"; + public static final String DATAMINER_SERVICE_CATEGORY = "DataAnalysis"; + public static final String DEFAULT_DATAMINER_URL = "http://dataminer-prototypes.d4science.org/wps/WebProcessingService"; public static final String TOKEN_PARAMETER = "gcube-token"; @@ -24,6 +29,7 @@ public class Constants { + /*public static String hexDump(byte[] bytes) { char[] hexArray = "0123456789ABCDEF".toCharArray(); char[] hexChars = new char[bytes.length * 3]; diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp index 0370f75..776125c 100644 --- a/src/main/webapp/index.jsp +++ b/src/main/webapp/index.jsp @@ -21,6 +21,7 @@