Repository: wicket
Updated Branches:
  refs/heads/wicket-7.x 5f0d2af08 -> 2daee37b4


WICKET-6025 improvements for static mounting / bugfix

Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/2daee37b
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/2daee37b
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/2daee37b

Branch: refs/heads/wicket-7.x
Commit: 2daee37b41868416ab7ea1c75730573fc8b2de35
Parents: 5f0d2af
Author: Tobias Soloschenko <[email protected]>
Authored: Fri Dec 11 22:49:26 2015 +0100
Committer: Tobias Soloschenko <[email protected]>
Committed: Sun Dec 13 06:05:13 2015 +0100

----------------------------------------------------------------------
 .../wicket/resource/FileSystemResource.java     | 35 ++++++--
 .../resource/FileSystemResourceReference.java   | 90 +++++++++++++++++---
 .../src/docs/guide/resources/resources_15.gdoc  | 35 ++++++++
 3 files changed, 144 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/2daee37b/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResource.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResource.java 
b/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResource.java
index 56e1b95..8585567 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResource.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResource.java
@@ -27,7 +27,6 @@ import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.request.cycle.RequestCycle;
 import org.apache.wicket.request.resource.AbstractResource;
 import org.apache.wicket.request.resource.PartWriterCallback;
-import org.apache.wicket.util.lang.Args;
 
 /**
  * Used to provide resources based on the on Java NIO FileSystem API.<br>
@@ -41,7 +40,7 @@ public class FileSystemResource extends AbstractResource
 {
        private static final long serialVersionUID = 1L;
 
-       private final Path path;
+       private Path path;
 
        /**
         * Creates a new file system resource based on the given path
@@ -51,18 +50,44 @@ public class FileSystemResource extends AbstractResource
         */
        public FileSystemResource(Path path)
        {
-               Args.notNull(path, "path");
                this.path = path;
        }
 
        /**
+        * Creates a new file system resource
+        * 
+        */
+       public FileSystemResource()
+       {
+       }
+
+       /**
         * Creates a new resource response and reads the given path
         */
        @Override
        protected ResourceResponse newResourceResponse(Attributes attributes)
        {
+
+               return createResourceResponse(path);
+       }
+
+       /**
+        * Creates a resource response based on the given attributes
+        * 
+        * @param attributes
+        *            the attributes to create the resource response on
+        * @return the actual resource response x
+        */
+       protected ResourceResponse createResourceResponse(Path path)
+       {
                try
                {
+                       if (path == null)
+                       {
+                               throw new WicketRuntimeException(
+                                       "Please override #newResourceResponse() 
and provide a path if using a constructor which doesn't take one as argument.");
+                       }
+                       this.path = path;
                        long size = getSize();
                        ResourceResponse resourceResponse = new 
ResourceResponse();
                        resourceResponse.setContentType(getMimeType());
@@ -71,8 +96,8 @@ public class FileSystemResource extends AbstractResource
                        RequestCycle cycle = RequestCycle.get();
                        Long startbyte = 
cycle.getMetaData(CONTENT_RANGE_STARTBYTE);
                        Long endbyte = cycle.getMetaData(CONTENT_RANGE_ENDBYTE);
-                       resourceResponse.setWriteCallback(new 
PartWriterCallback(getInputStream(), size,
-                               startbyte, endbyte));
+                       resourceResponse.setWriteCallback(
+                               new PartWriterCallback(getInputStream(), size, 
startbyte, endbyte));
                        return resourceResponse;
                }
                catch (IOException e)

http://git-wip-us.apache.org/repos/asf/wicket/blob/2daee37b/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResourceReference.java
----------------------------------------------------------------------
diff --git 
a/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResourceReference.java
 
b/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResourceReference.java
index e10fe54..d4330c1 100644
--- 
a/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResourceReference.java
+++ 
b/wicket-core/src/main/java/org/apache/wicket/resource/FileSystemResourceReference.java
@@ -28,9 +28,9 @@ import java.util.Map;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.MetaDataKey;
+import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.request.resource.IResource;
 import org.apache.wicket.request.resource.ResourceReference;
-import org.apache.wicket.util.lang.Args;
 
 /**
  * This resource reference is used to provide a reference to a resource based 
on Java NIO FileSystem
@@ -41,8 +41,8 @@ import org.apache.wicket.util.lang.Args;
  * 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<br>
  * <br>
- * You can optionally override {@link #getFileSystemResource()} to provide an 
inline mime type detection,
- * which is preferred to the default detection.<br>
+ * You can optionally override {@link #getFileSystemResource()} to provide an 
inline mime type
+ * detection, which is preferred to the default detection.<br>
  * <br>
  * Example:
  * 
@@ -53,16 +53,52 @@ import org.apache.wicket.util.lang.Args;
  * </code>
  * </pre>
  * 
+ * Example 2:
+ * 
+ * <pre>
+ * <code>
+ * mountResource("/filecontent/${name}", new 
FileSystemResourceReference("filesystem")
+ * {
+ *     private static final long serialVersionUID = 1L;
+ * 
+ *     {@literal @}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(
+ *                                             
FileSystemResourceReference.getPath(uri));
+ *                             }
+ *                             catch (IOException | URISyntaxException e)
+ *                             {
+ *                                     throw new WicketRuntimeException("Error 
while reading the file.", e);
+ *                             }
+ *                     };
+ *             };
+ *     }
+ * });
+ * </code>
+ * </pre>
+ * 
  * @author Tobias Soloschenko
  */
 public class FileSystemResourceReference extends ResourceReference
 {
        private static final long serialVersionUID = 1L;
 
-       private final Path path;
+       private Path path;
 
        /** The key for the file system meta data **/
-       public static final MetaDataKey<Map<URI, FileSystem>> 
FILE_SYSTEM_META_DATA_KEY = new MetaDataKey<Map<URI, FileSystem>>()
+       public static final MetaDataKey<Map<String, FileSystem>> 
FILE_SYSTEM_META_DATA_KEY = new MetaDataKey<Map<String, FileSystem>>()
        {
                private static final long serialVersionUID = 1L;
        };
@@ -78,11 +114,36 @@ public class FileSystemResourceReference extends 
ResourceReference
        public FileSystemResourceReference(String name, Path path)
        {
                super(name);
-               Args.notNull(path, "path");
                this.path = path;
        }
 
        /**
+        * Creates a file system resource reference based on the given name
+        * 
+        * @param name
+        *            the name of the resource reference
+        * 
+        */
+       public FileSystemResourceReference(String name)
+       {
+               super(name);
+       }
+
+       /**
+        * Creates a file system resource reference based on the given scope 
and name
+        * 
+        * @param scope
+        *            the scope as class
+        * @param name
+        *            the name of the resource reference
+        * 
+        */
+       public FileSystemResourceReference(Class<?> scope, String name)
+       {
+               super(scope, name);
+       }
+
+       /**
         * Creates a new {@link 
org.apache.wicket.markup.html.media.FileSystemResource} and applies the
         * path to it.
         */
@@ -99,6 +160,11 @@ public class FileSystemResourceReference extends 
ResourceReference
         */
        protected FileSystemResource getFileSystemResource()
        {
+               if (path == null)
+               {
+                       throw new WicketRuntimeException(
+                               "Please override #getResource() and provide a 
path if using a constructor which doesn't take one as argument.");
+               }
                return new FileSystemResource(path);
        }
 
@@ -115,8 +181,8 @@ public class FileSystemResourceReference extends 
ResourceReference
         * @throws URISyntaxException
         *             if the URI has no valid syntax
         */
-       public static Path getPath(URI uri, Map<String, String> env) throws 
IOException,
-               URISyntaxException
+       public static Path getPath(URI uri, Map<String, String> env)
+               throws IOException, URISyntaxException
        {
                String uriString = uri.toString();
                int indexOfExclamationMark = uriString.indexOf('!');
@@ -129,13 +195,14 @@ public class FileSystemResourceReference extends 
ResourceReference
 
                synchronized (FILE_SYSTEM_META_DATA_KEY)
                {
-                       Map<URI, FileSystem> metaData = Application.get()
+                       Map<String, FileSystem> metaData = Application.get()
                                .getMetaData(FILE_SYSTEM_META_DATA_KEY);
                        if (metaData == null)
                        {
-                               metaData = new HashMap<URI, FileSystem>();
+                               metaData = new HashMap<String, FileSystem>();
                                
Application.get().setMetaData(FILE_SYSTEM_META_DATA_KEY, metaData);
                        }
+                       fileSystem = metaData.get(zipFile);
                        if (fileSystem == null)
                        {
                                if (env == null)
@@ -145,10 +212,11 @@ public class FileSystemResourceReference extends 
ResourceReference
                                        env.put("encoding", "UTF-8");
                                }
                                fileSystem = FileSystems.newFileSystem(new 
URI(zipFile), env);
-                               metaData.put(uri, fileSystem);
+                               metaData.put(zipFile, fileSystem);
                        }
                }
                String fileName = uriString.substring(uriString.indexOf('!') + 
1);
+
                return fileSystem.getPath(fileName);
        }
 

http://git-wip-us.apache.org/repos/asf/wicket/blob/2daee37b/wicket-user-guide/src/docs/guide/resources/resources_15.gdoc
----------------------------------------------------------------------
diff --git a/wicket-user-guide/src/docs/guide/resources/resources_15.gdoc 
b/wicket-user-guide/src/docs/guide/resources/resources_15.gdoc
index b3b4582..69d61a4 100644
--- a/wicket-user-guide/src/docs/guide/resources/resources_15.gdoc
+++ b/wicket-user-guide/src/docs/guide/resources/resources_15.gdoc
@@ -16,6 +16,41 @@ HTML:
 <video wicket:id="video"/>
 {code}
 
+Using FileSystemResourceReference mounted:
+
+Java:
+{code}
+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(
+                                               
FileSystemResourceReference.getPath(uri));
+                               }
+                               catch (IOException | URISyntaxException e)
+                               {
+                                       throw new WicketRuntimeException("Error 
while reading the file.", e);
+                               }
+                       };
+               };
+       }
+});
+{code}
+
 Further FileSystemProviders and the corresponding FileSystems can be 
implemented as described here:
 
 
[http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/filesystemprovider.html|http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/filesystemprovider.html]
\ No newline at end of file

Reply via email to