This page was generated
March  13,  2012
4:49  AM
XQuery & XSLT Built-In & Modules Function Reference

Module: rest Library

The REST library enables you to create RESTful functions that are independent of the language used in applications.

The REST library consists of a set of XQuery functions that support URL rewriting and endpoint validation and a MarkLogic REST vocabulary that simplifies the task of describing web service endpoints. The REST vocabulary is used to write declarative descriptions of the endpoints. These descriptions include the mapping of URL parts to parameters and conditions that must be met in order for the incoming request to be mapped to an endpoint.

The REST library is installed as the following file:

install_dir/Modules/MarkLogic/appservices/utils/rest.xqy

where install_dir is the directory in which MarkLogic Server is installed.

To use the rest.xqy module in your own XQuery modules, include the following line in your XQuery prolog:

  
  import module namespace rest = "http://marklogic.com/appservices/rest"  
      at "/MarkLogic/appservices/utils/rest.xqy";
  

The library uses the rest: namespace, which is not predefined in the server.

Function Summary
rest:check-options This function validates the specified options node.
rest:check-request This function takes a request element and returns a report of the problems found.
rest:get-raw-query-params This function extracts all of the query parameters and returns them in a map.
rest:matching-request This function returns the request element in the options node that matches the specified URI.
rest:process-request This function is used in the endpoint main module to parse the incoming request against the options.
rest:report-error This function formats the specified error structure.
rest:rewrite This function is used in the URL rewriter to map the incoming request to an endpoint.
Function Detail
rest:check-options(
$options as element(rest:options)
)  as   element(rest:report)*
Summary:

This function validates the specified options node. Validation includes both schema validation and some additional rule-based validation. An empty sequence indicates valid options and any problems are reported via rest:report elements.

Parameters:
$options : The options node to be validated.

Example:
  xquery version "1.0-ml"; 
 
  import module namespace rest="http://marklogic.com/appservices/rest"
      at "/MarkLogic/appservices/utils/rest.xqy";

      try {
         rest:check-options(
           <options xmlns="http://marklogic.com/appservices/rest">
              <request uri="^/(.+)$" endpoint="/endpoint.xqy">
                <uri-param name="play">$1.xml</uri-param>
              </request>
           </options>)
    } catch ($e) {
       rest:report-error($e)
    }
    

rest:check-request(
$options as element(rest:request)
)  as   element(rest:report)?
Summary:

This function takes a request element and returns a report of the problems found. If this function does not return an empty sequence, you have made a mistake and the library will not perform reliably.

Parameters:
$options : The options node that defines the request.

Example:
  xquery version "1.0-ml"; 
 
  import module namespace rest="http://marklogic.com/appservices/rest"
      at "/MarkLogic/appservices/utils/rest.xqy";

  rest:check-request(
    <request uri="^/(.+)$" 
             endpoint="/endpoint.xqy" 
             xmlns="http://marklogic.com/appservices/rest">
      <uri-param name="play">$1.xml</uri-param>
    </request>)

 
    

rest:get-raw-query-params(  ) as  map:map
Summary:

This function extracts all of the query parameters and returns them in a map. This does not include the parameters that would be derived from matching the URI string. No error checking or type conversion is performed by this function. The parameters returned by this function are all strings, as they are not type checked.

Example:
  xquery version "1.0-ml"; 
 
  import module namespace rest="http://marklogic.com/appservices/rest"
     at "/MarkLogic/appservices/utils/rest.xqy";

  rest:get-raw-query-params ( )

  (: Returns a map of the query parameters. :)
    

rest:matching-request(
$options as element(rest:options),
[$uri as xs:string],
[$method as xs:string],
[$accept-headers as xs:string],
[$user-params as map:map]
)  as   element(rest:request)?
Summary:

This function returns the request element in the options node that matches the specified URI. If you only specify options parameter, then the current environment is used for all the other parameters.

Parameters:
$options : The options node.
$uri (optional): The URI with which to locate the matching request element.
$method (optional): The HTTP method to match with the request element.
$accept-headers (optional): The accept header. The Accept request-header to match any accept conditions in the request element.
$user-params (optional): The user parameters to match with those in the request element.

