Rewrite – Ignore a request that mathches a pattern.

Splash Forums PrettyFaces Users Rewrite – Ignore a request that mathches a pattern.

This topic contains 6 replies, has 2 voices, and was last updated by  rbento 4 years, 7 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #18782

    rbento
    Participant

    Please, could you guys help me with this.

    Currently I have to deal with a URL that has the following pattern.

    • http://somedomain/somekey
    • The context is ‘/’ and the key a 32 chars hash

      I’m using a PrettyFaces rule that sets this key to a bean and executes an action:

      <url-mapping id="load">

      <pattern value="/#{ key : bean.key }" />

      <view-id value="/index.xhtml" />

      <action>#{bean.load}</action>

      </url-mapping>

      As I read that //rewrite can be really flexible, I would like to ask some help in translating this mapping to //rewrite, as I could not find any example of setting bean parameters and executing an action in //rewrite section.

      Also I would like to ask if is there any way to add a rule that would ignore the request in case the parameter doesn’t match a pattern. For example in this example above, if the key doesn’t match 32 chars, do nothing. This would solve the problem that the context is set to ‘/’.

      Also I think about giving this app a context and writing a rule to match this old pattern and redirect to this new context. Does it sounds better?

      Please if you could point me to some link with examples or directly help me I would be grateful.

      Hugs,

      Rod

    #23046

    Hey,

    nice to hear you are interested in Rewrite. For a start I recommend to have a look at the homepage:

    Rewrite

    And also look at the examples:

    Rewrite Examples

    For your usecase a rule like this should do the job:

    .addRule(
    Join.path("/{key}").to("/index.xhtml")
    .where("key").matches("[a-zA-Z]{32}").bindsTo(El.property("bean.key"))
    .perform(Invoke.binding(El.retrievalMethod("bean.load")))
    )

    I think it is very self explaining.

    However, the 32 character restriction can also be implemented in PrettyFaces. It’s very easy. You can use a custom regex for this:

    <url-mapping id="load">
    <pattern value="/#{ /[a-zA-Z]{32}/ key : bean.key }" />
    <view-id value="/index.xhtml" />
    <action>#{bean.load}</action>
    </url-mapping>

    See the PrettyFaces documentation for details:

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

    I hope this helps. :)

    Christian

    #23047

    I forgot to mention: There won’t be any further active development for PrettyFaces. So Rewrite is the way to go in the future. :)

    #23048

    rbento
    Participant

    Thank you Christian!

    You example is very clear and helpful. I’ll stick to Rewrite then.

    Regards,

    Rod

    #23049

    rbento
    Participant

    Hello Christian,

    I tried to do based in your code, but I had some issues.

    My settings are the following:

    – Env

    - Glassfish 3.1.2.2 b05
    - Mojarra 2.1.13
    - rewrite-servlet 1.1.0-Final
    - rewrite-integration-cdi 1.1.0-Final

    – Bean

    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;

    @ManagedBean
    @SessionScoped
    public class MyBean {
    private String key;
    public void load(){
    // log the key here
    }
    public String getKey() {
    return key;
    }
    public void setKey(String key) {
    this.key = key;
    }
    }

    – Configuration

    @Override
    public Configuration getConfiguration(final ServletContext context)
    {
    return ConfigurationBuilder.begin()
    .addRule(Join.path("/{k}").to("/index.xhtml")
    .where("k").matches("[a-zA-Z0-9]{32}").bindsTo(El.property("myBean.key"))
    .perform(Invoke.binding(El.retrievalMethod("myBean.load"))));

    }

    My rules are automatically loaded:

    INFO: Loaded [2] org.ocpsoft.rewrite.config.ConfigurationProvider [com.acme.interceptor.ActionInterceptor<10>]
    INFO: RewriteFilter initialized.

    By accessing this URL: http://localhost:8080/K8D28002569EE71286C1492C19K32U6X

    I get the following error:

    ...
    org.ocpsoft.rewrite.exception.RewriteException: El provider [org.ocpsoft.rewrite.cdi.CdiExpressionLanguageProvider] could not retrieve value from property [myBean.load]
    ...
    Caused by: javax.el.PropertyNotFoundException: ELResolver cannot handle a null base Object with identifier 'myBean'
    ...

    And in my error page I get this message from #{requestScope}:

    El provider [org.ocpsoft.rewrite.cdi.CdiExpressionLanguageProvider] could not inject property [myBean.key} with value [[Ljava.lang.String;@44baae87]

    I read all the examples and did a forum search but couldn’t find what is wrong. Also I removed the matches to let the param be directly set but got the same error.

    Wouldn’t El.property(“myBean.key”) create the bean upon access?

    Could you please help with this?

    Hugs,

    Rod

    #23050

    Did you add the JSF integration module to your classpath?

    You are using JSF managed bean annotations on your class. So the CdiExpressionLanguageProvider won’t work here. If you add the JSF integration module to your classpath, there should also be a FacesExpressionLanguageProvider that should work.

    However, I don’t recommend to use the JSF managed bean annotations at all. As you are deploying to a JEE container, you should use CDI instead. This would look similar to this:

    import javax.inject.Named;
    import javax.enterprise.context.SessionScoped;

    @Named
    @SessionScoped
    public class MyBean {
    private String key;
    public void load(){
    // log the key here
    }
    public String getKey() {
    return key;
    }
    public void setKey(String key) {
    this.key = key;
    }
    }

    And don’t forget to add the beans.xml file. :)

    #23051

    rbento
    Participant

    Christian, thank you so much for your help.

    Hugs,

    Rod

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

You must be logged in to reply to this topic.

Comments are closed.