Rewrite every *.jsf

Splash Forums PrettyFaces Users Rewrite every *.jsf

This topic contains 24 replies, has 4 voices, and was last updated by  Lincoln Baxter III 2 years, 10 months ago.

Viewing 15 posts - 1 through 15 (of 25 total)
  • Author
    Posts
  • #17850

    Ulisses Constantini
    Participant

    I have a question. It’s possible create a rule to rewrite every request to put .jsf in the end?

    Eg: Request: myapp/admin/mypage rewrite to myapp/admin/mypage.jsf

    Thanks

    #20586

    Yep!

    http://ocpsoft.com/docs/prettyfaces/snapshot/en-US/html_single/#inbound_rewriting

    <rewrite match="^(.*)(?!\.jsf)$" substitute="/$1.jsf" redirect="301"/>

    #20587

    Ulisses Constantini
    Participant

    I think you didn’t understand. I only want something like

    myapp/admin/mypage, without respond with a redirect, but my server understand that i request myapp/admin/mypage.jsf.

    My link will be myapp/admin/mypage and this will appear on

    address bar.

    This is possible?`

    #20588

    Change 301 to chain in my example. This is all in the documentation :)

    #20589

    Ulisses Constantini
    Participant

    The regex ^(.*)(?!\.jsf)$ did’t work.

    Any request is being rewritten.

    I request: http://localhost:8080/myapp/teste

    and the rewrite rule entered into a loop…

    See the stack:

    javax.servlet.ServletException: com.ocpsoft.pretty.PrettyException: Error occurred during canonicalization of request <[/teste]>

    root cause

    com.ocpsoft.pretty.PrettyException: Error occurred during canonicalization of request <[/teste]>

    root cause

    javax.servlet.ServletException: com.ocpsoft.pretty.PrettyException: Error occurred during canonicalization of request <[/teste.jsf]>

    root cause

    com.ocpsoft.pretty.PrettyException: Error occurred during canonicalization of request <[/teste.jsf]>

    root cause

    javax.servlet.ServletException: com.ocpsoft.pretty.PrettyException: Error occurred during canonicalization of request <[/teste.jsf.jsf]>

    root cause

    com.ocpsoft.pretty.PrettyException: Error occurred during canonicalization of request <[/teste.jsf.jsf]>

    root cause

    javax.servlet.ServletException: com.ocpsoft.pretty.PrettyException: Error occurred during canonicalization of request <[/teste.jsf.jsf.jsf]>

    ......

    I try to change the regex without success.

    Any suggestion?

    #20590

    Try creating a rewrite-processor instead of using Regexes, you’ll have more control in Java:

    http://ocpsoft.com/docs/prettyfaces/3.2.0/en-US/html/inbound_rewriting.html#inbound_rewriting.options

    #20591

    Ulisses Constantini
    Participant

    Thanks Lincoln. This solved my problem.

    #20592

    Ulisses Constantini
    Participant

    More one issue. It’s possible rewrite only the 404’s requests?

    #20593

    mraible
    Participant

    I’m trying to do something similar – where every request that comes through tries to forward to *.jsf (if there isn’t a previous mapping that forwards it to a different resource). With the UrlRewriteFilter, I’m almost able to accomplish what I want using the following:

    <rule>

    <from>/</from>

    <to type="redirect" last="true">mainMenu</to>

    </rule>

    <rule>

    <from>/j_security_check</from>

    <to>/j_security_check</to>

    </rule>

    <rule>

    <from>/images/**</from>

    <to>/images/$1</to>

    </rule>

    <rule>

    <from>/scripts/**</from>

    <to>/scripts/$1</to>

    </rule>

    <rule>

    <from>/styles/**</from>

    <to>/styles/$1</to>

    </rule>

    <rule>

    <from>/login*</from>

    <to>/login.jsp</to>

    </rule>

    <rule>

    <from>/logout*</from>

    <to>/logout.jsp</to>

    </rule>

    <!-- Struts -->

    <rule match-type="regex">

    <from>^([^?]*)/([^?/.]+)(?.*)?$</from>

    <to last="true">$1/$2.jsf$3</to>

    </rule>

    <outbound-rule match-type="regex">

    <from>^(.*).jsf(?.*)?$</from>

    <to last="false">$1$2</to>

    </outbound-rule>

    However, I’ve run into an issue where UrlRewriteFilter intercepts a post on my File Upload form and doesn’t submit it to my managed bean properly. So I’ve turned to Pretty Faces in hopes of getting something similar setup.

    Here’s what I have so far:

    <rewrite match="^/login$" substitute="/login.jsp" redirect="chain"/>

    <rewrite match="/$" substitute="/mainMenu" redirect="302"/>

    <rewrite match="^([^?]*)/([^?/.]+)(?.*)?$" substitute="$1/$2.jsf$3" redirect="chain"/>

    Unfortunately, this results in the following, which I’m guessing is caused by a continuous redirect.

    2011-01-28 07:58:49.761:WARN::Error for /mainMenu

    java.lang.StackOverflowError

    at java.util.HashMap.removeEntryForKey(HashMap.java:548)

    at java.util.HashMap.remove(HashMap.java:538)

    at org.mortbay.util.AttributesMap.removeAttribute(AttributesMap.java:51)

    at org.mortbay.jetty.servlet.Dispatcher$ForwardAttributes.setAttribute(Dispatcher.java:441)

    at org.mortbay.jetty.servlet.Dispatcher$ForwardAttributes.removeAttribute(Dispatcher.java:461)

    Does Pretty Faces have the ability to say “last” on a rewrite rule and forward from there w/o processing again?

    Thanks,

    Matt

    #20594

    Hey Matt,

    I was going to write a longer post to answer your question, but my first glance tells me that your assumption is correct; you’re using a regexp that will continually loop –

    You might consider using a negative lookahead (or lookbehind) for the purposes of restricting what is actually matched. Unfortunately, we don’t have the concept of *last* yet (it might take some header manipulation/trickery for that to work.)

    Negative lookbehind:

    (?<!.jsf)

    <rewrite match="^([^?]*)/([^?/.]+)(?<!.jsf)(?.*)?$" substitute="$1/$2.jsf$3" redirect="chain"/>

    This should prevent matching on URLs that already inclue .jsf.

    If this isn’t quite working, you could try the custum rewrite processor:

    http://ocpsoft.com/docs/prettyfaces/3.2.0/en-US/html/inbound_rewriting.html

    <rewrite processor="com.raible.AddJSFProcessor" inbound="true" />

    public class AddJSFProcessor implements com.ocpsoft.pretty.faces.rewrite.Processor
    {
    public String process(RewriteRule rule, String url)
    {
    return "new/url/";
    }
    }

    We are currently working on refactoring and improving the processor interface to provide access to the request/response, and also enable more complete control over the prettyfaces lifecycle. So stay tuned for these updates in the next snapshot.

    Sorry for the difficulty you’ve been having, we’re coming up to speed slowly but surely :) This is all supposed to be “easy,” but as I’m sure you know, “easy” only comes once you see what many kinds of things people want to do, and design accordingly.

    –Lincoln

    #20595

    mraible
    Participant

    Doesn’t look like negative lookback is a valid syntax:

    ERROR [main] Digester.fatalError(1655) | Parse Fatal Error at line 11 column 42: The value of attribute “match” associated with an element type “null” must not contain the ‘<‘ character.

    org.xml.sax.SAXParseException: The value of attribute “match” associated with an element type “null” must not contain the ‘<‘ character.

    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)

    #20596

    Ok, in that case a lookahead should work as well – I guess Digester doesn’t like <, even in quoted strings. Though, looking at this regex, I don’t think you even need the lookahead, since there’s no way a ‘.’ could be matched in the first place.

    ^([^?]*)/([^?/.]+)(?.*)?$

    Or you could just do the same in a Processor.

    However, in looking at your example again, I don’t think you really want to be using rewrite rules for this.

    Why not just use URL mappings, which is what are really intended to solve this issue.

    <url-mapping>
    <pattern value="/login" />
    <view-id value="/login.jsf" />
    </url-mapping>

    You’ll have to set up multiple mappings, but this will work. Query strings are automatically handled and propagated for you.

    #20597

    mraible
    Participant

    The following processor works, but my app still shows .jsf on URLs in my browser, which is what I’m trying to get rid of:

    public class UrlProcessor implements Processor {

    public String process(RewriteRule rule, String url) {

    if (url.contains(“/images”) || url.contains(“/styles”) || url.contains(“/scripts”) ||

    url.contains(“/login”) || url.contains(“/logout”) || url.contains(“/j_security_check”)

    || url.equals(“/”)) {

    return url;

    } else if (!url.contains(“.jsf”)) {

    return url + “.jsf”;

    } else {

    return url;

    }

    }

    }

    My pretty-config.xml is as follows, but it seems the final one gets hit everytime, even for /login.

    <url-mapping>

    <pattern value=”/login” />

    <view-id value=”/login.jsp” />

    </url-mapping>

    <url-mapping>

    <pattern value=”/logout” />

    <view-id value=”/logout.jsp” />

    </url-mapping>

    <rewrite processor=”org.appfuse.webapp.util.UrlProcessor” inbound=”true”/>

    #20598

    Keep in mind that your rewrite rule is explicitly *adding* “.jsf” to inbound URLs, so it would certainly update the browser URL unless you specify a chaining/internal redirect (servlet forward.)

    Try: <rewrite processor="org.appfuse.webapp.util.UrlProcessor" inbound="true" redirect="chain"/>

    Note: You do not need this rewrite rule if you create a mapping for each page in your application – as you have done for ‘login’ and ‘logout.’

    It is on our short list of enhancements to enable “default” discovery of mappings / removing *.jsf* from URLs, making this process simpler.

    #20599

    Also note that inbound / outbound rewriting are both enabled by default, so inbound="true" is redundant :) Just FYI!

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

You must be logged in to reply to this topic.

Comments are closed.