Changes

Jump to navigation Jump to search
1,065 bytes added ,  16:19, 21 May 2018
m
update custom response according to spec. Hopefully BaseX implemented it this way
* Parameters are implicitly cast to the type of the function argument
* The [[#Paths|Path Annotation]] can contain regular expressions
* Quality factors in the [[#Content Negotiation|Accept header]] will be evaluated
* <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
* {{Version|9.0}}: Support for server-side quality factors in the [[#Content Negotiation|<code>%rest:produces</code>]] annotation
<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.
If a RESTXQ URL is requested, the [[Options#RESTXQPATH{{Option|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. Sub-directories that include an {{Code|.ignore}} file will be skipped.
To speed up processing, the functions of parsed the existing XQuery modules will are automatically be cached in main memory. A cached module :* Functions will be invalidated and parsed again if its the timestamp of their module changes. The timestamp check * File monitoring can be disabled by enabling adjusted via the [[Options#CACHERESTXQ{{Option|CACHERESTXQ]] PARSERESTXQ}} option. This is recommedable for In productive environments with a high load, it may be recommendable to change the timeout, or completely disable monitoring. * If files are all the same replaced while the web server is running, 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.
==Examples==
Two following annotations can be used to restrict functions to specific content types:
* '''HTTP Content Types''': a A function will only be invoked if the HTTP {{Code|Content-Type}} header of the request matches one of the given mime content types. Example:
<pre class="brush:xquery">%rest:consumes("application/xml", "text/xml")</pre>
* '''HTTP Accept''': a A function will only be invoked if the HTTP {{Code|Accept}} header of the request matches one of the defined mime content types. Example:
<pre class="brush:xquery">%rest:produces("application/atom+xml")</pre>
By default, both mime content types are {{Code|*/*}}. Quality factors supplied by a client will also be considered in the path selection process.If a client supplies the following accept header…
<pre>
</pre>
…and if two RESTXQ functions exist with the same {{Code|path}} annotation and , one with the {{Code|produces}} annotations annotation <code>*/*</code> , and another with <code>text/html</code>, respectively, the second function with the second annotation will be called, because the quality factor for <code>text/html</code> documents is higher than highest. With {{Version|9.0}}, support for server-side quality factors was added. If multiple function candidates are left over after the above steps, the one for arbitrary other mime types<code>qs</code> parameter will be considered. The function with the highest quality factor will be favored: <pre class="brush:xquery">%rest:produces("text/html;qs=1")%rest:produces("*/*;qs=0.8")</pre>
Note that this the annotation will ''not'' affect the content-type of the HTTP actual response. Instead, you You will need to add a supply an additional <code>[[#Output|%output:media-type]]</code> annotation.
===HTTP Methods===
==Query Execution==
 
{{Mark|Updated with Version 9.0:}} The old status code {{Code|410}} (which indicates to the client that a resource is permanently removed) was replaced with {{Code|460}}.
In many RESTXQ search scenarios, input from browser forms is processed and search results are returned. User experience can generally be made more interactive if an updated search request is triggered with each key click. However, this may lead to 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. If the same function will be called for the second time, the already running query will be stopped, and the HTTP error code {{Code|410460}} (Gone) will be returned instead:
<pre class="brush:xquery">
(: If fast enough, returns the result. Otherwise, if called again, raises 410 460 :)
declare
%rest:path("/search")
By specifying a string along with the annotation, functions can be bundled together, 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>410460</code>, and the second one will return <xml>stopped</xml>.
<pre class="brush:xquery">
declare %rest:path("") function page:error404() {
<rest:response>
<http:response status="404" messagereason="I was not found.">
<http:header name="Content-Language" value="en"/>
<http:header name="Content-Type" value="text/html; charset=utf-8"/>
};
</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. With {{Version|9.0}} of BaseX, a [[Permissions]] layer was introduced to facilitate permission checks.
=Functions=
=References=
Currently, the following external resources on RESTXQ existDocumentation:
* [http://exquery.org/spec/restxq RESTXQ Specification], First Draft
* [http://files.basex.org/publications/xmlprague/2013/Develop-RESTXQ-WebApps-with-BaseX.pdf Web Application Development]. Slides from XMLPrague 2013
External examplesExamples: * Sample code combining XQuery and JavaScript: [https://sharepoint.mathworks.com/dev/doc/kb/_layouts/15/WopiFrame2.aspx?sourcedoc=%7bd3016bb5-9ec5-4289-ba13-0b35636982b1%7dhttphttp://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=
 
;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
administrator, editor
1

edit

Navigation menu