This page was generated
July 7, 2010
4:02 PM
XQuery Built-In and Modules Function Reference

Built-In: AppServer

The application server built-in functions are XQuery functions for many HTTP application server functions. Many of the application server functions (for example, xdmp:get-request-field, xdmp:login, etc.) are executable only on HTTP servers; those functions all have no effect and return the empty sequence when run from an XDBC server.
Function Summary
xdmp:add-response-header Adds an HTTP response header field.
xdmp:get-request-body Returns the POST or PUT body of this request, if it is not application/x-www-form-urlencoded.
xdmp:get-request-client-address Returns as a string the internet address of the client from which the HTTP server request is issued.
xdmp:get-request-field Returns the value of a named request field.
xdmp:get-request-field-content-type This function is used to extract the content type from the request field.
xdmp:get-request-field-filename Returns a list of filenames from a multipart request for the field name specified.
xdmp:get-request-field-names Returns a sequence of the request field names.
xdmp:get-request-header Returns the value of a named request header.
xdmp:get-request-header-names Returns a sequence of request header names.
xdmp:get-request-method Returns the HTTP request method.
xdmp:get-request-path Returns the HTTP request path.
xdmp:get-request-username Returns the username from the Authorization header of this App Server request.
xdmp:get-response-code Returns two nodes, the first containing the HTTP response code and the second containing the HTTP response message.
xdmp:get-response-encoding Returns the encoding that the response from this server is in.
xdmp:get-session-field Returns the value of a named HTTP session field.
xdmp:get-session-field-names Returns a sequence of the HTTP session field names.
xdmp:login Logs in a user on an application server that is using application-level authentication and deposits a session cookie.
xdmp:logout Sets the current user to the default user defined in application-level authentication.
xdmp:redirect-response Redirects the App Server response to a given location.
xdmp:set-request-time-limit Changes the time limit for an actively running request to the specified value.
xdmp:set-response-code Sets the response code and message.
xdmp:set-response-content-type Sets the response content-type.
xdmp:set-response-encoding Sets the response encoding.
xdmp:set-session-field Sets the value of a named HTTP session field.
xdmp:uri-is-file Returns true if a given URI refers to a file which exists on the current application server.
xdmp:url-decode Converts URL-encoded string to plaintext.
xdmp:url-encode Converts plaintext into URL-encoded string.
Function Detail
xdmp:add-response-header(
$name as xs:string,
$value as xs:string
)  as  empty-sequence()
Summary:

Adds an HTTP response header field.

Parameters:
$name : The response header name.
$value : The value to set for this response header.

Required Privilege:

http://marklogic.com/xdmp/privileges/xdmp-add-response-header


Example:
  xdmp:add-response-header("meta", "description")

xdmp:get-request-body(
[$format as xs:string]
)  as  item()*
Summary:

Returns the POST or PUT body of this request, if it is not application/x-www-form-urlencoded.

Returns the empty sequence if it is not called from an application server.


Parameters:
$format (optional): The format ("xml", "text", or "binary") to interpret the body as. If not supplied, the format associated with the content-type header in Mimetypes.xml is used. If no content-type header exists, the default format is "binary".

Usage Notes:

If the content-type of the POST or PUT body is application/x-www-form-urlencoded, it is not available here, but instead is available in its decoded form through xdmp:get-request-field-names() and xdmp:get-request-field().

If there is no content-type header in the request, then the request body defaults to application/x-www-form-urlencoded, and therefore xdmp:get-request-body will return the empty sequence. If you want to read the request body, then the POST must include a content-type header.

You can use this function to process certain types of web service SOAP requests with MarkLogic Server.

The output of an xdmp:get-request-body call is typically a document node, so if you want to get the contents of the POST, you should add a /node() XPath step to the output. The contents of the document node could be a a text node, a document node, or a binary node.


Example:
  xdmp:get-request-body()/node()
  => "<a>Contents of POST body.</a>"

xdmp:get-request-client-address( ) as xs:string?
Summary:

Returns as a string the internet address of the client from which the HTTP server request is issued.

Returns the empty sequence if it is not called from an HTTP server.


Usage Notes:

Use this function if you need to get the internet protocol (IP) address of the requesting client. For example, you can create an application that contains conditional code based on IP addresses (see the example below).

Example:

The following example shows logic which checks if the request was submitted from the "localhost" IP address (127.0.0.1).

  if (xdmp:get-request-client-address() eq "127.0.0.1")
  then "Submitted from localhost."
  else "Only localhost access is allowed for this application."
Example:
  xdmp:get-request-client-address()
  => "127.0.0.1"

xdmp:get-request-field(
$name as xs:string,
[$default as xs:string]
)  as  xs:string*
Summary:

Returns the value of a named request field. If the request field is a multipart/form-data type in a POST form, you can use xdmp:get-request-field for file upload applications (see the second example below).

Parameters:
$name : Request field name.
$default (optional): A default value to return if there is no request field.

Example:
  xdmp:get-request-field("index")
  => "10"
Example:
 
