Difference between revisions of "RESTXQ"

From BaseX Documentation
Jump to navigation Jump to search
m (correct 'method' to 'media-type')
m (Text replacement - "syntaxhighlight" to "pre")
 
(192 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
This page presents one of the [[Web Application]] services. It describes how to use the RESTXQ API of BaseX.
 
This page presents one of the [[Web Application]] services. It describes how to use the RESTXQ API of BaseX.
  
RESTXQ, introduced by [http://www.adamretter.org.uk/ Adam Retter], is a new API that facilitates the use of XQuery
+
RESTXQ, introduced by [http://www.adamretter.org.uk/ Adam Retter], is an API that facilitates the use of XQuery as a server-side processing language for the Web. It has been inspired by the Java [https://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services JAX-RS API]: It provides a pre-defined set of XQuery 3.0 annotations for mapping HTTP requests to XQuery functions, which in turn generate and return HTTP responses.
as a Server Side processing language for the Web. RESTXQ has been inspired by Java’s
 
[http://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services JAX-RS API]: it defines a pre-defined set of
 
XQuery 3.0 annotations for mapping HTTP requests to XQuery functions, which in turn generate and return
 
HTTP responses.
 
  
Note that some extensions presented in this documentation are BaseX-specific; they may be integrated
+
BaseX provides various extensions to the original draft of the specification:
in future versions of the RESTXQ draft.
 
  
With {{Version|7.7}}, the following features have been added:
+
* Multipart types are supported, including {{Code|multipart/form-data}}
 +
* A {{Code|%rest:error}} annotation can be used to catch XQuery errors
 +
* Servlet errors can be redirected to other RESTXQ pages
 +
* A [[RESTXQ Module]] provides some helper functions
 +
* Parameters are implicitly cast to the type of the function argument
 +
* The [[#Paths|Path Annotation]] can contain regular expressions
 +
* <code>%input</code> annotations, support for input-specific content-type parameters
 +
* <code>%rest:single</code> annotation to cancel running RESTXQ functions
 +
* Quality factors in the [[#Content Negotiation|Accept header]] will be evaluated
 +
* Support for server-side quality factors in the [[#Content Negotiation|<code>%rest:produces</code>]] annotation
 +
* Better support for the OPTIONS and HEAD methods
 +
<br/>
  
* RESTXQ functions may also be specified in main modules (extension: {{Code|.xq}})
+
=Introduction=
* multipart types are now supported, including {{Code|multipart/form-data}}
 
* a new {{Code|%rest:error}} annotation can be used to catch XQuery errors
 
* servlet errors can be redirected to other RESTXQ pages
 
* a [[RESTXQ Module]] provides some helper functions
 
* parameters are implicitly cast to the type of the function argument
 
* the RESTXQ namespace prefix has been changed to {{Code|rest}}
 
* by default, RESTXQ is now available on top level via {{Code|http://localhost:8984/}}<br/>&nbsp;
 
  
=Usage=
+
==Preliminaries==
  
The RESTXQ service is accessible via {{Code|http://localhost:8984/}}, and the HTTP server is started with admin credentials ([[Web Application#User Management|see further]]).
+
The RESTXQ service is accessible via {{Code|http://localhost:8080/}}.
  
All RESTXQ [[XQuery 3.0#Annotations|annotations]] are assigned to the {{Code|<nowiki>http://exquery.org/ns/restxq</nowiki>}} 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.
+
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 a RESTXQ URL is requested, the [[Options#RESTXQPATH|RESTXQPATH]] module directory and its sub-directories will be parsed for functions with RESTXQ annotations in library modules (detected by the extension {{Code|.xqm}}) and main modules (detected by {{Code|.xq}}). In main expressions, the main module will never be evaluated. All modules will be cached and parsed again when their timestamp changes.
+
If a RESTXQ URL is requested, the {{Option|RESTXQPATH}} module directory and its subdirectories will be traversed, and all [[XQuery Extensions#Suffixes|XQuery files]] will be parsed for functions with RESTXQ annotations. Subdirectories that include an {{Code|.ignore}} file will be skipped.
  
A simple RESTXQ module is shown below. It is part of a clean installation and available at http://localhost:8984/ .
+
To speed up processing, the functions of the existing XQuery modules are automatically cached in main memory:
 +
* Functions will be invalidated and parsed again if the timestamp of their module changes.
 +
* File monitoring can be adjusted via the {{Option|PARSERESTXQ}} option. In productive environments with a high load, it may be recommendable to change the timeout, or completely disable monitoring.
 +
* If files are replaced while the web server is running, the RESTXQ module cache should be explicitly invalidated by calling the static root path {{Code|/.init}} or by calling the {{Function|RESTXQ|rest:init}} function.
  
<pre class="brush:xquery">(: simplified version of the function found in webapp/restxq.xqm :)
+
==Examples==
 +
 
 +
A first RESTXQ function is shown below:
 +
 
 +
<pre lang='xquery'>
 
module namespace page = 'http://basex.org/examples/web-page';
 
module namespace page = 'http://basex.org/examples/web-page';
  
declare %rest:path("hello/{$world}")
+
declare %rest:path("hello/{$who}") %rest:GET function page:hello($who) {
        %rest:GET
 
        %rest:header-param("User-Agent", "{$agent}")
 
        function page:hello($world as xs:string, $agent as xs:string*) {
 
 
   <response>
 
   <response>
     <title>Hello { $world }!</title>
+
     <title>Hello { $who }!</title>
    <info>You requested this page with { $agent }.</info>
 
 
   </response>
 
   </response>
};</pre>
+
};
 +
</pre>
  
If the URI http://localhost:8984/hello/World is accessed, the result will be similar to:
+
If the URI http://localhost:8080/hello/World is accessed, the result will be:
  
<pre class="brush:xml">
+
<pre lang="xml">
&lt;response&gt;
+
<response>
   &lt;title&gt;Hello World!&lt;/title&gt;
+
   <title>Hello World!</title>
  &lt;time&gt;The current time is: 18:42:02.306+02:00&lt;/time&gt;
+
</response>
&lt;/response&gt;
 
 
</pre>
 
</pre>
  
The RESTXQ module contains yet another function:
+
The next function demonstrates a POST request:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare
 
declare
 
   %rest:path("/form")
 
   %rest:path("/form")
Line 61: Line 63:
 
   %rest:form-param("message","{$message}", "(no message)")
 
   %rest:form-param("message","{$message}", "(no message)")
 
   %rest:header-param("User-Agent", "{$agent}")
 
   %rest:header-param("User-Agent", "{$agent}")
  function page:hello-postman(
+
function page:hello-postman(
    $message as xs:string,
+
  $message as xs:string,
    $agent   as xs:string*)
+
  $agent   as xs:string*
    as element(response)
+
) as element(response) {
{
+
   <response type='form'>
   &lt;response type='form'&gt;
+
     <message>{ $message }</message>
     &lt;message&gt;{ $message }&lt;/message&gt;
+
     <user-agent>{ $agent }</user-agent>
     &lt;user-agent&gt;{ $agent }&lt;/user-agent&gt;
+
   </response>
   &lt;/response&gt;
 
 
};
 
};
 
</pre>
 
</pre>
  
If you post something (e.g. using curl or the embedded form at http://localhost:8984/)...
+
If you post something (e.g. using curl or the embedded form at http://localhost:8080/)...
  
<pre class="brush:shell">
+
<pre lang="shell">
curl -i -X POST --data "message='CONTENT'" http://localhost:8984/form
+
curl -i -X POST --data "message='CONTENT'" http://localhost:8080/form
 
</pre>
 
</pre>
  
 
...you will receive something similar to the following result:
 
...you will receive something similar to the following result:
  
<pre class="brush:shell">
+
<pre lang="shell">
 
HTTP/1.1 200 OK
 
HTTP/1.1 200 OK
 
Content-Type: application/xml; charset=UTF-8
 
Content-Type: application/xml; charset=UTF-8
Line 88: Line 89:
 
</pre>
 
</pre>
  
<pre class="brush:xml">
+
<pre lang="xml">
 
<response type="form">
 
<response type="form">
 
   <message>'CONTENT'</message>
 
   <message>'CONTENT'</message>
Line 95: Line 96:
 
</pre>
 
</pre>
  
=Requests=
+
=Request=
  
 
This section shows how annotations are used to handle and process HTTP requests.
 
This section shows how annotations are used to handle and process HTTP requests.
Line 105: Line 106:
 
===Paths===
 
===Paths===
  
A resource function must have a single ''Path Annotation'' with a single string as argument. The function will be called if a URL matches the path segments and templates of the argument. ''Path templates'' contain variables in curly brackets, and map the corresponding segments of the request path to the arguments of the resource function.
+
A resource function must have a single ''Path Annotation'' with a single string as argument. The function will be called if a URL matches the path segments and templates of the argument. ''Path templates'' contain variables in curly brackets, and map the corresponding segments of the request path to the arguments of the resource function. The first slash in the path is optional.
  
 
The following example contains a path annotation with three segments and two templates. One of the function arguments is further specified with a data type, which means that the value for <code>$variable</code> will be cast to an <code>xs:integer</code> before being bound:
 
The following example contains a path annotation with three segments and two templates. One of the function arguments is further specified with a data type, which means that the value for <code>$variable</code> will be cast to an <code>xs:integer</code> before being bound:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare %rest:path("/a/path/{$with}/some/{$variable}")
 
declare %rest:path("/a/path/{$with}/some/{$variable}")
 
   function page:test($with, $variable as xs:integer) { ... };
 
   function page:test($with, $variable as xs:integer) { ... };
 
</pre>
 
</pre>
 
<!-- TODO how matching works -->
 
<!-- TODO how matching works -->
 +
 +
Variables can be enhanced by regular expressions:
 +
 +
<pre lang='xquery'>
 +
(: Matches all paths with "app" as first, a number as second, and "order" as third segment :)
 +
declare %rest:path("app/{$code=[0-9]+}/order")
 +
  function page:order($code) { ... };
 +
 +
(: Matches all other all paths starting with "app/" :)
 +
declare %rest:path("app/{$path=.+}")
 +
  function page:others($path) { ... };
 +
</pre>
 +
<!-- TODO how matching works -->
 +
 +
If multiple path candidates are found for the request, the one with more segments will be preferred.
  
 
===Content Negotiation===
 
===Content Negotiation===
  
Two following annotations can be used to restrict functions to specific content types:
+
Functions can be restricted to specific Media Types. The default type is {{Code|*/*}}. Multiple types can either be specified by a single or by multiple annotations.
 +
 
 +
====Consuming Data====
 +
 
 +
A function will only be taken into consideration if the HTTP {{Code|Content-Type}} header of the request matches one of the given types:
 +
 
 +
<pre lang='xquery'>
 +
declare
 +
  %rest:POST("{$body}")
 +
  %rest:path("/xml")
 +
  %rest:consumes("application/xml")
 +
  %rest:consumes("text/xml")
 +
function page:xml($body) { $body };
 +
</pre>
 +
 
 +
====Producing Data====
 +
 
 +
A function will only be chosen if the HTTP {{Code|Accept}} header of the request matches one of the given types:
 +
 
 +
<pre lang='xquery'>
 +
declare
 +
  %rest:path("/xml")
 +
  %rest:produces("application/xml", "text/xml")
 +
function page:xml() { <xml/> };
 +
</pre>
 +
 
 +
Note that the annotations will ''not'' affect the type of the actual response: You will need to supply an additional <code>[[#Output|%output:media-type]]</code> annotation or (if a single function may produce results of different types) generate an apt [[#Custom_Response|Custom Response]].
 +
 
 +
====Quality Factors====
 +
 
 +
A client can supply quality factors to influence the server-side function selection process. If a client sends the following HTTP header with quality factors…
  
* '''HTTP Content Types''': a function will only be invoked if the HTTP {{Code|Content-Type}} header of the request matches one of the given mime types. Example:
+
<pre>
<pre class="brush:xquery">%rest:consumes("application/xml", "text/xml")</pre>
+
Accept: */*;q=0.5,text/html;q=1.0
* '''HTTP Accept''': a function will only be invoked if the HTTP {{Code|Accept}} header of the request matches one of the defined mime types. Example:
+
</pre>
<pre class="brush:xquery">%rest:produces("application/atom+xml")</pre>
 
  
By default, both mime types are {{Code|*/*}}. Note that this annotation will ''not'' affect the content-type of the HTTP response. Instead, you will need to add a <code>[[#Output|%output:media-type]]</code> annotation.
+
…and if two RESTXQ functions exist for the addressed path with two different annotations for producing data…
 +
 
 +
<pre lang='xquery'>
 +
declare function %rest:produces("text/html") ...
 +
...
 +
declare function %rest:produces("*/*") ...
 +
</pre>
 +
 
 +
…the first of these function will be chosen, as the quality factor for <code>text/html</code> documents is highest.
 +
 
 +
As we cannot ensure that the client may supply quality factors, the selection process can also be controlled server-side. The <code>qs</code> parameter can be attached server-side to the Media Type. If multiple functions are left in the selection process, the one with the highest quality factor will be favored:
 +
 
 +
<pre lang='xquery'>
 +
declare function %rest:produces("application/json;qs=1") ...
 +
...
 +
declare function %rest:produces("*/*;qs=0.5") ...
 +
</pre>
  
 
===HTTP Methods===
 
===HTTP Methods===
  
The HTTP method annotations are equivalent to all [http://en.wikipedia.org/wiki/HTTP#Request_methods HTTP request methods] except for TRACE and CONNECT. Zero or more methods may be used on a function; if none is specified, the function will be invoked for each method.
+
====Default Methods====
 +
 
 +
The HTTP method annotations are equivalent to all [https://en.wikipedia.org/wiki/HTTP#Request_methods HTTP request methods] except TRACE and CONNECT. Zero or more methods may be used on a function; if none is specified, the function will be invoked for each method.
  
 
The following function will be called if GET or POST is used as request method:
 
The following function will be called if GET or POST is used as request method:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare %rest:GET %rest:POST %rest:path("/post")
 
declare %rest:GET %rest:POST %rest:path("/post")
 
   function page:post() { "This was a GET or POST request" };
 
   function page:post() { "This was a GET or POST request" };
Line 139: Line 202:
 
The POST and PUT annotations may optionally take a string literal in order to map the HTTP request body to a [[#Parameters|function argument]]. Once again, the target variable must be embraced by curly brackets:
 
The POST and PUT annotations may optionally take a string literal in order to map the HTTP request body to a [[#Parameters|function argument]]. Once again, the target variable must be embraced by curly brackets:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare %rest:PUT("{$body}") %rest:path("/put")
 
declare %rest:PUT("{$body}") %rest:path("/put")
 
   function page:put($body) { "Request body: " || $body };
 
   function page:put($body) { "Request body: " || $body };
 
</pre>
 
</pre>
  
If a content-type is specified in the request, the content is converted to the following XQuery type:
+
====Custom Methods====
  
{| class="wikitable" width="100%"
+
Custom HTTP methods can be specified with the {{Code|%rest:method}} annotation. An optional body variable can be supplied as second argument:
 +
 
 +
<pre lang='xquery'>
 +
declare
 +
  %rest:path("binary-size")
 +
  %rest:method("SIZE", "{$body}")
 +
function page:patch(
 +
  $body  as xs:base64Binary
 +
) {
 +
  "Request method: " || request:method(),
 +
  "Size of body: " || bin:length($body)
 +
};
 +
</pre>
 +
 
 +
If an OPTIONS request is received, and if no function is defined, an automatic response will be generated, which includes an <code>Allow</code> header with all supported methods.
 +
 
 +
If a HEAD request is received, and if no function is defined, the corresponding GET function will be processed, but the response body will be discarded.
 +
 
 +
==Content Types==
 +
 
 +
The body of a POST or PUT request will be converted to an XQuery item. Conversion can be
 +
controlled by specifying a content type. It can be further influenced
 +
by specifying additional content-type parameters:
 +
 
 +
{| class="wikitable"
 
|- valign="top"
 
|- valign="top"
 
! Content-Type
 
! Content-Type
! XQuery type
+
! Parameters (<code>;name=value</code>)
|-
+
! Type of resulting XQuery item
| {{Code|application/json}}, {{Code|application/jsonml+json}}
+
|- valign="top"
| {{Code|document-node()}} (conversion is described in the [[JSON Module]])
 
|-
 
| {{Code|text/html}}
 
| {{Code|document-node()}} (conversion is described in the [[HTML Module]])
 
|-
 
| {{Code|text/comma-separated-values}}
 
| {{Code|document-node()}}
 
|-
 
 
| {{Code|text/xml}}, {{Code|application/xml}}
 
| {{Code|text/xml}}, {{Code|application/xml}}
 +
|
 
| {{Code|document-node()}}
 
| {{Code|document-node()}}
|-
+
|- valign="top"
 
| {{Code|text/*}}
 
| {{Code|text/*}}
 +
|
 
| {{Code|xs:string}}
 
| {{Code|xs:string}}
|-
+
|- valign="top"
 +
| {{Code|application/json}}
 +
| [[JSON Module#Options|JSON Options]]
 +
| {{Code|document-node()}} or {{Code|map(*)}}
 +
|- valign="top"
 +
| {{Code|text/html}}
 +
| [[HTML Module#Options|HTML Options]]
 +
| {{Code|document-node()}}
 +
|- valign="top"
 +
| {{Code|text/comma-separated-values}}
 +
| [[CSV Module#Options|CSV Options]]
 +
| {{Code|document-node()}} or {{Code|map(*)}}
 +
|- valign="top"
 
| ''others''
 
| ''others''
 +
|
 
| {{Code|xs:base64Binary}}
 
| {{Code|xs:base64Binary}}
|-
+
|- valign="top"
 
| {{Code|multipart/*}}
 
| {{Code|multipart/*}}
 +
|
 
| sequence (see next paragraph)
 
| sequence (see next paragraph)
 
|}
 
|}
  
====Multipart Types====
+
For example, if <code>application/json;lax=yes</code> is specified as content type, the input will be transformed to JSON, and the lax QName conversion rules will be applied, as described in the [[JSON Module]].
 +
 
 +
===Input options===
  
Some first support for {{Code|multipart}} content-types has been added.
+
Conversion options for {{Option|JSON}}, {{Option|CSV}} and {{Option|HTML}} can also be specified via annotations with the <code>input</code> prefix. The following function interprets the input as text with the CP1252 encoding and treats the first line as header:
The parts of a multipart message are represented as a sequence,
+
 
 +
<pre lang='xquery'>
 +
declare
 +
  %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)
 +
};
 +
</pre>
 +
 
 +
===Multipart Types===
 +
 
 +
The single parts of a multipart message are represented as a sequence,
 
and each part is converted to an XQuery item as described in the last paragraph.
 
and each part is converted to an XQuery item as described in the last paragraph.
  
 
A function that is capable of handling multipart types is identical to other RESTXQ functions:
 
A function that is capable of handling multipart types is identical to other RESTXQ functions:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare
 
declare
 
   %rest:path("/multipart")
 
   %rest:path("/multipart")
 
   %rest:POST("{$data}")
 
   %rest:POST("{$data}")
 
   %rest:consumes("multipart/mixed") (: optional :)
 
   %rest:consumes("multipart/mixed") (: optional :)
  function page:multipart($data as item()*)
+
function page:multipart($data as item()*) {
{
 
 
   "Number of items: " || count($data)
 
   "Number of items: " || count($data)
 
};
 
};
 
</pre>
 
</pre>
 
Please note that support for multipart types is still experimental,
 
and it may change in a future version BaseX. Your feedback is welcome.
 
  
 
==Parameters==
 
==Parameters==
Line 203: Line 309:
 
The value of the ''first parameter'', if found in the [[Request_Module#Conventions|query component]], will be assigned to the variable specified as ''second parameter''. If no value is specified in the HTTP request, all additional parameters will be bound to the variable (if no additional parameter is given, an empty sequence will be bound):
 
The value of the ''first parameter'', if found in the [[Request_Module#Conventions|query component]], will be assigned to the variable specified as ''second parameter''. If no value is specified in the HTTP request, all additional parameters will be bound to the variable (if no additional parameter is given, an empty sequence will be bound):
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare
 
declare
 
   %rest:path("/params")
 
   %rest:path("/params")
 
   %rest:query-param("id", "{$id}")
 
   %rest:query-param("id", "{$id}")
 
   %rest:query-param("add", "{$add}", 42, 43, 44)
 
   %rest:query-param("add", "{$add}", 42, 43, 44)
  function page:params($value as xs:string?, $answer as xs:integer+)
+
function page:params($id as xs:string?, $add as xs:integer+) {
{
 
 
   <result id="{ $id }" sum="{ sum($add) }"/>
 
   <result id="{ $id }" sum="{ sum($add) }"/>
 
};
 
};
Line 216: Line 321:
 
===HTML Form Fields===
 
===HTML Form Fields===
  
Form parameters are specified the same way as [[#Query Parameters|query parameters]]. Their values are extracted from GET or POST requests.
+
Form parameters are specified the same way as [[#Query Parameters|query parameters]]:
 +
 
 +
<pre lang='xquery'>
 +
%rest:form-param("city", "{$city}", "no-city-specified")
 +
</pre>
 +
 
 +
The values are the result of HTML forms submitted with the (default) content type <code>application/x-www-form-urlencoded</code>:
  
<pre class="brush:xquery">
+
<pre lang="xml">
%rest:form-param("parameter", "{$value}", "default")
+
<form action="/process" method="POST" enctype="application/x-www-form-urlencoded">
 +
  <input type="text" name="city"/>
 +
  <input type="submit"/>
 +
</form>
 
</pre>
 
</pre>
  
Line 226: Line 340:
 
Files can be uploaded to the server by using the content type {{Code|multipart/form-data}} (the HTML5 {{Code|multiple}} attribute enables the upload of multiple files):
 
Files can be uploaded to the server by using the content type {{Code|multipart/form-data}} (the HTML5 {{Code|multiple}} attribute enables the upload of multiple files):
  
<pre class="brush:xml">
+
<pre lang="xml">
 
<form action="/upload" method="POST" enctype="multipart/form-data">
 
<form action="/upload" method="POST" enctype="multipart/form-data">
   <input type="file" name="files" multiple="multiple" />
+
   <input type="file" name="files" multiple="multiple"/>
 +
  <input type="submit"/>
 
</form>
 
</form>
 
</pre>
 
</pre>
Line 234: Line 349:
 
The file contents are placed in a [[Map Module|map]], with the filename serving as key. The following example shows how uploaded files can be stored in a temporary directory:
 
The file contents are placed in a [[Map Module|map]], with the filename serving as key. The following example shows how uploaded files can be stored in a temporary directory:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare
 
declare
 
   %rest:POST
 
   %rest:POST
 
   %rest:path("/upload")
 
   %rest:path("/upload")
 
   %rest:form-param("files", "{$files}")
 
   %rest:form-param("files", "{$files}")
  function page:upload($files)
+
function page:upload($files) {
{
 
 
   for $name    in map:keys($files)
 
   for $name    in map:keys($files)
 
   let $content := $files($name)
 
   let $content := $files($name)
Line 255: Line 369:
 
Header parameters are specified the same way as [[#Query Parameters|query parameters]]:
 
Header parameters are specified the same way as [[#Query Parameters|query parameters]]:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
%rest:header-param("User-Agent", "{$user-agent}")
 
%rest:header-param("User-Agent", "{$user-agent}")
 
%rest:header-param("Referer", "{$referer}", "none")
 
%rest:header-param("Referer", "{$referer}", "none")
Line 264: Line 378:
 
Cookie parameters are specified the same way as [[#Query Parameters|query parameters]]:
 
Cookie parameters are specified the same way as [[#Query Parameters|query parameters]]:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
%rest:cookie-param("username", "{$user}")
 
%rest:cookie-param("username", "{$user}")
 
%rest:cookie-param("authentication", "{$auth}", "no_auth")
 
%rest:cookie-param("authentication", "{$auth}", "no_auth")
 
</pre>
 
</pre>
  
=Responses=
+
==Query Execution==
 +
 
 +
In many web search scenarios, user input from browser forms is processed and search results are returned. Such operations can be made more interactive by sending a new search request to the server with each key click. However, this may lead to many parallel server-side 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 run at the same time and for the same client. If the same function will be called for the second time, a currently executed query will be stopped, and the HTTP error code {{Code|460}} will be returned instead:
 +
 
 +
<pre lang='xquery'>
 +
(: If fast enough, returns the result. Otherwise, if called again, raises 460 :)
 +
declare
 +
  %rest:path("/search")
 +
  %rest:query-param("term", "{$term}")
 +
  %rest:single
 +
function page:search($term as xs:string) {
 +
  <ul>{
 +
    for $result in db:get('large-db')//*[text() = $term]
 +
    return <li>{ $result }</li>
 +
  }</ul>
 +
};
 +
</pre>
 +
 
 +
By adding a string value to with the annotation, functions can be bundled together, and a running query can be canceled by calling another one that has the same annotation value. 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>460</code>, and the second one will return <xml>stopped</xml>.
 +
 
 +
<pre lang='xquery'>
 +
declare
 +
  %rest:path("/compute")
 +
  %rest:single("EXPENSIVE")
 +
function local:compute() {
 +
  (1 to 100000000000000)[. = 0]
 +
};
 +
 
 +
declare
 +
  %rest:path("/stop")
 +
  %rest:single("EXPENSIVE")
 +
function local:stop() {
 +
  <xml>stopped</xml>
 +
};
 +
</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 abort a query if it is currently updating the database or performing 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 current session. This way, 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 initial session data was returned to the client.
 +
 
 +
=Response=
  
 
By default, a successful request is answered with the HTTP status code {{Code|200}} (OK) and is followed by the given content. An erroneous request leads to an error code and an optional error message (e.g. {{Code|404}} for “resource not found”).
 
By default, a successful request is answered with the HTTP status code {{Code|200}} (OK) and is followed by the given content. An erroneous request leads to an error code and an optional error message (e.g. {{Code|404}} for “resource not found”).
  
==Custom Responses==
+
A {{Code|Server-Timing}} HTTP header is attached to each response. It indicates how much time was spent for parsing, compiling, evaluating and serializing the query. The last value will not necessarily reflect the full time for serializing the result, as the header is generated before the result is sent to the client. Server-side serialization can be enforced by annotating a function with the <code>[[#Query Execution|%rest:single]]</code> annotation.
  
Custom responses can be built from within XQuery by returning an <code>rest:response</code> element, an <code>http:response</code> child node that matches the syntax of the [http://expath.org/spec/http-client EXPath HTTP Client Module] specification, and more optional child nodes that will be serialized as usual. A function that reacts on an unknown resource may look as follows:
+
==Custom Response==
  
<pre class="brush:xquery">
+
Custom responses can be generated in XQuery by returning an <code>rest:response</code> element, an <code>http:response</code> child node that matches the syntax of the [http://expath.org/spec/http-client EXPath HTTP Client Module] specification, and optional child nodes that will be serialized as usual. A function that yields a response on an unknown resource may look as follows:
declare %rest:path("") function page:error404() {
+
 
 +
<pre lang='xquery'>
 +
declare %output:method("text") %rest:path("") function page:error404() {
 
   <rest:response>
 
   <rest:response>
     <http:response status="404" message="I was not found.">
+
     <http:response status="404">
 
       <http:header name="Content-Language" value="en"/>
 
       <http:header name="Content-Language" value="en"/>
       <http:header name="Content-Type" value="text/html; charset=utf-8"/>
+
       <http:header name="Content-Type" value="text/plain; charset=utf-8"/>
 
     </http:response>
 
     </http:response>
   </rest:response>
+
   </rest:response>,
 +
  "The requested resource is not available."
 
};
 
};
 
</pre>
 
</pre>
 +
 +
For the time being, it is not possible to create multipart responses.
  
 
==Forwards and Redirects==
 
==Forwards and Redirects==
  
The two XML elements <code>rest:forward</code> and <code>rest:redirect</code> can be used in the context of [[Web Application]]s, precisely in the context of RESTXQ. These nodes allow e.g. multiple [[XQuery Update]]s in a row by redirecting to the RESTXQ path of updating functions. Both wrap a URL to a RESTXQ path. The wrapped URL should be properly encoded via <code>fn:encode-for-uri()</code>.
+
===Redirects===
  
Note that, currently, these elements are not part of RESTXQ specification.
+
The server can invite the client (e.g., the web browser) to make a second request to another URL by sending a 302 response:
  
===rest:forward===
+
<pre lang="xml">
 +
<rest:response>
 +
  <http:response status="302">
 +
    <http:header name="Location" value="new-location"/>
 +
  </http:response>
 +
</rest:response>
 +
</pre>
  
Usage: wrap the location as follows
+
The convenience function {{Function|Web|web:redirect}} can be called to create such a response.
<pre class="brush:xml"><rest:forward>{ $location }</rest:forward></pre>
 
  
This results in a server-side forwarding, which as well reduces traffic among client and server. A forwarding of this kind will not change the URL seen from the client's perspective.
+
In the XQuery context, redirects are particularly helpful if [[XQuery Update|Updates]] are performed. An updating request may send a redirect to a second function that generates a success message, or evaluates an updated database:
  
As an example, returning
+
<pre lang='xquery'>
<pre class="brush:xml">
+
declare %updating %rest:path('/app/init') function local:create() {
<rest:forward>/hello/universe</rest:forward>
+
  db:create('app', <root/>, 'root.xml'),
 +
  db:output(web:redirect('/app/ok'))
 +
};
 +
 
 +
declare %rest:path('/app/ok') function local:ok() {
 +
  'Stored documents: ' || count(db:get('app'))
 +
};
 
</pre>
 
</pre>
would internally forward to http://localhost:8984/hello/universe
 
  
===rest:redirect===
+
===Forwards===
  
<pre class="brush:xml"><rest:redirect>{ $location }</rest:redirect></pre>
+
A server-side redirect is called forwarding. It reduces traffic among client and server, and the forwarding will not change the URL seen from the client’s perspective:
  
…is basically an abbreviation for…
+
<pre lang="xml">
 +
<rest:forward>new-location</rest:forward>
 +
</pre>
  
<pre class="brush:xml">
+
The response can also be created with the convenience function {{Function|Web|web:forward}}.
<rest:response>
 
  <http:response status="302" message="Temporary Redirect">
 
    <http:header name="location" value="{ $location }"/>
 
  </http:response>
 
</rest:response>
 
</pre>
 
  
The client decides whether to follow this redirection. Browsers usually will, tools like [http://curl.haxx.se/ curl] won’t unless {{Code|-L}} is specified.
+
With {{Announce|Version 11}}, a log entry with the status code {{Code|204}} will be output before the forwarding takes place.
  
 
==Output==
 
==Output==
  
Similar to the [[REST#Content Type|REST]] interface, result serialization can be modified via [[XQuery 3.0#Serialization|XQuery 3.0 serialization parameters]]; in RESTXQ, serialization parameters may be specified in the query prolog, via annotations, or within REST response element. Global parameters are overwritten by more local parameters.
+
The content-type of a response can be influenced by the user via [[Serialization|Serialization Parameters]]. The steps are described in the [[REST#Content Type|REST]] chapter. In RESTXQ, serialization parameters can be specified in the query prolog, via annotations, or within the REST response element:
  
 
===Query Prolog===
 
===Query Prolog===
Line 331: Line 499:
 
In main modules, serialization parameters may be specified in the query prolog. These parameters will then apply to all functions in a module. In the following example, the content type of the response is overwritten with the {{Code|media-type}} parameter:
 
In main modules, serialization parameters may be specified in the query prolog. These parameters will then apply to all functions in a module. In the following example, the content type of the response is overwritten with the {{Code|media-type}} parameter:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare option output:media-type 'text/plain';
 
declare option output:media-type 'text/plain';
  
Line 341: Line 509:
 
===Annotations===
 
===Annotations===
  
The serialization can also be parameterized via annotations:
+
Global serialization parameters can be overwritten via <code>%output</code> annotations. The following example serializes XML nodes as JSON, using the [[JSON Module|JsonML]] format:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
declare %output:media-type("text/plain") %rest:path("version2") function page:version2() {
+
declare
   'Still somewhat simple.'
+
  %rest:path("cities")
 +
  %output:method("json")
 +
  %output:json("format=jsonml")
 +
function page:cities() {
 +
  element cities {
 +
    db:get('factbook')//city/name
 +
  }
 +
};
 +
</pre>
 +
 
 +
The next function, when called, generates XHTML headers, and {{Code|text/html}} will be set as content type:
 +
 
 +
<pre lang='xquery'>
 +
declare
 +
  %rest:path("done")
 +
  %output:method("xhtml")
 +
  %output:omit-xml-declaration("no")
 +
  %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>
 +
  </html>
 
};
 
};
 
</pre>
 
</pre>
Line 351: Line 541:
 
===Response Element===
 
===Response Element===
  
The following example demonstrates how serialization parameters can be dynamically set within a query:
+
Serialization parameters can also be specified in a REST reponse element in a query. Serialization parameters will be overwritten:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare %rest:path("version3") function page:version3() {
 
declare %rest:path("version3") function page:version3() {
 
   <rest:response>
 
   <rest:response>
Line 364: Line 554:
 
</pre>
 
</pre>
  
The content type can also be overwritten by specifying an output {{Code|method}}. The following method mappings are available:
+
=Error Handling=
 +
 
 +
If an error is raised when RESTXQ code is parsed, compiled or evaluated, an HTTP response with the status code 500 is generated.
 +
 
 +
By default, all server-side errors will be passed on to the client. This is particularly helpful during the development process. In a productive environment, however, it is advisable not to expose errors to the client. This can be realized via the {{Option|RESTXQERRORS}} option. If disabled,
  
* <code>xml</code> → <code>application/xml</code>
+
* XQuery modules that cannot be parsed will be ignored and
* <code>xhtml</code> → <code>text/html</code>
+
* full error messages and stack traces will be suppressed and not included in the HTTP response.
* <code>html</code> → <code>text/html</code>
 
* <code>text</code> → <code>text/plain</code>
 
* <code>raw</code> → <code>application/octet-stream</code>
 
* <code>json</code> or <code>jsonml</code> → <code>application/json</code>
 
  
By default, {{Code|application/xml}} is returned as content type. In the following example, XHTML headers will be generated, and {{Code|text/html}} will be set as content type:
+
The full error information can still be looked up in the database logs.
  
<pre class="brush:xquery">
+
==Raise Errors==
 +
 
 +
With {{Function|Web|web:error}}, you can abort query evaluation, enforce a premature HTTP response and report errors back to the client:
 +
 
 +
<pre lang='xquery'>
 
declare
 
declare
   %rest:path("")
+
   %rest:path("/teapot")
  %output:media-type("xhtml")
+
function page:teapot() {
  %output:omit-xml-declaration("no")
+
   web:error(418, "I'm a pretty teapot")
  %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:start()
 
{
 
   <html xmlns="http://www.w3.org/1999/xhtml">
 
    <body>done</body>
 
  </html>
 
 
};
 
};
 
</pre>
 
</pre>
  
=Error Handling=
+
In contrast to the standard <code>fn:error</code> function, a status code can be supplied, and the response body will only contain the specified error message and no stack trace.
  
==XQuery Errors==
+
==Catch XQuery Errors==
  
XQuery runtime errors can be processed via ''error annotations''. A single argument must be supplied, which represents the QName of the error to be caught. A wildcard {{Code|*}} may be specified to catch all possible errors. A function can only have a single error annotation:
+
XQuery runtime errors can be processed via ''error annotations''. Error annotations have one or more arguments, which represent the error codes to be caught. The codes equal the names of the [[XQuery 3.0#Try.2FCatch|try/catch]] construct:
  
<pre class="brush:xquery">
+
{| class="wikitable"
declare %rest:error("*") function page:error() {
+
|- valign="top"
  "An error occurred while processing your RESTXQ code!"
+
! Precedence
};
+
! Syntax
</pre>
+
! Example
<!-- TODO how matching works -->
+
|- valign="top"
 +
| 1
 +
| <code>prefix:name</code><br/><code>Q{uri}name</code>
 +
| <code>err:FORG0001</code><br/><code><nowiki>Q{http://www.w3.org/2005/xqt-errors}FORG0001</nowiki></code>
 +
|- valign="top"
 +
| 2
 +
| <code>prefix:*</code><br/><code>Q{uri}*</code>
 +
| <code>err:*</code><br/><code><nowiki>Q{http://www.w3.org/2005/xqt-errors}*</nowiki></code>
 +
|- valign="top"
 +
| 3
 +
| <code>*:name</code>
 +
| <code>*:FORG0001</code>
 +
|- valign="top"
 +
| 4
 +
| <code>*</code>
 +
| <code>*</code>
 +
|}
 +
 
 +
All error codes that are specified for a function must have the same precedence.
 +
The following rules apply when catching errors:
 +
 
 +
* Codes with a higher precedence (smaller number) will be given preference.
 +
* A global RESTXQ error will be raised if two functions with conflicting codes are found.
  
The XQuery [[XQuery 3.0#Try.2FCatch|try/catch]] construct assigns error information to a number of pre-defined variables ({{Code|code}}, {{Code|description}}, {{Code|value}}, {{Code|module}}, {{Code|line-number}}, {{Code|column-number}}, {{Code|additional}}). These variables can be bound to variables via ''error parameter annotations'', which are specified the same way as [[#Query Parameters|query parameters]].
+
Similar to try/catch, the pre-defined variables ({{Code|code}}, {{Code|description}}, {{Code|value}}, {{Code|module}}, {{Code|line-number}}, {{Code|column-number}}, {{Code|additional}}) can be bound to variables via ''error parameter annotations'', which are specified the same way as [[#Query Parameters|query parameters]].
  
Errors may occur unexpectedly. However, they can also be triggered by a query, as the following example shows:
+
Errors may occur unexpectedly. However, they can also be triggered by a query, as demonstrated by the following example:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
declare %rest:path("/check/{$user}") function page:check($user) {
+
declare
 +
  %rest:path("/check/{$user}")
 +
function page:check($user) {
 
   if($user = ('jack', 'lisa'))
 
   if($user = ('jack', 'lisa'))
 
   then 'User exists'
 
   then 'User exists'
Line 414: Line 625:
 
};
 
};
  
declare %rest:error("err:user") %rest:error-param("description", "{$user}")
+
declare  
  function page:user-error($user) {
+
  %rest:error("err:user")
 +
  %rest:error-param("description", "{$user}")
 +
function page:user-error($user) {
 
   'User "' || $user || '" is unknown'
 
   'User "' || $user || '" is unknown'
 
};
 
};
 
</pre>
 
</pre>
  
==HTTP Errors==
+
==Catch HTTP Errors==
  
 
Errors that occur outside RESTXQ can be caught by adding {{Code|error-page}} elements with an error code and a target location to the {{Code|web.xml}} configuration file (find more details in the [http://www.eclipse.org/jetty/documentation/current/custom-error-pages.html Jetty Documentation]):
 
Errors that occur outside RESTXQ can be caught by adding {{Code|error-page}} elements with an error code and a target location to the {{Code|web.xml}} configuration file (find more details in the [http://www.eclipse.org/jetty/documentation/current/custom-error-pages.html Jetty Documentation]):
  
<pre class="brush:xml">
+
<pre lang="xml">
 
<error-page>
 
<error-page>
 
   <error-code>404</error-code>
 
   <error-code>404</error-code>
Line 431: Line 644:
 
</pre>
 
</pre>
  
The target location may be another RESTXQ function. The [[Request Module#request:attribute|request:attribute]] function can be used to request details on the caught error:
+
The target location may be another RESTXQ function. The {{Function|Request|request:attribute}} function can be used to request details on the caught error:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
declare %rest:path("/error404") function page:error404() {
 
declare %rest:path("/error404") function page:error404() {
 
   "URL: " || request:attribute("javax.servlet.error.request_uri") || ", " ||  
 
   "URL: " || request:attribute("javax.servlet.error.request_uri") || ", " ||  
Line 439: Line 652:
 
};
 
};
 
</pre>
 
</pre>
 +
 +
=User Authentication=
 +
 +
If you want to provide restricted access to parts of a web applications, you will need to check permissions before returning a response to the client. The [[Permissions]] layer is a nice abstraction for defining permission checks.
  
 
=Functions=
 
=Functions=
  
The [[Request Module]] contains functions for accessing data related to the current HTTP request. Two modules exist for setting and retrieving server-side session data of the current user ([[Session Module]]) and all users known to the HTTP server ([[Sessions Module]]). The [[RESTXQ Module]] provides functions for requesting RESTXQ base URIs and generating a [http://www.w3.org/Submission/wadl/ WADL description] of all services. Please note that the namespaces of all of these modules must be explicitly specified via module imports in the query prolog.
+
The [[Request Module]] contains functions for accessing data related to the current HTTP request. Two modules exist for setting and retrieving server-side session data of the current user ([[Session Module]]) and all users known to the HTTP server ([[Sessions Module]]). The [[RESTXQ Module]] provides functions for requesting RESTXQ base URIs and generating a [https://www.w3.org/Submission/wadl/ WADL description] of all services. Please note that the namespaces of all of these modules must be explicitly specified via module imports in the query prolog.
  
 
The following example returns the current host name:
 
The following example returns the current host name:
  
<pre class="brush:xquery">
+
<pre lang='xquery'>
 
import module namespace request = "http://exquery.org/ns/request";
 
import module namespace request = "http://exquery.org/ns/request";
  
Line 456: Line 673:
 
=References=
 
=References=
  
RESTXQ has been proposed by [http://www.adamretter.org.uk/ Adam Retter].
+
Documentation:
More information on all specifics can be found in the following two documents:
 
  
 +
* [http://exquery.org/spec/restxq RESTXQ Specification], First Draft
 
* [http://www.adamretter.org.uk/papers/restful-xquery_january-2012.pdf RESTful XQuery, Standardised XQuery 3.0 Annotations for REST]. Paper, XMLPrague, 2012
 
* [http://www.adamretter.org.uk/papers/restful-xquery_january-2012.pdf RESTful XQuery, Standardised XQuery 3.0 Annotations for REST]. Paper, XMLPrague, 2012
 
* [http://www.adamretter.org.uk/presentations/restxq_mugl_20120308.pdf RESTXQ]. Slides, MarkLogic User Group London, 2012
 
* [http://www.adamretter.org.uk/presentations/restxq_mugl_20120308.pdf RESTXQ]. Slides, MarkLogic User Group London, 2012
* [http://exquery.github.com/exquery/exquery-restxq-specification/restxq-1.0-specification.html RESTXQ Specification], Unofficial Draft
+
* [https://files.basex.org/publications/xmlprague/2013/Develop-RESTXQ-WebApps-with-BaseX.pdf Web Application Development]. Slides from XMLPrague 2013
* [http://files.basex.org/xmlprague2013/slides/Develop-RESTXQ-WebApps-with-BaseX.pdf Web Application, RESTXQ Development]. Web Application Development with RESTXQ Slides from XMLPrague 2013
+
 
 +
Examples:
 +
 
 +
* Sample code combining XQuery and JavaScript: [https://www.balisage.net/Proceedings/vol17/author-pkg/Galtman01/BalisageVol17-Galtman01.html Materials] and [https://www.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=
 
=Changelog=
 +
 +
;Version 11.0
 +
* Updated: [[#Forwards|Forwards]]: A log entry with the status code {{Code|204}} will be output.
 +
 +
;Version 9.6
 +
* Updated: [[#Response|Response]]: {{Code|Server-Timing}} HTTP header.
 +
 +
;Version 9.5
 +
* Updated: [[#Raise Errors|Raise Errors]]: Status code {{Code|400}} changed to {{Code|500}}, omit stack trace.
 +
 +
;Version 9.3
 +
* Updated: [[#Custom Methods|Custom Methods]]: Better support for the OPTIONS and HEAD methods.
 +
* Updated: [[#Catch XQuery Errors|XQuery Errors]]: Suppress stack trace and error code in the HTTP response.
 +
* Removed: {{Code|rest:redirect}} element ({{Function|Web|web:redirect}} can be used instead)
 +
 +
;Version 9.2
 +
* Updated: Ignore XQuery modules that cannot be parsed
 +
 +
;Version 9.0
 +
* Added: Support for server-side quality factors in the [[#Content Negotiation|<code>%rest:produces</code>]] annotation
 +
* Updated: Status code {{Code|410}} was replaced with {{Code|460}}
 +
* Removed: {{Code|restxq}} prefix
 +
 +
;Version 8.4
 +
* Added: <code>%rest:single</code> annotation
 +
 +
;Version 8.1
 +
* Added: support for input-specific content-type parameters
 +
* Added: <code>%input</code> annotations
 +
 +
;Version 8.0
 +
* Added: Support for regular expresssions in the [[#Paths|Path Annotation]]
 +
* Added: Evaluation of quality factors that are supplied in the [[#Content Negotiation|Accept header]]
 +
 +
;Version 7.9
 +
* Updated: [[#Catch XQuery Errors|XQuery Errors]], extended error annotations
 +
* Added: {{Code|%rest:method}}
  
 
;Version 7.7
 
;Version 7.7
 
 
* Added: [[#Error Handling|Error Handling]], [[#File Uploads|File Uploads]], [[#Multipart Types|Multipart Types]]
 
* Added: [[#Error Handling|Error Handling]], [[#File Uploads|File Uploads]], [[#Multipart Types|Multipart Types]]
 
* Updated: RESTXQ function may now also be specified in main modules (suffix: {{Code|*.xq}}).
 
* Updated: RESTXQ function may now also be specified in main modules (suffix: {{Code|*.xq}}).
 
* Updated: the RESTXQ prefix has been changed from {{Code|restxq}} to {{Code|rest}}.
 
* Updated: the RESTXQ prefix has been changed from {{Code|restxq}} to {{Code|rest}}.
 
* Updated: parameters are implicitly cast to the type of the function argument
 
* Updated: parameters are implicitly cast to the type of the function argument
* Updated: the RESTXQ root url has been changed to {{Code|http://localhost:8984/}}
+
* Updated: the RESTXQ root url has been changed to {{Code|http://localhost:8080/}}
  
 
;Version 7.5
 
;Version 7.5
 
 
* Added: new XML elements {{Code|<rest:redirect/>}} and {{Code|<rest:forward/>}}
 
* Added: new XML elements {{Code|<rest:redirect/>}} and {{Code|<rest:forward/>}}
 
[[Category:HTTP]]
 
[[Category:Developer]]
 

Latest revision as of 17:39, 1 December 2023

This page presents one of the Web Application services. It describes how to use the RESTXQ API of BaseX.

RESTXQ, introduced by Adam Retter, is an API that facilitates the use of XQuery as a server-side processing language for the Web. It has been inspired by the Java JAX-RS API: It provides a pre-defined set of XQuery 3.0 annotations for mapping HTTP requests to XQuery functions, which in turn generate and return HTTP responses.

BaseX provides various extensions to the original draft of the specification:

  • Multipart types are supported, including multipart/form-data
  • A %rest:error annotation can be used to catch XQuery errors
  • Servlet errors can be redirected to other RESTXQ pages
  • A RESTXQ Module provides some helper functions
  • Parameters are implicitly cast to the type of the function argument
  • The Path Annotation can contain regular expressions
  • %input annotations, support for input-specific content-type parameters
  • %rest:single annotation to cancel running RESTXQ functions
  • Quality factors in the Accept header will be evaluated
  • Support for server-side quality factors in the %rest:produces annotation
  • Better support for the OPTIONS and HEAD methods


Introduction[edit]

Preliminaries[edit]

The RESTXQ service is accessible via http://localhost:8080/.

All RESTXQ annotations are assigned to the http://exquery.org/ns/restxq namespace, which is statically bound to the 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.

If a RESTXQ URL is requested, the RESTXQPATH module directory and its subdirectories will be traversed, and all XQuery files will be parsed for functions with RESTXQ annotations. Subdirectories that include an .ignore file will be skipped.

To speed up processing, the functions of the existing XQuery modules are automatically cached in main memory:

  • Functions will be invalidated and parsed again if the timestamp of their module changes.
  • File monitoring can be adjusted via the PARSERESTXQ option. In productive environments with a high load, it may be recommendable to change the timeout, or completely disable monitoring.
  • If files are replaced while the web server is running, the RESTXQ module cache should be explicitly invalidated by calling the static root path /.init or by calling the rest:init function.

Examples[edit]

A first RESTXQ function is shown below:

module namespace page = 'http://basex.org/examples/web-page';

declare %rest:path("hello/{$who}") %rest:GET function page:hello($who) {
  <response>
    <title>Hello { $who }!</title>
  </response>
};

If the URI http://localhost:8080/hello/World is accessed, the result will be:

<response>
  <title>Hello World!</title>
</response>

The next function demonstrates a POST request:

declare
  %rest:path("/form")
  %rest:POST
  %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) {
  <response type='form'>
    <message>{ $message }</message>
    <user-agent>{ $agent }</user-agent>
  </response>
};

If you post something (e.g. using curl or the embedded form at http://localhost:8080/)...

curl -i -X POST --data "message='CONTENT'" http://localhost:8080/form

...you will receive something similar to the following result:

HTTP/1.1 200 OK
Content-Type: application/xml; charset=UTF-8
Content-Length: 107
Server: Jetty(8.1.11.v20130520)
<response type="form">
  <message>'CONTENT'</message>
  <user-agent>curl/7.31.0</user-agent>
</response>

Request[edit]

This section shows how annotations are used to handle and process HTTP requests.

Constraints[edit]

Constraints restrict the HTTP requests that a resource function may process.

Paths[edit]

A resource function must have a single Path Annotation with a single string as argument. The function will be called if a URL matches the path segments and templates of the argument. Path templates contain variables in curly brackets, and map the corresponding segments of the request path to the arguments of the resource function. The first slash in the path is optional.

The following example contains a path annotation with three segments and two templates. One of the function arguments is further specified with a data type, which means that the value for $variable will be cast to an xs:integer before being bound:

declare %rest:path("/a/path/{$with}/some/{$variable}")
  function page:test($with, $variable as xs:integer) { ... };

Variables can be enhanced by regular expressions:

(: Matches all paths with "app" as first, a number as second, and "order" as third segment :)
declare %rest:path("app/{$code=[0-9]+}/order")
  function page:order($code) { ... };

(: Matches all other all paths starting with "app/" :)
declare %rest:path("app/{$path=.+}")
  function page:others($path) { ... };

If multiple path candidates are found for the request, the one with more segments will be preferred.

Content Negotiation[edit]

Functions can be restricted to specific Media Types. The default type is */*. Multiple types can either be specified by a single or by multiple annotations.

Consuming Data[edit]

A function will only be taken into consideration if the HTTP Content-Type header of the request matches one of the given types:

declare
  %rest:POST("{$body}")
  %rest:path("/xml")
  %rest:consumes("application/xml")
  %rest:consumes("text/xml")
function page:xml($body) { $body };

Producing Data[edit]

A function will only be chosen if the HTTP Accept header of the request matches one of the given types:

declare
  %rest:path("/xml")
  %rest:produces("application/xml", "text/xml")
function page:xml() { <xml/> };

Note that the annotations will not affect the type of the actual response: You will need to supply an additional %output:media-type annotation or (if a single function may produce results of different types) generate an apt Custom Response.

Quality Factors[edit]

A client can supply quality factors to influence the server-side function selection process. If a client sends the following HTTP header with quality factors…

Accept: */*;q=0.5,text/html;q=1.0

…and if two RESTXQ functions exist for the addressed path with two different annotations for producing data…

declare function %rest:produces("text/html") ...
...
declare function %rest:produces("*/*") ...

…the first of these function will be chosen, as the quality factor for text/html documents is highest.

As we cannot ensure that the client may supply quality factors, the selection process can also be controlled server-side. The qs parameter can be attached server-side to the Media Type. If multiple functions are left in the selection process, the one with the highest quality factor will be favored:

declare function %rest:produces("application/json;qs=1") ...
...
declare function %rest:produces("*/*;qs=0.5") ...

HTTP Methods[edit]

Default Methods[edit]

The HTTP method annotations are equivalent to all HTTP request methods except TRACE and CONNECT. Zero or more methods may be used on a function; if none is specified, the function will be invoked for each method.

The following function will be called if GET or POST is used as request method:

declare %rest:GET %rest:POST %rest:path("/post")
  function page:post() { "This was a GET or POST request" };

The POST and PUT annotations may optionally take a string literal in order to map the HTTP request body to a function argument. Once again, the target variable must be embraced by curly brackets:

declare %rest:PUT("{$body}") %rest:path("/put")
  function page:put($body) { "Request body: " || $body };

Custom Methods[edit]

Custom HTTP methods can be specified with the %rest:method annotation. An optional body variable can be supplied as second argument:

declare
  %rest:path("binary-size")
  %rest:method("SIZE", "{$body}")
function page:patch(
  $body  as xs:base64Binary
) {
  "Request method: " || request:method(),
  "Size of body: " || bin:length($body)
};

If an OPTIONS request is received, and if no function is defined, an automatic response will be generated, which includes an Allow header with all supported methods.

If a HEAD request is received, and if no function is defined, the corresponding GET function will be processed, but the response body will be discarded.

Content Types[edit]

The body of a POST or PUT request will be converted to an XQuery item. Conversion can be controlled by specifying a content type. It can be further influenced by specifying additional content-type parameters:

Content-Type Parameters (;name=value) Type of resulting XQuery item
text/xml, application/xml document-node()
text/* xs:string
application/json JSON Options document-node() or map(*)
text/html HTML Options document-node()
text/comma-separated-values CSV Options document-node() or map(*)
others xs:base64Binary
multipart/* sequence (see next paragraph)

For example, if application/json;lax=yes is specified as content type, the input will be transformed to JSON, and the lax QName conversion rules will be applied, as described in the JSON Module.

Input options[edit]

Conversion options for JSON, CSV and HTML can also be specified via annotations with the input prefix. The following function interprets the input as text with the CP1252 encoding and treats the first line as header:

declare
  %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)
};

Multipart Types[edit]

The single parts of a multipart message are represented as a sequence, and each part is converted to an XQuery item as described in the last paragraph.

A function that is capable of handling multipart types is identical to other RESTXQ functions:

declare
  %rest:path("/multipart")
  %rest:POST("{$data}")
  %rest:consumes("multipart/mixed") (: optional :)
function page:multipart($data as item()*) {
  "Number of items: " || count($data)
};

Parameters[edit]

The following annotations can be used to bind request values to function arguments. Values will implicitly be cast to the type of the argument.

Query Parameters[edit]

The value of the first parameter, if found in the query component, will be assigned to the variable specified as second parameter. If no value is specified in the HTTP request, all additional parameters will be bound to the variable (if no additional parameter is given, an empty sequence will be bound):

declare
  %rest:path("/params")
  %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) }"/>
};

HTML Form Fields[edit]

Form parameters are specified the same way as query parameters:

%rest:form-param("city", "{$city}", "no-city-specified")

The values are the result of HTML forms submitted with the (default) content type application/x-www-form-urlencoded:

<form action="/process" method="POST" enctype="application/x-www-form-urlencoded">
  <input type="text" name="city"/>
  <input type="submit"/>
</form>

File Uploads[edit]

Files can be uploaded to the server by using the content type multipart/form-data (the HTML5 multiple attribute enables the upload of multiple files):

<form action="/upload" method="POST" enctype="multipart/form-data">
  <input type="file" name="files" multiple="multiple"/>
  <input type="submit"/>
</form>

The file contents are placed in a map, with the filename serving as key. The following example shows how uploaded files can be stored in a temporary directory:

declare
  %rest:POST
  %rest:path("/upload")
  %rest:form-param("files", "{$files}")
function page:upload($files) {
  for $name    in map:keys($files)
  let $content := $files($name)
  let $path    := file:temp-dir() || $name
  return (
    file:write-binary($path, $content),
    <file name="{ $name }" size="{ file:size($path) }"/>
  )
};

HTTP Headers[edit]

Header parameters are specified the same way as query parameters:

%rest:header-param("User-Agent", "{$user-agent}")
%rest:header-param("Referer", "{$referer}", "none")

Cookies[edit]

Cookie parameters are specified the same way as query parameters:

%rest:cookie-param("username", "{$user}")
%rest:cookie-param("authentication", "{$auth}", "no_auth")

Query Execution[edit]

In many web search scenarios, user input from browser forms is processed and search results are returned. Such operations can be made more interactive by sending a new search request to the server with each key click. However, this may lead to many parallel server-side requests, from which only the result of the last request will be relevant for the client.

With the %rest:single annotation, it can be enforced that only one instance of a function will run at the same time and for the same client. If the same function will be called for the second time, a currently executed query will be stopped, and the HTTP error code 460 will be returned instead:

(: If fast enough, returns the result. Otherwise, if called again, raises 460 :)
declare
  %rest:path("/search")
  %rest:query-param("term", "{$term}")
  %rest:single
function page:search($term as xs:string) {
  <ul>{
    for $result in db:get('large-db')//*[text() = $term]
    return <li>{ $result }</li>
  }</ul>
};

By adding a string value to with the annotation, functions can be bundled together, and a running query can be canceled by calling another one that has the same annotation value. 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 460, and the second one will return <xml>stopped</xml>.

declare 
  %rest:path("/compute")
  %rest:single("EXPENSIVE")
function local:compute() {
  (1 to 100000000000000)[. = 0]
};

declare 
  %rest:path("/stop")
  %rest:single("EXPENSIVE")
function local:stop() {
  <xml>stopped</xml>
};

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 abort a query if it is currently updating the database or performing 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 current session. This way, 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 initial session data was returned to the client.

Response[edit]

By default, a successful request is answered with the HTTP status code 200 (OK) and is followed by the given content. An erroneous request leads to an error code and an optional error message (e.g. 404 for “resource not found”).

A Server-Timing HTTP header is attached to each response. It indicates how much time was spent for parsing, compiling, evaluating and serializing the query. The last value will not necessarily reflect the full time for serializing the result, as the header is generated before the result is sent to the client. Server-side serialization can be enforced by annotating a function with the %rest:single annotation.

Custom Response[edit]

Custom responses can be generated in XQuery by returning an rest:response element, an http:response child node that matches the syntax of the EXPath HTTP Client Module specification, and optional child nodes that will be serialized as usual. A function that yields a response on an unknown resource may look as follows:

declare %output:method("text") %rest:path("") function page:error404() {
  <rest:response>
    <http:response status="404">
      <http:header name="Content-Language" value="en"/>
      <http:header name="Content-Type" value="text/plain; charset=utf-8"/>
    </http:response>
  </rest:response>,
  "The requested resource is not available."
};

For the time being, it is not possible to create multipart responses.

Forwards and Redirects[edit]

Redirects[edit]

The server can invite the client (e.g., the web browser) to make a second request to another URL by sending a 302 response:

<rest:response>
  <http:response status="302">
    <http:header name="Location" value="new-location"/>
  </http:response>
</rest:response>

The convenience function web:redirect can be called to create such a response.

In the XQuery context, redirects are particularly helpful if Updates are performed. An updating request may send a redirect to a second function that generates a success message, or evaluates an updated database:

declare %updating %rest:path('/app/init') function local:create() {
  db:create('app', <root/>, 'root.xml'),
  db:output(web:redirect('/app/ok'))
};

declare %rest:path('/app/ok') function local:ok() {
  'Stored documents: ' || count(db:get('app'))
};

Forwards[edit]

A server-side redirect is called forwarding. It reduces traffic among client and server, and the forwarding will not change the URL seen from the client’s perspective:

<rest:forward>new-location</rest:forward>

The response can also be created with the convenience function web:forward.

With Version 11, a log entry with the status code 204 will be output before the forwarding takes place.

Output[edit]

The content-type of a response can be influenced by the user via Serialization Parameters. The steps are described in the REST chapter. In RESTXQ, serialization parameters can be specified in the query prolog, via annotations, or within the REST response element:

Query Prolog[edit]

In main modules, serialization parameters may be specified in the query prolog. These parameters will then apply to all functions in a module. In the following example, the content type of the response is overwritten with the media-type parameter:

declare option output:media-type 'text/plain';

declare %rest:path("version1") function page:version1() {
  'Keep it simple, stupid'
};

Annotations[edit]

Global serialization parameters can be overwritten via %output annotations. The following example serializes XML nodes as JSON, using the JsonML format:

declare
  %rest:path("cities")
  %output:method("json")
  %output:json("format=jsonml")
function page:cities() {
  element cities {
    db:get('factbook')//city/name
  }
};

The next function, when called, generates XHTML headers, and text/html will be set as content type:

declare
  %rest:path("done")
  %output:method("xhtml")
  %output:omit-xml-declaration("no")
  %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>
  </html>
};

Response Element[edit]

Serialization parameters can also be specified in a REST reponse element in a query. Serialization parameters will be overwritten:

declare %rest:path("version3") function page:version3() {
  <rest:response>
    <output:serialization-parameters>
      <output:media-type value='text/plain'/>
    </output:serialization-parameters>
  </rest:response>,
  'Not that simple anymore'
};

Error Handling[edit]

If an error is raised when RESTXQ code is parsed, compiled or evaluated, an HTTP response with the status code 500 is generated.

By default, all server-side errors will be passed on to the client. This is particularly helpful during the development process. In a productive environment, however, it is advisable not to expose errors to the client. This can be realized via the RESTXQERRORS option. If disabled,

  • XQuery modules that cannot be parsed will be ignored and
  • full error messages and stack traces will be suppressed and not included in the HTTP response.

The full error information can still be looked up in the database logs.

Raise Errors[edit]

With web:error, you can abort query evaluation, enforce a premature HTTP response and report errors back to the client:

declare
  %rest:path("/teapot")
function page:teapot() {
  web:error(418, "I'm a pretty teapot")
};

In contrast to the standard fn:error function, a status code can be supplied, and the response body will only contain the specified error message and no stack trace.

Catch XQuery Errors[edit]

XQuery runtime errors can be processed via error annotations. Error annotations have one or more arguments, which represent the error codes to be caught. The codes equal the names of the try/catch construct:

Precedence Syntax Example
1 prefix:name
Q{uri}name
err:FORG0001
Q{http://www.w3.org/2005/xqt-errors}FORG0001
2 prefix:*
Q{uri}*
err:*
Q{http://www.w3.org/2005/xqt-errors}*
3 *:name *:FORG0001
4 * *

All error codes that are specified for a function must have the same precedence. The following rules apply when catching errors:

  • Codes with a higher precedence (smaller number) will be given preference.
  • A global RESTXQ error will be raised if two functions with conflicting codes are found.

Similar to try/catch, the pre-defined variables (code, description, value, module, line-number, column-number, additional) can be bound to variables via error parameter annotations, which are specified the same way as query parameters.

Errors may occur unexpectedly. However, they can also be triggered by a query, as demonstrated by the following example:

declare
  %rest:path("/check/{$user}")
function page:check($user) {
  if($user = ('jack', 'lisa'))
  then 'User exists'
  else fn:error(xs:QName('err:user'), $user)
};

declare 
  %rest:error("err:user")
  %rest:error-param("description", "{$user}")
function page:user-error($user) {
  'User "' || $user || '" is unknown'
};

Catch HTTP Errors[edit]

Errors that occur outside RESTXQ can be caught by adding error-page elements with an error code and a target location to the web.xml configuration file (find more details in the Jetty Documentation):

<error-page>
  <error-code>404</error-code>
  <location>/error404</location>
</error-page>

The target location may be another RESTXQ function. The request:attribute function can be used to request details on the caught error:

declare %rest:path("/error404") function page:error404() {
  "URL: " || request:attribute("javax.servlet.error.request_uri") || ", " || 
  "Error message: " || request:attribute("javax.servlet.error.message")
};

User Authentication[edit]

If you want to provide restricted access to parts of a web applications, you will need to check permissions before returning a response to the client. The Permissions layer is a nice abstraction for defining permission checks.

Functions[edit]

The Request Module contains functions for accessing data related to the current HTTP request. Two modules exist for setting and retrieving server-side session data of the current user (Session Module) and all users known to the HTTP server (Sessions Module). The RESTXQ Module provides functions for requesting RESTXQ base URIs and generating a WADL description of all services. Please note that the namespaces of all of these modules must be explicitly specified via module imports in the query prolog.

The following example returns the current host name:

import module namespace request = "http://exquery.org/ns/request";

declare %rest:path("/host-name") function page:host() {
  'Remote host name: ' || request:remote-hostname()
};

References[edit]

Documentation:

Examples:

  • Sample code combining XQuery and JavaScript: Materials and paper from Amanda Galtman, Balisage 2016.
  • DBA: The Database Administration interface, bundled with the full distributions of BaseX.

Changelog[edit]

Version 11.0
  • Updated: Forwards: A log entry with the status code 204 will be output.
Version 9.6
  • Updated: Response: Server-Timing HTTP header.
Version 9.5
  • Updated: Raise Errors: Status code 400 changed to 500, omit stack trace.
Version 9.3
  • Updated: Custom Methods: Better support for the OPTIONS and HEAD methods.
  • Updated: XQuery Errors: Suppress stack trace and error code in the HTTP response.
  • Removed: rest:redirect element (web:redirect can be used instead)
Version 9.2
  • Updated: Ignore XQuery modules that cannot be parsed
Version 9.0
  • Added: Support for server-side quality factors in the %rest:produces annotation
  • Updated: Status code 410 was replaced with 460
  • Removed: restxq prefix
Version 8.4
  • Added: %rest:single annotation
Version 8.1
  • Added: support for input-specific content-type parameters
  • Added: %input annotations
Version 8.0
Version 7.9
  • Updated: XQuery Errors, extended error annotations
  • Added: %rest:method
Version 7.7
  • Added: Error Handling, File Uploads, Multipart Types
  • Updated: RESTXQ function may now also be specified in main modules (suffix: *.xq).
  • Updated: the RESTXQ prefix has been changed from restxq to rest.
  • Updated: parameters are implicitly cast to the type of the function argument
  • Updated: the RESTXQ root url has been changed to http://localhost:8080/
Version 7.5
  • Added: new XML elements <rest:redirect/> and <rest:forward/>