From c7f90167eedc65639e17025ffdf8ace592b63e73 Mon Sep 17 00:00:00 2001 From: Erik Perrone Date: Thu, 22 Mar 2018 12:10:09 +0000 Subject: [PATCH] Gestione parametro link file esterno. git-svn-id: http://svn.research-infrastructures.eu/public/d4science/gcube/trunk/data-analysis/nlphub@165329 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../java/org/gcube/nlphub/NLPUploader.java | 16 +- .../org/gcube/nlphub/legacy/Constants.java | 2 +- src/main/webapp/external-test.html | 17 ++ src/main/webapp/js/main.js | 239 ++++++++++-------- 4 files changed, 166 insertions(+), 108 deletions(-) create mode 100644 src/main/webapp/external-test.html diff --git a/src/main/java/org/gcube/nlphub/NLPUploader.java b/src/main/java/org/gcube/nlphub/NLPUploader.java index 1e15578..d61c71d 100644 --- a/src/main/java/org/gcube/nlphub/NLPUploader.java +++ b/src/main/java/org/gcube/nlphub/NLPUploader.java @@ -86,7 +86,16 @@ public class NLPUploader extends HttpServlet { return; } String link = ws.getPublicLink(fileName, token); - writer.println(new JsonManager().getSuccessJsonResponse("" + link)); + if(request.getParameter("getlang") != null) { + String sentence = NlpUtils.getLanguageRecognizerDigest(new String(content)); + logger.info(sentence); + try { + NLpLanguageRecognizer.run(sentence, token, link, response); + } catch (NlpHubException ex) { + writer.println(new JsonManager().getSuccessJsonResponse(Constants.UNAVAILABLE, link)); + } + } + else writer.println(new JsonManager().getSuccessJsonResponse("" + link)); } catch (Exception x) { x.printStackTrace(); logger.error(x.getClass().getName() + ": " + x.getLocalizedMessage()); @@ -138,11 +147,6 @@ public class NLPUploader extends HttpServlet { String link = ws.getPublicLink(fileName, token); String sentence = NlpUtils.getLanguageRecognizerDigest(stringContent); logger.info(sentence); - // System.out.println(sentence); - // NLpLanguageRecognizer recognizer = new - // NLpLanguageRecognizer(NLPHub.service, token, sentence, link, - // response); - // recognizer.run(); try { NLpLanguageRecognizer.run(sentence, token, link, response); } catch (NlpHubException ex) { diff --git a/src/main/java/org/gcube/nlphub/legacy/Constants.java b/src/main/java/org/gcube/nlphub/legacy/Constants.java index 092c6dd..c455876 100644 --- a/src/main/java/org/gcube/nlphub/legacy/Constants.java +++ b/src/main/java/org/gcube/nlphub/legacy/Constants.java @@ -12,7 +12,7 @@ public class Constants { public static String CONTENT_TYPE = "Content-Type"; public static String UNAVAILABLE = "unavailable"; public static String ERROR_ID = "ERROR"; - public static String INPUT_FILE_PARAMETER = "input-file"; + public static String INPUT_FILE_PARAMETER = "input"; public static String getToken(HttpServletRequest request, boolean devMode) { String token = request.getParameter(TOKEN_PARAMETER); diff --git a/src/main/webapp/external-test.html b/src/main/webapp/external-test.html new file mode 100644 index 0000000..e9ea2c6 --- /dev/null +++ b/src/main/webapp/external-test.html @@ -0,0 +1,17 @@ + + + + +External Test + + + + + + \ No newline at end of file diff --git a/src/main/webapp/js/main.js b/src/main/webapp/js/main.js index 593a058..1eea5dc 100644 --- a/src/main/webapp/js/main.js +++ b/src/main/webapp/js/main.js @@ -12,8 +12,8 @@ var resultText = ""; var hexLetters = '0123456789ABCDEF'; var txtFlag = true; var checkedAnnotation = ""; -//var checkedAlgs = []; var algIndexedArray = []; +var computedLanguage = ""; // ------------------------------------------------------------------------------------------------------ // Starting point: get the csv file with algorithm parameters and set the page @@ -23,7 +23,57 @@ var algIndexedArray = []; $(document).ready(function() { disableExecuteButton(); showAnnotationsList(); - + checkInputLink(); +}); + +checkInputLink = function() { + if(inputFile != "") { + showProgress(); + $.ajax({ + url : inputFile, + type : "GET", + async : true, + success : function(data, stato) { + $("#input-textarea").val(data); + freeText = $("#input-textarea").val(); + $.ajax({ + url : "/nlphub/nlphub-uploader-servlet?getlang=on&freetext=" + + encodeURIComponent(freeText), + type : "POST", + async : true, + contentType : "text/plain; charset=utf-8", + success : function(data, stato) { + //publicLink = data.message; + //if (publicLink == null) { + // alert("Error uploading file."); + //} + hideProgress(); + computedLanguage = data.language; + //textAreaEnable(false); + initApplication(); + }, + error : function(richiesta, stato, + errori) { + alert("Unexpected Error. Status: " + + richiesta.status); + hideProgress(); + textAreaEnable(true); + $("#file-info").empty(); + initApplication(); + } + }); + }, + error : function(richiesta, stato, errori) { + alert("Unexpected error reading from link: '" + inputFile + "'.\nStatus: " + richiesta.status + "\nClick OK to start application."); + hideProgress(); + initApplication(); + } + }); + } + else initApplication(); +} + +initApplication = function() { $.ajax({ url : "https://data.d4science.org/bnp4UDNyb21lSURkQXdDUnlSS0JkVWgzWk1KMUxWVGZHbWJQNStIS0N6Yz0", type : "GET", @@ -40,7 +90,7 @@ $(document).ready(function() { resizeLogo(); enableAjaxFileUpload(); collapsibleHandler(); - + $("#logo-image").click(function() { backHandler(); }); @@ -134,13 +184,96 @@ $(document).ready(function() { alert(errMsg); } }); + + if($("#input-textarea").val() != "") { + disableAjaxFileUpload(); + if(computedLanguage != "") { + checkLanguage(computedLanguage); + selectAnnotationsByLanguage(); + computedLanguage = ""; + } + enableExecuteButton(); + } }, error : function(richiesta, stato, errori) { alert("Unexpected Error. Status: " + richiesta.status); } }); -}); +} +/* + * Set the listeners on the text area and the execute button + */ +setEventListeners = function() { + $(".ajax-file-upload").attr("id", "ajax-file-upload-id"); + + $("#input-textarea").on("keyup", function() { + enableDisableTextArea(); + }); + + $("#input-textarea").change(function() { + enableDisableTextArea(); + }); + + window.addEventListener('contextmenu', function(e) { + //alert("You've tried to open context menu"); //here you draw your own menu + //e.preventDefault(); + enableDisableTextArea(); + }, false); + + $("#execute-button") + .click( + function() { + if(!isEnabled()) + return; + + if ((publicLink == null) + && ($("#input-textarea").val() == "")) { + alert("You must upload a file or write some text in the text area before submit a request."); + return; + } + + disableExecuteButton(); + + showProgress(); + // if some text has been written in the text area, then + // a corresponding text file + // must be written in the workspace and the public link + // is set. + if ($("#input-textarea").val().length > 0) { + if(publicLink == null) { + freeText = $("#input-textarea").val(); + $.ajax({ + url : "/nlphub/nlphub-uploader-servlet?freetext=" + + encodeURIComponent(freeText), + type : "POST", + async : true, + contentType : "text/plain; charset=utf-8", + success : function(data, stato) { + publicLink = data.message; + if (publicLink == null) { + alert("Error uploading file."); + } + runAlgorithm(); + }, + error : function(richiesta, stato, + errori) { + alert("Unexpected Error. Status: " + + richiesta.status); + hideProgress(); + textAreaEnable(true); + $("#file-info").empty(); + } + }); + } + else runAlgorithm(); + } + + else { + runAlgorithm(); + } + }); +} collapsibleHandler = function() { // $('.collapsible').collapsible(); @@ -360,91 +493,11 @@ enableDisableTextArea = function() { } } -/* - * Set the listeners on the text area and the execute button - */ -setEventListeners = function() { - $(".ajax-file-upload").attr("id", "ajax-file-upload-id"); - - $("#input-textarea").on("keyup", function() { - enableDisableTextArea(); - }); - - $("#input-textarea").change(function() { - enableDisableTextArea(); - }); - - window.addEventListener('contextmenu', function(e) { - //alert("You've tried to open context menu"); //here you draw your own menu - //e.preventDefault(); - enableDisableTextArea(); - }, false); - - $("#execute-button") - .click( - function() { - if(!isEnabled()) - return; - - if ((publicLink == null) - && ($("#input-textarea").val() == "")) { - alert("You must upload a file or write some text in the text area before submit a request."); - return; - } - - disableExecuteButton(); - - showProgress(); - // if some text has been written in the text area, then - // a corresponding text file - // must be written in the workspace and the public link - // is set. - if ($("#input-textarea").val().length > 0) { - - freeText = $("#input-textarea").val(); - console.log("/nlphub/nlphub-uploader-servlet?freetext=" - + encodeURIComponent(freeText)); - $ - .ajax({ - url : "/nlphub/nlphub-uploader-servlet?freetext=" - + encodeURIComponent(freeText), - type : "POST", - async : true, - contentType : "text/plain; charset=utf-8", - success : function(data, stato) { - publicLink = data.message; - if (publicLink == null) { - alert("Error uploading file."); - } - launchAlgorithm(); - }, - error : function(richiesta, stato, - errori) { - alert("Unexpected Error. Status: " - + richiesta.status); - hideProgress(); - textAreaEnable(true); - $("#file-info").empty(); - } - }); - } - - else { - launchAlgorithm(); - } - }); -} /* * show the in-progress popup */ showProgress = function() { -// var width = $(".progress-circular-div").width(); -// var height = $(".progress-circular-div").height(); -// var left = parseInt((window.innerWidth - width) / 2); -// var top = parseInt((window.innerHeight - height) / 2); -// $(".progress-circular-div").css("left", left); -// $(".progress-circular-div").css("top", top); $(".hidden-div").css("display", "block"); $(".hidden-div").show(); } @@ -460,7 +513,7 @@ hideProgress = function() { /* * run the algorithms selected by the chosen language */ -launchAlgorithm = function() { +runAlgorithm = function() { if (publicLink == null) { alert("No input text available for the service."); return; @@ -519,13 +572,11 @@ launchAlgorithm = function() { hideProgress(); alert("Unexpected response"); } - //resetExecuteButton(); }, error : function(richiesta, stato, errori) { hideProgress(); textAreaEnable(true); alert("Unexpected Error. Status: " + richiesta.status); - //resetExecuteButton(); } }); } @@ -683,26 +734,12 @@ buildAlgortihmList = function() { } - //buildCheckedAlgs(); - $("#algorithm-params-div :checkbox").change(function () { //buildCheckedAlgs(); rewriteText($("#colored-annotations :checked")[0].value); }); } -/* - * build the internal global variable checkedAlgs used to filter results - */ -//buildCheckedAlgs = function() { -// checkedAlgs = []; -// for (var i = 0; i < jsonContent.output.result.length; i++) { -// if($("#algorithm" + i).is(":checked")) { -// checkedAlgs.push($("#algorithm" + i).attr("named")); -// } -// } -//} - /* * Utility function */