Changeset 17


Ignore:
Timestamp:
03/17/09 10:49:55 (16 years ago)
Author:
lgiessmann
Message:

changed the hunchentoot version from 0.15.7 to 1.0.0 and all depending code-fragments using the hunchentoot interface; further the json interface was modified and better structured; the little example of prototype was also modiefied, so you can get all psis of isidorus, you can get all fragments and you can commit fragments+ssh://lgiessmann@common-lisp.net/project/isidorus/svn

Location:
trunk/src
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/src/isidorus.asd

    r15 r17  
    122122                                     (:static-file "json_interface.html"))
    123123                        :depends-on ("model" "xml"))
    124                (:module "threading"
    125                         :components ((:file "reader-writer"))))
     124               )
     125               ;;(:module "threading"
     126               ;;       :components ((:file "reader-writer"))))
    126127  :depends-on (:cxml
    127128               :drakma
  • TabularUnified trunk/src/json/json_interface.html

    r15 r17  
    1 <html>
     1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2<html xmlns="http://www.w3.org/1999/xhtml">
    23  <head>
    3     <title>isidorus</title>
    4     <script type="text/javascript">
    5      // --- here we can handle timeouts of the passed XMLHttpRequest-objects
    6      // --- this function has to be set and cleared in every XMLHttpRequest-object
    7      function ajaxTimeout(xhr){
    8          xhr.abort();
    9          alert("The AJAX request timed out. Did you lose network connectivity for some reason?");
    10      }
     4    <title>isidorus with protype and scriptaclous</title>
     5    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
     6    <link rel="stylesheet" type="text/css" href="main.css"/>
    117
    12      // --- the timeout interval in seconds
     8    <!-- includes the prototype framework -->
     9    <script language="JavaScript" type="text/javascript" src="prototype-1.6.0.3.js"></script>
     10
     11    <!-- own javascript code -->
     12    <script language="JavaScript" type="text/javascript">
     13     // --- some constants -----------------------------------------------------
    1314     const TIMEOUT = 5000;
    14      // --- the XMLHttpRequest base url
    15      const BASE_URL = "http://localhost:8000/json/psi/";
     15     const GET_PREFIX = "http://localhost:8000/json/get/";
     16     const COMMIT_URL = "http://localhost:8000/json/commit/";
     17     const ALL_PSIS_URL = "http://localhost:8000/json/psis/";
    1618     const OWN_URL = "http://localhost:8000/isidorus";
    1719
     20     // --- some globals -------------------------------------------------------
     21     var ALL_PSIS = null;
     22     var CURRENT_URL = null;
     23     var CURRENT_FRAGMENT = null;
    1824
    19      function back()
     25
     26     // --- default error handler for all ajax objects
     27     function errorHandler(ajaxReq)
    2028     {
    21          window.location.href = OWN_URL;
     29         alert("Something went wrong ... -> " + ajaxReq.status);
    2230     }
    2331
    2432
    25      // --- creates a XMLHttpReques object
    26      function connect()
     33     // --- redirects the client to the initial url
     34     function goBack()
    2735     {
    28          try { return new XMLHttpRequest(); } catch(err){}
    29          try { return new AcitveXObject("Msxml2.XMLHTTP"); } catch(err){}
    30          try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(err){}
    31 
    32          alert("error creating request object");
    33          return null;
     36         window.location = OWN_URL;
     37         $("textArea").value = "";
    3438     }
    3539
    3640
    37      // ========================================================================
    38      // --- get request -> aks for json-data
    39      // ========================================================================
    40      var xhrGet = null;
     41     // --- ajax functions -----------------------------------------------------
     42     function commitPut()
     43     {
     44         function commitPutHandler(ajaxReq)
     45         {
     46             alert(CURRENT_URL + "\n\n" + $("textArea").value + "\n\n" + ajaxReq.status);
     47         }
    4148
    42      // --- creates a XMLHttpReques object
    43      function connectGet()
    44      {
    45          // --- firefox
    46          try{ return new XMLHttpRequest(); } catch(err){}
    4749
    48          // --- internet explorer
    49          try{ return new ActiveXObject("Msxml2.XMLHTTP"); } catch(err){}
    50          try{ return new ActiveXObject("Microsoft.XMLHTTP"); } catch(err){}
    51 
    52          alert("error creating request object");
    53          return null;
     50         new Ajax.Request(COMMIT_URL,
     51                          {
     52                              method: "post",
     53                                  requestHeaders:{ "Content-Type":"application/json"},
     54                                  onSuccess: commitPutHandler,
     55                                  onFailure: errorHandler,
     56                                  postBody: $("textArea").value
     57                          });
    5458     }
    5559
    5660
    57      // --- handles the json response
    58      function handleJson()
     61     // --- hides the table with all psis and sends a request to the server for a specific topic-psi
     62     function selectTopic(topicIdx, psiIdx)
    5963     {
    60          if(xhrGet.readyState == 4){ // state 4 --> response is complete
    61              if(xhrGet.status != 200){
    62                  alert("error: " + xhrGet.status);
    63                  return false;
     64         if(ALL_PSIS != null && ALL_PSIS.length != 0){
     65             // --- handler for the ajax request aobject - if the request was successful
     66             function getFragment(ajaxReq)
     67             {
     68                 var response = ajaxReq.responseText;
     69                 try{
     70                     var jsonObj = CURRENT_FRAGMENT = response.evalJSON();
     71                     $("textArea").value = response;
     72                 }catch(err){
     73                     alert("got bad JSON data: " + err);
     74                 }
    6475             }
    6576             
    66              // --- resets the timeout
    67              clearTimeout(xhrGet.timeout);
    68 
    69              // --- handle the data
    70              var json = eval("(" + xhrGet.responseText + ")");
    71              var psis = json.topic.subjectIdentifiers;
    72              document.getElementById("psis").innerHTML = "";
    73              for each(var psi in psis)
    74                          document.getElementById("psis").innerHTML += "psi: " + psi + '<br/>';
    75              
    76              document.getElementById("real_text").value = xhrGet.responseText;
    77              //alert("header: " + xhrGet.getAllResponseHeaders());
    78          }
    79          else{
    80              return false;
     77             var url = CURRENT_URL = GET_PREFIX + ALL_PSIS[topicIdx][psiIdx].gsub("#", "%23");
     78             $("allPsisTable").remove();
     79             $("allPsisButton").insert({after: '<input id="commitPutButton" style="margin-top:10px; margin-bottom:10px;" type="button" onclick="commitPut()" value="commit fragment"/>'});
     80             // --- ajax request
     81             new Ajax.Request(url,
     82                              {
     83                                  method:'get',
     84                                      onSuccess: getFragment,
     85                                      onFailure: errorHandler
     86                              });
    8187         }
    8288     }
    8389
    8490
    85      // --- sends a request for the json data
    86      function getData(xhr)
     91     // --- gets all topic psis are currently contained in isidorus
     92     function getAllPsis()
    8793     {
    88          var topic_psi = document.getElementById("topic_psi").value;
    89          var url = BASE_URL + topic_psi;
     94         // --- handler for the ajax request aobject - if the request was successful
     95         function getAllPsisSuccessHandler(ajaxReq)
     96         {
     97             var btn= $("commitPutButton");
     98             if(btn)btn.remove();
    9099
    91          // --- sets the timeout for this XMLHttpRequest object; 5 seconds
    92          xhrGet.timeout = setTimeout("ajaxTimeout(xhrGet);", TIMEOUT);
     100             var response = ajaxReq.responseText;
     101             try{
     102                 var jsonObj = ALL_PSIS = response.evalJSON();
     103                 var htmlStr = '<table id="allPsisTable"><tr><th>topic #</th><th>psis</th></tr>';
     104                 
     105                 jsonObj.each(function(topic, idx)
     106                              {
     107                                  htmlStr += '<tr><td rowspan="' + topic.length + '">' + idx + '</td>';
     108                                  topic.each(function(psi, innerIdx)
     109                                             {
     110                                                 if(innerIdx != 0)htmlStr += "<tr>";
     111                                                 htmlStr += '<td><input id="psiRow" type="radio" name="psi" onclick="selectTopic(' + idx + ',' + innerIdx +')">' + psi + '</input></td>';
     112                                                 htmlStr += "</tr>";
     113                                             });
     114                                  if(topic.length != 1)htmlStr += "</tr>";
     115                              });
     116                 htmlStr += "</table>";
     117                 $("allPsisButton").insert({after: htmlStr});
     118             }catch(err){
     119                 alert("got bad JSON data: " + err);
     120             }
     121         }
    93122
    94          try{
    95          xhrGet.open("GET", url, true); // true --> asynchronous call, so the user is able to continue working on other things
    96          }catch(err) {alert("err: " + err); }
    97 
    98          // --- registers a callback handler for the readystatechange event of the XMLHttpRequest/ActiveXObject-Object
    99          xhrGet.onreadystatechange = handleJson;
    100          xhrGet.send(null);
     123         // --- ajax request
     124         new Ajax.Request(ALL_PSIS_URL,
     125                          {
     126                              method:'get',
     127                                  onSuccess: getAllPsisSuccessHandler,
     128                                  onFailure: errorHandler
     129                          });
    101130     }
    102 
    103 
    104      // --- calls all necessary functions to get a fragment belonging to the
    105      // --- psi of the topic_psi text field
    106      function doIt()
    107      {
    108          xhrGet = connectGet();
    109 
    110          if(xhrGet != null)
    111              getData(xhrGet);
    112      }
    113 
    114      // ========================================================================
    115      // --- put request -> commit json-data
    116      // ========================================================================
    117      var xhrPut = null;
    118 
    119      // --- commits the textarea's json data to the server
    120      function commitJson()
    121      {
    122          xhrPut = connect();
    123 
    124          if(xhrPut != null)
    125              sendData(xhrPut);
    126      }
    127 
    128 
    129      // --- handles the committing of json data
    130      function handleCommit()
    131      {
    132          alert("readyState: " + xhrPut.readyState + "\nstatus: " + xhrPut.status + "\nresponsetext: " + xhrPut.responseText);
    133          if(xhrPut.readyState == 4){ // state 4 --> response is complete
    134              //if(xhrPut.status == 200){
    135              // alert("error: " + xhrPut.status);
    136              // return false;
    137              //}
    138 
    139              // --- resets the timeout
    140              clearTimeout(xhrPut.timeout);
    141              alert("data commited successfully");
    142              //doIt();
    143          }
    144          else{
    145              return false;
    146          }
    147      }
    148 
    149 
    150      // --- sends the json data to the server
    151      function sendData(xhr)
    152      {
    153          var json =  document.getElementById("real_text").value;
    154          var topicPsi = document.getElementById("topic_psi").value;
    155          var url = BASE_URL + topicPsi;
    156          xhrPut.open("PUT", url, true);
    157 
    158          // --- sets the timeout for this XMLHttpRequest object; 5 seconds
    159          xhrPut.timeout = setTimeout("ajaxTimeout(xhrPut);", TIMEOUT);
    160 
    161          // --- registers a callback handler for the readystatechange event of the XMLHttpRequest/ActiveXObject-Object
    162          xhrPut.onreadystatechange = handleCommit;
    163          xhrPut.setRequestHeader("Content-type", "application/json");
    164          xhrPut.send(json);
    165      }
    166 
    167 
    168      // ========================================================================
    169      // --- post request -> commit json-data
    170      // ========================================================================
    171      var xhrPost = null;
    172 
    173 
    174      function commitJsonPost()
    175      {
    176          xhrPost = connect();
    177 
    178          if(xhrPost != null)
    179              sendDataPost(xhrPost);
    180      }
    181 
    182 
    183      function handlePostCommit()
    184      {
    185          alert("readyState: " + xhrPost.readyState + "\nstatus: " + xhrPost.status + "\nresponsetext: " + xhrPost.responseText);
    186          if(xhrPost.readyState == 4){ // state 4 --> response is complete
    187              //if(xhrPut.status == 200){
    188              // alert("error: " + xhrPut.status);
    189              // return false;
    190              //}
    191 
    192              // --- resets the timeout
    193              clearTimeout(xhrPost.timeout);
    194              alert("data commited successfully");
    195              //doIt();
    196          }
    197          else{
    198              return false;
    199          }
    200      }
    201 
    202 
    203      function sendDataPost(xhr)
    204      {
    205          var json =  document.getElementById("real_text").value;
    206          var topicPsi = document.getElementById("topic_psi").value;
    207          var url = BASE_URL + topicPsi;
    208          xhrPost.open("POST", url, true);
    209 
    210          // --- sets the timeout for this XMLHttpRequest object; 5 seconds
    211          xhrPost.timeout = setTimeout("ajaxTimeout(xhrPost);", TIMEOUT);
    212 
    213          // --- registers a callback handler for the readystatechange event of the XMLHttpRequest/ActiveXObject-Object
    214          xhrPost.onreadystatechange = handlePostCommit;
    215      }
    216 
    217 
    218131    </script>
    219132  </head>
     133
    220134  <body>
    221     <div id="content" style="width: 80%; height: 80%; border: dashed 1px;">
    222       <input id="topic_psi" type="text" value="http://psi.egovpt.org/types/topicInTaxonomy" name="topic_psi" style="margin-left:10px; margin-top:10px;"/>
    223       <input type="button" onclick="doIt();" value="get json" style="margin-top:10px;"/>
    224       <div id="psis" style="background-color: silver; width: 70%; margin: 10px;"></div>
    225       <textarea id ="real_text" name="text" cols="120" rows="10" style="margin: 10px;"></textarea><br/>
    226       <input type="button" onclick="commitJson()" value="commit json via PUT" style="margin-left: 10px;"/>
    227       <input type="button" onclick="commitJsonPost()" value="commit json via POST" style="margin-left: 10px; margin-right: 10px;"/>
    228       <input type="button" onclick="back()" value="back"/>
     135    <div id="content" style="margin-top:20px; margin-left:auto; margin-right:auto; border:dashed; width:60%;">
     136      <input id="allPsisButton" style="margin:10px;" type="button" onclick="getAllPsis()" value="get all psis"/><br/>
     137      <textarea id="textArea" style="margin-left:10px; margin-bottom:10px;" cols="100" rows="10"></textarea><br/>
     138      <input id="backButton" style="margin-left:10px; margin-bottom:10px;" type="button" onclick="goBack()" value="clear"/><br/>
    229139    </div>
    230140  </body>
  • TabularUnified trunk/src/rest_interface/publish_feeds.lisp

    r2 r17  
    55
    66(defmacro as-feed (feed)
    7   `(setf (content-type) "application/atom+xml; charset=UTF-8")
     7  `(setf (hunchentoot:content-type*) "application/atom+xml; charset=UTF-8")
    88  `(cxml:with-xml-output (cxml:make-string-sink :canonical t)
    99     (atom:feed-to-elem ,feed)))
     
    1111(defun overview-feed ()
    1212  "Interface function to the corresponding Atom method"
    13   (setf (content-type) "application/atom+xml; charset=UTF-8")
     13  (setf (hunchentoot:content-type*) "application/atom+xml; charset=UTF-8")
    1414  (as-feed atom:*tm-feed*))
    1515
     
    1717(defmethod publish-feed ((feed atom:feed))
    1818  (push
    19    (create-regex-dispatcher 
     19   (create-regex-dispatcher
    2020    (format nil "~a~a" (path feed) "/?$") #'overview-feed)
    2121   hunchentoot:*dispatch-table*)
     
    2727    (format nil "~a~a" (path feed) "/?$")
    2828    (lambda ()
    29  (setf (content-type) "application/atom+xml; charset=UTF-8")
     29      (setf (hunchentoot:content-type*) "application/atom+xml; charset=UTF-8")
    3030      (as-feed feed)))
    3131   hunchentoot:*dispatch-table*)
     
    3737    (format nil "~a~a" (path feed) "/?$")
    3838    (lambda ()
    39  (setf (content-type) "application/atom+xml; charset=UTF-8")
     39      (setf (hunchentoot:content-type*) "application/atom+xml; charset=UTF-8")
    4040      (as-feed feed)))
    4141   hunchentoot:*dispatch-table*)
     
    4545    (format nil "~a~a" (path feed) "/([0-9]+)$")
    4646    (lambda (&optional unique-id)
    47       (setf (content-type) "application/x-tm+xml;version=1.0; charset=utf-8")
     47      (setf (hunchentoot:content-type*) "application/x-tm+xml;version=1.0; charset=utf-8")
    4848      (let
    4949          ((fragment
     
    5959    (format nil "~a~a" (path feed) "/?$")
    6060    (lambda ()
    61  (setf (content-type) "application/atom+xml; charset=UTF-8")
     61      (setf (hunchentoot:content-type*) "application/atom+xml; charset=UTF-8")
    6262      (as-feed feed)))
    6363   hunchentoot:*dispatch-table*)
     
    6767    (format nil "~a~a" (path feed) "/([0-9]+)$")
    6868    (lambda (&optional revision)
    69       (setf (content-type) "application/x-tm+xml;version=1.0; charset=utf-8")
     69      (setf (hunchentoot:content-type*) "application/x-tm+xml;version=1.0; charset=utf-8")
    7070      (exporter:export-xtm-to-string
    7171       :revision (parse-integer revision)
  • TabularUnified trunk/src/rest_interface/rest-interface.lisp

    r15 r17  
    1717           :start-tm-engine
    1818           :shutdown-tm-engine
    19            :*json-rest-prefix*
     19           :*json-get-prefix*
     20           :*json-commit-url*
     21           :*json-get-all-psis*
    2022           :*json-user-interface-url*
    2123           :*json-user-interface-file-path*))
     
    7173
    7274
    73 ;;(defun make-json (&optional uri)
    74 ;;  "returns a json-string of the topic with the passed psi-uri"
    75 ;;  (assert uri)
    76 ;;  ;decodes the url-encoding "%23" to "#" character (only the first which will be found)
    77 ;;  (let ((identifier (let ((pos (search "%23" uri)))
    78 ;;                    (if pos
    79 ;;                        (let ((str-1 (subseq uri 0 pos))
    80 ;;                              (str-2 (if (> (length uri) (+ pos 3))
    81 ;;                                         (subseq uri (+ pos 3))
    82 ;;                                         "")))
    83 ;;                          (concatenate 'string str-1 "#" str-2))
    84 ;;                        uri)))
    85 ;;      (http-method (request-method))
    86 ;;      (external-format (flexi-streams:make-external-format :UTF-8 :eol-style :LF))) ;;is needed to get a string of the put-request
    87 ;;    (if (eq http-method :GET)
    88 ;;      (progn
    89 ;;        (setf (hunchentoot:content-type) "application/json")
    90 ;;        (let ((fragment
    91 ;;               (get-latest-fragment-of-topic identifier)))
    92 ;;          (if fragment
    93 ;;              (handler-case (to-json-string fragment)
    94 ;;                (condition (err) (format nil "{\"fault\":\"~a\"}" err)))
    95 ;;              "{}")))
    96 ;;      (if (eq http-method :PUT)
    97 ;;          (let ((put-data (raw-post-data :external-format external-format :force-text t)))         
    98 ;;            (handler-case (json-importer:json-to-elem put-data)
    99 ;;              (condition () (setf (return-code) +http-internal-server-error+))))   
    100 ;;          (setf (return-code) +http-internal-server-error+))))) ; for all htt-methods except for get and post
    101 
    102 
    10375;; (push
    10476;;  (create-regex-dispatcher "/feeds/?$" #'feeds)
     
    12597;;  hunchentoot:*dispatch-table*)
    12698
    127 ;;(push
    128 ;; (create-regex-dispatcher "/json/psi/(.+)$" #'make-json)
    129 ;; hunchentoot:*dispatch-table*)
    130    
    13199
    132 (defvar *server*)
     100
     101(defvar *acceptor*)
    133102
    134103(defun start-tm-engine (repository-path &key (conffile "atom/conf.lisp") (host-name "localhost") (port 8000))
    135104  "Start the Topic Map Engine on a given port, assuming a given
    136105hostname. Use the repository under repository-path"
    137   (setf hunchentoot:*show-lisp-errors-p* t) ;for now 
    138   (setf hunchentoot:*show-lisp-backtraces-p* t)
     106  (setf hunchentoot:*show-lisp-errors-p* t) ;for now
     107  ;(setf hunchentoot:*show-lisp-backtraces-p* t) ;hunchentoot 0.15.7
    139108  (setf hunchentoot:*hunchentoot-default-external-format*
    140109        (flex:make-external-format :utf-8 :eol-style :lf))
     
    145114  (publish-feed atom:*tm-feed*)
    146115  (set-up-json-interface)
    147   (setf *server* (hunchentoot:start-server :address host-name :port port)))
     116  (setf *acceptor* (make-instance 'hunchentoot:acceptor :address host-name :port port))
     117  (setf hunchentoot:*lisp-errors-log-level* :info)
     118  (setf hunchentoot:*message-log-pathname* "./hunchentoot-errors.log")
     119  (hunchentoot:start *acceptor*))
    148120
    149121(defun shutdown-tm-engine ()
    150122  "Shut down the Topic Map Engine"
    151   (hunchentoot:stop-server *server*)
     123  (hunchentoot:stop *acceptor*)
    152124  (elephant:close-store))
  • TabularUnified trunk/src/rest_interface/set-up-json-interface.lisp

    r16 r17  
    11(in-package :rest-interface)
    22
    3 (defparameter *json-rest-prefix* "/json/psi") ;the prefix to get a fragment by the psis -> localhost:8000/json/psi/<fragment-psi>
    4 (defparameter *json-rest-all-psis* "/json/psis") ;the url to get all topic psis of isidorus -> localhost:8000/json/psis
    5 (defparameter *json-user-interface-url* "/isidorus") ;the url to the user interface -> localhost:8000/isidorus
     3(defparameter *json-get-prefix* "/json/get/(.+)$") ;the prefix to get a fragment by the psis -> localhost:8000/json/get/<fragment-psi>
     4(defparameter *json-commit-url* "/json/commit/?$") ;the url to commit a json fragment by "put" or "post"
     5(defparameter *json-get-all-psis* "/json/psis/?$") ;the url to get all topic psis of isidorus -> localhost:8000/json/psis
     6(defparameter *json-user-interface-url* "/isidorus/?$") ;the url to the user interface -> localhost:8000/isidorus
    67(defparameter *json-user-interface-file-path* "json/json_interface.html") ;the file path to the HTML file implements the user interface
    78
    89
    9 (defun set-up-json-interface (&key (rest-prefix *json-rest-prefix*) (rest-all-psis *json-rest-all-psis*)
    10                               (ui-url *json-user-interface-url*) (ui-file-path *json-user-interface-file-path*))
     10(defun set-up-json-interface (&key (json-get-prefix *json-get-prefix*) (json-get-all-psis *json-get-all-psis*)
     11                              (json-commit-url *json-commit-url*) (json-user-interface-url *json-user-interface-url*)
     12                              (json-user-interface-file-path *json-user-interface-file-path*))
    1113  "registers the json im/exporter to the passed base-url in hunchentoot's dispatch-table
    1214   and also registers a file-hanlder to the html-user-interface"
    13   (declare (string rest-prefix ui-url ui-file-path))
    14   (let ((rest-regex (concatenate 'string rest-prefix "/(.+)$"))
    15         (ui-regex (concatenate 'string ui-url "/?$"))
    16         (all-psis-regex (concatenate 'string rest-all-psis "/?$")))
    17     ;(format t "rest-interface: ~a~%user-interface: ~a~%user-interface-file-path: ~a~%" rest-regex ui-regex ui-file-path)
    18     (push
    19      (create-regex-dispatcher ui-regex #'(lambda()
    20                                          (hunchentoot:handle-static-file ui-file-path)))
    21      hunchentoot:*dispatch-table*)
    22     (push
    23      (create-regex-dispatcher all-psis-regex #'(lambda()
    24                                                  (setf (hunchentoot:content-type) "application/json") ;RFC 4627
    25                                                  (get-all-topic-psis)))
    26      hunchentoot:*dispatch-table*)
    27     (push
    28      (create-regex-dispatcher rest-regex
    29                               #'(lambda (&optional uri)
    30                                   (assert uri)
    31                                         ;decodes the url-encoding "%23" to "#" character (only the first which will be found)
    32                                   (let ((identifier (let ((pos (search "%23" uri)))
    33                                                       (if pos
    34                                                           (let ((str-1 (subseq uri 0 pos))
    35                                                                 (str-2 (if (> (length uri) (+ pos 3))
    36                                                                            (subseq uri (+ pos 3))
    37                                                                            "")))
    38                                                             (concatenate 'string str-1 "#" str-2))
    39                                                           uri)))
    40                                         (http-method (request-method))
    41                                         (external-format (flexi-streams:make-external-format :UTF-8 :eol-style :LF))) ;is needed to get a string of the put-request
    42                                     (cond
    43                                       ((eq http-method :GET)
    44                                        (progn
    45                                          (setf (hunchentoot:content-type) "application/json") ;RFC 4627
    46                                          (let ((fragment
    47                                                 (get-latest-fragment-of-topic identifier)))
    48                                            (if fragment
    49                                                (handler-case (to-json-string fragment)
    50                                                  (condition (err) (progn
    51                                                                     (setf (hunchentoot:return-code) hunchentoot:+http-internal-server-error+)
    52                                                                     (format nil "<p style=\"color:red\">Condition: \"~a\"</p>" err))))
    53                                                "{}"))))
    54                                       ((eq http-method :PUT)
    55                                        (let ((put-data (hunchentoot:raw-post-data :external-format external-format :force-text t)))           
    56                                          (handler-case (progn
    57                                                          (json-importer:json-to-elem put-data)
    58                                                          (setf (hunchentoot:return-code) hunchentoot:+http-ok+)
    59                                                          (setf (hunchentoot:content-type) "text")
    60                                                          (format nil "~a" hunchentoot:+http-ok+))
    61                                            (condition (err) (progn
    62                                                               (setf (hunchentoot:return-code) hunchentoot:+http-internal-server-error+)
    63                                                               (format nil "<p style=\"color:red\">Condition: \"~a\"</p>" err))))))
    64                                       ))))
    65 ;;                                    ((eq http-method :POST)
    66 ;;                                     (let ((post-data (hunchentoot:raw-post-data :external-format external-format :force-text t)))
    67 ;;                                       (handler-case (progn
    68 ;;                                                       (json-importer:json-to-elem post-data)
    69 ;;                                                       (setf (hunchentoot:return-code) hunchentoot:+http-ok+)
    70 ;;                                                       (setf (hunchentoot:content-type) "text")
    71 ;;                                                       (format nil "~a" hunchentoot:+http-ok+))
    72 ;;                                         (condition (err) (progn
    73 ;;                                                            (setf (hunchentoot:return-code) hunchentoot:+http-internal-server-error+)
    74 ;;                                                            (format nil "<p style=\"color:red\">Condition: \"~a\"</p>" err))))))
    75 ;;                                    (t
    76 ;;                                     (progn ;for all htt-methods except for get and post
    77 ;;                                       (setf (hunchentoot:return-code) hunchentoot:+http-internal-server-error+)
    78 ;;                                       (format nil "<p style=\"color:red\">You have to use either the HTTP-Method \"GET\" or \"PUT\", but you used \"~a\"</p>" http-method)))))))
    79      hunchentoot:*dispatch-table*)))
     15  (declare (string json-get-prefix json-get-all-psis json-commit-url json-user-interface-url json-user-interface-file-path))
     16  (push ;TODO create a static-file-and-folder-handler for all static files
     17   (create-regex-dispatcher json-user-interface-url #'(lambda()
     18                                                        (hunchentoot:handle-static-file json-user-interface-file-path)))
     19   hunchentoot:*dispatch-table*)
     20  (push
     21   (create-regex-dispatcher "/prototype-1.6.0.3.js" #'(lambda()
     22                                                        (hunchentoot:handle-static-file "json/prototype-1.6.0.3.js")))
     23   hunchentoot:*dispatch-table*)
     24  (push
     25   (create-regex-dispatcher json-get-all-psis #'return-all-topic-psis)
     26   hunchentoot:*dispatch-table*)
     27  (push
     28   (create-regex-dispatcher json-get-prefix #'return-json-fragment)
     29   hunchentoot:*dispatch-table*)
     30  (push
     31   (create-regex-dispatcher json-commit-url #'json-commit)
     32   hunchentoot:*dispatch-table*))
     33
     34
     35;; =============================================================================
     36;; --- some handlers for the json-rest-interface -------------------------------
     37;; =============================================================================
     38(defun return-all-topic-psis (&optional param)
     39  "return all psis currently existing in isidorus as a list of list. every topic is a list
     40   of psis and the entire list contains a list of topics"
     41  (declare (ignorable param))
     42  (let ((http-method (hunchentoot:request-method*)))
     43    (if (eq http-method :GET)
     44        (progn
     45          (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
     46          (get-all-topic-psis))
     47        (setf (hunchentoot:return-code*) hunchentoot:+http-bad-request+))))
     48
     49
     50(defun return-json-fragment(&optional psi)
     51  "returns the json-fragmen belonging to the psi passed by the parameter psi"
     52  (assert psi)
     53  (let ((http-method (hunchentoot:request-method*)))
     54    (if (eq http-method :GET)
     55        (let ((identifier (let ((pos (search "%23" psi)))
     56                            (if pos
     57                                (let ((str-1 (subseq psi 0 pos))
     58                                      (str-2 (if (> (length psi) (+ pos 3))
     59                                                 (subseq psi (+ pos 3))
     60                                                 "")))
     61                                  (concatenate 'string str-1 "#" str-2))
     62                                psi))))
     63          (setf (hunchentoot:content-type*) "application/json") ;RFC 4627
     64          (let ((fragment
     65                 (get-latest-fragment-of-topic identifier)))
     66            (if fragment
     67                (handler-case (to-json-string fragment)
     68                  (condition (err)
     69                    (progn
     70                      (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
     71                      (format nil "<p>Condition: \"~a\"</p>" err))))
     72                "{}")))
     73        (setf (hunchentoot:return-code*) hunchentoot:+http-bad-request+))))
     74
     75
     76(defun json-commit(&optional param)
     77  "calls the json-to-elem method for a json-fragment and imports it to elephant"
     78  (declare (ignorable param)) ;param is currently not used
     79  (let ((http-method (hunchentoot:request-method*)))
     80    (if (or (eq http-method :PUT)
     81            (eq http-method :POST))
     82        (let ((external-format (flexi-streams:make-external-format :UTF-8 :eol-style :LF)))
     83          (let ((json-data (hunchentoot:raw-post-data :external-format external-format :force-text t)))
     84            (handler-case (json-importer:json-to-elem json-data)
     85              (condition (err)
     86                (progn
     87                  (setf (hunchentoot:return-code*) hunchentoot:+http-internal-server-error+)
     88                  (format nil "<p>Condition: \"~a\"</p>" err))))))
     89        (setf (hunchentoot:return-code*) hunchentoot:+http-bad-request+))))
  • TabularUnified trunk/src/threading/reader-writer.lisp

    r4 r17  
    11(defpackage :isidorus-reader-writer
    2   (:use :cl :hunchentoot-mp)
     2  (:use :cl :hunchentoot-mp) ;hunchentoot 0.15.7
    33  (:export :current-readers
    44           :with-reader-lock
     
    77(in-package :isidorus-reader-writer)
    88
    9 (defvar *readerlist-mutex* (make-lock "isidorus current-readers lock"))
    10 (defvar *writer-mutex* (make-lock "isidorus writer lock"))
     9(defvar *readerlist-mutex* (make-lock "isidorus current-readers lock")) ;hunchentoot 0.15.7
     10(defvar *writer-mutex* (make-lock "isidorus writer lock")) ;hunchentoot 0.15.7
     11;;(defvar *readerlist-mutex* (hunchentoot::make-lock "isidorus current-readers lock")) ;hunchentoot 1.0.0
     12;;(defvar *writer-mutex* (hunchentoot::make-lock "isidorus writer lock")) ;hunchentoot 1.0.0
    1113
    1214(defvar *current-readers* nil)
     
    1517  (let
    1618      ((result nil))
    17     (with-lock (*readerlist-mutex*)
     19    ;;(with-lock (*readerlist-mutex*) ;hunchentoot 0.15.7
     20    (hunchentoot::with-lock-held (*readerlist-mutex*) ;hunchentoot 1.0.0
    1821      (setf result (copy-list *current-readers*)))
    1922    result))
    2023
    2124(defun add-current-to-reader-list ()
    22   (with-lock (*writer-mutex*)
    23     (with-lock (*readerlist-mutex*)
     25  (with-lock (*writer-mutex*) ;hunchentoot 0.15.7
     26    (with-lock (*readerlist-mutex*) ;hunchentoot 0.15.7
     27  ;;(hunchentoot::with-lock-held (*writer-mutex*) ;hunchentoot 1.0.0
     28    ;;(hunchentoot::with-lock-held (*readerlist-mutex*) ;hunchentoot 1.0.0
    2429      (push *current-process* *current-readers*))))
    2530
    2631(defun remove-current-from-reader-list ()
    27   (with-lock (*readerlist-mutex*)
     32  (with-lock (*readerlist-mutex*) ;hunchentoot 0.15.7
     33  ;;(hunchentoot::with-lock-held (*readerlist-mutex*) ;hunchentoot 1.0.0
    2834    (setf *current-readers*
    2935          (delete *current-process* *current-readers*))))
     
    4248
    4349(defmacro with-writer-lock (&body body)
    44   `(with-lock (*writer-mutex*)
     50  `(with-lock (*writer-mutex*) ;hunchentoot 0.15.7
     51  ;;`(hunchentoot::with-lock-held (*writer-mutex*) ;hunchetoot 1.0.0
    4552     (do
    4653      ((remaining-readers (current-readers) (current-readers)))
Note: See TracChangeset for help on using the changeset viewer.