Changes

Jump to navigation Jump to search
1,243 bytes added ,  19:42, 24 January 2017
* Quality factors in the [[#Content Negotiation|Accept header]] will be evaluated
* <code>%input</code> annotations, support for input-specific content-type parameters
* Since {{Version 8.4}}: <code>%rest:single</code> annotation to cancel running RESTXQ functions
<br />
All RESTXQ [[XQuery 3.0#Annotations|annotations]] are assigned to the <code><nowiki>http://exquery.org/ns/restxq</nowiki></code> namespace, which is statically bound to the {{Code|rest}} prefix. A ''Resource Function'' is an XQuery function that has been marked up with RESTXQ annotations. When an HTTP request comes in, a resource function will be invoked that matches the constraints indicated by its annotations.
Whenever If a RESTXQ URL is requested, the [[Options#RESTXQPATH|RESTXQPATH]] module directory and its sub-directories will be traversed, and all [[XQuery Extensions#Suffixes|XQuery files]] will be parsed for functions with RESTXQ annotations in library modules (detected by the extension {{Code|.xqm}}) and main modules (detected by Sub-directories that include an {{Code|.xqignore}}). In main expressions, the main module file will never be evaluated. All modules will be cached and parsed again when their timestamp changesskipped.
A sub-directory To speed up processing, the functions of the existing XQuery modules are automatically cached in main memory. Functions will not be invalidated and parsed for RESTXQ files again if it contains a file named the timestamp of their module changes. {{CodeMark|Updated with Version 8.ignore6:}}. Module caching  * RESTXQ file monitoring has been improved and can be turned on by setting adjusted via the new [[Options#CACHERESTXQPARSERESTXQ|CACHERESTXQPARSERESTXQ]] to trueoption. The option is helpful in In productive environments with a high load, but it may be recommendable to change the timeout, or completely disable monitoring. If files should not be are replaced while the web server is running. If files are still replaced, the RESTXQ module cache must should be explicitly invalidated by calling the static root path {{Code|/.init}}or by calling the [[RESTXQ Module#rest:init|rest:init]] function.* Authentication was readded to RESTXQ (see [[Web Application]] for more details).
==Examples==
%rest:form-param("message","{$message}", "(no message)")
%rest:header-param("User-Agent", "{$agent}")
function page:hello-postman( $message as xs:string, $agent as xs:string*) as element(response){
&lt;response type='form'&gt;
&lt;message&gt;{ $message }&lt;/message&gt;
===Input options===
Conversion options for [[Options#JSONPARSER|JSON]], [[Options#CSVPARSER|CSV ]] and [[Options#HTMLPARSER|HTML ]] can also be specified via annotations using with the <code>input</code> prefix. The following function interprets the input as text with the CP1252 encoding and treats the first line of the textual input as CSV header:
<pre class="brush:xquery">
%rest:path("/store.csv")
%rest:POST("{$csv}")
%input:csv("header=true,encoding=CP1252") function page:store-csv($csv as document-node()){
"Number of rows: " || count($csv/csv/record)
};
%rest:POST("{$data}")
%rest:consumes("multipart/mixed") (: optional :)
function page:multipart($data as item()*){
"Number of items: " || count($data)
};
%rest:query-param("id", "{$id}")
%rest:query-param("add", "{$add}", 42, 43, 44)
function page:params($id as xs:string?, $add as xs:integer+){
<result id="{ $id }" sum="{ sum($add) }"/>
};
%rest:path("/upload")
%rest:form-param("files", "{$files}")
function page:upload($files){
for $name in map:keys($files)
let $content := $files($name)
==Query Execution==
{{Mark|Introduced with Version 8.4}}: <code>%rest:single</code> annotation. In common many RESTXQ search scenarios, functions are defined for processing input from browser input forms is processed and returning search resultsare returned. User experience can generally be made more interactive if a an updated search request is triggered with each key click. However, such search patterns this may lead to numerous many expensive parallel requests, from which only the result of the last request will be relevant for the client.
With the <code>%rest:single</code> annotation, it can be enforced that only one instance of a function will be executed for the same client. As soon as If the same function will be called for the second time, a the already running instance query will be stopped, and the HTTP error code {{Code|410}} (Gone) will be returned instead of the result:
<pre class="brush:xquery">
(: If fast enough, returns the result. Otherwise, if called again, raises 410 :)
declare
%rest:path("/search")
%rest:query-param("term", "{$term}")
%rest:single
function page:search($term as xs:string){
<ul>{
for $result in db:open('large-db')//*[text() = $term]
</pre>
By specifying a string along with the annotation, functions can be bundledtogether, and one request can be canceled by calling another one:. This is shown by another example, in which the first function can be interrupted by the second one. If you call both functions in separate browser tabs, you will note that the first tab will return <code>410</code>, and the second one will return <xml>stopped</xml>.
<pre class="brush:xquery">
%rest:single("EXPENSIVE")
function local:stop() {
<htmlxml>interruptedstopped</htmlxml>
};
</pre>
The following things should be noted: * If a query will be canceled, there will be no undesirable side-effects. For example, it won’t be possible to kill a query if it is currenly updating the database or perfoming any other I/O operations. As a result, the termination of a running query can take some more time as expected.* The currently executed function is bound to the client current session. This way, it can be ensured that one a client will not be able to cancel requests from other clients. As a result, functions can only be stopped if there was at least one previous successful response, in which returns the initial session data was returned to the client.
=Response=
%output:method("json")
%output:json("format=jsonml")
function page:cities(){
element cities {
db:open('factbook')//city/name
%output:doctype-public("-//W3C//DTD XHTML 1.0 Transitional//EN")
%output:doctype-system("http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd")
function page:html(){
<html xmlns="http://www.w3.org/1999/xhtml">
<body>done</body>
declare
%rest:path("/check/{$user}")
function page:check($user){
if($user = ('jack', 'lisa'))
then 'User exists'
%rest:error("err:user")
%rest:error-param("description", "{$user}")
function page:user-error($user){
'User "' || $user || '" is unknown'
};
declare
%rest:path("/teapot")
function page:teapot(){
fn:error(xs:QName('error'), "I'm a teapot", 418)
};
=References=
Currently, the following external resources on RESTXQ existDocumentation:
* [http://exquery.org/spec/restxq RESTXQ Specification], First Draft
* [http://www.adamretter.org.uk/presentations/restxq_mugl_20120308.pdf RESTXQ]. Slides, MarkLogic User Group London, 2012
* [http://files.basex.org/publications/xmlprague/2013/Develop-RESTXQ-WebApps-with-BaseX.pdf Web Application Development]. Slides from XMLPrague 2013
 
Examples:
 
* Sample code combining XQuery and JavaScript: [http://balisage.net/Proceedings/vol17/author-pkg/Galtman01/BalisageVol17-Galtman01.html Materials] and [http://balisage.net/Proceedings/vol17/html/Galtman01/BalisageVol17-Galtman01.html paper] from Amanda Galtman, Balisage 2016.
* [[DBA]]: The Database Administration interface, bundled with the full distributions of BaseX.
=Changelog=
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu