title: Wicket serving images from File System
body: <p>
  I am pretty new to Wicket and i have some difficulties with using resource references. I am using wicket 1.5.4 and have following problem: I store images on the file system. I have class ImageElement which holds part of the file path relative to configured rootFilePath (i.e dir1/dir2/img1.png). On the page I add Image as follows: </p>
  
  <pre class="lang-java prettyprint-override"><code>new Image("id",ImagesResourceReference.get(), pageParameters)
  </code></pre>
  
  <p>where page parameters includes image path parameter (path="/dir1/dir2/img1.png"). My questions are:</p>
  
  <ul>
  <li>Is it the simplest way of serving images from the file system?</li>
  <li>Is it ok to use ResourceReference with static method? or I should construct each time new ResourceReference? I saw that in previous version it was possible to use new ResourceReference(globalId), but it seems not to be the case anymore. If so what is the global resource reference for? So far as I understand resource reference is supposed to be factory for resources so it would be rather strange to create new factory for each resource request. </li>
  <li>The last question is, how can i pass the path to the image in a better way so that i do not have to concatenate indexed parameters to build the path once respond method is invoked on ImageResource.</li>
  <li>What would be the best scenario to get it working in efficient and simple way, i saw the example in 'Wicket in action', but this is meant for dynamic image generation from db and am not sure if it suites for my case</li>
  </ul>
  
  <p>My implementation of ResourceReference which I mounted in Application under "/images" path, looks as follows:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ImagesResourceReference extends ResourceReference {
  
  private static String rootFileDirectory;
  
  private static ImagesResourceReference instance;
  
  private ImagesResourceReference() {
      super(ImagesResourceReference.class, "imagesResourcesReference");
  }
  
  public static ImagesResourceReference get() {
      if(instance == null) {
          if(StringUtils.isNotBlank(rootFileDirectory)) {
              instance = new ImagesResourceReference();
          } else {
              throw new IllegalStateException("Parameter configuring root directory " +
                      "where images are saved is not set");
          }
      }
      return instance;
  }
  
  public static void setRootFileDirectory(String rootFileDirectory) {
      ImagesResourceReference.rootFileDirectory = rootFileDirectory;
  }
  
  private static final long serialVersionUID = 1L;
  
  @Override
  public IResource getResource() {
  
      return new ImageResource(rootFileDirectory);
  }
  
  private static class ImageResource implements IResource {
  
      private static final long serialVersionUID = 1L;
  
      private final String rootFileDirectory;
  
      public ImageResource(String rootFileDirectory) {
          this.rootFileDirectory = rootFileDirectory;
      }
  
      @Override
      public void respond(Attributes attributes) {
  
           PageParameters parameters = attributes.getParameters();
           List&lt;String&gt; indexedParams = getAllIndexedParameters(parameters);
           if(!indexedParams.isEmpty() &amp;&amp; isValidImagePath(indexedParams)) {
               String pathToRequestedImage = getImagePath(indexedParams);
               FileResourceStream fileResourceStream = new FileResourceStream(new File(pathToRequestedImage));
               ResourceStreamResource resource = new ResourceStreamResource(fileResourceStream);
               resource.respond(attributes);
           }
      }
  
      private boolean isValidImagePath(List&lt;String&gt; indexedParams) {
          String fileName = indexedParams.get(indexedParams.size() -1);
          return !FilenameUtils.getExtension(fileName).isEmpty();
      }
  
      private List&lt;String&gt; getAllIndexedParameters(PageParameters parameters) {
          int indexedparamCount = parameters.getIndexedCount();
          List&lt;String&gt; indexedParameters = new ArrayList&lt;String&gt;();
          for(int i=0; i&lt;indexedparamCount ;i++) {
              indexedParameters.add(parameters.get(i).toString());
          }
          return indexedParameters;
      }
  
      private String getImagePath(List&lt;String&gt; indexedParams) {
          return rootFileDirectory + File.separator + StringUtils.join(indexedParams, File.separator);
      }
  
  }
  </code></pre>
  
  <p>Any help and advices appreciated! Thanks in advance.</p>