Skip to content

Instantly share code, notes, and snippets.

@dscape
Created April 14, 2010 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dscape/366471 to your computer and use it in GitHub Desktop.
Save dscape/366471 to your computer and use it in GitHub Desktop.
Exploring rest in marklogic
(:
: start by pointing the url-rewriter to 'rewrite.xqy'
:)
xdmp:get-request-path()
(:
: fix for params not working
:)
let $path := xdmp:get-request-path()
return fn:concat($path, "?" , fn:string-join(
for $field in xdmp:get-request-field-names()
return fn:concat($field, "=", xdmp:url-encode(xdmp:get-request-field($field)))
, "&") )
(:
: default in a function
:)
declare function local:default( $path ) {
fn:concat( $path, "?" , fn:string-join(
for $field in xdmp:get-request-field-names()
return fn:concat( $field, "=",
xdmp:url-encode(xdmp:get-request-field( $field ) ) ), "&" ) ) } ;
let $path := xdmp:get-request-path()
return local:default( $path )
(:
: supporting all paths
:)
declare function local:get-uri-encoded-fields() {
let $fields := fn:string-join( for $field in xdmp:get-request-field-names()
return fn:concat( $field, "=",
xdmp:url-encode(xdmp:get-request-field( $field ) ) ), "&" )
return if ($fields) then fn:concat("?", $fields) else "" } ;
declare function local:default( $path ) {
fn:concat( $path, local:get-uri-encoded-fields() ) } ;
declare function local:requested-root ( $path ) { fn:matches($path, "^/$") } ;
declare function local:requested-new ( $path ) {
fn:matches($path, "^/new$") } ;
declare function local:requested-show ( $path ) {
fn:matches($path, "^/show/\.*") } ;
declare function local:show( $path ) {
fn:concat( "show.xqy?uri=/", fn:replace($path, "^/show/(\.*)", "$2") ) };
let $path := xdmp:get-request-path()
return
if ( local:requested-root ( $path ) )
then "index.xqy"
else
if ( local:requested-new ( $path ) )
then fn:concat( "new.xqy", local:get-uri-encoded-fields() )
else
if ( local:requested-show ( $path ) )
then local:show( $path )
else fn:concat( $path, local:get-uri-encoded-fields() )
xquery version "1.0-ml";
module namespace h = "helpers";
declare function h:link_to_index() {
<a href="/" xmlns="http://www.w3.org/1999/xhtml">Back to main</a> } ;
declare function h:link_to_new() {
<a href="/new" xmlns="http://www.w3.org/1999/xhtml">Add new</a> } ;
declare function h:link_to_show( $path ) {
fn:concat( '/show', $path ) };
declare function h:article-submission-action() { "/new" } ;
(:
: removing the fallback
:)
declare function local:get-uri-encoded-fields() {
let $fields := fn:string-join( for $field in xdmp:get-request-field-names()
return fn:concat( $field, "=",
xdmp:url-encode(xdmp:get-request-field( $field ) ) ), "&amp;" )
return if ($fields) then fn:concat("?", $fields) else "" } ;
declare function local:default( $path ) {
fn:concat( $path, local:get-uri-encoded-fields() ) } ;
declare function local:requested-root ( $path ) { fn:matches($path, "^/$") } ;
declare function local:requested-new ( $path ) {
fn:matches($path, "^/new$") } ;
declare function local:requested-show ( $path ) {
fn:matches($path, "^/show/\.*") } ;
declare function local:show( $path ) {
fn:concat( "show.xqy?uri=/", fn:replace($path, "^/show/(\.*)", "$2") ) };
let $path := xdmp:get-request-path()
return
if ( local:requested-root ( $path ) )
then "index.xqy"
else
if ( local:requested-new ( $path ) )
then fn:concat( "new.xqy", local:get-uri-encoded-fields() )
else
if ( local:requested-show ( $path ) )
then local:show( $path )
else "404.xqy"
(:
: re-adding the fallback for images and css
:)
declare function local:get-uri-encoded-fields() {
let $fields := fn:string-join( for $field in xdmp:get-request-field-names()
return fn:concat( $field, "=",
xdmp:url-encode(xdmp:get-request-field( $field ) ) ), "&amp;" )
return if ($fields) then fn:concat("?", $fields) else "" } ;
declare function local:default( $path ) {
fn:concat( $path, local:get-uri-encoded-fields() ) } ;
declare function local:requested-root ( $path ) { fn:matches($path, "^/$") } ;
declare function local:requested-new ( $path ) {
fn:matches($path, "^/new$") } ;
declare function local:requested-show ( $path ) {
fn:matches($path, "^/show/\.*") } ;
declare function local:show( $path ) {
fn:concat( "show.xqy?uri=/", fn:replace($path, "^/show/(\.*)", "$2") ) };
declare function local:is-image-or-css( $path ) {
let $extension := fn:tokenize($path, "\.")[2]
let $_ := xdmp:set-response-content-type(xdmp:uri-content-type($path))
return ( $extension = ("png", "css", "jpg", "gif") ) } ;
let $path := xdmp:get-request-path()
return
if ( local:requested-root ( $path ) )
then "index.xqy"
else
if ( local:requested-new-article ( $path ) )
then fn:concat( "new.xqy", local:get-uri-encoded-fields() )
else
if ( local:requested-show-article ( $path ) )
then local:show( $path )
else
if (local:is-image-or-css( $path )) (: show be up, many img requests :)
then $path
else "404.xqy"
xquery version "1.0-ml";
module namespace h = "helpers";
declare function h:link_to_index() {
<a href="/" xmlns="http://www.w3.org/1999/xhtml">Back to main</a> } ;
declare function h:link_to_new() {
<a href="/article" xmlns="http://www.w3.org/1999/xhtml">Add new</a> } ;
declare function h:link_to_show( $path ) {
fn:concat( '/article', $path ) };
declare function h:article-submission-action() { "/article" } ;
(:
: re-adding the fallback for images and css
:)
declare function local:get-uri-encoded-fields() {
let $fields := fn:string-join( for $field in xdmp:get-request-field-names()
return fn:concat( $field, "=",
xdmp:url-encode(xdmp:get-request-field( $field ) ) ), "&amp;" )
return if ($fields) then fn:concat("?", $fields) else "" } ;
declare function local:default( $path ) {
fn:concat( $path, local:get-uri-encoded-fields() ) } ;
declare function local:requested-root ( $path ) { fn:matches($path, "^/$") } ;
declare function local:requested-new-article ( $path ) {
fn:matches($path, "^/article$") } ;
declare function local:requested-show-article ( $path ) {
fn:matches($path, "^/article/\.*") } ;
declare function local:show( $path ) {
fn:concat( "show.xqy?uri=/", fn:replace($path, "^/article/(\.*)", "$2") ) };
declare function local:is-image-or-css( $path ) {
let $extension := fn:tokenize($path, "\.")[2]
let $_ := xdmp:set-response-content-type(xdmp:uri-content-type($path))
return ( $extension = ("png", "css", "jpg", "gif") ) } ;
let $path := xdmp:get-request-path()
return
if ( local:requested-root ( $path ) )
then "index.xqy"
else
if ( local:requested-new-article ( $path ) )
then fn:concat( "new.xqy", local:get-uri-encoded-fields() )
else
if ( local:requested-show-article ( $path ) )
then local:show( $path )
else
if (local:is-image-or-css( $path )) (: show be up, many img requests :)
then $path
else "404.xqy"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment