| 1 | ;;+----------------------------------------------------------------------------- | 
|---|
| 2 | ;;+  Isidorus | 
|---|
| 3 | ;;+  (c) 2008-2010 Marc Kuester, Christoph Ludwig, Lukas Georgieff | 
|---|
| 4 | ;;+ | 
|---|
| 5 | ;;+  Isidorus is freely distributable under the LLGPL license. | 
|---|
| 6 | ;;+  You can find a detailed description in trunk/docs/LLGPL-LICENSE.txt and | 
|---|
| 7 | ;;+  trunk/docs/LGPL-LICENSE.txt. | 
|---|
| 8 | ;;+----------------------------------------------------------------------------- | 
|---|
| 9 |  | 
|---|
| 10 | (defpackage :rest-interface | 
|---|
| 11 | (:nicknames :rest) | 
|---|
| 12 | (:use :cl :hunchentoot | 
|---|
| 13 | :cxml | 
|---|
| 14 | :constants | 
|---|
| 15 | :exceptions | 
|---|
| 16 | :TM-SPARQL | 
|---|
| 17 | :atom | 
|---|
| 18 | :datamodel | 
|---|
| 19 | :xtm-exporter | 
|---|
| 20 | :xml-tools | 
|---|
| 21 | :xtm-importer | 
|---|
| 22 | :json-exporter | 
|---|
| 23 | :json-importer | 
|---|
| 24 | :base-tools | 
|---|
| 25 | :isidorus-threading) | 
|---|
| 26 | (:export :*use-overview-cache* | 
|---|
| 27 | :import-fragments-feed | 
|---|
| 28 | :import-snapshots-feed | 
|---|
| 29 | :import-tm-feed | 
|---|
| 30 | :read-url | 
|---|
| 31 | :read-fragment-feed | 
|---|
| 32 | :start-json-engine | 
|---|
| 33 | :start-atom-engine | 
|---|
| 34 | :start-admin-server | 
|---|
| 35 | :start-gdl-engine | 
|---|
| 36 | :shutdown-json-engine | 
|---|
| 37 | :shutdown-atom-engine | 
|---|
| 38 | :*admin-local-backup* | 
|---|
| 39 | :*admin-remote-backup* | 
|---|
| 40 | :*admin-shutdown* | 
|---|
| 41 | :shutdown-admin-server | 
|---|
| 42 | :*admin-host-name* | 
|---|
| 43 | :*admin-port* | 
|---|
| 44 | :*remote-backup-remote-address* | 
|---|
| 45 | :*local-backup-remote-address* | 
|---|
| 46 | :*shutdown-remote-address* | 
|---|
| 47 | :set-up-json-interface | 
|---|
| 48 | :set-up-gdl-interface | 
|---|
| 49 | :*json-get-prefix* | 
|---|
| 50 | :*get-rdf-prefix* | 
|---|
| 51 | :*json-commit-url* | 
|---|
| 52 | :*json-get-all-psis* | 
|---|
| 53 | :*json-get-summary-prefix* | 
|---|
| 54 | :*json-get-all-type-psis* | 
|---|
| 55 | :*json-get-all-instance-psis* | 
|---|
| 56 | :*json-get-topic-stub-prefix* | 
|---|
| 57 | :*json-get-type-tmcl-prefix* | 
|---|
| 58 | :*json-get-instance-tmcl-prefix* | 
|---|
| 59 | :*json-get-overview* | 
|---|
| 60 | :*ajax-user-interface-url* | 
|---|
| 61 | :*ajax-user-interface-file-path* | 
|---|
| 62 | :*ajax-javascript-directory-path* | 
|---|
| 63 | :*ajax-javascript-url-prefix* | 
|---|
| 64 | :*xtm-commit-prefix* | 
|---|
| 65 | :*ready-to-die* | 
|---|
| 66 | :die-when-finished | 
|---|
| 67 | :*use-http-authentication* | 
|---|
| 68 | :*users* | 
|---|
| 69 | :*sparql-url* | 
|---|
| 70 | :*gdl-get-fragment* | 
|---|
| 71 | :*gdl-get-schema* | 
|---|
| 72 | :*gdl-commit-fragment* | 
|---|
| 73 | :*gdl-delete-fragment* | 
|---|
| 74 | :*gdl-host-address-hash-object* | 
|---|
| 75 | :*gdl-host-address-environment* | 
|---|
| 76 | :*gdl-base-path* | 
|---|
| 77 | :*gdl-host-file* | 
|---|
| 78 | :*gdl-tm-id* | 
|---|
| 79 | :*gdl-sparql*)) | 
|---|
| 80 |  | 
|---|
| 81 |  | 
|---|
| 82 | (in-package :rest-interface) | 
|---|
| 83 |  | 
|---|
| 84 |  | 
|---|
| 85 | (defun create-regex-dispatcher (regex page-function) | 
|---|
| 86 | "Just like hunchentoot:create-regex-dispatcher except it extracts the matched values | 
|---|
| 87 | and passes them onto PAGE-FUNCTION as arguments. | 
|---|
| 88 |  | 
|---|
| 89 | Copied from http://uint32t.blogspot.com/2007/12/restful-handlers-with-hunchentoot.html" | 
|---|
| 90 | (let ((scanner (cl-ppcre:create-scanner regex))) | 
|---|
| 91 | (lambda (request) | 
|---|
| 92 | (multiple-value-bind (whole-match matched-registers) | 
|---|
| 93 | (cl-ppcre:scan-to-strings scanner (hunchentoot:script-name request)) | 
|---|
| 94 | (when whole-match | 
|---|
| 95 | (lambda () | 
|---|
| 96 | (apply page-function (coerce matched-registers 'list)))))))) | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 | (defvar *json-server-acceptor* nil) | 
|---|
| 100 | (defvar *gdl-server-acceptor* nil) | 
|---|
| 101 | (defvar *atom-server-acceptor* nil) | 
|---|
| 102 | (defvar *admin-server-acceptor* nil) | 
|---|
| 103 | (defvar *admin-host-name* "127.0.0.1") | 
|---|
| 104 | (defvar *admin-port* 11008) | 
|---|
| 105 | (defvar *remote-backup-remote-address* "127.0.0.1") | 
|---|
| 106 | (defvar *local-backup-remote-address* "127.0.0.1") | 
|---|
| 107 | (defvar *shutdown-remote-address* "127.0.0.1") | 
|---|
| 108 | (defvar *users* (list (list :uname "admin" :passwd "admin"))) | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 | (defun start-admin-server () | 
|---|
| 112 | (when *admin-server-acceptor* | 
|---|
| 113 | (error "The admin-server is already running")) | 
|---|
| 114 | (set-up-admin-interface ) | 
|---|
| 115 | (setf hunchentoot:*show-lisp-errors-p* t) | 
|---|
| 116 | (setf hunchentoot:*hunchentoot-default-external-format* | 
|---|
| 117 | (flex:make-external-format :utf-8 :eol-style :lf)) | 
|---|
| 118 | (setf *admin-server-acceptor* | 
|---|
| 119 | (make-instance 'hunchentoot:acceptor | 
|---|
| 120 | :address *admin-host-name* | 
|---|
| 121 | :port *admin-port*)) | 
|---|
| 122 | (hunchentoot:start *admin-server-acceptor*)) | 
|---|
| 123 |  | 
|---|
| 124 |  | 
|---|
| 125 | (defun shutdown-admin-server () | 
|---|
| 126 | "Shut down the admin server." | 
|---|
| 127 | (when *admin-server-acceptor* | 
|---|
| 128 | (hunchentoot:stop *admin-server-acceptor*)) | 
|---|
| 129 | (setf *admin-server-acceptor* nil)) | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 | (defun start-gdl-engine (repository-path &key | 
|---|
| 133 | (host-name "localhost") (port 8018)) | 
|---|
| 134 | "Starts the Topic Maps engine with a given port and address, | 
|---|
| 135 | so the engine can serve and consume gdl-fragments for the | 
|---|
| 136 | gdl-frontend anaToMia." | 
|---|
| 137 | (when *gdl-server-acceptor* | 
|---|
| 138 | (error "The gdl-server is already running")) | 
|---|
| 139 | (setf hunchentoot:*show-lisp-errors-p* t) ;for now | 
|---|
| 140 | (setf hunchentoot:*hunchentoot-default-external-format* | 
|---|
| 141 | (flex:make-external-format :utf-8 :eol-style :lf)) | 
|---|
| 142 | (open-tm-store repository-path) | 
|---|
| 143 | (set-up-gdl-interface) | 
|---|
| 144 | (setf *gdl-server-acceptor* | 
|---|
| 145 | (make-instance 'hunchentoot:acceptor :address host-name :port port)) | 
|---|
| 146 | (setf hunchentoot:*lisp-errors-log-level* :info) | 
|---|
| 147 | (setf hunchentoot:*message-log-pathname* "./gdl-hunchentoot-errors.log") | 
|---|
| 148 | (hunchentoot:start *gdl-server-acceptor*)) | 
|---|
| 149 |  | 
|---|
| 150 |  | 
|---|
| 151 | (defun start-json-engine (repository-path &key | 
|---|
| 152 | (host-name "localhost") (port 8000)) | 
|---|
| 153 | "Start the Topic Maps Engine on a given port, assuming a given | 
|---|
| 154 | hostname. Use the repository under repository-path. | 
|---|
| 155 | This function starts only the json/xtm/rdf handlers for the UI, | 
|---|
| 156 | The atom interface has to be started separately." | 
|---|
| 157 | (when *json-server-acceptor* | 
|---|
| 158 | (error "The json-server is already running")) | 
|---|
| 159 | (setf hunchentoot:*show-lisp-errors-p* t) ;for now | 
|---|
| 160 | (setf hunchentoot:*hunchentoot-default-external-format* | 
|---|
| 161 | (flex:make-external-format :utf-8 :eol-style :lf)) | 
|---|
| 162 | (open-tm-store repository-path) | 
|---|
| 163 | (set-up-json-interface) | 
|---|
| 164 | (setf *json-server-acceptor* | 
|---|
| 165 | (make-instance 'hunchentoot:acceptor :address host-name :port port)) | 
|---|
| 166 | (setf hunchentoot:*lisp-errors-log-level* :info) | 
|---|
| 167 | (setf hunchentoot:*message-log-pathname* "./json-hunchentoot-errors.log") | 
|---|
| 168 | (hunchentoot:start *json-server-acceptor*)) | 
|---|
| 169 |  | 
|---|
| 170 |  | 
|---|
| 171 | (defun shutdown-json-engine () | 
|---|
| 172 | "Shut down the Topic Map Engine, only the json part." | 
|---|
| 173 | (when *json-server-acceptor* | 
|---|
| 174 | (hunchentoot:stop *json-server-acceptor*)) | 
|---|
| 175 | (setf *json-server-acceptor* nil) | 
|---|
| 176 | (close-tm-store)) | 
|---|
| 177 |  | 
|---|
| 178 |  | 
|---|
| 179 | (defun start-atom-engine (repository-path &key (conf-file "atom/conf.lisp") | 
|---|
| 180 | (host-name "localhost") (port 8001)) | 
|---|
| 181 | "Start the Topic Maps Engine on a given port, assuming a given | 
|---|
| 182 | hostname. Use the repository under repository-path. | 
|---|
| 183 | This function starts only the atom interface. | 
|---|
| 184 | The json/xtm/rdf interface has to be started separately." | 
|---|
| 185 | (when *atom-server-acceptor* | 
|---|
| 186 | (error "The atom-server is already running")) | 
|---|
| 187 | (setf hunchentoot:*show-lisp-errors-p* t) ;for now | 
|---|
| 188 | (setf hunchentoot:*hunchentoot-default-external-format* | 
|---|
| 189 | (flex:make-external-format :utf-8 :eol-style :lf)) | 
|---|
| 190 | (setf atom:*base-url* (format nil "http://~a:~a" host-name port)) | 
|---|
| 191 | (open-tm-store repository-path) | 
|---|
| 192 | (load conf-file) | 
|---|
| 193 | (publish-feed atom:*tm-feed*) | 
|---|
| 194 | (setf *atom-server-acceptor* | 
|---|
| 195 | (make-instance 'hunchentoot:acceptor :address host-name :port port)) | 
|---|
| 196 | (setf hunchentoot:*lisp-errors-log-level* :info) | 
|---|
| 197 | (setf hunchentoot:*message-log-pathname* "./atom-hunchentoot-errors.log") | 
|---|
| 198 | (hunchentoot:start *atom-server-acceptor*)) | 
|---|
| 199 |  | 
|---|
| 200 |  | 
|---|
| 201 | (defun shutdown-atom-engine () | 
|---|
| 202 | "Shut down the Topic Map Engine, only the atom part." | 
|---|
| 203 | (when *atom-server-acceptor* | 
|---|
| 204 | (hunchentoot:stop *atom-server-acceptor*)) | 
|---|
| 205 | (setf *atom-server-acceptor* nil) | 
|---|
| 206 | (close-tm-store)) | 
|---|
| 207 |  | 
|---|
| 208 |  | 
|---|
| 209 | (defmacro with-http-authentication (&rest body) | 
|---|
| 210 | `(multiple-value-bind (username password) (hunchentoot:authorization) | 
|---|
| 211 | (if (find-if (lambda(item) | 
|---|
| 212 | (and (stringp (getf item :uname)) | 
|---|
| 213 | (stringp (getf item :passwd)) | 
|---|
| 214 | (string= (getf item :uname) username) | 
|---|
| 215 | (string= (getf item :passwd) password))) | 
|---|
| 216 | *users*) | 
|---|
| 217 | ,@body | 
|---|
| 218 | (hunchentoot:require-authorization "isidorus")))) | 
|---|