Consider a form.xqy XQuery module with the following content:

  <html xmlns="http://www.w3.org/1999/xhtml">
    <body>
    <form name="test" action="upload.xqy?uid={xdmp:random()}" method="post"
          enctype="multipart/form-data">
    <p><label>File to upload:
    <input type="file" class="name" name="upload" size="50"/></label></p>
    <p><input type="submit" value="Upload and Get Results"/></p>
    </form>
    </body>
  </html> 

Then have an upload.xqy XQuery module as follows:

 let $filename := xdmp:get-request-field-filename("upload")
 let $disposition := fn:concat("attachment; filename=""",$filename,"""")
 let $x := xdmp:add-response-header("Content-Disposition", $disposition)
 let $x:= xdmp:set-response-content-type(
             xdmp:get-request-field-content-type("upload"))
 return
 xdmp:get-request-field("upload")

Execute the form.xqy file, select a file, and click the 
"Upload and Get Results" button.  The file you uploaded
will open according to the mime type the browser.  If you
wanted to save it to the database, you could use 
xdmp:document-insert to do so.


xdmp:get-request-field-content-type(
$field-name as xs:string
)  as  xs:string*
Summary:

This function is used to extract the content type from the request field. It returns a sequence of content types, one for each filename, in the same order as the filenames returned from xdmp:get-request-field-filename.

Parameters:
$field-name : The name of the request field with the multipart request.

Usage Notes:

This function is useful for file upload applications. For an example, see the second example in the xdmp:get-request-field documentation.

Example:
  (: 
     Returns the content type of the files loaded in the 
     "upload" input form element.
  :)
  xdmp:get-request-field-content-type("upload")
  => "application/msword"

xdmp:get-request-field-filename(
$field-name as xs:string
)  as  xs:string*
Summary:

Returns a list of filenames from a multipart request for the field name specified. Returns an empty sequence for a field that does not exist.

Parameters:
$field-name : The name of the request field with the multipart request.

Usage Notes:

This function is useful for file upload applications. For an example, see the second example in the xdmp:get-request-field documentation.

Example:
  (: 
     Returns the filename of the files loaded in the 
     "upload" input form element.
  :)
  xdmp:get-request-field-filename("upload")
  => "myfile.doc"

xdmp:get-request-field-names( ) as xs:string*
Summary:

Returns a sequence of the request field names.

Example:
  xdmp:get-request-field-names()
  => ("section", "name", ...)

xdmp:get-request-header(
$name as xs:string,
[$default as xs:string]
)  as  xs:string*
Summary:

Returns the value of a named request header.

Parameters:
$name : Request header name.
$default (optional): A default value to return if there is no request header.

Example:
  xdmp:get-request-header("A")
  => "foo"

xdmp:get-request-header-names( ) as xs:string*
Summary:

Returns a sequence of request header names.

Example:
  xdmp:get-request-header-names()
  => ("A", "B", ...)

xdmp:get-request-method( ) as xs:string
Summary:

Returns the HTTP request method.

Example:
  xdmp:get-request-method()
  => "GET"

xdmp:get-request-path( ) as xs:string
Summary:

Returns the HTTP request path.

Example:
  xdmp:get-request-path()
  => "/example.xqy"

xdmp:get-request-username( ) as xs:string
Summary:

Returns the username from the Authorization header of this App Server request.

Example:
  xdmp:get-request-username()
  => "fred"

xdmp:get-response-code( ) as item()*
Summary:

Returns two nodes, the first containing the HTTP response code and the second containing the HTTP response message.

Usage Notes:

You can use this with an HTTP Server error handler to create custom error pages.

Example:
  xdmp:response-code()
  => 200
     OK

xdmp:get-response-encoding( ) as xs:string
Summary:

Returns the encoding that the response from this server is in.


xdmp:get-session-field(
$name as xs:string,
[$default as item()*]
)  as  item()*
Summary:

Returns the value of a named HTTP session field.

Parameters:
$name : The name of the session field.
$default (optional): A default value to return if there is no session field.

Required Privilege:

http://marklogic.com/xdmp/privileges/xdmp-get-session-field


Example:
  xdmp:get-session-field("user")
  => "marklogic"

xdmp:get-session-field-names( ) as xs:string*
Summary:

Returns a sequence of the HTTP session field names.

Required Privilege:

http://marklogic.com/xdmp/privileges/xdmp-get-session-field-names


Example:
  xdmp:get-session-field-names()
  => ("user", "role", "action", "index")

xdmp:login(
$name as xs:string,
[$password as xs:string?],
[$set-session as xs:boolean]
)  as  xs:boolean
Summary:

Logs in a user on an application server that is using application-level authentication and deposits a session cookie. Returns true on success, false on failure.

If the user calling this function has the xdmp:login privilege, this function can be called without a password or with the empty sequence as the password. In this case, login will succeed if the specified user exists. Therefore, use the xdmp:login privilege carefully, as any user with that privilege will be able to execute code that uses the xdmp:login function to log in as any user.


Parameters:
$name : The username of the user to be logged in.
$password (optional): The user's password. The password is not needed if the user calling the function has the xdmp:login execute privilege.
$set-session (optional): A boolean value specifying whether to set a session variable for the login. The default is true. Set to false to not set the session variable to maintain the user logged in.

Example:
  xdmp:login("mark","secret")
  => true() -- if user "mark" has password "secret"
Example:
  xdmp:login("username") or xdmp:login("username", ()) 
  => true() -- if user calling the function has the 
               xdmp:login privilege
Example:
  xdmp:login("username") or xdmp:login("username", ()) 
  => SEC-PRIV exception if the user calling the function does 
              not have the xdmp:login privilege
Example:
  xdmp:login("username")
  => true() -- if current user has the xdmp:login privilege
Example:
  xdmp:login("username")
  => SEC-PRIV exception if current user does not have the 
              xdmp:login privilege

xdmp:logout( ) as empty-sequence()
Summary:

Sets the current user to the default user defined in application-level authentication.

Example:
  xdmp:logout()
  => ()

xdmp:redirect-response(
$name as xs:string
)  as  empty-sequence()
Summary:

Redirects the App Server response to a given location.

Parameters:
$name : The redirect URL.

Example:
  xdmp:redirect-response("http://marklogic.com/howtobuy.xqy")

xdmp:set-request-time-limit(
$time-limit as xs:unsignedInt,
[$hostID as xs:unsignedLong],
[$serverID as xs:unsignedLong],
[$requestID as xs:unsignedLong]
)  as  empty-sequence()
Summary:

Changes the time limit for an actively running request to the specified value. If you do not supply values for the last three parameters, the function sets the time limnit for the current request.

Parameters:
$time-limit : The desired time limit, in seconds.
$hostID (optional): The ID of the host on which the request is running. Typically, you get the ID of a host by executing code similar to:
    xdmp:host("myhost")
$serverID (optional): The ID of the App Server in which the request is running. Typically, you get the ID of an App Server by executing code similar to:
    xdmp:server("myAppServerName")
$requestID (optional): The ID of the request. You can access the request IDs in the request elements of the xdmp:server-status output. You get the request ID by executing code similar to:
    declare namespace status=
        "http://marklogic.com/xdmp/status/server"
    
    xdmp:server-status( xdmp:host("myhost"), 
      xdmp:server("myAppServerName") )//status:request

Required Privilege:

http://marklogic.com/xdmp/privileges/xdmp-set-request-time-limit-any or http://marklogic.com/xdmp/privileges/xdmp-set-request-time-limit-my


Example:
  xdmp:set-request-time-limit(10000)

xdmp:set-response-code(
$code as xs:integer,
$message as xs:string
)  as  empty-sequence()
Summary:

Sets the response code and message.

Parameters:
$code : The response code.
$message : The response message.

Example:
  xdmp:set-response-code(204,"No Content")

xdmp:set-response-content-type(
$name as xs:string
)  as  empty-sequence()
Summary:

Sets the response content-type.

Parameters:
$name : The content type.

Example:
  xdmp:set-response-content-type("text/html")

xdmp:set-response-encoding(
$encoding as xs:string
)  as  empty-sequence()
Summary:

Sets the response encoding.

Parameters:
$encoding : The desired response encoding.

Example:
  xdmp:set-response-encoding("ISO-8859-1")

xdmp:set-session-field(
$name as xs:string,
$value as item()*
)  as  item()*
Summary:

Sets the value of a named HTTP session field.

Parameters:
$name : The name of the session field.
$value : The value of the session field.

Required Privilege:

http://marklogic.com/xdmp/privileges/xdmp-set-session-field


Example:
  xdmp:set-session-field("user", "marklogic")
  => "marklogic"

xdmp:uri-is-file(
$uri as xs:string
)  as  xs:boolean?
Summary:

Returns true if a given URI refers to a file which exists on the current application server. Returns false if the file does not exist. Returns the empty sequence if the URI is the empty sequence.

Parameters:
$uri : The URI to check. If the URI begins with a "/", it is relative to the root directory of the application server. Otherwise, it is relative to the current request URI.

Example:
  xdmp:uri-is-file("apppages/test.xml")
  => true if test.xml exists in "apppages" sub-directory 
  relative to the directory of the file containing the request.
Example:
  xdmp:uri-is-file("/sub-directory/test.xml")
  => true if test.xml exists in "sub-directory" relative 
  to the root directory of the application server.

xdmp:url-decode(
$encoded as xs:string
)  as  xs:string
Summary:

Converts URL-encoded string to plaintext.

Parameters:
$encoded : Encoded text to be decoded.

Example:
  xdmp:url-decode("Why+not%3f")
  => "Why not?"

xdmp:url-encode(
$plaintext as xs:string,
[$noSpacePlus as xs:boolean]
)  as  xs:string
Summary:

Converts plaintext into URL-encoded string.

Parameters:
$plaintext : Plaintext to be encoded.
$noSpacePlus (optional): True to encode space as "%20" instead of "+".

Example:
  xdmp:url-encode("Why not?")
  => "Why+not%3f"