URL Rewrite on ratpack

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

URL Rewrite on ratpack

Roi
Hey!

I am trying to add a url rewrite rule on my ratpack server but I can't figure it out.
While controlling it via configuration will be ideal I don't mind add it to the RatpackServer setup in code (we are using java).

I basically want to rewrite  query so that it will eventually handle by one of the existing handlers, something like:
a/b/c.txt?... => a/d/c.txt?...

where a/d had an handler in the ratpack server.

is this possible? I couldn't find anything online about it

Thanks in advance,
Roi
Reply | Threaded
Open this post in threaded view
|

Re: URL Rewrite on ratpack

danhyun
There are a few mechanisms for delegating request processing.

import ratpack.handling.Handler;
import ratpack.server.RatpackServer;

public class Main {
  public static void main(String[] args) throws Exception {

    Handler d = ctx -> ctx.render("From D");

    RatpackServer.start(ratpackServerSpec -> ratpackServerSpec
      .handlers(chain -> chain
        .prefix("a", a -> a
          .get("b/:file", ctx -> ctx.render("a/b " + ctx.getRequest().getRawUri()))
          .get("d/:file", d))
        .prefix("b", b -> b
          .get("b/:file", ctx -> {
            String file = ctx.getPathTokens().get("file");
            ctx.redirect("/b/d/" + file); // use full redirect
          })
          .get("d/:file", d))
        .prefix("c", c -> c
          .get("d/:file", d)
          .get(":prefix/:file", ctx -> {
            String prefix = ctx.getPathTokens().get("prefix");
            if ("b".equals(prefix)) {
              ctx.insert(d); // use ratpack.handling.Context#insert(Handler...) to delegate to given handler
            } else {
              ctx.render("else block");
            }
          }))
      )
    );
  }
}

In the previous example, prefix for "a" is presumably close to something you have setup now, two individual handlers registered to two separate endpoints.

The chain for prefix "b" demonstrates using a full http redirect in which you tell the client to redirect to "/b/d/c.txt"

The chain for prefix "c" demonstrates handler delegation via Context#next() http://ratpack.io/manual/current/api/ratpack/handling/Context.html#next--

In this case we're invoking Context#insert(Handler...) http://ratpack.io/manual/current/api/ratpack/handling/Context.html#insert-ratpack.handling.Handler...-

Which dynamically appends the d handler to the c chain and invokes Context#next() on our behalf.

Hope this helps.
Roi
Reply | Threaded
Open this post in threaded view
|

Re: URL Rewrite on ratpack

Roi
Thanks Danhyun for the quick response, this is very helpful!
Reply | Threaded
Open this post in threaded view
|

Re: URL Rewrite on ratpack

danhyun
Glad to help. Please consider signing up for our slack channel.

Signup - http://slack-signup.ratpack.io/
Slack URL - http://slack.ratpack.io/

It's very handy for asking questions and getting quick feedback.

Don't hesitate to ask more questions.