Skip to content

Commit 498976f

Browse files
committed
feat: redirect legacy URLs with parameters
- redirect /index.html to / with parameters q, action and map type parameter to where - redirect /browse.html to /browse with parameters w3c, extensions and appmodules - redirect /view.html to /view with parameters location, uri and function - add tests for redirects
1 parent 39540fc commit 498976f

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed

src/main/xar-resources/controller.xq

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ declare variable $local:method := lower-case(request:get-method());
1313
declare variable $local:is-get := $local:method eq 'get';
1414
declare variable $local:user := login:set-user("org.exist.login", (), false());
1515

16+
declare function local:map-type-to-where ($type as xs:string?) as xs:string {
17+
(
18+
map:get(
19+
map {
20+
"name": "everywhere",
21+
"signature": "signature",
22+
"desc": "description"
23+
},
24+
$type
25+
),
26+
'everywhere'
27+
)[1]
28+
};
29+
1630
declare function local:render-view($view as xs:string) {
1731
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
1832
<forward url="templates/pages/{$view}.html"/>
@@ -25,10 +39,58 @@ declare function local:render-view($view as xs:string) {
2539
</dispatch>
2640
};
2741

28-
if ($exist:path eq '') then (
42+
declare function local:render-parameters ($parameters as map(*)) {
43+
map:for-each($parameters, function ($k, $v) {
44+
if (empty($v)) then () else (
45+
$k || '=' || escape-uri($v, true())
46+
)
47+
})
48+
=> string-join('&amp;')
49+
};
50+
51+
(: redirects :)
52+
if ($local:is-get and $exist:path eq '') then (
2953
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
3054
<redirect url="{concat(request:get-uri(), '/')}"/>
3155
</dispatch>
56+
) else if ($local:is-get and $exist:path eq '/index.html') then (
57+
let $url := substring-before(request:get-uri(), 'index.html')
58+
59+
let $parameters := map{
60+
'q': request:get-parameter('q', ()),
61+
'action': 'search',
62+
'where': local:map-type-to-where(request:get-parameter('type', ()))
63+
}
64+
65+
return
66+
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
67+
<redirect url="{$url}?{local:render-parameters($parameters)}" />
68+
</dispatch>
69+
) else if ($local:is-get and $exist:path eq '/view.html') then (
70+
let $url := substring-before(request:get-uri(), '.html')
71+
let $parameters := map{
72+
'location': request:get-parameter('location', ()),
73+
'uri': request:get-parameter('uri', ()),
74+
'function': request:get-parameter('function', ())
75+
}
76+
77+
return
78+
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
79+
<redirect url="{$url}?{local:render-parameters($parameters)}" />
80+
</dispatch>
81+
82+
) else if ($local:is-get and $exist:path eq '/browse.html') then (
83+
let $url := substring-before(request:get-uri(), '.html')
84+
let $parameters := map{
85+
'w3c': request:get-parameter('w3c', ()),
86+
'extensions': request:get-parameter('extensions', ()),
87+
'appmodules': request:get-parameter('appmodules', ())
88+
}
89+
90+
return
91+
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
92+
<redirect url="{$url}?{local:render-parameters($parameters)}" />
93+
</dispatch>
3294
) else if ($local:is-get and $exist:path eq "/") then (
3395
local:render-view('index')
3496
) else if ($local:is-get and $exist:path eq "/view") then (

src/main/xar-resources/modules/app.xqm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ declare
414414
%templates:default("appmodules", "false")
415415
function app:showmodules(
416416
$node as node(), $model as map(*),
417-
$w3c as xs:boolean, $extensions as xs:boolean, $appmodules as xs:boolean
417+
$w3c as xs:boolean?, $extensions as xs:boolean?, $appmodules as xs:boolean?
418418
) as element(tr)* {
419419
for $module in $app:data
420420
let $uri := $module/xqdoc:module/xqdoc:uri
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* global cy */
2+
/// <reference types="cypress" />
3+
4+
context('Visiting a legacy URL', () => {
5+
it('redirects to the new index route', () => {
6+
cy.visit('index.html?action=search&type=name&q=tei')
7+
cy.url().should('equal', 'http://localhost:8080/exist/apps/fundocs/?action=search&where=everywhere&q=tei')
8+
})
9+
it('redirects to the new view route', () => {
10+
cy.visit('view.html?uri=http://exist-db.org/xquery/file&function=file:sync&arity=3&location=java:org.exist.xquery.modules.file.FileModule')
11+
cy.url().should('equal', 'http://localhost:8080/exist/apps/fundocs/view?location=java%3Aorg.exist.xquery.modules.file.FileModule&uri=http%3A%2F%2Fexist-db.org%2Fxquery%2Ffile&function=file%3Async')
12+
})
13+
it('redirects to the new browse URL', () => {
14+
cy.visit('browse.html?w3c=true&appmodules=true')
15+
cy.url().should('equal', 'http://localhost:8080/exist/apps/fundocs/browse?w3c=true&appmodules=true')
16+
})
17+
})

0 commit comments

Comments
 (0)