Setting contextPath from client side

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

Setting contextPath from client side

Jan Niehusmann
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Setting contextPath from client side

Mike Wilson
Administrator
Hi Jan,

DWR should support moving its path around so I can help you look into
that. But could you first please test and tell me what doesn't seem
to work in DWR without using JAWR at the same time?

BTW, when you write "{contextPath}/dwr", do you mean that you have
actually set this value, or that you are just abbreviating your actual
context path with {contextPath} for easier reading on the forum?

Best regards
Mike Wilson

Jan Niehusmann wrote:

> Hi,
>
> is it possible to change the context path from client side?
>
> I have the following situation: The web page I'm developing
> is using DWR
> (3.0-RC2), JAWR (3.5.1) and the jawr-dwr3.x-extension
> (3.5.1-SNAPSHOT).
>
> It's accessed either directly, which is working fine, or via a reverse
> proxy which is rewriting URLs. And with the current versions, this
> rewriting is causing trouble:
>
> The Jawr-DWR-Plugin insert the following line into the html page:
>
> <script type="text/javascript">if(!JAWR){var JAWR =
> {};};;JAWR.jawr_dwr_path='/{contextPath}/dwr';JAWR.app_context
> _path='/{contextPath}script>
>
> The two paths contained therein are rewritten by the reverse proxy.
>
> With older versions of dwr this was sufficient to make DWR use that
> rewritten path. As far as I can tell, this was made possible by some
> clever rewriting in DWR3BeanGenerator, which injected references to
> these JAWR.* variables into engine.js.
>
> But now these variables seem to be completely ignored, so DWR accesses
> non-rewritten paths, which of course doesn't work.
>
> I was able to make it work again, using the following incantation:
>
> <script>
>     var pathToDwrServlet = "{contextPath}/dwr"; <%-- to be
> rewritten by reverse proxy --%>
> </script>
> <jwr:script src="/bundles/all.js" useRandomParam="false" />
> <script>
>     <%-- to be rewritten by reverse proxy --%>
>     dwr.engine._contextPath = "{contextPath}";
>     RemoteInterface._path = "{contextPath}/dwr";
> </script>
>
> (where /bundles/all.js is a jawr bundle which contains all
> the dwr stuff
> and other scripts)
>
> This seems to be enough to make most of dwr work with the reverse
> proxy. (File downloads still don't work, and there may be
> other features
> which I just don't use.)
>
> But I guess this is a hacky workaround and not a supported method.
> I wonder if I try to do something which is just not supported
> by DWR, or
> if I'm missing something.
>
> To me, it looks like setting pathToDwrServlet before loading engine.js
> should be enough. That would mean that there is some bug in DWR where
> the context path is hardcoded and pathToDwrServlet should be
> used instead.
> But again, I'm not sure if I'm missing something.
>
> (And, in case it's not obvious: I'm a happy user of dwr,
> thanks for all
> the good work!)
>
> Regards,
> Jan

Reply | Threaded
Open this post in threaded view
|

Re: Setting contextPath from client side

Jan Niehusmann
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Setting contextPath from client side

Mike Wilson
Administrator
Thanks for your explanation Jan, I'll look into this and get back
shortly!

Best regards
Mike

Jan Niehusmann wrote:

> Hi Mike,
>
> On Tue, Jul 29, 2014 at 10:54:23AM +0200, Mike Wilson wrote:
> > DWR should support moving its path around so I can help you
> look into
> > that. But could you first please test and tell me what doesn't seem
> > to work in DWR without using JAWR at the same time?
>
> Sure. While my application is tightly coupled to jawr so I
> can't easlily
> make it work without, I was able to reproduce the same scenario using
> the DWR demo site:
>
> I installed the demo app in a tomcat container (path /demo/), and made
> sure it worked ("DWR tests passed").
>
> Then I placed a reverse proxy in front. This proxy rewrites
> URLs in a way
> that the demo is now accessible on the proxy at a different
> path, /test/X/.
>
> It's expected that this doesn't work, and indeed, it gives
> a popup telling me "Not Found", because it tries to access
> /demo/dwr/call/plaincall/__System.generateId.dwr (which is
> not available
> on the proxy, in contrast to /test/X/dwr/....)
>
> Now I add the following script to index.html, in front of the line
> including engine.js:
>
> <script type='text/javascript'>
>     var pathToDwrServlet = "/demo/dwr";
> </script>
>
> When accessing the page directly, this is just a no-op. But when going
> through the reverse-proxy, it gets rewritten to:
>
> <script type='text/javascript'>
>     var pathToDwrServlet = "/test/X/dwr";
> </script>
>
> So now I'd expect dwr to access
> /test/X/dwr/call/plaincall/__System.generateId.dwr.
> But instread, it doesn't it still uses the old path.
>
> I can make it work by adding the following script to index.html,
> just after loading Intro.js:
>
> <script>
>     dwr.engine._contextPath = "/demo";
>     Intro._path = "/demo/dwr";
> </script>
>
> (Again, this is a no-op when accessing the page directly, but has an
> effect when going through the rewriting reverse-proxy.)
>
> > BTW, when you write "{contextPath}/dwr", do you mean that you have
> > actually set this value, or that you are just abbreviating
> your actual
> > context path with {contextPath} for easier reading on the forum?
>
> It is just a placeholder. The actual context path would have
> been longer
> and it wouldn't have been obvious that it's just the context
> path, so I
> replaced it by {contextPath} in my mail.
>
> Best regards,
> Jan

Reply | Threaded
Open this post in threaded view
|

Re: Setting contextPath from client side

Mike Wilson
Administrator
In reply to this post by Jan Niehusmann
So I've looked at this. First, what you probably want is to use
the <init-param> "overridePath" in web.xml instead of setting
pathToDwrServlet in JS.
See:
http://directwebremoting.org/dwr/documentation/server/configuration/servlet/
index.html

In short, this init-param will affect both pathToDwrServlet and
Intro._path. If you cannot determine the desired path statically
in web.xml then I'm afraid you have to do things the way you are
currently.

Secondly, about having to set dwr.engine._contextPath from JS,
this is an oversight from our side. I'll look into fixing it
and this will probably become official variables similar to the
servlet path with official overrides available from web.xml and
JS. In the meantime your workaround is the best way to solve it.

Best regards
Mike

Jan Niehusmann wrote:

> Hi Mike,
>
> On Tue, Jul 29, 2014 at 10:54:23AM +0200, Mike Wilson wrote:
> > DWR should support moving its path around so I can help you
> look into
> > that. But could you first please test and tell me what doesn't seem
> > to work in DWR without using JAWR at the same time?
>
> Sure. While my application is tightly coupled to jawr so I
> can't easlily
> make it work without, I was able to reproduce the same scenario using
> the DWR demo site:
>
> I installed the demo app in a tomcat container (path /demo/), and made
> sure it worked ("DWR tests passed").
>
> Then I placed a reverse proxy in front. This proxy rewrites
> URLs in a way
> that the demo is now accessible on the proxy at a different
> path, /test/X/.
>
> It's expected that this doesn't work, and indeed, it gives
> a popup telling me "Not Found", because it tries to access
> /demo/dwr/call/plaincall/__System.generateId.dwr (which is
> not available
> on the proxy, in contrast to /test/X/dwr/....)
>
> Now I add the following script to index.html, in front of the line
> including engine.js:
>
> <script type='text/javascript'>
>     var pathToDwrServlet = "/demo/dwr";
> </script>
>
> When accessing the page directly, this is just a no-op. But when going
> through the reverse-proxy, it gets rewritten to:
>
> <script type='text/javascript'>
>     var pathToDwrServlet = "/test/X/dwr";
> </script>
>
> So now I'd expect dwr to access
> /test/X/dwr/call/plaincall/__System.generateId.dwr.
> But instread, it doesn't it still uses the old path.
>
> I can make it work by adding the following script to index.html,
> just after loading Intro.js:
>
> <script>
>     dwr.engine._contextPath = "/demo";
>     Intro._path = "/demo/dwr";
> </script>
>
> (Again, this is a no-op when accessing the page directly, but has an
> effect when going through the rewriting reverse-proxy.)
>
> > BTW, when you write "{contextPath}/dwr", do you mean that you have
> > actually set this value, or that you are just abbreviating
> your actual
> > context path with {contextPath} for easier reading on the forum?
>
> It is just a placeholder. The actual context path would have
> been longer
> and it wouldn't have been obvious that it's just the context
> path, so I
> replaced it by {contextPath} in my mail.
>
> Best regards,
> Jan

Reply | Threaded
Open this post in threaded view
|

Re: Setting contextPath from client side

Jan Niehusmann
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Setting contextPath from client side

Mike Wilson
Administrator
Ah, then you are in the "can't use static" category then. Sorry,
unfortunately we don't have any better way to do this currently.
I would agree if anyone thinks it is a bit confusing that we have
s server-side setting that overrides all paths but client-side
settings that only override individual paths. This is something
that we may look into for the future.

BTW, one very minor improvement is possible; if you do this:
  Intro._path = null;
then Intro will pick up your pathToDwrServlet override.

Best regards
Mike

Jan Niehusmann wrote:

> Hi Mike,
>
> On Fri, Aug 01, 2014 at 04:08:37PM +0200, Mike Wilson wrote:
> > So I've looked at this. First, what you probably want is to use
> > the <init-param> "overridePath" in web.xml instead of setting
> > pathToDwrServlet in JS.
>
> The issue is, setting the init-param overridePath would affect all
> clients - but most clients access the servlet directy, so
> they need the
> original path. Only some go via the reverse proxy.
>
> By setting pathToDwrServlet from the html file, the reverse proxy can
> rewrite the path. (Of course, in theory, the proxy could rewrite paths
> in engine.js. But to do so, it would have to parse and analyse the
> javascript code - not a viable approach, IMHO)
>
> > In short, this init-param will affect both pathToDwrServlet and
> > Intro._path. If you cannot determine the desired path statically
> > in web.xml then I'm afraid you have to do things the way you are
> > currently.
>
> Ok. I guess this is an unusual situation. And in fact, I think this
> rewriting proxy is really bad design and has been the cause
> for several
> issues in the past. Unfortunately, it's not something I can change.
>
> > In the meantime your workaround is the best way to solve it.
>
> Good to know, and thanks having a look!
>
> Best regards,
> Jan