How to get property in HttpCondition?

Splash Forums Rewrite Users How to get property in HttpCondition?

This topic contains 3 replies, has 3 voices, and was last updated by  cilf 1 year, 10 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #26431

    cilf
    Participant

    Hey guys first of all thanks for the great tool!

    I’m trying to valide {path} in order to dynamically generate 404. Solution posted below works but I’d like to pass just {path} to my service to avoid the need to parse the url for the parameter.

    
    return ConfigurationBuilder.begin()
        .addRule()
        .when(Direction.isInbound()
                .and(Path.matches("/{path}"))
                .andNot(new HttpCondition() {
                            @Override
                            public boolean evaluateHttp(HttpServletRewrite event, EvaluationContext evaluationContext) {
                                String url = event.getRequest().getRequestURL().toString();
                                // this throws java.lang.IllegalArgumentException: Attempted to access the non-existent EvaluationContext property "{path}"
                                //String path = (String) Evaluation.property("path").retrieve(event, evaluationContext);
                                //System.out.println(path);
                                return service.isPageFound(url);
                            }
                        }
                ))
        .perform(Forward.to("/404byRewrite.xhtml").and(Response.setStatus(404)))
        .where("path").matches(".*")
        .where("path").bindsTo(El.property("errorBean.page"));
    

    With the commented out block I got insipered in http://ocpsoft.org/support/topic/com-ocpsoft-pretty-prettyexception-failed-to-load-configuration-2/,
    but this solution throws java.lang.IllegalArgumentException: Attempted to access the non-existent EvaluationContext property “{path}”

    Looks like it’s thrown every time since wrapping w/ try-catch doesn’t help.

    What would be the proper way to obtain the property? Or is there better solution for this kind of dynamic conditioning?

    I’m running it with PrimeFaces 5.0 and Java EE7 in Wildfly 8.1.0.Final. (github code)

    
    Error processing request
    Context Path:/rewrite-java-ee-7-primefaces-5-1.0-SNAPSHOT
    Servlet Path:/
    Path Info:null
    Query String:null
    Stack Trace
    java.lang.IllegalArgumentException: Attempted to access the non-existent EvaluationContext property "{path}"
    org.ocpsoft.rewrite.bind.Evaluation.retrieveFromProperty(Evaluation.java:132)
    org.ocpsoft.rewrite.bind.Evaluation.retrieve(Evaluation.java:118)
    cz.cilf.rewrite.RewriteConfig$1.evaluateHttp(RewriteConfig.java:39)
    org.ocpsoft.rewrite.servlet.config.HttpCondition.evaluate(HttpCondition.java:44)
    org.ocpsoft.rewrite.config.Not.evaluate(Not.java:56)
    org.ocpsoft.rewrite.config.And.evaluate(And.java:78)
    org.ocpsoft.rewrite.config.DefaultConditionBuilder$2.evaluate(DefaultConditionBuilder.java:70)
    org.ocpsoft.rewrite.config.RuleBuilder.evaluate(RuleBuilder.java:156)
    org.ocpsoft.rewrite.servlet.impl.DefaultHttpRewriteProvider.rewriteInbound(DefaultHttpRewriteProvider.java:199)
    org.ocpsoft.rewrite.servlet.impl.DefaultHttpRewriteProvider.rewriteHttp(DefaultHttpRewriteProvider.java:89)
    org.ocpsoft.rewrite.servlet.http.HttpRewriteProvider.rewrite(HttpRewriteProvider.java:43)
    org.ocpsoft.rewrite.servlet.RewriteFilter.rewrite(RewriteFilter.java:275)
    org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:198)
    io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:60)
    io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:132)
    io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:85)
    io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61)
    io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
    io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113)
    io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56)
    io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
    io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45)
    io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61)
    io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
    io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70)
    io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
    io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
    org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
    io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25)
    io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240)
    io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227)
    io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73)
    io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146)
    io.undertow.server.Connectors.executeRootHandler(Connectors.java:177)
    io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    java.lang.Thread.run(Thread.java:745)
    
    #26441

    The problem with this block is that the property is written into the evaluation context AFTER all conditions are evaluated. At least if I remember correctly.

    To work around this you could implement a custom HttpOperation and perform the logic you need there. I think this should work. What exactly are you trying to do with your rule?

    #26444

    If a custom HttpOperation does not work, you need to use:

    ParameterStore store = (ParameterStore) evaluationContext.get(ParameterStore.class);
    ParameterValueStore store = (ParameterValueStore) evaluationContext.get(ParameterValueStore.class);

    The best way to get the parameter store is to implement the Parameterized interface: https://github.com/ocpsoft/rewrite/blob/master/api/src/main/java/org/ocpsoft/rewrite/param/Parameterized.java

    Rewrite will pass you the parameter store on Rule instantiation, and it also allows you to specify the parameter names that are required for your Condition/Operation.

    If your condition does not need to be re-used/configurable, you can “cheat”, and get them directly from the EvaluationContext.

    Those two classes will allow you to retrieve parameters and their current stored values.

    ~Lincoln

    #26446

    cilf
    Participant

    Good to know I can get the properties in condition evaluation after all.

    I think I was looking for something like .constrainedBy:

    
    .addRule()
    .when(Direction.isInbound().and(Path.matches("/{artistUrl}")))
    .perform(Forward.to("/404.xhtml"), Response.setStatus(404))
    .where("artistUrl").constrainedBy(new Constraint<String>() {
        @Override
        public boolean isSatisfiedBy(Rewrite rewrite, EvaluationContext evaluationContext, String s) {
            if (s.contains(".xhtml")) {
                return false;
            }
    
            Artist artist = artistService.findByUrl(s);
            if (artist == null) {
                return true;
            }
    
            return false;
    
        }
    }).bindsTo(El.property("errorPage.artistUrl"))
    

    Can you perhaps recommend some source of useful examples? Other than examples and forum sections in here.
    Cheers

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

You must be logged in to reply to this topic.

Comments are closed.