Difference between revisions of "RESTXQ"

From BaseX Documentation
Jump to navigation Jump to search
Line 2: Line 2:
  
 
RESTXQ, specified by [http://www.adamretter.org.uk/ Adam Retter], is a new API that facilitates the use of XQuery
 
RESTXQ, specified by [http://www.adamretter.org.uk/ Adam Retter], is a new API that facilitates the use of XQuery
as a Server Side processing language for the Web whilst maintaining portability. It has been largely inspired by
+
as a Server Side processing language for the Web whilst maintaining portability. RESTXQ has been largely inspired by
Java’s [http://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services JAX-RS API], and it defines a pre-defined
+
Java’s [http://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services JAX-RS API]: it defines a pre-defined set of
set of XQuery 3.0 annotations for mapping HTTP requests to XQuery functions.
+
XQuery 3.0 annotations for mapping HTTP requests to XQuery functions, which in turn generate and return
As of {{Version|7.2}}, RESTXQ is supported by BaseX.
+
HTTP responses. As of {{Version|7.2}}, RESTXQ is supported by BaseX.
  
 
=Getting started=
 
=Getting started=
  
 +
<!-- [CG] I guess we need another general page on the HTTP Server and the Servlet that summarizes all this information... -->
 
First of all, launch the [[Startup#BaseX HTTP Server|BaseX HTTP Server]], which will itself start an instance of the [http://jetty.codehaus.org/jetty/ Jetty WebServer], which listens to the port <code>8984</code> by default (check out the additional [[Startup Options#BaseX HTTP Server|command-line options]]).
 
First of all, launch the [[Startup#BaseX HTTP Server|BaseX HTTP Server]], which will itself start an instance of the [http://jetty.codehaus.org/jetty/ Jetty WebServer], which listens to the port <code>8984</code> by default (check out the additional [[Startup Options#BaseX HTTP Server|command-line options]]).
  
By default, the HTTP Server will be accessible at http://localhost:8984.
+
By default, the RESTXQ service is available via {{Mono|http://localhost:8984/restxq/}}.  
<!-- [CG] I guess we need another general page on the HTTP Server and the Servlet that summarizes all this information...
+
If the server is started as Servlet, the {{Mono|.basex}} configuration file will be stored in the root of the web directory (usually {{Mono|src/main/webapp/}}). The .basex file contains all [[Options#Main Options|Main Options]], such as the path to the database, the HTTP directory and the [[Packaging|Package Repository]]. The initial configuration options can be adjusted in the {{Mono|src/main/webapp/WEB-INF/web.xml}} file.
Get a fresh copy of [https://github.com/basexdb/basex-api basex-api] from our github repositories. It contains the base server architecture. Refer to  [[Startup#BaseX HTTP Server|BaseX HTTP Server]] for details.
 
 
 
To start the HTTP Server, there are two possibilities:
 
 
 
* Inside the {{Mono|basex-api}} directory, run the command {{Mono|mvn jetty:run}}. (stop with {{Mono|CTRL + C}})
 
* Run the script {{Mono|etc/basexhttp}} (inside the {{Mono|basex-api}} package). There is a script to stop the server, located in the same folder.
 
 
 
By default, the HTTP Server will be accessible at http://localhost:8984. The server root is <code>src/main/webapp</code>.
 
-->
 
=Server configuration=
 
 
 
If the server is started as Servlet, the {{Mono|.basex}} configuration file will be stored in the root of the web directory. (usually {{Mono|src/main/webapp/}}). The .basex file contains all [[Options#Main Options|Main Options]], such as the path to the database, the HTTP directory and the [[Packaging|Package Repository]]. Initial configuration options can be adjusted in the {{Mono|src/main/webapp/WEB-INF/web.xml}} file.
 
  
 
=Module Declarations=
 
=Module Declarations=
  
All RESTXQ modules can be accessed at {{Mono|http://localhost:8984/restxq/}}. Some instructions on how to write such a module follow:
+
A regular module contains a declaration to the RESTXQ namespace {{Mono|http://exquery.org/ns/rest/annotation}}. RESTXQ annotations are introduced with the annotation character <code>%</code>, followed by the declared prefix assigned to the RESTXQ namespace. These annotations define when a modules function will be invoked and which values will be bound to its arguments. A RESTXQ module may look as follows:
 
 
A regular module contains a declaration to the rest namespace {{Mono|http://exquery.org/ns/rest/annotation}}. So-called REST annotations are introduced with the annotation character <code>%</code>, followed by the declared rest prefix. These annotations define when a modules function will be invoked and which values will be bound to its arguments.
 
  
 
<pre class="brush:xquery">(:~ A simple module with REST-annotations :)
 
<pre class="brush:xquery">(:~ A simple module with REST-annotations :)

Revision as of 20:07, 17 March 2012

This page is part of the Developer Section. It describes how to use RESTXQ of BaseX.

RESTXQ, specified by Adam Retter, is a new API that facilitates the use of XQuery as a Server Side processing language for the Web whilst maintaining portability. RESTXQ has been largely inspired by Java’s 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. As of Version 7.2, RESTXQ is supported by BaseX.

Getting started

First of all, launch the BaseX HTTP Server, which will itself start an instance of the Jetty WebServer, which listens to the port 8984 by default (check out the additional command-line options).

By default, the RESTXQ service is available via http://localhost:8984/restxq/. If the server is started as Servlet, the .basex configuration file will be stored in the root of the web directory (usually src/main/webapp/). The .basex file contains all Main Options, such as the path to the database, the HTTP directory and the Package Repository. The initial configuration options can be adjusted in the src/main/webapp/WEB-INF/web.xml file.

Module Declarations

A regular module contains a declaration to the RESTXQ namespace http://exquery.org/ns/rest/annotation. RESTXQ annotations are introduced with the annotation character %, followed by the declared prefix assigned to the RESTXQ namespace. These annotations define when a modules function will be invoked and which values will be bound to its arguments. A RESTXQ module may look as follows:

(:~ A simple module with REST-annotations :)
module namespace hw = 'http://basex.org/modules/restxq-demo';

declare namespace rest = 'http://exquery.org/ns/rest/annotation';

declare
  %rest:path("{$path}")
  %output:media-type("application/xml")
function hw:demo($path as xs:string) as document-node() {
  <xml>
    Hello World! You accessed the path { $path }.
  </xml>
};

If the URI localhost:8984/restxq/demo-module is accessed, the result will be

<xml>
  Hello World! You accessed the path demo-module.
</xml>

Annotations

This section lists all annotations provided by RESTXQ. rest is used as namespace prefix.

Constraints

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

Paths

%rest:path("/a/path/{$with}/some/{$variable}")

If a URL matches the given pattern, variables (in curly brackets) will be assigned. The variables serve as input arguments for the function. The type will be converted, as defined by the function.

A function must have exactly one path annotation.

HTTP Methods

  • Simple Method Annotations
%rest:GET
%rest:HEAD
%rest:DELETE
  • Content Method Annotations
%rest:POST
%rest:POST("{$post-body}")
%rest:PUT
%rest:PUT("{$put-body}")

Content Types

  • HTTP Content Types: One or more media-types may be specified as strings, e.g.:
%rest:consumes("application/xml", "text/xml")
  • HTTP Accept: One or more media-types may be specified as strings, e.g.:
%rest:produces("application/atom+xml")

These default to */* if no media-type annotations are given.

Parameters

Parameters are optional annotations that can be used to bind additional values to function arguments:

Query Strings

The value of the first parameter, if found in the Query String, will be assigned to the variable specified as second parameter. Optionally, a third parameter may be specified as default:

%rest:query-param("parameter", "{$value}", "default")
%rest:query-param("answer", "{$answer}", 42)
%rest:query-param("search", "{$search-param}")

HTML Form Fields

Form parameters are specified the same way as query strings. Their values are extracted from GET or POST requests.

%rest:form-param("parameter", "{$value}", "default")

HTTP Headers

Header parameters are specified the same way as query strings:

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

Cookies

Cookie parameters are specified the same way as query strings:

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

References

RESTXQ has been proposed by Adam Retter. More information on all specifics can be found in the following two documents: