Changeset 31542


Ignore:
Timestamp:
2017-03-27T21:39:33+13:00 (7 years ago)
Author:
ak19
Message:
  1. Rewriting callMetadataServer to do the ajax requests as POST operations rather than the default GET. This rewrite presently uses gsajaxapi.js functions now, particularly ones recently ported from the GS2 version of the file. This was the quickest way, with a minimum number of changes, to get the existing code to POST the requests. In a subsequent commit will do a jQuery ajax call to POST, which requires more code changes. 2. The gsajaxapi.js file's own modifications in this commit are merely indentation based. 3. usercomments.xsl modified to additionally import gsajaxapi.js script.
Location:
main/trunk/greenstone3/web/interfaces/default
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/gsajaxapi.js

    r31527 r31542  
    167167// http://www.w3schools.com/ajax/ajax_xmlhttprequest_send.asp
    168168    this.urlPostSync = function(scriptURL, params) {
    169     var xmlHttp=false;
    170        try {
    171          // Firefox, Opera 8.0+, Safari
    172          xmlHttp=new XMLHttpRequest();
    173        }
    174        catch (e) {
    175          // Internet Explorer
    176          try {
    177            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    178          }
    179          catch (e) {
    180            try {
    181              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    182            }
    183            catch (e) {
    184              alert("Your browser does not support AJAX!");
    185              return false;
    186            }
    187          }
    188        }
    189 
    190     // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
    191     xmlHttp.open('POST', scriptURL, false); // false means synchronous
    192     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    193    
    194     if(un_ != null) {
    195     params += "&un=" + un_;
    196     }
    197     if(ky_ != null) {
    198     params += "&ky=" + ky_;
    199     }
    200 
    201     xmlHttp.send(params); // needs to be escaped/encoded
    202 
    203     //alert(scriptURL + "?" + params);
    204     //alert(xmlHttp.responseText); // if synchronous, process xmlHttp.responseText AFTER send() call
    205     return xmlHttp.responseText;
    206 }
     169    var xmlHttp=false;
     170    try {
     171            // Firefox, Opera 8.0+, Safari
     172            xmlHttp=new XMLHttpRequest();
     173    }
     174    catch (e) {
     175            // Internet Explorer
     176            try {
     177        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     178            }
     179            catch (e) {
     180        try {
     181            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     182        }
     183        catch (e) {
     184            alert("Your browser does not support AJAX!");
     185            return false;
     186        }
     187            }
     188    }
     189
     190    // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
     191    xmlHttp.open('POST', scriptURL, false); // false means synchronous
     192    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     193   
     194    if(un_ != null) {
     195        params += "&un=" + un_;
     196    }
     197    if(ky_ != null) {
     198        params += "&ky=" + ky_;
     199    }
     200   
     201    xmlHttp.send(params); // needs to be escaped/encoded
     202   
     203    //alert(scriptURL + "?" + params);
     204    //alert(xmlHttp.responseText); // if synchronous, process xmlHttp.responseText AFTER send() call
     205    return xmlHttp.responseText;
     206    }
    207207
    208208    // New, an Ajax Asynchronous Post method.
    209209    // For helpful links, see the urlPostSync() method above
    210210    this.urlPostAsync = function(scriptURL, params, callback) {
    211     var xmlHttp=false;
    212        try {
    213          // Firefox, Opera 8.0+, Safari
    214          xmlHttp=new XMLHttpRequest();
    215        }
    216        catch (e) {
    217          // Internet Explorer
    218          try {
    219            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    220          }
    221          catch (e) {
    222            try {
    223              xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    224            }
    225            catch (e) {
    226              alert("Your browser does not support AJAX!");
    227              return false;
    228            }
    229          }
    230        }
    231 
    232 
    233 
    234     // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
    235     xmlHttp.open('POST', scriptURL, true); // true means asynchronous
    236     xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    237 
    238 
    239     // If asynchronous:
    240     // If the callback param is a function, we will set it up to get called when
    241     // the async post has finished (is ready)
    242     // if the callback parameter isn't a function, the param represents a field
    243     // that we want to dynamically update when the async post process has finished
    244 
     211    var xmlHttp=false;
     212    try {
     213            // Firefox, Opera 8.0+, Safari
     214            xmlHttp=new XMLHttpRequest();
     215    }
     216    catch (e) {
     217            // Internet Explorer
     218            try {
     219        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
     220            }
     221            catch (e) {
     222        try {
     223            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
     224        }
     225        catch (e) {
     226            alert("Your browser does not support AJAX!");
     227            return false;
     228        }
     229            }
     230    }
     231   
     232   
     233   
     234    // e.g. scriptURL: /greenstone/cgi-bin/metadata-server.pl
     235    xmlHttp.open('POST', scriptURL, true); // true means asynchronous
     236    xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
     237   
     238   
     239    // If asynchronous:
     240    // If the callback param is a function, we will set it up to get called when
     241    // the async post has finished (is ready)
     242    // if the callback parameter isn't a function, the param represents a field
     243    // that we want to dynamically update when the async post process has finished
     244   
    245245    var typeof_callback = typeof(callback);
    246246    if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) {
    247247        var locid = callback;
    248 
     248       
    249249        xmlHttp.onreadystatechange=function() {
    250250        if(xmlHttp.readyState==4) {
     
    267267        alert("Unrecognized type of callback value: " + typeof_callback);
    268268    }
    269    
    270     if(un_ != null) {
    271     params += "&un=" + un_;
    272     }
    273     if(ky_ != null) {
    274     params += "&ky=" + ky_;
    275     }
    276     //alert("Posting Async: " + scriptURL + "?" + params);
    277 
    278     xmlHttp.send(params); // needs to be escaped/encoded
    279     // if synchronous, would process xmlHttp AFTER send() call, such as by
    280     // accessing xmlHttp.responseText to return that to the caller at this point.
    281 }
     269   
     270    if(un_ != null) {
     271        params += "&un=" + un_;
     272    }
     273    if(ky_ != null) {
     274        params += "&ky=" + ky_;
     275    }
     276    //alert("Posting Async: " + scriptURL + "?" + params);
     277   
     278    xmlHttp.send(params); // needs to be escaped/encoded
     279    // if synchronous, would process xmlHttp AFTER send() call, such as by
     280    // accessing xmlHttp.responseText to return that to the caller at this point.
     281    }
    282282
    283283    // New
  • main/trunk/greenstone3/web/interfaces/default/js/javascript-global-functions.js

    r31537 r31542  
    424424// No function overloading in JavaScript. Can pass a custom object, however, see
    425425// http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices
    426 function callMetadataServer(callingFunction, url, responseFunction, opts)
     426function callMetadataServerGET(callingFunction, url, responseFunction, opts)
    427427{
    428428    var async_setting = true; // Internal processing of 'read' operations (get meta) is not order dependent
     
    457457    .success(function(response)
    458458    {
    459         console.log("(" + callingFunction + ") Response received from server: " + response);
     459        console.log("(" + callingFunction + ") Response received from server: " + ajaxResponse);
    460460
    461461        ajaxResponse = response;
     
    480480    return ajaxResponse;
    481481}
     482
     483
     484// No function overloading in JavaScript. Can pass a custom object, however, see
     485// http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices
     486function callMetadataServer(callingFunction, url, responseFunction, opts)
     487{
     488    var async_setting = true; // Internal processing of 'read' operations (get meta) is not order dependent
     489
     490    // If doing set- or remove- (not get-) metadata, then rewrite URLs to call GS2Construct's ModfiyMetadata service instead (which will ensure this only works when authenticated).
     491    // From:
     492    // <gs3server>/cgi-bin/metadata-server.pl?a=set-archives-metadata&c=smallcol&site=localsite&d=HASH01454f31011f6b6b26eaf8d7&metaname=Title&metavalue=Moo&prevmetavalue=Blabla&metamode=override
     493    // To:
     494    // <gs3server>/library?a=g&rt=r&ro=1&s=ModifyMetadata&s1.a=set-archives-metadata&s1.collection=smallcol&s1.site=localsite&s1.d=HASH01454f31011f6b6b26eaf8d7&s1.metaname=Title&s1.metavalue=Moo&s1.prevmetavalue=Blabla&s1.metamode=override
     495
     496    // if we're doing a set- or remove- metadata operations, then we'll be changing the URL to make sure we go through GS3's authentication
     497    if(url.indexOf("set-") != -1 || url.indexOf("remove-") != -1) {
     498   
     499    url = url.replace("&c=",  "&collection="); // c is a special param name for GS2Construct
     500    url = url.replace(/(&|\?)([^=]*=)/g, "$1"+"s1.$2"); // prefix param names with "s1."
     501    url = url.replace("cgi-bin/metadata-server.pl?",  gs.xsltParams.library_name + "?a=g&rt=r&ro=1&s=ModifyMetadata&");
     502   
     503    //console.log("@@@@@ URL is " + url);
     504
     505    async_setting = false; // for 'write' operations (set/remove meta), we force sequential processing of the internal operation.
     506
     507    } // otherwise, such as for get- metadata operation, we proceed as before, which will not require authentication
     508
     509    if (opts != null) {
     510    if(opts["forceSync"] != null) {
     511        async_setting = (!opts["forceSync"]);
     512    }
     513    }
     514
     515    console.log("Away to call: " + url);
     516    var ajaxResponse = "No response received yet, async ajax request";
     517
     518    var splitURL = url.split("?");
     519    url = splitURL[0];
     520    var params = splitURL[1];
     521    var gsapi = new GSAjaxAPI(url);
     522
     523    // ajax calls default to using method GET, we want to do POST operations for get-meta and set-meta requests
     524    // since get-meta-array and especially set-meta-array can be large, e.g. for user comments.
     525
     526    if(async_setting) {
     527    gsapi.urlPostAsync(url, params, responseFunction);
     528    } else {
     529    ajaxResponse = gsapi.urlPostSync(url, params); 
     530    }
     531
     532    console.log("(" + callingFunction + ") Response received from server: " + ajaxResponse);
     533
     534    console.log("Finished ajax call to: " + url);
     535   
     536    console.log("Got response: " + ajaxResponse);
     537    return ajaxResponse;
     538}
     539
    482540
    483541/*************************
  • main/trunk/greenstone3/web/interfaces/default/transform/layouts/usercomments.xsl

    r31540 r31542  
    2121
    2222  <!-- 2. Load the javascript, which will do stuff on window load/ready for which it needs the above gs.variables -->
     23  <script type="text/javascript" src="interfaces/{$interface_name}/js/gsajaxapi.js"><xsl:text> </xsl:text></script>
    2324  <script type="text/javascript" src="interfaces/{$interface_name}/js/user_comments.js"><xsl:text> </xsl:text></script>
    2425
Note: See TracChangeset for help on using the changeset viewer.