Reload configuration (or add/remove filters) at runtime.

Splash Forums Rewrite Users Reload configuration (or add/remove filters) at runtime.

Tagged: 

This topic contains 6 replies, has 3 voices, and was last updated by  Lincoln Baxter III 1 year, 7 months ago.

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

    AndreaCatania
    Participant

    Hi all,

    I’m developing a panel that show a list of pictures. All pictures have their URLs (e.g. /imgs/pic1.jpg, /imgs/pic2.jpg, /imgs/pic3.jpg) this pages show the picture and their info.
    When a picture will be uploaded or removed the rewrite configuration must be reload automatically, to fix the URLs.

    I think that you understand me because I can’t reload the server or the context for each picture added/removed, to fix URLs…

    What is the best way to solve that?

    Thanks in advance Andrea.

    #26693

    Why do you need to reload the configuration. Typically you will only need a single rule for all images. Something like:

    .addRule()
    .when(Direction.isInbound().and(Path.matches("/imgs/{name}.jpg")))
    .perform( ..... )
    
    #26694

    AndreaCatania
    Participant

    Hi, and thanks for the reply.

    I have to reload the config becouse some img have custom URL like this http://www.mydomainpanel.com/photo_tree.jpg

    For do that i can’t set the rules as you say to avoid conflicts with all other resouces files (e.g. css, js) or pages.

    My customer said explicit that the img, if needed, must have the URL base as domain root, like written above.

    So the idea is that for each photo there is one rule.

    If this problem can be fixed extending a class, please let me know the class name.

    #26695

    You really shouldn’t try to reload the Rewrite configuration. And there is really no need to create one rule for each uploaded file. There are many ways to implement something like this. But this depends on your specific requirements. Where are the files images? Stored in the database?

    This is one possible way to implement this. I didn’t test it but it shows how it could work. You create a rule that matches all inbound requests and then create a HttpOperation which checks if the requested URL is an uploaded image or not. If so, you load the image (from the db or whatever) and then write it to the response. At the end you tell Rewrite that it should end the request processing as you already sent the response.

    .addRule()
    .when(Direction.isInbound())
    .perform(new HttpOperation() {
       public void performHttp(HttpServletRewrite event, EvaluationContext context)
       {
          
          String uri = event.getRequest().getRequestURI();
          
          if(isUploadedGraphic(uri)) {
             
             byte[] data = getGraphic(uri);
             
             Response.setContentType("image/jpg").perform(event, context);
             Response.write(data).perform(event, context);
             
             Lifecycle.handled().perform(event, context);
             
          }
          
       }
    })
    

    IMHO this is a much better approach compared to dynamically creating and removing rules at runtime.

    #26696

    AndreaCatania
    Participant

    It’s a good starting point.

    Now the table is this:

    
    +---------------------------------------------+
    | <strong>IMAGES</strong>                                      |
    +-----------+---------------------------------+
    |customUrl  | realImageUrl                    |
    +-----------+---------------------------------+
    |/test/test | www.example.com/imgs/tree/1.jpg |
    +-----------+---------------------------------+
    

    Now imagine that when arrive a request, will be performed a search of URI into the DB than if it is found will be invoked the servlet “ImageInfo” with GET parameter imgUrl=”/test/test”.
    The servlet “ImageInfo” generates and shows the HTML with the image and their info.
    Otherwise if the URI isn’t found the request goes normally and static files as css, js or servlet can be invoked normally (without rewriting).

    If what I’ve said it’s possoble, how can I do that?

    The only thing that not convince me is that for each request this approach require a connection on DB and 1 query.
    When I open a page, the browser require a lot of resources as (css, js, API) to draw it… So a lot of resources will be used. That can be avoided if we have “static” predefined URLs filters.
    So I think the best way (surely more complicated for developer but with more performance for the users) is to change filters dynamically.

    I know that you have a lot of experience in more me with this lib, what do you advise me to do?

    #26708

    AndreaCatania
    Participant

    I thought that as you said, the best solution is to have only one rule that matches to all images.

    As I said the servlet “imageinfo” return the HTML with image info.

    http://www.example.com/imageinfo?imgUrl=/red/tree

    If the rewrite must be:
    http://www.example.com/image/red/tree

    How can I write a rule that catch all after “/image” including slasshes as variable value to forward the request to http://www.example.com/imageinfo?imgUrl=/red/tree

    #26710

    Just configure the parameter to match over slashes 🙂

    .addRule(Join.path("/image/{imgUrl}").to("/imginfo"))
    .where("imgUrl").matches("[^&]*")
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

Comments are closed.