3

I'm using Ocpsoft Rewrite, to perform URL rewriting in a JSF project. I have a redirect rule, which works fine:

.addRule()
        .when(Direction.isInbound().and(Path.matches("/venue/{id}")))
        .perform(Redirect.temporary(context.getContextPath() + 
                "/protected/index.xhtml?venueID={id}"));

However, because of the redirect, this changes the URL in the navigation bar. I thought I could use a Join rule instead, but it doesn't work as i expected:

.addRule(Join.path("/venue/{venueID}").to("/protected/index.xhtml"))
        .perform(Log.message(Level.INFO, "Rewrite is active!"));

I thought this rule would redirect from, for example, foo/venue/123 to foo/protected/index.xhtml?venueID=123, but I don't get the ?venueID=... parameter appended to the URL.

Anyone knows what the correct rule should look like?

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
user473453
  • 894
  • 2
  • 11
  • 23

2 Answers2

2

Your rule looks correct. But a Join doesn't result in a redirect. Instead it forwards the request internally. This means that the URL isn't changed. So you won't "see" the parameter venueID in the URL. But you will be able to read the parameter using the standard Servlet API:

String id = HttpServletRequest.getParameter("venueID");
chkal
  • 5,598
  • 21
  • 26
  • I have the same problem in a GWT application, but I can't get the parameter on GWT side with: Window.Location.getParameter("param"); – Daniel Hári Jun 21 '16 at 18:55
1

If you really want, you can do a Forward instead:

.addRule()
.when(Direction.isInbound().and(Path.matches("/venue/{id}")))
.perform(Forward.to("/protected/index.xhtml?venueID={id}"));

But this won't handle outbound HTML link correction like Join will!

Lincoln
  • 3,151
  • 17
  • 22
  • OK, but if there are other parameters, those would be forwarded or not? – Daniel Hári Jun 21 '16 at 18:57
  • No, it will only forward the parameters you specify in the pattern. – Lincoln Jun 22 '16 at 14:17
  • However, as Christian mentioned earlier. All parameters, including original ones from the original URL, will be available via the standard Servlet API: – Lincoln Jun 22 '16 at 14:18
  • String id = HttpServletRequest.getParameter("venueID"); – Lincoln Jun 22 '16 at 14:18
  • How to reach those parameters on GWT side? Window.Location.getParameter("param") does not work. – Daniel Hári Jun 22 '16 at 14:31
  • You will need to provide the parameters via a web-service. Server-side parameters are not available on the client side unless you expose them directly. – Lincoln Jun 22 '16 at 17:58
  • You can also parse the values out of the URL directly on the client side if you need them. – Lincoln Jul 05 '16 at 14:11
  • But in that case on change I have to also rewrite manually on the client side skipping the ocl library rigth? To be concrete, I want to pretty this app http://log4jtester.com/ to replace "?p=" with "/pattern/" for example. (It rewrites url instantly as pattern changes) – Daniel Hári Jul 05 '16 at 14:56
  • Not at all. You just need to parse the new URL of whatever the page URL is, and pull out the information you need on the client side: `window.getLocation()` etc... It's completely independent of the server side. – Lincoln Jul 05 '16 at 14:59