Sempa
Sempa

Reputation: 167

Wicket serving images from File System

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.

Upvotes: 4

Views: 5963

Answers (2)

Till
Till

Reputation: 994

You can still use ResourceReferences with global IDs. You just have to use a SharedResourceReference. This is probably better, too.

add(new Image("image", new SharedResourceReference("mySharedResourceRef", parameters));

I would try to avoid building paths from URL parameters. This can easily end up in security leaks.

Upvotes: 0

tetsuo
tetsuo

Reputation: 10896

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);
            }
        }
    }

Upvotes: 8

Related Questions