|
46 | 46 |
|
47 | 47 | <!--
|
48 | 48 | Package the source XML into an HTTP response:
|
49 |
| - If it's JSON-XML, regard it as an error: convert it to JSON and return it with an HTTP 400 error code. |
50 |
| - If it's other XML including XHTML, regard it as a success: return it as XML with a 200 OK |
| 49 | + If it's a JSON-XML map containing a 'name' and a 'code' property, regard it as an error: convert it to JSON and return it with an HTTP 400 error code. |
| 50 | + If it's other JSON-XML, return it as JSON with a 200 OK status |
| 51 | + If it's XML including XHTML, return it as XML with a 200 OK |
51 | 52 | -->
|
52 |
| - <p:declare-step name="make-http-response" type="nyingarn:make-http-response"> |
| 53 | + <p:declare-step name="make-http-response" type="nyingarn:make-http-response" xmlns:fn="http://www.w3.org/2005/xpath-functions"> |
53 | 54 | <p:input port="source"/>
|
54 | 55 | <p:output port="result"/>
|
55 |
| - <p:choose> |
56 |
| - <p:when test="/fn:map" xmlns:fn="http://www.w3.org/2005/xpath-functions"><!-- an error --> |
57 |
| - <p:template name="http-response"> |
58 |
| - <p:input port="parameters"><p:empty/></p:input> |
59 |
| - <p:input port="template"> |
60 |
| - <p:inline> |
61 |
| - <c:response status="400"> |
62 |
| - <c:body content-type="application/json">{xml-to-json(/*)}</c:body> |
63 |
| - </c:response> |
64 |
| - </p:inline> |
65 |
| - </p:input> |
66 |
| - </p:template> |
67 |
| - </p:when> |
68 |
| - <p:otherwise><!-- TEI or XHTML --> |
69 |
| - <z:make-http-response> |
70 |
| - <p:with-option name="content-type" select=" |
71 |
| - if (/xhtml:html) then |
72 |
| - 'application/xhtml+xml; charset=utf-8' |
73 |
| - else |
74 |
| - 'application/xml; charset=utf-8' |
75 |
| - "/> |
76 |
| - </z:make-http-response> |
77 |
| - </p:otherwise> |
78 |
| - </p:choose> |
| 56 | + <!-- if the result to be returned is a JSON object containing both a "name" and a "code" property, then it's assumed to represent an error --> |
| 57 | + <p:variable name="status" select=" |
| 58 | + if (/fn:map[fn:string/@key='name'][fn:string/@key='code']) then |
| 59 | + '400' |
| 60 | + else |
| 61 | + '200' |
| 62 | + "/> |
| 63 | + <p:variable name="content-type" select=" |
| 64 | + if (/xhtml:html) then |
| 65 | + 'application/xhtml+xml; charset=utf-8' |
| 66 | + else if (/fn:string) then |
| 67 | + 'text/plain; charset=utf-8' |
| 68 | + else if (/fn:*) then |
| 69 | + 'application/json' |
| 70 | + else |
| 71 | + 'application/xml; charset=utf-8' |
| 72 | + "/> |
| 73 | + <p:template name="http-response"> |
| 74 | + <p:with-param name="status" select="$status"/> |
| 75 | + <p:with-param name="content-type" select="$content-type"/> |
| 76 | + <p:input port="template"> |
| 77 | + <p:inline> |
| 78 | + <c:response status="{$status}"> |
| 79 | + <c:body content-type="{$content-type}">{ |
| 80 | + if ($content-type='text/plain; charset=utf-8') then |
| 81 | + (: convert a single JSON-XML string to plain text :) |
| 82 | + string(/*) |
| 83 | + else if ($content-type='application/json') then |
| 84 | + (: convert other JSON-XML to JSON :) |
| 85 | + xml-to-json(/*) |
| 86 | + else |
| 87 | + (: copy other XML unchanged :) |
| 88 | + /* |
| 89 | + }</c:body> |
| 90 | + </c:response> |
| 91 | + </p:inline> |
| 92 | + </p:input> |
| 93 | + </p:template> |
79 | 94 | </p:declare-step>
|
80 | 95 |
|
81 | 96 | <p:variable name="uri-path" select="replace(/c:request/@href, 'http://.*?/([^?]*).*', '$1')"/>
|
| 97 | + <p:variable name="method" select="/c:request/@method"/> |
82 | 98 |
|
83 | 99 | <z:dump href="/tmp/request.xml"/>
|
84 | 100 |
|
|
165 | 181 | </p:otherwise>
|
166 | 182 | </p:choose>
|
167 | 183 | </p:when>
|
| 184 | + <p:when test="$uri-path='nyingarn/extract-text'"> |
| 185 | + <p:choose> |
| 186 | + <p:when test="//c:body/@disposition[starts-with(., 'form-data; name="source"')]"> |
| 187 | + <!-- pass the document to the text-extraction stylesheet --> |
| 188 | + <p:xslt name="extract-plain-text"> |
| 189 | + <p:input port="parameters"><p:empty/></p:input> |
| 190 | + <p:input port="source" select="//c:body[starts-with(@disposition, 'form-data; name="source"')]/*"> |
| 191 | + <p:pipe step="nyingarn" port="source"/> |
| 192 | + </p:input> |
| 193 | + <p:input port="stylesheet"> |
| 194 | + <p:document href="../xslt/extract-plain-text.xsl"/> |
| 195 | + </p:input> |
| 196 | + </p:xslt> |
| 197 | + </p:when> |
| 198 | + <p:otherwise> |
| 199 | + <!-- file was not uploaded; display an upload form, for manual testing --> |
| 200 | + <nyingarn:html-page page="extract-text"/> |
| 201 | + </p:otherwise> |
| 202 | + </p:choose> |
| 203 | + </p:when> |
| 204 | + |
| 205 | + <p:when test="$uri-path='nyingarn/extract-text-by-page'"> |
| 206 | + <p:choose> |
| 207 | + <p:when test="//c:body/@disposition[starts-with(., 'form-data; name="source"')]"> |
| 208 | + <!-- pass the document to the text-extraction stylesheet --> |
| 209 | + <p:xslt name="extract-plain-text"> |
| 210 | + <p:input port="parameters"><p:empty/></p:input> |
| 211 | + <p:input port="source" select="//c:body[starts-with(@disposition, 'form-data; name="source"')]/*"> |
| 212 | + <p:pipe step="nyingarn" port="source"/> |
| 213 | + </p:input> |
| 214 | + <p:input port="stylesheet"> |
| 215 | + <p:document href="../xslt/extract-plain-text-by-page.xsl"/> |
| 216 | + </p:input> |
| 217 | + </p:xslt> |
| 218 | + </p:when> |
| 219 | + <p:otherwise> |
| 220 | + <!-- file was not uploaded; display an upload form, for manual testing --> |
| 221 | + <nyingarn:html-page page="extract-text-by-page"/> |
| 222 | + </p:otherwise> |
| 223 | + </p:choose> |
| 224 | + </p:when> |
168 | 225 | <p:when test="$uri-path='nyingarn/ingest-tei'">
|
169 | 226 | <p:choose>
|
170 | 227 | <p:when test="//c:body/@disposition[starts-with(., 'form-data; name="source"')]">
|
|
257 | 314 | <cx:message name="log-request">
|
258 | 315 | <p:with-option name="message" select="
|
259 | 316 | concat(
|
260 |
| - 'Request URI [', |
| 317 | + 'Method [', |
| 318 | + $method, |
| 319 | + '] URI [', |
261 | 320 | $uri-path,
|
262 | 321 | '] returning status code [',
|
263 | 322 | /c:response/@status,
|
|
0 commit comments