RESTXQ: If you ever need to set an http status code, or overwrite the annotations

Monday, 21 May 2012 · 1 message
PR
Philippe Rathé21 May 2012 01:10 #
Hello, this is an example of setting a 404 by overwriting what the REST annotations would produce. I had to gathered pieces of specs together in order to have it working. Please tell me if I'm missing the documentation that cover this. Feedback are welcome.
    declare %rest:path("/categories/{$category}")
            %rest:GET
            %rest:produces("application/atomcat+xml")
            %output:omit-xml-declaration("no")
            %output:media-type("application/atomcat+xml")
      function page:category($category as xs:string) {
        if (db:exists('categories', $category || '.xml')) then
          doc('categories/' || $category || '.xml')
        else
          let $reason := "Category scheme not found" return
          <rest:response>
            <http:response status="404" reason="{$reason}">
              <!--<http:header name="string" value="string"/>-->
            </http:response>
            <output:serialization-parameters>
              <output:omit-xml-declaration value="yes"/>
              <output:media-type value="text/plain"/>
            </output:serialization-parameters>
          </rest:response>
    };
Would produce the following on a not found document: HTTP/1.1 404 Not Found Content-Type: text/plain; charset=utf-8 Content-Length: 25 Server: Jetty(6.1.26) Category scheme not found -- Philippe

⚡Generated with XQuery