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:
new Image("id",ImagesResourceReference.get(), pageParameters)
where page parameters includes image path parameter (path="/dir1/dir2/img1.png"). My questions are:
My implementation of ResourceReference which I mounted in Application under "/images" path, looks as follows:
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<String> indexedParams = getAllIndexedParameters(parameters);
         if(!indexedParams.isEmpty() && 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<String> indexedParams) {
        String fileName = indexedParams.get(indexedParams.size() -1);
        return !FilenameUtils.getExtension(fileName).isEmpty();
    }
    private List<String> getAllIndexedParameters(PageParameters parameters) {
        int indexedparamCount = parameters.getIndexedCount();
        List<String> indexedParameters = new ArrayList<String>();
        for(int i=0; i<indexedparamCount ;i++) {
            indexedParameters.add(parameters.get(i).toString());
        }
        return indexedParameters;
    }
    private String getImagePath(List<String> indexedParams) {
        return rootFileDirectory + File.separator + StringUtils.join(indexedParams, File.separator);
    }
}
Any help and advices appreciated! Thanks in advance.
You could use it as a shared resource:
    public class WicketApplication extends WebApplication {
        @Override
        public Class<HomePage> getHomePage() {
            return HomePage.class;
        }
        @Override
        public void init() {
            super.init();
            getSharedResources().add("downloads", new FolderContentResource(new File("C:\\Users\\ronald.tetsuo\\Downloads")));
            mountResource("downloads", new SharedResourceReference("downloads"));
        }
        static class FolderContentResource implements IResource {
            private final File rootFolder;
            public FolderContentResource(File rootFolder) {
                this.rootFolder = rootFolder;
            }
            public void respond(Attributes attributes) {
                PageParameters parameters = attributes.getParameters();
                String fileName = parameters.get(0).toString();
                File file = new File(rootFolder, fileName);
                FileResourceStream fileResourceStream = new FileResourceStream(file);
                ResourceStreamResource resource = new ResourceStreamResource(fileResourceStream);
                resource.respond(attributes);
            }
        }
    }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With