Class FileSystemResourceReference

java.lang.Object
org.apache.wicket.request.resource.ResourceReference
org.apache.wicket.resource.FileSystemResourceReference
All Implemented Interfaces:
Serializable, IClusterable

This resource reference is used to provide a reference to a resource based on Java NIO FileSystem API.

To implement a mime type detection refer to the documentation of Files.probeContentType(Path) and provide an implementation for java.nio.file.spi.FileTypeDetector in the META-INF/services folder for jars or in the /WEB-INF/classes/META-INF/services folder for webapps

You can optionally override getFileSystemResource() to provide an inline mime type detection, which is preferred to the default detection.

Example:
 
 Path path = FileSystemResourceReference.getPath(URI.create("jar:file:///folder/file.zip!/folderInZip/video.mp4"));
 add(new Video("video", new FileSystemResourceReference(path)));
 
 
Example 2:
 
 mountResource("/filecontent/${name}", new FileSystemResourceReference("filesystem")
 {
        private static final long serialVersionUID = 1L;
 
        @Override
        public IResource getResource()
        {
                return new FileSystemResource()
                {
                        private static final long serialVersionUID = 1L;
 
                        protected ResourceResponse newResourceResponse(Attributes attributes)
                        {
                                try
                                {
                                        String name = attributes.getParameters().get("name").toString("");
                                        URI uri = URI.create(
                                                "jar:file:////folder/example.zip!/zipfolder/" + name);
                                        return createResourceResponse(attributes, 
                                                FileSystemResourceReference.getPath(uri));
                                }
                                catch (IOException | URISyntaxException e)
                                {
                                        throw new WicketRuntimeException("Error while reading the file.", e);
                                }
                        };
                };
        }
 });
 
 
Author:
Tobias Soloschenko
See Also:
  • Constructor Details

    • FileSystemResourceReference

      Creates a file system resource reference based on the given path
      Parameters:
      name - the name of the resource reference to expose data
      path - the path to create the resource reference
    • FileSystemResourceReference

      Creates a file system resource reference based on the given name
      Parameters:
      name - the name of the resource reference
    • FileSystemResourceReference

      public FileSystemResourceReference(Class<?> scope, String name)
      Creates a file system resource reference based on the given scope and name
      Parameters:
      scope - the scope as class
      name - the name of the resource reference
  • Method Details

    • getResource

      Creates a new FileSystemResource and applies the path to it.
      Specified by:
      getResource in class ResourceReference
      Returns:
      resource instance
    • getFileSystemResource

      Gets the file system resource to be used for the resource reference
      Returns:
      the file system resource to be used for the resource reference
    • getPath

      public static Path getPath(URI uri, Map<String,String> env)
      Creates a path and a file system (if required) based on the given URI
      Parameters:
      uri - the URI to create the file system and the path of
      env - the environment parameter to create the file system with
      Returns:
      the path of the file in the file system
    • getPath

      public static Path getPath(URI uri) throws IOException, URISyntaxException
      Creates a path and a file system (if required) based on the given URI
      Parameters:
      uri - the URI to create the file system and the path of
      Returns:
      the path of the file in the file system
      Throws:
      IOException - if the file system could'nt be created
      URISyntaxException - if the URI has no valid syntax