Question

I'm using wicket 1.5.10, tomcat 7 and Java 6. In my code i do:

public final class Images{
  public static void mountGlobalStaticImages(){
    //loading images from database - works perfect
    List<Object> imagesParams = GlobalTemplatesDAO.getGlobalImages();
    for(Object record : imagesParams){
      Map<String, Object> image = (Map<String, Object>) record;
      //here mount point is path like '/images/mylogo.png'
      String imagePath = (String) image.get("file_mount_point");
      //ComponentDynamicImage is extending DynamicImageResource, looks it works (it provides required byte[] data)
      ComponentDynamicImage imageData = new ComponentDynamicImage((byte[]) image.get("file_data"));
      //problem is here: looks that it mount image isn't in app and in log is http status code '302'
      //AppStart is extending WebApplication class
      AppStart.get().getSharedResources().add(imagePath, imageData);
    }
  }
}

Problem is that images isn't here, in localhost_access_log.2013-11-04.txt are lines:

127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/icon-fb.png HTTP/1.1" 302 -
127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/cz-ico.gif?53 HTTP/1.1" 200 9067
127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/icon-fb.png?54 HTTP/1.1" 200 9068
127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/en-ico.gif HTTP/1.1" 302 -
127.0.0.1 - - [04/Nov/2013:23:52:09 +0100] "GET /images/images/en-ico.gif?55 HTTP/1.1" 200 9067

How to mount them properly? As far as i know problem is in AppStart.get().getSharedResources().add(imagePath, imageData) but not know how to do it properly.

UPDATE For others on that page, this is working code based on answer below:

public static void mountGlobalStaticImages(){
    //loading images from database
    List<Object> imagesParams = GlobalTemplatesDAO.getGlobalImages();
    for(Object record : imagesParams){
        Map<String, Object> image = (Map<String, Object>) record;
        //here mount point is path like '/images/mylogo.png'
        String imagePath = (String) image.get("file_mount_point");
        //ComponentDynamicImage extends DynamicImageResource, it provides required byte[] data
        ComponentDynamicImage imageData = new ComponentDynamicImage((byte[]) image.get("file_data"));
        //add imageData into shared resources on path
        AppStart.get().getSharedResources().add(imagePath, imageData);
        //mount from shared resources on path
        AppStart.get().mountResource(imagePath, new SharedResourceReference(imagePath));
    }
}
Was it helpful?

Solution

You not only need to add the resources to your shared resources, you need to register a resource reference to access it (at this point I assume you made sure that imageData contains the correct data)

getSharedResources().add("resourcePath", imageData);
mountResource("resourcePath", new SharedResourceReference("resourceName"));

After that you can access the shared resource everywhere in the code by using

new SharedResourceReference("resourceName")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top