You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by mg...@apache.org on 2017/12/30 21:23:25 UTC

wicket git commit: WICKET-6504 Use a serializable model for FileSystemResource's path

Repository: wicket
Updated Branches:
  refs/heads/master 63a3296c7 -> cbf458380


WICKET-6504 Use a serializable model for FileSystemResource's path


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

Branch: refs/heads/master
Commit: cbf458380afb1a71d97ee2b77196ff85ea258c37
Parents: 63a3296
Author: Martin Tzvetanov Grigorov <mg...@apache.org>
Authored: Sat Dec 30 23:22:21 2017 +0200
Committer: Martin Tzvetanov Grigorov <mg...@apache.org>
Committed: Sat Dec 30 23:22:21 2017 +0200

----------------------------------------------------------------------
 .../wicket/resource/FileSystemResource.java     | 59 +++++++++++++++++---
 .../FileSystemResourceReferenceTest.java        | 31 ++++++++++
 2 files changed, 82 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/wicket/blob/cbf45838/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 a940b1c..bc5c859 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
@@ -20,13 +20,16 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.nio.file.attribute.BasicFileAttributes;
 
 import org.apache.wicket.Application;
 import org.apache.wicket.WicketRuntimeException;
+import org.apache.wicket.model.LoadableDetachableModel;
 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>
@@ -40,7 +43,7 @@ public class FileSystemResource extends AbstractResource
 {
 	private static final long serialVersionUID = 1L;
 
-	private Path path;
+	private LoadableDetachableModel<Path> path;
 
 	/**
 	 * Creates a new file system resource based on the given path
@@ -50,7 +53,7 @@ public class FileSystemResource extends AbstractResource
 	 */
 	public FileSystemResource(Path path)
 	{
-		this.path = path;
+		this.path = new PathModel(path);
 	}
 
 	/**
@@ -68,7 +71,23 @@ public class FileSystemResource extends AbstractResource
 	@Override
 	protected ResourceResponse newResourceResponse(Attributes attributes)
 	{
-		return createResourceResponse(attributes, path);
+		return createResourceResponse(attributes, getPath());
+	}
+
+	@Override
+	public void respond(Attributes attributes)
+	{
+		try
+		{
+			super.respond(attributes);
+		}
+		finally
+		{
+			if (path != null)
+			{
+				path.detach();
+			}
+		}
 	}
 
 	/**
@@ -89,7 +108,7 @@ public class FileSystemResource extends AbstractResource
 				throw new WicketRuntimeException(
 					"Please override #newResourceResponse() and provide a path if using a constructor which doesn't take one as argument.");
 			}
-			this.path = path;
+			this.path = new PathModel(path);
 			long size = getSize();
 			ResourceResponse resourceResponse = new ResourceResponse();
 			resourceResponse.setContentType(getMimeType());
@@ -121,7 +140,7 @@ public class FileSystemResource extends AbstractResource
 	 */
 	protected long getSize() throws IOException
 	{
-		return Files.readAttributes(path, BasicFileAttributes.class).size();
+		return Files.readAttributes(getPath(), BasicFileAttributes.class).size();
 	}
 
 	/**
@@ -134,14 +153,15 @@ public class FileSystemResource extends AbstractResource
 	 */
 	protected String getMimeType() throws IOException
 	{
+		final Path _path = getPath();
 		String mimeType = null;
 		if (Application.exists())
 		{
-			mimeType = Application.get().getMimeType(path.getFileName().toString());
+			mimeType = Application.get().getMimeType(_path.getFileName().toString());
 		}
 		if (mimeType == null)
 		{
-			mimeType = Files.probeContentType(path);
+			mimeType = Files.probeContentType(_path);
 		}
 		return mimeType;
 	}
@@ -155,6 +175,29 @@ public class FileSystemResource extends AbstractResource
 	 */
 	protected InputStream getInputStream() throws IOException
 	{
-		return Files.newInputStream(path);
+		return Files.newInputStream(getPath());
+	}
+
+	private Path getPath()
+	{
+		return path.getObject();
+	}
+
+	private static class PathModel extends LoadableDetachableModel<Path>
+	{
+		private final String pathAsString;
+
+		public PathModel(Path path)
+		{
+			super(path);
+			Args.notNull(path, "path");
+			this.pathAsString = path.toString();
+		}
+
+		@Override
+		protected Path load()
+		{
+			return Paths.get(pathAsString);
+		}
 	}
 }

http://git-wip-us.apache.org/repos/asf/wicket/blob/cbf45838/wicket-core/src/test/java/org/apache/wicket/resource/FileSystemResourceReferenceTest.java
----------------------------------------------------------------------
diff --git a/wicket-core/src/test/java/org/apache/wicket/resource/FileSystemResourceReferenceTest.java b/wicket-core/src/test/java/org/apache/wicket/resource/FileSystemResourceReferenceTest.java
index acbb986..dab0e44 100644
--- a/wicket-core/src/test/java/org/apache/wicket/resource/FileSystemResourceReferenceTest.java
+++ b/wicket-core/src/test/java/org/apache/wicket/resource/FileSystemResourceReferenceTest.java
@@ -23,6 +23,7 @@ import java.net.URISyntaxException;
 import java.net.URL;
 import java.nio.file.Path;
 
+import org.apache.wicket.core.util.lang.WicketObjects;
 import org.apache.wicket.util.io.ByteArrayOutputStream;
 import org.apache.wicket.util.io.IOUtils;
 import org.apache.wicket.util.tester.WicketTestCase;
@@ -215,4 +216,34 @@ public class FileSystemResourceReferenceTest extends WicketTestCase
 		};
 		Assert.assertEquals("text/plain", fileSystemResourceMime.getMimeType());
 	}
+
+	/**
+	 * Test serialization of {@link FileSystemResource}
+	 */
+	@Test
+	public void testSerialization() throws IOException, URISyntaxException
+	{
+		InputStream inputStream = null;
+		try
+		{
+			URL resource = FileSystemResourceReferenceTest.class.getResource("FileSystemResourceReference.txt");
+			Path path = FileSystemResourceReference.getPath(resource.toURI());
+			final FileSystemResource fileSystemResource = new FileSystemResource(path);
+			final FileSystemResource cloned = WicketObjects.cloneObject(fileSystemResource);
+
+			Assert.assertEquals(cloned.getSize(), 54);
+
+			// Content
+			inputStream = cloned.getInputStream();
+			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+			IOUtils.copy(inputStream, outputStream);
+			Assert.assertEquals("FileSystemResourceReference.zip content in normal file",
+					outputStream.toString());
+		}
+		finally
+		{
+			IOUtils.closeQuietly(inputStream);
+		}
+	}
+
 }