Basic Examples

Splash Forums Rewrite Users Basic Examples

This topic contains 34 replies, has 3 voices, and was last updated by  Lincoln Baxter III 5 years, 5 months ago.

Viewing 15 posts - 1 through 15 (of 35 total)
  • Author
    Posts
  • #18253

    bean77
    Participant

    I have setup rewrite which does work now but the documentation does lack a bit of examples on simple used cases.

    For example I would like to forward every request that matches http://mydomain/myapp/html/RESOURCE to http://mydomain/myapp/RESOURCE (simply cut out something). In addition I want to remove all the .jsp extensions inside every path.

    Hope to hear from you – Thanks :)

    #22304

    Hi There,

    Yes, we really need to work on our documentation for Rewrite – it’s a very new project.

    To do this, you can create a rule like so:

    .defineRule().when(Path.matches("/html/{resource}")).perform(Forward.to("/{resource}"))

    or you could do it the simpler way:

    .addRule(Join.path("/html/{resource}").to("/{resource}"))

    To answer your second question, you can do something like this:

    .addRule(Join.path("/{file}.jsp").to("/{file}").where("file").matches(".*").withInboundCorrection())

    And that should handle just about all the scenarios :), however, note that this is a VERY general rule, and you should be careful that you do not match more than you intended.

    ~Lincoln

    #22305

    bean77
    Participant

    Thanks for the quick response but ReWrite does not work up to now.

    The RewriteFilter starts up successfully on application deployment -> when I hit one of the defined URL patterns like ‘http://mydomain/myapp/html/RESOURCE’ it returns a 404 with no logged output on JBoss AS7 ?

    (using version 1.0.0.Alpha11)

    #22306

    Is your ConfigurationProvider correctly picked up by Rewrite? You should see a log on the console telling about the providers found. And could you post your rules and which URL you entered that didn’t work correctly?

    #22307

    bean77
    Participant

    I think it does startup correctly:

    10:29:53,450 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) RewriteFilter starting up…

    10:29:53,458 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [0] com.ocpsoft.rewrite.servlet.spi.RewriteLifecycleListener []

    10:29:53,465 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [1] com.ocpsoft.rewrite.servlet.spi.RequestCycleWrapper [com.ocpsoft.rewrite.servlet.impl.HttpRewriteRequestCycleWrapper<0>]

    10:29:53,472 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [1] com.ocpsoft.rewrite.spi.RewriteProvider [com.ocpsoft.rewrite.servlet.impl.DefaultHttpRewriteProvider<0>]

    10:29:53,479 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [1] com.ocpsoft.rewrite.servlet.spi.InboundRewriteProducer [com.ocpsoft.rewrite.servlet.impl.HttpInboundRewriteProducer<0>]

    10:29:53,489 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [1] com.ocpsoft.rewrite.servlet.spi.OutboundRewriteProducer [com.ocpsoft.rewrite.servlet.impl.HttpOutboundRewriteProducer<0>]

    10:29:53,503 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [0] com.ocpsoft.rewrite.servlet.spi.ContextListener []

    10:29:53,518 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [0] com.ocpsoft.rewrite.servlet.spi.RequestListener []

    10:29:53,534 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [0] com.ocpsoft.rewrite.servlet.spi.RequestParameterProvider []

    10:29:53,548 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [0] com.ocpsoft.rewrite.spi.ExpressionLanguageProvider []

    10:29:53,567 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [0] com.ocpsoft.rewrite.spi.InvocationResultHandler []

    10:29:53,573 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [0] com.ocpsoft.common.spi.ServiceEnricher []

    10:29:53,579 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) Loaded [1] com.ocpsoft.rewrite.config.ConfigurationProvider [com.example.UrlRewriteConfig<10>]

    10:29:53,584 INFO [com.ocpsoft.rewrite.servlet.RewriteFilter] (MSC service thread 1-7) RewriteFilter initialized.

    Here is my config:

    return ConfigurationBuilder.begin() .defineRule().when(Path.matches(“/html/{resource}”)).perform(Forward.to(“/{resource}”))

    .addRule(Join.path(“/{file}.jsp”).to(“/{file}”).where(“file”).matches(“.*”));

    #22308

    And this is not working for you? I’m very surprised. Could you attach your entire application as a ZIP or GZ to this thread? (see attachments below.)

    #22309

    Your example should work, it’s possible there is something else going on. Did you forget to access your context-path/ ?

    #22310

    bean77
    Participant

    :P sorry my previous post was maybe a bit in confusion

    ReWrite itself is working absolutely correct but the rule which I asked for was simply not what I was thinking of. Because I want when you hit a URL like for example ‘/myapp/test.jsp’ the user should see in his browser ‘/myapp/test’ without the ‘.jsp’ extension. Nevertheless the content which should be served from the server should be still the one from ‘/myapp/test.jsp’ (same with the /html/ inside a URL)

    I explained it wrong by using the word forward with is technically not what I want …

    Is this possible with ReWrite?

    #22311

    You want to use “withInboundCorrection”

    .addRule(Join.path("/{file}.jsp").to("/{file}").where("file").matches(".*").withInboundCorrection());

    #22312

    bean77
    Participant

    But .matches(“.*”) returns a JoinParameterBuilder which has no method .withInboundCorrection() ? (that’s a method of Join)

    #22313

    This should be fixed in latest SNAPSHOT. You’re right – this was broken!

    #22314

    bean77
    Participant

    Now we are closely to what I want :)

    (this example should explain what I wanted to achieve with ‘.withInboundCorrection()’ – I still think this not doing what I want)

    #22315

    GAH! I see what I did wrong, sorry:

    public class UrlRewriteConfig extends HttpConfigurationProvider
    {

    @Override
    public int priority()
    {
    return 10;
    }

    @Override
    public Configuration getConfiguration(final ServletContext context)
    {
    return ConfigurationBuilder
    .begin()
    // hide .jsp extension
    .addRule(Join.path("/{file}").to("/{file}.jsp").where("file").matches("[^/.]+").withInboundCorrection()
    .when(DispatchType.isRequest()))
    // hide /html/ inside path
    .addRule(Join.path("/html/{resource}").to("/{resource}").where("resource").matches(".*")
    .withInboundCorrection());
    }

    }

    Do you understand why these are changed?

    1. Join.path("/{file}").to("/{file}.jsp")

    2. .where("file").matches("[^/.]+")

    ~Lincoln

    #22316

    bean77
    Participant

    Yes now I got it!

    Thanks a lot for the great support :)

    (after flipping also the second part path (“/{resource}”).to(“/html/{resource}”) everything is perfect)

    #22317

    bean77
    Participant

    :D Sorry but there is just one little thing missing. How can I add exceptions to rules?

    Because I defined in web.xml:

    <error-page>

    <error-code>404</error-code>

    <location>/html/error.jsp</location>

    </error-page>

    Which leads to:

    ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host]] (http-127.0.0.1-8080-5) Exception Processing ErrorPage[errorCode=404,location=/html/error.jsp]: java.lang.UnsupportedOperationException at …

Viewing 15 posts - 1 through 15 (of 35 total)

You must be logged in to reply to this topic.

Comments are closed.