Match anything in pattern using regexp

Splash Forums PrettyFaces Users Match anything in pattern using regexp

This topic contains 4 replies, has 2 voices, and was last updated by  djmj 2 years, 11 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #24605

    djmj
    Participant

    How do I match anything in pattern using regular expression?

    I have following structure:

    
    <!-- display entity welcome page-->
    <url-mapping>
    	<pattern value="/entity"/>
    	<view-id value="/sites/entity.xhtml"/>
    </url-mapping>
    <!-- display particular entity -->
    <url-mapping>
    	<pattern value="/([^/]+)/#{entityId}"/>
    	<view-id value="/sites/entity.xhtml"/>
    </url-mapping>
    

    The first part of the path should match anything that is not empty, its value is currently not important:

    Using:
    /([^/]+)/#{entityId}

    I get following strange exception:

    java.util.regex.PatternSyntaxException: Unclosed character class near index 5
    /([^\/
    ^

    I also tried to match it as an optional parameter with following patterns which resulted in correct redirection and link building but jsf could not load my resources like js/css anymore which broke my page.

    
    <pattern value="/entity/#{entityId}"/>
    <pattern value="/#{entitySeoId}/#{entityId}"/>
    

    The idea of allowing random string is much more convenient for my use case which is simliar to product site urls

    • This topic was modified 2 years, 12 months ago by  djmj.
    • This topic was modified 2 years, 12 months ago by  Christian Kaltepoth.
    • This topic was modified 2 years, 11 months ago by  djmj. Reason: last pattern not shown
    #24609

    There are two things you could try:

    First try to remove ( and ) from the pattern. I don’t see any reason to add them here. So use something like this:

    <pattern value="/[^/]+/#{entityId}"/>

    You should also be able to simply use another path parameter instead of you regex. Just name it whatever you want. [^/]+ is the default pattern, so something like this should work:

    <pattern value="/#{something}/#{entityId}"/>

    #24619

    djmj
    Participant

    Removing brackets ( and ) leads to same log error.

    I narrowed the error down:

    The scope of this pattern is to wide.

    All ressources follow that pattern, thats why my converter for #{entityId} gets called also for resources since the pattern also matches those which result in a number exception in my converter since i assume an integer.

    /javax.faces.resource/cssFile.css.xhtml

    Is also matched by it.

    Following workarounds:

    • 1. Exclude all other patterns matching in regexp.
    • 2. Change mapping of Resources. javax.faces.resource.
    • 3. Add new path separator like: <pattern value="/#{something}/e/#{entityId}"/>

    Thinking about 2 and/or 3.


    Site note:

    Now i maybe understand why amazon added /db/ between their product informations.

    http://www.amazon.de/dp/B007HCCOD0
    http://www.amazon.de/kindle-ereader-ebook-reader/dp/B007HCCOD0
    http://www.amazon.de/something/dp/B007HCCOD0

    • This reply was modified 2 years, 11 months ago by  djmj.
    #24621

    Yeah, this is a problem people often run into. I typically recommend to either add some kind of prefix to the URL (essential you suggestion 3.) or to restrict the regular expressions of the path parameters. In your case you could for example restrict the first parameter to a string containing characters but no periods. Something like this:

    <pattern value="/#{ /[a-z]+/ something }/#{ /[0-9]+/ entityId }"/>

    See this part of the documentation for details:

    http://ocpsoft.org/docs/prettyfaces/3.3.3/en-US/html/Configuration.html#config.pathparams.regex

    Our new project Rewrite, which is the successor of PrettyFaces, allows to handle such cases more easy. See Lincoln’s post here:

    http://ocpsoft.org/support/topic/rewrite-conflicts-with-imageservlet/#post-24593

    If you want to learn more about how to migrate from PrettyFaces to Rewrite (and keeping your pretty-config.xml), have a look at the migration guide I wrote:

    http://ocpsoft.org/rewrite/docs/migration/prettyfaces3

    Hope this helps. 🙂

    Christian

    #24633

    djmj
    Participant

    I will go for point 3, since excluding or restriction can lead to problems in the future with additional similar mappings.

    The complexity would increase greatly making it hard to maintain and performance wise it is also not such a good idea.

    I am also going to use rewrite programatically since all my possible query-parameter key values are hardcoded and duplicate definitions should be avoided.

    • This reply was modified 2 years, 11 months ago by  djmj.
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

Comments are closed.