Browse Source

Introduced customizeConnection callbacks for URLConnection used by exists() / contentLength() / lastModified()

Issue: SPR-11320
pull/443/head
Juergen Hoeller 11 years ago
parent
commit
4f45ad549e
  1. 43
      spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java

43
spring-core/src/main/java/org/springframework/core/io/AbstractFileResolvingResource.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2011 the original author or authors. * Copyright 2002-2013 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -95,11 +95,10 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
else { else {
// Try a URL connection content-length header... // Try a URL connection content-length header...
URLConnection con = url.openConnection(); URLConnection con = url.openConnection();
ResourceUtils.useCachesIfNecessary(con); customizeConnection(con);
HttpURLConnection httpCon = HttpURLConnection httpCon =
(con instanceof HttpURLConnection ? (HttpURLConnection) con : null); (con instanceof HttpURLConnection ? (HttpURLConnection) con : null);
if (httpCon != null) { if (httpCon != null) {
httpCon.setRequestMethod("HEAD");
int code = httpCon.getResponseCode(); int code = httpCon.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) { if (code == HttpURLConnection.HTTP_OK) {
return true; return true;
@ -157,10 +156,7 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
else { else {
// Try a URL connection content-length header... // Try a URL connection content-length header...
URLConnection con = url.openConnection(); URLConnection con = url.openConnection();
ResourceUtils.useCachesIfNecessary(con); customizeConnection(con);
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).setRequestMethod("HEAD");
}
return con.getContentLength(); return con.getContentLength();
} }
} }
@ -175,15 +171,40 @@ public abstract class AbstractFileResolvingResource extends AbstractResource {
else { else {
// Try a URL connection last-modified header... // Try a URL connection last-modified header...
URLConnection con = url.openConnection(); URLConnection con = url.openConnection();
ResourceUtils.useCachesIfNecessary(con); customizeConnection(con);
if (con instanceof HttpURLConnection) {
((HttpURLConnection) con).setRequestMethod("HEAD");
}
return con.getLastModified(); return con.getLastModified();
} }
} }
/**
* Customize the given {@link URLConnection}, obtained in the course of an
* {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
* <p>Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and
* delegates to {@link #customizeConnection(HttpURLConnection)} if possible.
* Can be overridden in subclasses.
* @param con the URLConnection to customize
* @throws IOException if thrown from URLConnection methods
*/
protected void customizeConnection(URLConnection con) throws IOException {
ResourceUtils.useCachesIfNecessary(con);
if (con instanceof HttpURLConnection) {
customizeConnection((HttpURLConnection) con);
}
}
/**
* Customize the given {@link HttpURLConnection}, obtained in the course of an
* {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
* <p>Sets request method "HEAD" by default. Can be overridden in subclasses.
* @param con the HttpURLConnection to customize
* @throws IOException if thrown from HttpURLConnection methods
*/
protected void customizeConnection(HttpURLConnection con) throws IOException {
con.setRequestMethod("HEAD");
}
/** /**
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime. * Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.
*/ */

Loading…
Cancel
Save