Author: rgoers
Date: Sat Sep 24 08:07:53 2011
New Revision: 1175125
URL: http://svn.apache.org/viewvc?rev=1175125&view=rev
Log:
Apply patch for VFS-325
Added:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/UrlTests.java
commons/proper/vfs/trunk/core/src/test/resources/test-data/test-hash-#test.txt
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFileName.java
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java
commons/proper/vfs/trunk/src/changes/changes.xml
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java?rev=1175125&r1=1175124&r2=1175125&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java
(original)
+++
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFile.java
Sat Sep 24 08:07:53 2011
@@ -256,6 +256,24 @@ public class LocalFile extends AbstractF
{
throw new FileSystemException(e);
}
-
+ }
+
+ /**
+ * Returns the URI of the file.
+ * @return The URI of the file.
+ */
+ @Override
+ public String toString()
+ {
+ try
+ {
+ // VFS-325: URI may contain percent-encoded values as part of
filename, so decode
+ // those characters before returning
+ return UriParser.decode(getName().getURI());
+ }
+ catch(FileSystemException e)
+ {
+ return getName().getURI();
+ }
}
}
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFileName.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFileName.java?rev=1175125&r1=1175124&r2=1175125&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFileName.java
(original)
+++
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/provider/local/LocalFileName.java
Sat Sep 24 08:07:53 2011
@@ -18,7 +18,9 @@ package org.apache.commons.vfs2.provider
import org.apache.commons.vfs2.FileName;
import org.apache.commons.vfs2.FileType;
+import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.provider.AbstractFileName;
+import org.apache.commons.vfs2.provider.UriParser;
/**
* A local file URI.
@@ -27,6 +29,21 @@ import org.apache.commons.vfs2.provider.
*/
public class LocalFileName extends AbstractFileName
{
+ // URI Characters that are possible in local filenames, but must be
escaped
+ // for proper URI handling.
+ //
+ // How reserved URI chars were selected:
+ //
+ // URIs can contain :, /, ?, #, @
+ // See http://download.oracle.com/javase/6/docs/api/java/net/URI.html
+ // http://tools.ietf.org/html/rfc3986#section-2.2
+ //
+ // Since : and / occur before the path, only chars after path are escaped
(i.e., # and ?)
+ // ? is a reserved filesystem character for Windows and Unix, so can't be
part of a filename.
+ // Therefore only # is a reserved char in a URI as part of the path that
can be in the filename.
+ private static final char RESERVED_URI_CHARS[] = {'#'};
+
+
private final String rootFile;
protected LocalFileName(final String scheme,
@@ -58,6 +75,81 @@ public class LocalFileName extends Abstr
{
return new LocalFileName(getScheme(), rootFile, path, type);
}
+
+ /**
+ * Returns the absolute URI of the file.
+ * @return The absolute URI of the file.
+ */
+ @Override
+ public String getURI()
+ {
+ String uri = super.getURI();
+
+ if (uri != null && uri.length() > 0)
+ {
+ try
+ {
+ // VFS-325: Handle URI special characters in filename
+ // Decode the base uri and re-encode with URI special
characters
+ uri = UriParser.decode(uri);
+
+ uri = UriParser.encode(uri, RESERVED_URI_CHARS);
+ }
+ catch(FileSystemException e)
+ {
+ // Default to base uri value
+ }
+ }
+
+ return uri;
+ }
+
+ /**
+ * returns a "friendly path", this is a path without a password.
+ * @return The "friendly" URI.
+ */
+ @Override
+ public String getFriendlyURI()
+ {
+ String uri = super.getFriendlyURI();
+
+ if (uri != null && uri.length() > 0)
+ {
+ try
+ {
+ // VFS-325: Handle URI special characters in filename
+ // Decode the base uri and re-encode with URI special
characters
+ uri = UriParser.decode(uri);
+
+ uri = UriParser.encode(uri, RESERVED_URI_CHARS);
+ }
+ catch(FileSystemException e)
+ {
+ // Default to base uri value
+ }
+ }
+
+ return uri;
+ }
+
+ /**
+ * Returns the decoded URI of the file.
+ * @return the FileName as a URI.
+ */
+ @Override
+ public String toString()
+ {
+ try
+ {
+ return UriParser.decode(super.getURI());
+ }
+ catch(FileSystemException e)
+ {
+ return super.getURI();
+ }
+ }
+
+
/**
* Builds the root URI for this file name.
Modified:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java?rev=1175125&r1=1175124&r2=1175125&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java
(original)
+++
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/LocalProviderTestCase.java
Sat Sep 24 08:07:53 2011
@@ -43,6 +43,10 @@ public class LocalProviderTestCase
{
final ProviderTestSuite testSuite = new ProviderTestSuite(new
LocalProviderTestCase());
testSuite.addTests(FileNameTests.class);
+
+ // VFS-325
+ testSuite.addTests(UrlTests.class);
+
return testSuite;
}
Added:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/UrlTests.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/UrlTests.java?rev=1175125&view=auto
==============================================================================
---
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/UrlTests.java
(added)
+++
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/provider/local/test/UrlTests.java
Sat Sep 24 08:07:53 2011
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs2.provider.local.test;
+
+import java.io.File;
+import java.net.URL;
+
+import org.apache.commons.vfs2.FileObject;
+import org.apache.commons.vfs2.FileSelectInfo;
+import org.apache.commons.vfs2.FileSelector;
+import org.apache.commons.vfs2.FileSystemManager;
+import org.apache.commons.vfs2.provider.UriParser;
+import org.apache.commons.vfs2.Selectors;
+import org.apache.commons.vfs2.VFS;
+import org.apache.commons.vfs2.test.AbstractProviderTestCase;
+
+/**
+ * Additional URL tests for local file system.
+ *
+ * @version $Revision$ $Date$
+ */
+public class UrlTests
+ extends AbstractProviderTestCase
+{
+ /**
+ * Tests resolution of an absolute file name.
+ */
+ public void testHashURL() throws Exception
+ {
+ final FileObject file =
getReadFolder().resolveFile("test-hash-#test.txt");
+
+ assertEquals(file.toString(),
UriParser.decode(file.getURL().toString()));
+ }
+
+ /**
+ * Tests FindFiles with a filename that has a hash sign in it.
+ */
+ public void testHashFindFiles() throws Exception
+ {
+ FileSystemManager fsManager = VFS.getManager();
+
+ FileObject[] foList =
getBaseFolder().findFiles(Selectors.SELECT_FILES);
+
+ boolean hashFileFound = false;
+ for (FileObject fo : foList)
+ {
+ if (fo.getURL().toString().contains("test-hash"))
+ {
+ hashFileFound = true;
+
+ assertEquals(fo.toString(),
UriParser.decode(fo.getURL().toString()));
+ }
+ }
+
+ if (!hashFileFound)
+ {
+ fail("Test hash file containing 'test-hash' not found");
+ }
+ }
+}
Added:
commons/proper/vfs/trunk/core/src/test/resources/test-data/test-hash-#test.txt
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/resources/test-data/test-hash-%23test.txt?rev=1175125&view=auto
==============================================================================
---
commons/proper/vfs/trunk/core/src/test/resources/test-data/test-hash-#test.txt
(added)
+++
commons/proper/vfs/trunk/core/src/test/resources/test-data/test-hash-#test.txt
Sat Sep 24 08:07:53 2011
@@ -0,0 +1 @@
+Test file for VFS-325: Bad handling of hashs (#) in file names when walking a
file tree using findFiles().
\ No newline at end of file
Modified: commons/proper/vfs/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/src/changes/changes.xml?rev=1175125&r1=1175124&r2=1175125&view=diff
==============================================================================
--- commons/proper/vfs/trunk/src/changes/changes.xml (original)
+++ commons/proper/vfs/trunk/src/changes/changes.xml Sat Sep 24 08:07:53 2011
@@ -23,6 +23,9 @@
<body>
<release version="2.1" date="TBD" description="">
+ <action issue="VFS-325" dev="rgoers" type="fix" due-to="Larry Reeve">
+ Allow # character in file names.
+ </action>
<action issue="VFS-335" dev="rgoers" type="fix">
Use atomic variables in MonitorInputStream.
</action>