Example:
  xquery version "1.0-ml"; 
 
  import module namespace rest="http://marklogic.com/appservices/rest"
  at "/MarkLogic/appservices/utils/rest.xqy";

  declare option xdmp:mapping "false";

  let $options :=
    <options xmlns="http://marklogic.com/appservices/rest">
      <request uri="^/shakespeare/(.+)/(.+)" endpoint="/redirect.xqy">
        <uri-param name="__ml_redirect__">/$1/$2</uri-param>
      </request>
      <request uri="^/shakespeare/(.+)" endpoint="/redirect.xqy">
        <uri-param name="__ml_redirect__">/$1</uri-param>
      </request>
      <request uri="^/(.+)/act(\d+)" endpoint="/endpoint.xqy">
        <uri-param name="play">$1.xml</uri-param>
        <uri-param name="act" as="integer">$2</uri-param>
        <param name="scene" as="integer" values="1|2|3" default="1"/>
      </request>
      <request uri="^/(.+)$" endpoint="/endpoint.xqy">
        <uri-param name="play">$1.xml</uri-param>
      </request>
      <request uri="^(.+)$" endpoint="/options.xqy" user-params="allow">
        <uri-param name="__ml_options__">$1</uri-param>
        <http method="OPTIONS"/>
      </request>
    </options>

  let $uri     := "/shakespeare/hamlet"
  let $accept  := xdmp:get-request-header("Accept") 
  let $params  := map:map()

  return rest:matching-request($options, $uri, "GET", $accept, $params)

  (: Returns the request node that matches the URI, "/shakespeare/hamlet". :)
    

rest:process-request(
$request as element(rest:request)
)  as   map:map
Summary:

This function is used in the endpoint main module to parse the incoming request against the options. It returns a map that contains all of the parameters as typed values. Processing the request also checks all of the associated conditions and will raise an error if any condition is not met.

If the request is processed successfully, then all of the conditions have been met and the returned map contains all of the parameters. If not, an error occurs.


Parameters:
$request : The request to be processed into a map.

Example:
  xquery version "1.0-ml"; 
 
  import module namespace rest="http://marklogic.com/appservices/rest"
      at "/MarkLogic/appservices/utils/rest.xqy";

  declare default function namespace "http://www.w3.org/2005/xpath-functions";

  declare option xdmp:mapping "false";

  let $request :=
    <rest:request uri="^/endpoint/(.+)/(\d+)$" endpoint="/endpoint.xqy">
      <rest:uri-param name="play">$1.xml</rest:uri-param>
      <rest:uri-param name="act" as="decimal" optional="true">$2</rest:uri-param>
    </rest:request>
 
  let $map  := rest:process-request($request) 
  let $play := map:get($map, "play") 
  let $num  := map:get($map, "act") 

  return  
    if (empty($num))
    then
      fn:doc($play)
    else
      fn:doc($play)/PLAY/ACT[$num]
  
  (: The rest:process-request returns a map from $request that contains the 
     keys 'play' and 'act', which are use to return either the contents of 
     an entire play or a specific act in the play. :)
    

rest:report-error(
$error as element()
)  as   element()
Summary:

This function formats the specified error structure.

Parameters:
$error : The error structure to be formatted.

Example:
  xquery version "1.0-ml"; 
 
  import module namespace rest="http://marklogic.com/appservices/rest"
      at "/MarkLogic/appservices/utils/rest.xqy";

  try {
     let $params := rest:process-request($request)
     return
       ...the non-error case...
   } catch ($e) {
       rest:report-error($e)
   }

 
    

rest:rewrite(
$options as element(rest:options)
)  as   xs:string?
Summary:

This function is used in the URL rewriter to map the incoming request to an endpoint.

Parameters:
$options : The options node that defines the request.

Example:
  xquery version "1.0-ml"; 
 
  import module namespace rest="http://marklogic.com/appservices/rest"
      at "/MarkLogic/appservices/utils/rest.xqy";

  declare default function namespace "http://www.w3.org/2005/xpath-functions";

  declare option xdmp:mapping "false";

  let $options :=
    <rest:options>
      <rest:request uri="^/shake/endpoint/(.+)/(\d+)$" endpoint="/shake/endpoint.xqy">
        <rest:uri-param name="play">$1.xml</rest:uri-param>
        <rest:uri-param name="act" as="decimal">$2</rest:uri-param>
      </rest:request>
      <rest:request uri="^/shake/endpoint/(.+)/?$" endpoint="/shake/endpoint.xqy">
        <rest:uri-param name="play">$1.xml</rest:uri-param>
      </rest:request>
    </rest:options>
 
  let $rewrite := rest:rewrite($options)
 
  return
    $rewrite

  (: Rewrites a URL ending with either /shake/endpoint/play or /shake/endpoint/play/{number}
     for execution by the endpoint.xqy module. :)