Spark URL pattern

I am trying to create a filter that matches the following URL:

/foo and /foo/*

So everything under /foo/ as well as the base case /foo

I have this filter:

  Spark.before("/foo/*", (request, response) -> { String ticket = request.cookie("session"); if (ticket == null) { Spark.halt(302); } }); 

But of course this fails when I enter /foo

I tried the following but no luck:

/foo* /foo.* /foo/

Anyway, for this? Or maybe use a list of URLs? so that I can assign both url to the same filter.

And please don’t say to save the function in a variable so that I use it twice, because I think it is not at all clean.

+8
java url-routing spark-java
source share
1 answer

According to https://github.com/perwendel/spark/blob/1ecd428c8e2e5b0d1b8f221e9bf9e82429bd73bb/src/main/java/spark/route/RouteEntry.java#L31 (where the path mapping happens), this doesn't seem to do that.

The RouteEntry code breaks the given pattern and the given URL into a β€œ/” character, and then searches for β€œmatches” (equality or wildcard) for each component.

Here is a more detailed explanation:

The URL /foo/blah has 2 parts (in the terminology of the RouteEntry code above), and /foo 1 part. For a template that matches the first, it should have 2 parts: /foo/* - the only one that makes sense. But this pattern has 2 parts, which makes /foo unsuccessful if the check is performed on both lines 49 and 78. The only special case is to break line 71, which should make the pattern /foo/* match URL /foo/ , but not /foo .

+7
source share

All Articles