0 Replies Latest reply on Mar 25, 2011 4:26 AM by andrewwheeler

    Faces Resource Observer

    andrewwheeler

      I have created a custom faces resource observer which would permit the serving of resources such as images and style sheets from alternate locations or to modify resources before they are served.


      Some examples for serving from alternate locations is retrieving images from the database or java content repository. Even as simple as serving images from file locations outside the application.


      Examples for observing resources after they have been retrieved is to modify an image before it is served - e.g. add a water mark to a picture if it hasn't been purchased.


      It works simply using the existing resource model of graphic images (and style sheets):


      <h:graphicImage library="jcr" name="the-node-id-of-the-content"/>



      The resource library does not have to exist, simply observe the library and or resource and set the content:


      public class ObserveResource {
      
           // There is no jcr library in /resources but it can be intercepted and completed.
           public void beforeObserver(@Observes @ResourcePath(library="jcr") @Before MutableResource resource) {
                resource.setInputStream(JCRManager.getInputStream(resource.getResourceName()));
           }
           
           // The images exist in our /resources/assets directory, but unless the user has purchased the image
           // it will be shrunk and watermarked. 
           public void afterObserver(@Observes @ResourcePath(library="assets") @After MutableResource resource) {
                if (!purchaseHistory.contains(resource.getResourceName()))
                     resource.setInputStream(Images.addWaterMark(resource.getInputStream(), "Demo Only"));
           }
      
           // Individual resources can be observed
           public void observeSingle(@Observes @ResourcePath(library="images", resource="myimage.png") @After MutableResource resource) {
                
           }     
      }
      



      I've put some working code together that functions as above but it is probably rather rough. Up until this point I've only been a 'user' of JSF and not really delved into the workings. To implement this I actually had to read the RI code for Mojarra!


      Is there any interest in perhaps including this as a seam resources module? The only concern I have is with regards to performance. A page that is content rich could raise a considerable number of events. Custom content is not cached as is the case for regular resources in Mojarra in production mode.


      Feedback welcome