404 rewrite handler for images

Splash Forums Rewrite Users 404 rewrite handler for images

This topic contains 3 replies, has 2 voices, and was last updated by  Christian Kaltepoth 5 years ago.

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

    aaronwalker
    Participant

    Hey,

    I have a JSF app which is loading images as resources and I’m looking to try and support the ability to define a rewrite rule for images when they don’t exist. As in have these rewritten to a placeholder image. I’m not sure if this is currently supported by Rewrite and if not I’d be happy to have a go at adding it.

    cheers

    Aaron

    #22568

    Hey Aaron,

    I think something like this should be easy to do with Rewrite. The following example shows how something like this *could* work in a plain Servlet environment (without JSF resource loading):

    .defineRule()
    .when(new HttpCondition() {
    @Override
    public boolean evaluateHttp(HttpServletRewrite event, EvaluationContext context)
    {
    if(Direction.isInbound().evaluate(event, context)) {

    String path = event.getRequestPath();
    InputStream stream = event.getRequest().getServletContext().getResourceAsStream(path);
    if(stream == null) {
    return true;
    }

    }
    return false;
    }
    }
    ).perform(Forward.to("/images/fallback.png"));

    But if you are using the JSF resource loading, checking whether a resource exists would work a bit different.

    Christian

    #22569

    aaronwalker
    Participant

    Thanks Christian.

    Pretty much what I ended up with some additional filtering based in on the request path.

    I’ve been trying to set a EvaluationContext variable in condition and use in the perform step something like

    .defineRule()
    .when(new HttpCondition() {
    @Override
    public boolean evaluateHttp(HttpServletRewrite event, EvaluationContext context) {
    if (Direction.isInbound().evaluate(event, context)) {
    String path = event.getRequestPath();
    if(path.contains(".jpg")) {
    InputStream stream = event.getRequest().getServletContext().getResourceAsStream(path);
    if (stream == null) {
    context.put("text",event.getRequestQueryString());
    return true;
    }
    }
    }
    return false;
    }
    }
    ).perform(Redirect.temporary("http://placehold.it/128&text={text}"));

    but I get java.lang.IllegalArgumentException: Attempted to access the non-existent EvaluationContext property “{text}”

    I’m not sure if I can access this variable in this way

    thanks

    Aaron

    #22570

    Actually I’m note sure if it is possible to access values from the EvaluationContext in such a way.

    But you could simply implement the perform step yourself in a few lines of code.

    Something like this:

    .perform(new HttpOperation() {
    @Override
    public void performHttp(HttpServletRewrite event, EvaluationContext context)
    {
    HttpInboundServletRewrite inboundEvent = (HttpInboundServletRewrite) event;
    inboundEvent.redirectTemporary("http://placehold.it/128&text=" + event.getRequestQueryString());
    }
    });

    @lincoln: Or what is the common way to do something like this?

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

You must be logged in to reply to this topic.

Comments are closed.