From e211c0906536d8f941e92950b2dc84c608865fc6 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 29 Sep 2010 14:45:44 +0000 Subject: [PATCH] fixed exists() check for resources in zipped files (SPR-7559) --- .../io/AbstractFileResolvingResource.java | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java b/org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java index d285503697..a6456a8235 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java +++ b/org.springframework.core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java @@ -18,6 +18,7 @@ package org.springframework.core.io; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URI; import java.net.URL; @@ -95,14 +96,28 @@ public abstract class AbstractFileResolvingResource extends AbstractResource { // Try a URL connection content-length header... URLConnection con = url.openConnection(); con.setUseCaches(false); - if (con instanceof HttpURLConnection) { - ((HttpURLConnection) con).setRequestMethod("HEAD"); + HttpURLConnection httpCon = + (con instanceof HttpURLConnection ? (HttpURLConnection) con : null); + if (httpCon != null) { + httpCon.setRequestMethod("HEAD"); + if (httpCon.getResponseCode() == HttpURLConnection.HTTP_OK) { + return true; + } } - boolean doesExist = (con.getContentLength() >= 0); - if (!doesExist && con instanceof HttpURLConnection) { - ((HttpURLConnection) con).disconnect(); + if (con.getContentLength() >= 0) { + return true; + } + if (httpCon != null) { + // no HTTP OK status, and no content-length header: give up + httpCon.disconnect(); + return false; + } + else { + // Fall back to stream existence: can we open the stream? + InputStream is = getInputStream(); + is.close(); + return true; } - return doesExist; } } catch (IOException ex) { @@ -178,4 +193,5 @@ public abstract class AbstractFileResolvingResource extends AbstractResource { return new VfsResource(VfsUtils.getRoot(uri)); } } -} \ No newline at end of file + +}