Difference between revisions of "RESTXQ"

From BaseX Documentation
Jump to navigation Jump to search
m (moved RESTfulXQ to RESTXQ)
Line 1: Line 1:
This page is part of the [[Developer Section]]. It describes how to use the RESTfulXQ API of BaseX.
+
This page is part of the [[Developer Section]]. It describes how to use RESTXQ of BaseX.
 
 
At XML Prague Conference 2012, Adam Retter introduced RESTfulXQ. It adds webapplication development power to XQuery.
 
 
 
As of {{Version|7.2}} RESTfulXQ is supported by BaseX.
 
  
 +
RESTXQ is a new standard API with RESTful XQuery Annotations, presented by
 +
Adam Retter at this year's XML Prague Conference and MarkLogic User Group.
 +
It further simplifies web application development in XQuery.
  
 +
As of {{Version|7.2}}, RESTfulXQ is supported by BaseX.
  
 
=Getting started=
 
=Getting started=
  
<!-- TODO add how to run without github package -->
+
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]]).
  
Get a fresh copy of [https://github.com/basexdb/basex-api basex-api] from our github repositories. It contains the base server architecure. Refer to  [[Startup#BaseX HTTP Server|BaseX HTTP Server]] for details.
+
By default, the HTTP Server will be accessible at http://localhost:8984.
 +
<!-- [CG] I guess we need another general page on the HTTP Server and the Servlet that summarizes all this information...
 +
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 server, there are two possibilities.
+
To start the HTTP Server, there are two possibilities:
 
 
* Inside the basex-api directory run the command <code>mvn jetty:run</code>. (stop with <code>CTRL + C</code>)
 
* Run the script <code>etc/basexhttp</code> (inside basex-api package). There is a script to stop the server, located in the same folder.
 
 
 
By default the server will be accessible at http://localhost:8984/. The serverroot is <code>src/main/webapp</code>.
 
  
 +
* 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=
 
=Server configuration=
With the first serverrun a configuration file will be put to the serverroot (by default <code>src/main/webapp/.basex</code>). Other configurations may be done within
 
<code>src/main/webapp/WEB-INF/web.xml</code>.
 
  
E.G. in <code>.basex</code> pathes for database (variable <code>DBPATH</code>), RESTfulXQ modules (variable <code>HTTPPATH</code>) and [[Packaging | BaseX Package]] repository (variable <code>REPOPATH</code>) are declared.
+
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.
  
 +
=RESTXQ Modules=
  
 +
All RESTXQ modules can be accessed at {{Mono|http://localhost:8984/restxq/}}. Some instructions on how to write such a module follow:
  
=RESTfulXQ Modules=
+
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.
  
All RESTfulXQ modules can be accessed at http://localhost:8984/restxq/. It follows instructions on how to write such a module.
+
<pre class="brush:xquery">(:~ A simple module with REST-annotations :)
 +
module namespace hw = 'http://basex.org/modules/restxq-demo';
  
To a regular model you add a namespace for REST annotations. With <code>%</code> followed by the namespace one applies REST annotations to existing functions. These annotations define when a modules function should be invoked and with what arguments.
+
declare namespace rest = 'http://exquery.org/ns/rest/annotation';
 
 
<pre class="brush:xquery">(:~
 
: A simple module with REST-annotations
 
:)
 
module namespace hw = 'http://basex.org/modules/restxq-demo';
 
declare namespace rest-hw = 'http://exquery.org/ns/rest/annotation';
 
  
 
declare
 
declare
%rest-hw:path("{$path}")
+
  %rest:path("{$path}")
%output:media-type("application/xml")
+
  %output:media-type("application/xml")
 
function hw:demo($path as xs:string) as document-node() {
 
function hw:demo($path as xs:string) as document-node() {
&lt;xml&gt;
+
  &lt;xml&gt;
Hello World! You accessed the path {$path}.
+
    Hello World! You accessed the path { $path }.
&lt;/xml&gt;
+
  &lt;/xml&gt;
 
};</pre>
 
};</pre>
  
If you accessed the server at localhost:8984/restxq/demo-module you would see
+
If the URI {{Mono|localhost:8984/restxq/demo-module}} is accessed, the result will be
  
 
<pre class="brush:xml">&lt;xml&gt;
 
<pre class="brush:xml">&lt;xml&gt;
Hello World! You accessed the path demo-module.
+
  Hello World! You accessed the path demo-module.
 
&lt;/xml&gt;</pre>
 
&lt;/xml&gt;</pre>
 
 
  
 
=RESTful Annotations=
 
=RESTful Annotations=

Revision as of 23:22, 16 March 2012

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

RESTXQ is a new standard API with RESTful XQuery Annotations, presented by Adam Retter at this year's XML Prague Conference and MarkLogic User Group. It further simplifies web application development in XQuery.

As of Version 7.2, RESTfulXQ 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 HTTP Server will be accessible at http://localhost:8984.

Server configuration

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. Initial configuration options can be adjusted in the src/main/webapp/WEB-INF/web.xml file.

RESTXQ Modules

All RESTXQ modules can be accessed at http://localhost:8984/restxq/. Some instructions on how to write such a module follow:

A regular module contains a declaration to the rest namespace http://exquery.org/ns/rest/annotation. So-called REST annotations are introduced with the annotation character %, 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.

(:~ 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>

RESTful Annotations

A List of all implemented annotations. As sample namespace for REST annotations rest is used.


Path Annotation (constraint)

%rest:path("/a/path/{$with}/some/{$variable}") if 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 is allowed to have at least one path annotation.


HTTP Method Annotations (constraints)

  • Simple Method Annotations

%rest:GET %rest:HEAD %rest:DELETE


  • Content Method Annotations

%rest:POST %rest:POST("{$post-body}") %rest:PUT %rest:PUT("{$put-body}")


Media Type Annotations (constraint)

  • HTTP Content Type
    %rest:consumes() one or more media-types as string. e.g.%rest:consumes("application/xml", "text/xml")
  • HTTP Accept
    %rest:produces() one media-type as string. e.g. %rest:produces("application/atom+xml")

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

Query String Annotations (Parameter)

%rest:query-param() Assigns first parameter to the variable in second parameter if found in the Query String. As optional third parameter a default value may be given. Is no default set and the parameter not contained in the Query-String, the REST annotation will be ignored. The variable will be type-casted to match the function declaration.

Examples

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


HTML Form field Annotations (Parameter)

%rest:form-param() same usage as #Query String Annotations (Parameter), but extracted from POST or GET.


HTTP Header Annotations (Parameter)

%rest:header-param() same usage as #Query String Annotations (Parameter), but extracted from HTTP Header.

Examples

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

Cookie Annotations (Parameter)

%rest:cookie-param() same usage as #Query String Annotations (Parameter), but extracted from Cookie.

Examples

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