how do I forward to other apps, etc?

Splash Forums Rewrite Users how do I forward to other apps, etc?

This topic contains 10 replies, has 2 voices, and was last updated by  Christian Kaltepoth 2 years, 4 months ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #25948

    heyoka
    Participant

    I have created a router app which I want to eventually handle all the traffic to my site. In some cases I want it to forward to a different app. In others, I want it to forward to the html site. I’ve tried both setting the router as the default app and setting an empty context path. The router activates either way. It also properly forwards to a page inside the router app. The issues I have are when I want to forward outside the router app.

    Here’s what I want it to do:
    (1) localhost:8080/other properly forwards to other.jsp in the router app
    (2) localhost:8080/key1 should forward to /anotherapp/task/key1
    (3) localhost:8080/anotherapp/anything should forward to /anotherapp/anything
    (4) localhost:8080 should forward to docroot/index.html
    (5) localhost:8080/anythingelse should forward to docroot/anythingelse

    Here’s what I’ve got so far which I only expect to cover items 1, 2, and 4:

         return ConfigurationBuilder.begin()
           .addRule()
             .when(Direction.isInbound().and(Path.matches("/")))
             .perform(Forward.to("/index.html"))
           .addRule()
             .when(Direction.isInbound().and(Path.matches("/other")))
             .perform(Forward.to("/other.jsp"))
           .addRule()
             .when(Direction.isInbound().and(Path.matches("/key1")))
             .perform(Forward.to("/otherapp/task/key1"));

    I’ve tried forwarding to complete urls, ie localhost:8080/otherapp/task/key1 but it doesn’t work. How do I reference targets outside of the router app?

    • This topic was modified 2 years, 5 months ago by  heyoka.
    #25949

    heyoka
    Participant

    I’ve also tried simpler syntax: .addRule(Join.path(“/”).to(“/index.html”));

    #25950

    heyoka
    Participant

    So I think my problem was using forward rather than redirect. Hard to say since I can’t find doc for either. But this code works:

         return ConfigurationBuilder.begin()
                .addRule()
                    .when(Direction.isInbound().and(Path.matches("/{path}")))
                    .perform(
                         Redirect.temporary("/myapp/task/{path}")
                     )
                    .where("path").matches("firstkey")
                .addRule()
                    .when(Direction.isInbound().and(Path.matches("/{path}")))
                    .perform(
                         Redirect.temporary("/myapp/task/{path}")
                     )
                    .where("path").matches("secondkey");

    Now I am trying to understand how to combine these two rules with a compound where clause, ie where(“path”).matches(“firstkey” or “secondkey”), or better yet where(“path”).in(“firstkey”,”secondkey”,”thirdkey”…)

    Any clues?

    #25952

    heyoka
    Participant

    Turns out I haven’t fixed anything. I’ve got redirects to other apps working, but only when I set the router’s context path to blank. And then I can no longer access anything under docroot. So I’m down to these questions:

    1. How to do a compound where as described above
    2. How to reference files/urls under the docroot which are not apps
    3. (most of all) is there documentation hidden somewhere?

    #25953

    I don’t think that you can do forwards to other web applications in the same container. Forwards are used to forward WITHIN an application. But redirects should work find. You just have to make sure to add the correct context path when doing the redirect.

    The matches() part is a regular expression. So you should be able to do this:

    .where("path").matches("firstkey|secondkey")

    Unfortunately there isn’t much documentation currently. All we have is here:

    http://ocpsoft.org/rewrite/docs/

    I hope this helps.

    #25955

    heyoka
    Participant

    Thanks for your help.

    The redirects do work to different apps if I make the router’s context path blank. But when I do that, I can’t access anything in docroot. For example, I have index.html which I can access fine when the router isn’t there. Do you know how I can access files in docroot?

    Matching multiple keys is much easier than I thought. Thanks for clearing that up. I was trying to use some sort of .or().

    #25956

    Could you explain this docroot issue a bit more? Which docroot? The other files in the ROOT.war application? What do you mean with “accessing” them?

    #25960

    heyoka
    Participant

    I am talking about the glassfish docroot folder from which static pages are served. The only file there is index.html which I can access at localhost:8080/index.html when the router is not in place. But when the router is intercepting requests I can’t reference anything from that folder. App references work so long as the router’s context path is blank.

    #25962

    I think there is nothing you can do about it. If you deploy an app to the root context path, all requests will be processed by this webapp.

    Isn’t the docroot meant to be some kind of fallback if there is no app in the root context?

    I think the only way to work around this would be to package the resource into the router app.

    #25992

    heyoka
    Participant

    OK it IS possible! Here’s how:

    The router app is set with an empty context path. Rules are set to forward to other apps:

                   .addRule()
                    .when(Direction.isInbound().and(Path.matches("/b/{path}")))
                    .perform(
                         Redirect.temporary("/app/task-a/{path}")
                     )
                   .addRule()
                    .when(Direction.isInbound().and(Path.matches("/{path}")))
                    .perform(
                         Redirect.temporary("/app/task/{path}")
                     )
                    .where("path").matches("key1|key2|key3");

    Now here’s the tricky part. I want to have a static directory which serves images to my apps. But since the router has an empty context path, all requests go to it. EXCEPT……

    Glassfish allows you to set up an alternate docroot (besides the default one which is now inaccessible). You do this by adding it to glassfish-web.xml like this:

    <property name="alternatedocroot_1" value="from=/img/* dir=/docroot"/>

    When a request comes in, glassfish first checks against alternate docroots. If it finds one, the request is served from there. Otherwise it goes to the application docroot, sees that there is a default app with an empty context path, and hands the request to this app (router).

    #25993

    Great. Thanks for sharing your solution to the problem.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.

Comments are closed.