19
19
<para >Good web applications provide meaningful and consistent URLs to the user. eXist-db's
20
20
<emphasis >URL Rewriting</emphasis > facility is eXist's oldest internal mechanism for
21
21
providing short, human-readable URLs to XQuery web applications, conveniently masking the
22
- often complex heirarchy of XQuery modules, HTML files, data, and other resources. (A newer
22
+ often complex hierarchy of XQuery modules, HTML files, data, and other resources. (A newer
23
23
facility for achieving these goals is eXist-db's implementation of <link
24
24
xlink : href =" http://exquery.github.io/exquery/exquery-restxq-specification/restxq-1.0-specification.html"
25
25
>RESTXQ</link >. A third method is to place a <link xlink : href =" production_web_proxying"
41
41
<emphasis >root pattern</emphasis >, which is <code >/apps</code > by default, eXist-db will
42
42
look within the associated <emphasis >controller root path</emphasis >, which is the
43
43
<code >/db/apps</code > database collection by default, for <emphasis >controller
44
- scripts</emphasis >: special XQuery files named <code >controller.xq</code >. These
45
- controller scripts apply to the collections where they are stored and their descendant
46
- collections. They form a <emphasis >collection hierarchy</emphasis >—a URL space within
47
- which URL rewriting can be flexibly applied.</para >
44
+ scripts</emphasis >: special XQuery files named <code >controller.xq</code > (or in older
45
+ applications <code >controller.xql</code >). A controller script applies to the collection
46
+ it is stored in and its descendants. They form a <emphasis >collection
47
+ hierarchy</emphasis >—a URL space within which URL rewriting can be flexibly
48
+ applied.</para >
48
49
</listitem >
49
50
<listitem >
50
51
<para >The <code >controller.xq</code > script examines the URL using the provided <xref
54
55
<listitem >
55
56
<para >The <code >XQueryURLRewrite</code > servlet interprets the directive as a series of
56
57
instructions on what to do next. These instructions may be as simple as forwarding the
57
- request to a resource on the server (or elsewhere), or it may be as complex as a pipeline
58
- using the <emphasis >Model-View-Controller</emphasis > pattern (see <xref
59
- linkend =" mvc-pipelines" />) and other servlets such as eXist-db's <xref
60
- linkend =" xq-servlet" /> or <xref linkend =" xslt-servlet" />.</para >
58
+ request to a resource on the server (or elsewhere), or as complex as a pipeline using the
59
+ <emphasis >Model-View-Controller</emphasis > pattern (see <xref linkend =" mvc-pipelines" />)
60
+ and other servlets such as eXist-db's <xref linkend =" xq-servlet" /> or <xref
61
+ linkend =" xslt-servlet" />. The controller script isn't limited to returning these types
62
+ of instructions; like any XQuery main module, they can return any data you may wish. But
63
+ URL Rewriting instructions are the special capability of controller scripts.</para >
61
64
</listitem >
62
65
</orderedlist >
63
66
<sect2 xml : id =" eg1" >
86
89
script</emphasis >, an XQuery main module named <literal >controller.xq</literal >. This
87
90
script is invoked for all URL paths targeting the collection in which it resides. It has
88
91
access to a number of <xref linkend =" variables" /> pre-bound with details about the request,
89
- including <literal >$exist:resource</literal >, which conveniently contains the name of the
92
+ including <code >$exist:resource</code >, which conveniently contains the name of the
90
93
requested resource (without any leading path components). It also includes
91
- <literal >$exist:controller</literal >, which contains the path to the database collection
92
- where the <literal >controller.xq</literal > is located.</para >
94
+ <code >$exist:controller</code >, which contains the path to the database collection where
95
+ the <literal >controller.xq</literal > is located.</para >
93
96
<para >This information allows a controller script to forward all requests for
94
97
<literal >/exist/apps/doc/{resource}</literal > to an XQuery,
95
98
<literal >transform.xq</literal >, which converts the XML document located via the
138
141
<sect1 xml : id =" controller-xml" >
139
142
<title >Controller XML Format</title >
140
143
<para >As we have seen, the <code >controller.xq</code > script is expected to return a single XML
141
- element: either a <tag >dispatch</tag > element or an <tag >ignore</tag > element.</para >
142
- <para >Note that all of the elements discussed in this article must be in the
144
+ element: a <tag >dispatch</tag > or <tag >ignore</tag > element. The script can actually return
145
+ any data, as you wish, but the <tag >dispatch</tag > or <tag >ignore</tag > elements have special
146
+ properties in the context of a <code >controller.xq</code > script.</para >
147
+ <para >Note that all of the elements described in this section must be in the
143
148
<code >http://exist.sourceforge.net/NS/exist</code > namespace.</para >
144
149
<sect2 xml : id =" dispatch" >
145
150
<title >The <tag >dispatch</tag > Action</title >
@@ -365,7 +370,7 @@ Content-Length: 0</programlisting>
365
370
<row >
366
371
<entry >
367
372
<para >
368
- <literal >$exist:root</literal >
373
+ <code >$exist:root</code >
369
374
</para >
370
375
</entry >
371
376
<entry >
@@ -377,7 +382,7 @@ Content-Length: 0</programlisting>
377
382
<row >
378
383
<entry >
379
384
<para >
380
- <literal >$exist:prefix</literal >
385
+ <code >$exist:prefix</code >
381
386
</para >
382
387
</entry >
383
388
<entry >
@@ -389,7 +394,7 @@ Content-Length: 0</programlisting>
389
394
<row >
390
395
<entry >
391
396
<para >
392
- <literal >$exist:controller</literal >
397
+ <code >$exist:controller</code >
393
398
</para >
394
399
</entry >
395
400
<entry >
@@ -401,7 +406,7 @@ Content-Length: 0</programlisting>
401
406
<row >
402
407
<entry >
403
408
<para >
404
- <literal >$exist:path</literal >
409
+ <code >$exist:path</code >
405
410
</para >
406
411
</entry >
407
412
<entry >
@@ -413,7 +418,7 @@ Content-Length: 0</programlisting>
413
418
<row >
414
419
<entry >
415
420
<para >
416
- <literal >$exist:resource</literal >
421
+ <code >$exist:resource</code >
417
422
</para >
418
423
</entry >
419
424
<entry >
@@ -444,7 +449,7 @@ declare variable $exist:resource external;
444
449
<variablelist >
445
450
<varlistentry >
446
451
<term >
447
- <code >exist:root</code >
452
+ <code >$ exist:root</code >
448
453
</term >
449
454
<listitem >
450
455
<para >Is bound to the root of the current controller hierarchy. This may either point to
@@ -455,51 +460,49 @@ declare variable $exist:resource external;
455
460
the <literal >/stylesheets</literal > directory in the root of the webapp or, if the app
456
461
is running from within the database, the corresponding <literal >/stylesheets</literal >
457
462
collection. You want your app to be able to run from either location. The solution is to
458
- incorporate the value of the <literal >exist:root</literal > variable into your
459
- logic:</para >
463
+ incorporate the value of the <code >$exist:root</code > variable into your logic:</para >
460
464
<programlisting language =" xml" xlink : href =" listings/listing-5.xml" />
461
465
</listitem >
462
466
</varlistentry >
463
467
<varlistentry >
464
468
<term >
465
- <code >exist:prefix</code >
469
+ <code >$ exist:prefix</code >
466
470
</term >
467
471
<listitem >
468
472
<para >If the current controller hierarchy is mapped to a certain path prefix, then the
469
- <literal >$exist:prefix</literal > variable will be bound to that prefix.</para >
473
+ <code >$exist:prefix</code > variable will be bound to that prefix.</para >
470
474
<para >For example: the default configuration maps the path <literal >/apps</literal > to a
471
- collection in the database (see below). In this case, the
472
- <literal >$exist:prefix</literal > variable would be bound to the value
473
- <literal >/apps</literal >.</para >
475
+ collection in the database (see below). In this case, the <code >$exist:prefix</code >
476
+ variable would be bound to the value <literal >/apps</literal >.</para >
474
477
</listitem >
475
478
</varlistentry >
476
479
<varlistentry >
477
480
<term >
478
- <code >exist:controller</code >
481
+ <code >$ exist:controller</code >
479
482
</term >
480
483
<listitem >
481
484
<para >Is bound to the part of the URL leading to the current
482
485
<literal >controller.xq</literal >.</para >
483
486
<para >For example, if the request path is <literal >/sandbox/test.xq</literal > and the
484
487
controller is in the <literal >xquery</literal > collection, the
485
- <literal >$exist:controller</literal > variable will be bound to the value
488
+ <code >$exist:controller</code > variable will be bound to the value
486
489
<literal >/sandbox</literal >.</para >
487
490
</listitem >
488
491
</varlistentry >
489
492
<varlistentry >
490
493
<term >
491
- <code >exist:path</code >
494
+ <code >$ exist:path</code >
492
495
</term >
493
496
<listitem >
494
497
<para >Is bound to the last part of the request URL, i.e., after the section leading to the
495
498
collection containing the controller.xq.</para >
496
499
<para >For instance, if the resource <literal >example.xml</literal > resides within the same
497
- collection as the controller query, the <literal >$exist:path</literal > variable would be
498
- bound to the value <literal >/example.xml</literal >.</para >
500
+ collection as the controller query, the <code >$exist:path</code > variable would be bound
501
+ to the value <literal >/example.xml</literal >.</para >
499
502
</listitem >
500
503
</varlistentry >
501
504
<varlistentry >
502
- <term ><code >exist:resource</code ></term >
505
+ <term ><code >$ exist:resource</code ></term >
503
506
<listitem >
504
507
<para >Is bound to the part of the URL after the last <literal >/</literal >; this is usually
505
508
pointing to a resource.</para >
@@ -542,14 +545,16 @@ declare variable $exist:resource external;
542
545
<title >Locating Controller Scripts and Configuring Base Mappings</title >
543
546
544
547
<para >By convention, the controller script must be named <literal >controller.xq</literal > (or in
545
- older applications <code >controller.xql</code >). If you wish to allow anonymous users to
546
- access resources using the URL Rewriting framework then you need to ensure that the
547
- <literal >controller.xq</literal > script is world-executable, e.g.,
548
- <code >sm:chmod(xs:anyURI("/path/to/controller.xq")), "o+x")</code >.</para >
548
+ older applications <code >controller.xql</code >; if both <literal >controller.xq</literal > and
549
+ <code >controller.xql</code > are placed in the same collection, the former takes precedence,
550
+ and the latter is ignored).</para >
551
+ <para >If you wish to allow anonymous users to access resources using the URL Rewriting framework
552
+ then you need to ensure that the <literal >controller.xq</literal > script is world-executable,
553
+ e.g., <code >sm:chmod(xs:anyURI("/path/to/controller.xq")), "o+x")</code >.</para >
549
554
<para >By default, the URL Rewriting framework will try to guess the path to the controller
550
555
script by looking at the request path, starting with the deepest, most specific collection,
551
- and then looking into each parent collection until the controller script is found or the root
552
- of the database has been reached.</para >
556
+ and then examining each parent collection until a controller script is found or the root of
557
+ the database has been reached.</para >
553
558
<para >This can be configured and overridden via the <literal >controller-config.xml</literal >
554
559
configuration file in <literal >$EXIST_HOME/etc/webapp/WEB-INF</literal >, which defines the
555
560
base mappings used.</para >
@@ -576,7 +581,7 @@ declare variable $exist:resource external;
576
581
entire web application, and no additional handling of the servlet paths is necessary in the
577
582
controller script.</para >
578
583
<para >For example, if we had registered a servlet mapping for <literal >/rest</literal > in
579
- <literal >web.xml</literal >, we would jave needed to make sure that this path is ignored in
584
+ <literal >web.xml</literal >, we would have needed to make sure that this path is ignored in
580
585
our main <literal >controller.xq</literal > scripts. However, since the mapping is done via
581
586
<literal >controller-config.xml</literal >, XQueryURLRewrite handles the path, which then
582
587
doesn't need to be accounted for in our controller.</para >
0 commit comments