time condition in rewrite rule along with other rules

Splash Forums Rewrite Users time condition in rewrite rule along with other rules

This topic contains 9 replies, has 3 voices, and was last updated by  Serkan Durusoy 5 years, 6 months ago.

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #18220

    Serkan Durusoy
    Participant

    Hi Everyone,

    I have the following configuration:

    package com.dna.bifincan.root.urlrewrite;

    import com.ocpsoft.rewrite.config.Configuration;

    import com.ocpsoft.rewrite.config.ConfigurationBuilder;

    import com.ocpsoft.rewrite.config.Direction;

    import com.ocpsoft.rewrite.servlet.config.HttpConfigurationProvider;

    import com.ocpsoft.rewrite.servlet.config.Path;

    import com.ocpsoft.rewrite.servlet.config.Redirect;

    import javax.servlet.ServletContext;

    public class BifincanConfigurationProvider extends HttpConfigurationProvider {

    @Override

    public int priority() {

    return 10;

    }

    @Override

    public Configuration getConfiguration(final ServletContext context) {

    return ConfigurationBuilder.begin()

    .defineRule()

    .when(Direction.isInbound().and(Path.matches(“{path}”)

    .where(“path”).matches(“.*”)))

    .perform(Redirect.temporary(“/fi{path}”));

    }

    }

    reference: http://ocpsoft.org/support/topic/redirect-from-root-app-to-app-with-other-context-in-same-host-in-tomcat

    Now, I also want a time based rule integrated into this, namely:

    If current time is before some date (for example 2012-03-15 00:00:00), then don’t use the above config, but instead redirect (maybe forward) all requests to /somepath/somepage.html

    I’ll use this to deploy the app, but have a countdown page and have the app show the countdown page until the grand opening

    So, does anyone have an idea how I can accomplish this within rewrite?

    Thanks

    #22270

    Rewrite provides a JodaTime integration module.

    <dependency>
    <groupId>com.ocpsoft.rewrite</groupId>
    <artifactId>rewrite-config-jodatime</artifactId>
    <version>1.0.0.Alpha11</version>
    </dependency>

    With this you can do something like:

    return ConfigurationBuilder.begin()
    .defineRule()
    .when(Direction.isInbound().and(Path.matches("{page}").where("page").matches(".*"))
    .and(JodaTime.matches(new TimeCondition() {
    @Override
    public boolean matches(DateTime date) {
    return date.isAfter(new DateMidnight(2013, 1, 1));
    }
    })))
    .perform(Redirect.temporary("/myapp/{page}"));

    You can do EVERYTHING with Rewrite! ;)

    #22271

    Serkan Durusoy
    Participant

    Wow, that was great, thanks !!

    And if I don’t want to bloat my libraries with jodatime, how can use builtin date/calendar packages?

    Can I use basic date operators within .and(JodaTime.matches(new TimeCondition() instead of using JodaTime?

    #22272

    Serkan Durusoy
    Participant

    Hmm, I think instead of complicating the configuration,

    can I write more than one configuration provider

    use one for time

    and the other for general purpose redirect

    and set priority for time config 10

    and set priority for general config 20

    would that work?

    #22273

    Yes, you could create different configuration with different priorities to separate the rules. This totally makes sense if you have completely independent configurations.

    If you don’t want to use JodaTime for this, you could implement a matching condition yourself. Something like:

    return ConfigurationBuilder.begin()
    .defineRule()
    .when(Direction.isInbound().and(Path.matches("{page}").where("page").matches(".*"))
    .and(new Condition() {
    @Override
    public boolean evaluate(Rewrite rewrite, EvaluationContext ctx)
    {
    // do whatever you want
    }
    }))
    .perform(Redirect.temporary("/myapp/{page}"));

    Just return true if you want the condition to match and false otherwise.

    #22274

    Serkan Durusoy
    Participant

    This was really helpful.

    By the way, are you planning a documentation for rewrite?

    I mean yes, there are good examples (although they are confusing since most are embedded in the socialpm project) but a document like the one for prettyfaces would be very good.

    Not only for developers, but for you guys as well, documentation is key to getting good traction, promoting rewrite, getting people like me to use it without being afraid and all :)

    #22275

    You are totally right. We will definitively write some documentation soon.

    The only problem is that currently Rewrite has only some Alpha release. So we are currently not sure whether there will be major API changes until 1.0 get’s released. So it makes sense for us to do the documentation a bit later.

    BTW: We are really looking for feedback on the Rewrite API. If you have any ideas or problems please let us know! :)

    #22276

    Serkan Durusoy
    Participant

    We are going to use rewrite on a hopefully high traffic web site, although for basic purposes (to pass root paths to app paths), but I’ll share our experience once we get some stats.

    keep up the good work!

    #22277

    Awesome! I’d love to know how your app performs, if and where there are any performance bottlenecks! Is the API suitable to your needs?

    Looking forward to your feedback!

    Also, if you’d like to get involved in the source-code, feel free to hack on GitHub! https://github.com/ocpsoft/rewrite

    Everyone is welcome to contribute!

    My biggest questions right now are:

    * Where and how do you think we should organize the built-in Conditions/Rules?

    * What package structure makes the most sense?

    * Are the static Condition builders (e.g. Redirect.to(“blah”)) convenient or confusing?

    #22278

    Serkan Durusoy
    Participant

    Hi Lincoln,

    It is too early for me to reply your questions since we are in our first few days into using rewrite, and also ours is a very basic use-case, but I’ll sure share our experience.

    Thanks

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

You must be logged in to reply to this topic.

Comments are closed.