-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.xql
277 lines (253 loc) · 12 KB
/
controller.xql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
xquery version "3.0";
import module namespace login="http://exist-db.org/xquery/login" at "resource:org/exist/xquery/modules/persistentlogin/login.xql";
import module namespace console="http://exist-db.org/xquery/console";
import module namespace templates="http://exist-db.org/xquery/templates";
declare variable $exist:path external;
declare variable $exist:resource external;
declare variable $exist:controller external;
declare variable $exist:prefix external;
declare variable $exist:root external;
console:log("controller path: " || $exist:path),
(:util:log('info', util:unescape-uri($exist:path,'UTF-8')),:)
if ($exist:path eq '') then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{request:get-uri()}/"/>
</dispatch>
else if ($exist:path = "/") then(
console:log("matched '/'" || $exist:path),
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="index.html"/>
</dispatch>
)
(:
restricted.html is secured by the following rules
:)
else if (ends-with($exist:path, "restricted.html") or ends-with($exist:path,".xql")) then (
(: login:set-user creates a authenticated session for a user :)
login:set-user("org.exist.login", (), true()),
(:
the login:set-user function internally sets the following request attribute. If this is set we have a logged in
user.
:)
let $user := request:get-attribute("org.exist.login.user")
(: when the request comes in with a user request param the request was sent by a login form :)
let $userParam := request:get-parameter("user","")
(: in case of a logout we get a request param 'logout' :)
let $logout := request:get-parameter("logout",())
(:let $result := if (not($userParam != data($user))) then "true" else "false":)
return
(:
when we get a logout the user is redirected to the index.html page in this example. The redirect target
can be changed to application needs. E.g. redirecting to restricted.html here would pop up the login page
again as the user is not logged in any more.
:)
if($logout = "true") then(
(:
When there is a logout request parameter we send the user back to the unrestricted page.
:)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="index.html"/>
</dispatch>
)
else if ($user and sm:is-dba($user)) then
(
if (ends-with($exist:path,".xql")) then
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
<error-handler>
<forward url="{$exist:controller}/error-page.html" method="get"/>
<forward url="{$exist:controller}/modules/view.xql"/>
</error-handler>
</dispatch>
else
(:
successful login. The user has authenticated and is in the 'dba' group. It's important however to keep
the cache-control set to 'cache="no"'. Otherwise re-authentication after a logout won't be forced. The
page will get served from cache and not hit the controller any more.
:)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<cache-control cache="no"/>
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
<error-handler>
<forward url="{$exist:controller}/error-page.html" method="get"/>
<forward url="{$exist:controller}/modules/view.xql"/>
</error-handler>
</dispatch>
)
else if(not(string($userParam) eq string($user))) then
(:
if a user was send as request param 'user'
AND it is NOT the same as $user
a former login attempt has failed.
Here a duplicate of the login.html is used. This is certainly not the most elegant solution. Just here
to not complicate things further with templating etc.
:)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="/fdsn-station/fail.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
</dispatch>
else
(: if nothing of the above matched we got a login attempt. :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="/fdsn-station/login.html"/>
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
</dispatch>
)
else
(:import module namespace stationutil="http://exist-db.org/apps/fdsn-station/modules/stationutil" at "modules/util.xql";:)
(:declare variable $exist:path external;:)
(:declare variable $exist:resource external;:)
(:declare variable $exist:controller external;:)
(:declare variable $exist:prefix external;:)
(:declare variable $exist:root external;:)
if ($exist:path eq '') then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="{request:get-uri()}/"/>
</dispatch>
)
else if ($exist:path eq "/") then (
(: forward root path to index.xql :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<redirect url="index.html"/>
</dispatch>
)
else if (ends-with($exist:resource, ".html")) then (
(: the html page is run through view.xql to expand templates :)
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<view>
<forward url="{$exist:controller}/modules/view.xql"/>
</view>
<error-handler>
<forward url="{$exist:controller}/error-page.html" method="get"/>
<forward url="{$exist:controller}/modules/view.xql"/>
</error-handler>
</dispatch>
)
(: Resource paths starting with $shared are loaded from the shared-resources app :)
else if (contains($exist:path, "/$shared/")) then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="/shared-resources/{substring-after($exist:path, '/$shared/')}">
<set-header name="Cache-Control" value="max-age=3600, must-revalidate"/>
</forward>
</dispatch>
)
else if ( $exist:path = "/fdsnws/station/1/query") then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/query.xql">
<set-header name="Cache-Control" value="max-age=60, must-revalidate"/>
<!--<set-header name="PATH" value="query.xql"/>-->
</forward>
</dispatch>
)
else if ($exist:path = "/fdsnws/station/1/application.wadl") then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/Static/application-wadl.xml">
<set-header name="Content-Type" value="application/xml; charset=UTF-8" />
<set-header name="Cache-Control" value="max-age=3600, must-revalidate"/>
</forward>
</dispatch>
)
else if ($exist:path = "/fdsnws/station/1/swagger.json") then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/Static/application-Swagger20.json">
<set-header name="Content-Type" value="application/json; charset=UTF-8" />
<set-header name="Cache-Control" value="max-age=3600, must-revalidate"/>
</forward>
</dispatch>
)
else if ($exist:path = "/fdsnws/station/1/version") then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/version.xql">
<set-header name="Content-Type" value="text/plain; charset=UTF-8" />
<set-header name="Cache-Control" value="max-age=3600, must-revalidate" />
<!-- Passed to version.xql took by request:getparam-->
<!-- <add-parameter name="exist_path" value="{$exist:path}"/>-->
<!-- Passed in response -->
<!-- <set-header name="Exist-path" value="{$exist:path}"/>-->
</forward>
</dispatch>
)
else if ( $exist:path = "/virtualnetwork/1/codes" ) then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/virtualnetwork.xql">
<set-header name="Cache-Control" value="max-age=300, must-revalidate"/>
<!--<set-header name="Exist-path" value="{$exist:path}"/>-->
</forward>
</dispatch>
)
else if ( $exist:path = "/fdsnws/station/1/test" ) then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/test.xql">
<!--<set-header name="Cache-Control" value="max-age=60, must-revalidate"/>-->
<!--<set-header name="Exist-path" value="{$exist:path}"/>-->
</forward>
</dispatch>
)
else if ( $exist:path = "/fdsnws/station/1/" ) then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<set-header name="Content-Type" value="text/html; charset=UTF-8" />
<!--<forward url="{$exist:controller}/static/doc_index.html" method="get"/>-->
<forward url="{$exist:controller}/modules/document_index.xql" method="get"/>
</dispatch>
)
else if ( matches($exist:path ,"/management/network")) then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/management.xql">
<!-- <set-header name="Cache-Control" value="max-age=60, must-revalidate"/>-->
<!--<set-header name="PATH" value="query.xql"/>-->
</forward>
</dispatch>
)
else if (ends-with($exist:resource, ".xml") ) then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<!-- <view> TODO REMOVE view_xml.xql-->
<!-- pass the results through view_xml.xql to display xml resources in browser-->
<!-- <forward url="{$exist:controller}/modules/view_xml.xql">
<set-header name="Content-Type" value="application/xml; charset=UTF-8" />
<set-attribute name="$exist:prefix" value="{$exist:prefix}"/>
<set-attribute name="$exist:controller" value="{$exist:controller}"/>
</forward>
</view> -->
<error-handler>
<forward url="{$exist:controller}/error-page.html" method="get"/>
<forward url="{$exist:controller}/modules/view.xql">
<set-header name="XML-PASS" value="valore"/>
</forward>
</error-handler>
</dispatch>
)
else if (ends-with($exist:resource, ".css")) then (
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<set-header name="Content-Type" value="text/css; charset=UTF-8" />
<!--<forward url="{$exist:controller}/static/doc_index.html" method="get"/>-->
</dispatch>
)
(: everything else is error :)
else
<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
<forward url="{$exist:controller}/modules/errors.xql" method="get">
</forward>
</dispatch>
(:Nel blocco forward :)
(: <set-header name="Exist-path" value="{$exist:path}"/>:)
(: <set-header name="Exist-resource" value="{$exist:resource}"/>:)
(: <set-header name="Exist-root" value="{$exist:root}"/>:)
(: <set-header name="Exist-prefix" value="{$exist:prefix}"/>:)
(: <set-header name="Exist-controller" value="{$exist:controller}"/>:)
(:Exist-controller: /fdsn-station:)
(:Exist-path: /fdsnws/station/1/query:)
(:Exist-prefix: /apps:)
(:Exist-resource: query:)
(:Exist-root: xmldb:exist:///db/apps:)
(: <forward url="{$exist:controller}/modules/view.xql">:)
(: <set-header name="Cache-Control" value="max-age=60, must-revalidate"/>:)
(: <set-header name="Exist-path" value="{$exist:path}"/>:)
(: </forward>:)