I am new to Wicket and I have some difficulties using resource links. I am using wicket 1.5.4 and have the following problem: I am storing images in a file system. I have an ImageElement class that contains part of the file path relative to the configured rootFilePath (ie dir1 / dir2 / img1.png). On the page, I add an image as follows:
new Image("id",ImagesResourceReference.get(), pageParameters)
where the page options include the image path parameter (path = "/dir1/dir2/img1.png"). My questions:
- Is this the easiest way to serve images from a file system?
- Is it possible to use ResourceReference with a static method? Or should I create a new ResourceReference each time? I saw that in the previous version it was possible to use the new ResourceReference (globalId), but it seems that this is not so. If so, what is the global link to resources? As far as I understand, the resource reference should be a factory for resources, so it would be rather strange to create a new factory for each resource request.
- Last question: how can I better pass the path to the image, so I donβt need to concatenate the indexed parameters to create the path after calling the response method on ImageResource.
- What would be the best scenario to make it work in an efficient and easy way, I saw an example in "Wicket in action", but this is intended to generate dynamic images from db, and I'm not sure if it suits my case
My implementation of the ResourceReference, which I mounted in the application in the "/ images" section, is 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 advice appreciated! Thanks in advance.
Sempa source share