From 2eaad85a0cbc7903eafa013cd37d0116683d0c7c Mon Sep 17 00:00:00 2001 From: Michael Gorovoy Date: Thu, 7 Jul 2011 18:02:31 -0400 Subject: [PATCH] 350634 Reverted FileResource constructor changes This reverts commit 95ff9d73f0b00a527706ace2698bd3db103b01fa. --- VERSION.txt | 1 + .../jetty/util/resource/FileResource.java | 65 +++++++++++-------- .../eclipse/jetty/util/resource/Resource.java | 7 +- .../jetty/util/resource/ResourceTest.java | 2 - 4 files changed, 45 insertions(+), 30 deletions(-) diff --git a/VERSION.txt b/VERSION.txt index ec6d1286579..cae477833bd 100644 --- a/VERSION.txt +++ b/VERSION.txt @@ -2,6 +2,7 @@ jetty-7.4.4-SNAPSHOT + 308851 Converted all jetty-client module tests to JUnit 4 + 345268 JDBCSessionManager does not work with maxInactiveInterval = -1 + 350397 SelectChannelConnector does not shutdown gracefully + + 350634 Reverted FileResource constructor changes + 351039 Forward dispatch should retain locale + 351199 HttpServletResponse.encodeURL() wrongly encodes an url without path when cookies are disabled + JETTY-1153 Default charset/encoding of HTTP POST requests diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/FileResource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/FileResource.java index a7ef6f75b91..e37b223e88f 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/FileResource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/FileResource.java @@ -66,16 +66,6 @@ public class FileResource extends URLResource { return __checkAliases; } - - /* -------------------------------------------------------- */ - public FileResource(File file) throws MalformedURLException - { - super(file.toURI().toURL(),null); - - _file=file; - - fixUrlString(); - } /* -------------------------------------------------------- */ public FileResource(URL url) @@ -83,24 +73,35 @@ public class FileResource extends URLResource { super(url,null); - _file =new File(url.toURI()); + try + { + // Try standard API to convert URL to file. + _file =new File(new URI(url.toString())); + } + catch (Exception e) + { + Log.ignore(e); + try + { + // Assume that File.toURL produced unencoded chars. So try + // encoding them. + String file_url="file:"+URIUtil.encodePath(url.toString().substring(5)); + URI uri = new URI(file_url); + if (uri.getAuthority()==null) + _file = new File(uri); + else + _file = new File("//"+uri.getAuthority()+URIUtil.decodePath(url.getFile())); + } + catch (Exception e2) + { + Log.ignore(e2); - fixUrlString(); - } - - /* -------------------------------------------------------- */ - FileResource(URL url, URLConnection connection, File file) - { - super(url,connection); - - _file=file; - - fixUrlString(); - } - - /* -------------------------------------------------------- */ - private void fixUrlString() - { + // Still can't get the file. Doh! try good old hack! + checkConnection(); + Permission perm = _connection.getPermission(); + _file = new File(perm==null?url.getFile():perm.getName()); + } + } if (_file.isDirectory()) { if (!_urlString.endsWith("/")) @@ -111,6 +112,16 @@ public class FileResource extends URLResource if (_urlString.endsWith("/")) _urlString=_urlString.substring(0,_urlString.length()-1); } + + } + + /* -------------------------------------------------------- */ + FileResource(URL url, URLConnection connection, File file) + { + super(url,connection); + _file=file; + if (_file.isDirectory() && !_urlString.endsWith("/")) + _urlString=_urlString+"/"; } /* -------------------------------------------------------- */ diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java index 14c2791e04b..ae3945f7376 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/resource/Resource.java @@ -160,7 +160,12 @@ public abstract class Resource implements ResourceFactory resource=resource.substring(2); File file=new File(resource).getCanonicalFile(); - return new FileResource(file); + url=file.toURI().toURL(); + + URLConnection connection=url.openConnection(); + connection.setUseCaches(useCaches); + FileResource fileResource= new FileResource(url,connection,file); + return fileResource; } catch(Exception e2) { diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java index 29c74765ff7..c8d46eff9e8 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/resource/ResourceTest.java @@ -120,11 +120,9 @@ public class ResourceTest __userDir = new File(perm.getName()).getCanonicalPath() + File.separatorChar; __relDir = "src/test/java/org/eclipse/jetty/util/resource/".replace('/', File.separatorChar); - /* System.err.println("User Dir="+__userDir); System.err.println("Rel Dir="+__relDir); System.err.println("User URL="+__userURL); - */ tmpFile=File.createTempFile("test",null).getCanonicalFile(); tmpFile.deleteOnExit();