passing request parameters to other servlets in controller?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

passing request parameters to other servlets in controller?

ron.vandenbranden
Hi,

I have a -hopefully stupid- question on URL forwarding (eXist-3.0). I'd
like to create a clean URL for retrieving a PDF file from the db and
rendering it in the PDF.js viewer (https://mozilla.github.io/pdf.js/).
After installing the required files, this can be done directly by
passing following URL to the browser:
http://localhost:8080/exist/myapp/<path-to-PDF.js>/viewer.xhtml?file=<path-to-file>.
The crux is that it needs the PDF file name in a "file" request
parameter. Now, suppose I want to hide the path to that viewer.xhtml
file behind a cleaner URL like
http://localhost:8080/exist/myapp/view/<path-to-file>, I would need to
pass the <path-to-PDF> part to the forwarded request in controller.xql.
This seems harder than I had hoped:

   if (matches($exist:path, '^/viewer/', 'i')) then
     <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
       let $path-to-PDF.js := "path-to-PDF.js"
       let $path-to-file := substring-after($exist:path, '/viewer/')
       return
         <forward url="{$path-to-PDF.js}/viewer.xhtml">
           <add-parameter name="file" value={$path-to-file}"/>
         </forward>
     </dispatch>

When I try this, the <add-parameter/> instruction seems to be ignored,
and no "file" parameter ends up in the forwarded request. Am I missing
anything? I've tried by using

   <forward url="{$path-to-PDF.js}/viewer.xhtml?file={$path-to-file}">

but that didn't work, either.

Therefore, I guess my question is: how do I add request parameters to a
request that's not treated by XQueryServlet?

Best,

Ron

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|

Re: passing request parameters to other servlets in controller?

Joe Wicentowski
Hi Ron,

In your snippet I do not see curly braces inside the <dispatch>
element, so the FLWOR expression inside won't be evaluated.

>    if (matches($exist:path, '^/viewer/', 'i')) then
>      <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
>        let $path-to-PDF.js := "path-to-PDF.js"
>        let $path-to-file := substring-after($exist:path, '/viewer/')
>        return
>          <forward url="{$path-to-PDF.js}/viewer.xhtml">
>            <add-parameter name="file" value={$path-to-file}"/>
>          </forward>
>      </dispatch>

Or was that just omitted from your email?

Joe

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|

Re: passing request parameters to other servlets in controller?

ron.vandenbranden
Hi Joe,

Apologies, things got messed up. This simplified version is correct, but
doesn't work:

   if (matches($exist:path, '^/viewer/', 'i')) then
     <dispatch xmlns="http://exist.sourceforge.net/NS/exist">{
         <forward url="{$exist:root}{$exist:controller}/js/pdf.js/web/viewer.xhtml">
           <add-parameter name="file" value="{substring-after($exist:path, '/viewer/')}"/>
         </forward>
     }</dispatch>

I mean, while the viewer.xhtml file is being found, I find no trace of
the "file" parameter I'm trying to add.

Best,

Ron


On 21/02/2017 5:31, Joe Wicentowski wrote:

> Hi Ron,
>
> In your snippet I do not see curly braces inside the <dispatch>
> element, so the FLWOR expression inside won't be evaluated.
>
>>     if (matches($exist:path, '^/viewer/', 'i')) then
>>       <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
>>         let $path-to-PDF.js := "path-to-PDF.js"
>>         let $path-to-file := substring-after($exist:path, '/viewer/')
>>         return
>>           <forward url="{$path-to-PDF.js}/viewer.xhtml">
>>             <add-parameter name="file" value={$path-to-file}"/>
>>           </forward>
>>       </dispatch>
> Or was that just omitted from your email?
>
> Joe
>


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|

Re: passing request parameters to other servlets in controller?

Alister Pillow-3
In reply to this post by ron.vandenbranden
Hi Ron,
I think I know what the problem is - but I’m having a hard time explaining it. 
The file parameter is only going to be passed to an XQuery - but there’s no query. 

On 21 Feb 2017, at 12:41 am, Ron Van den Branden <[hidden email]> wrote:

Now, suppose I want to hide the path to that viewer.xhtml 
file behind a cleaner URL like 
http://localhost:8080/exist/myapp/view/<path-to-file>, I would need to 
pass the <path-to-PDF> part to the forwarded request in controller.xql. 
This seems harder than I had hoped:

This is an alternative: (controller.xql xquery version 3.1)

if (matches($exist:path, "/view/",'i')) then 
        let $short-path := substring-after($exist:path, "/view/")
        let $full-path := "/db/apps/myapp/path/to/" || $short-path
        return
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
                <forward url="{$exist:controller}/viewer.xql">
                    <add-parameter name="file" value="{$full-path}" />
                    <set-header name="Cache-Control" value="no-cache"/>
                </forward>
        </dispatch>
… etc

And viewer.xql:

xquery version "3.1";

declare option output:method "html5";
declare option output:media-type "text/html";

<html>
    <head><title>Viewer</title>
    <!-- PDF module here -->
    <script>
        var url = '{ request:get-parameter("file", "no/file/param")}';
    </script>
    </head>
    <body>
        <h1>loading</h1>
        <script>
            //PDFJS.getDocument(url);
            document.write("<h2>url " + url + "</h2>");
        </script>
    </body>
</html>

Alternatively, include the PDF module in the original page and use a button or Javascript href to load the file.

<html>
<head>
<title>Viewer</title>
<!-- script src="pdf.js"></script -->
<script>function load(b) {
  var url = b.getAttribute('value');
  console.log('url: ' + url);
  // PDFJS.getDocument(url);
}
</script>
</head>
<body>
<button onclick='load(this);' value='path/to/file.pdf'>My File</button>
</body>
</html>




------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open
Reply | Threaded
Open this post in threaded view
|

Re: passing request parameters to other servlets in controller?

ron.vandenbranden

Hi Alister,

Ok, thanks for confirming the issue: <add-parameter> is probably only effective for requests that are being forwarded to the XQueryServlet. I'm not sure what servlet this request (for a static XHTML file) is forwarded to, but it definitely doesn't receive the parameters, and I don't know how to do it (if it is possible at all).

Many thanks for your helpful suggestions; meanwhile, I've resorted to a similar workaround by generating the page via an XQuery script.

Best,

Ron

On 22/02/2017 0:48, Alister Pillow wrote:
Hi Ron,
I think I know what the problem is - but I’m having a hard time explaining it. 
The file parameter is only going to be passed to an XQuery - but there’s no query. 

On 21 Feb 2017, at 12:41 am, Ron Van den Branden <[hidden email]> wrote:

Now, suppose I want to hide the path to that viewer.xhtml 
file behind a cleaner URL like 
http://localhost:8080/exist/myapp/view/<path-to-file>, I would need to 
pass the <path-to-PDF> part to the forwarded request in controller.xql. 
This seems harder than I had hoped:

This is an alternative: (controller.xql xquery version 3.1)

if (matches($exist:path, "/view/",'i')) then 
        let $short-path := substring-after($exist:path, "/view/")
        let $full-path := "/db/apps/myapp/path/to/" || $short-path
        return
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
                <forward url="{$exist:controller}/viewer.xql">
                    <add-parameter name="file" value="{$full-path}" />
                    <set-header name="Cache-Control" value="no-cache"/>
                </forward>
        </dispatch>
… etc

And viewer.xql:

xquery version "3.1";

declare option output:method "html5";
declare option output:media-type "text/html";

<html>
    <head><title>Viewer</title>
    <!-- PDF module here -->
    <script>
        var url = '{ request:get-parameter("file", "no/file/param")}';
    </script>
    </head>
    <body>
        <h1>loading</h1>
        <script>
            //PDFJS.getDocument(url);
            document.write("<h2>url " + url + "</h2>");
        </script>
    </body>
</html>

Alternatively, include the PDF module in the original page and use a button or Javascript href to load the file.

<html>
<head>
<title>Viewer</title>
<!-- script src="pdf.js"></script -->
<script>function load(b) {
  var url = b.getAttribute('value');
  console.log('url: ' + url);
  // PDFJS.getDocument(url);
}
</script>
</head>
<body>
<button onclick='load(this);' value='path/to/file.pdf'>My File</button>
</body>
</html>





------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
Exist-open mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/exist-open