Skip to content

Instantly share code, notes, and snippets.

@DALDEI
Last active November 15, 2015 16:12
Show Gist options
  • Save DALDEI/ff03774a9a9cb195a4cc to your computer and use it in GitHub Desktop.
Save DALDEI/ff03774a9a9cb195a4cc to your computer and use it in GitHub Desktop.
MarkLogic Declarative Rewriter - Simulate a Redirect using xdbc eval
Demonstrate invoking the internal /eval xdbc endpoint on a HTTP server in MarkLogic to create a Redirect.
Note that there are simplier and easier ways to accomplish this, e.g using the user defined error handler or
rewriting to a endpoint that calls xdmp:redirect-response(). This example shows that it can be done 'out of the box'
by utilizing the builtin support for XDBC handling.
On Port 8000 in the default installation, and on any new REST servers a block of the declarative rewriter dispatches
XDBC requests to the internal XCDBC endpoints directly.
The xdbc.xml attached file shows the relevent block of XML. The enableing factor is the use of <dispatch xdbc="true">.
A custom rewriter can rewrite any request to one of the builtin endpoints ( /eval, /invoke , /spawn , /insert ) and then dispatch to xdbc.
In port 8000 and REST apps this simply 'passes through' untouched XDBC requests, and also is used for the /v1/eval endpoint to support REST
Eval requests.
The following curl command demonstrates this functionality. Note that a POST is required to for /eval
$ curl -i -u admin:secret --anyauth --data-urlencode 'xquery=xdmp:redirect-response("http://www.google.com")' http://localhost:8000/eval
HTTP/1.1 401 Unauthorized
Server: MarkLogic
WWW-Authenticate: Digest realm="public", qop="auth", nonce="7fc7cb2e37fa7fc000becb95b8a9bdf5", opaque="adad318baaa22742"
Content-Type: text/html; charset=utf-8
Content-Length: 209
Connection: Keep-Alive
Keep-Alive: timeout=5
HTTP/1.1 302 Found
Server: MarkLogic 8.0-20151109DL1
Location: http://www.google.com
Set-Cookie: TxnID=null; path=/
Content-Length: 0
Connection: Keep-Alive
Keep-Alive: timeout=5
<!-- XDBC -->
<match-path any-of="/eval /invoke /spawn /insert">
<trace event="Rewriter Evaluator">$0</trace>
<set-error-format>compatible</set-error-format>
<dispatch xdbc="true">$0</dispatch>
</match-path>
<!-- XDBC -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment