1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -95,11 +95,10 @@ public boolean exists() {
95
95
else {
96
96
// Try a URL connection content-length header...
97
97
URLConnection con = url .openConnection ();
98
- ResourceUtils . useCachesIfNecessary (con );
98
+ customizeConnection (con );
99
99
HttpURLConnection httpCon =
100
100
(con instanceof HttpURLConnection ? (HttpURLConnection ) con : null );
101
101
if (httpCon != null ) {
102
- httpCon .setRequestMethod ("HEAD" );
103
102
int code = httpCon .getResponseCode ();
104
103
if (code == HttpURLConnection .HTTP_OK ) {
105
104
return true ;
@@ -157,10 +156,7 @@ public long contentLength() throws IOException {
157
156
else {
158
157
// Try a URL connection content-length header...
159
158
URLConnection con = url .openConnection ();
160
- ResourceUtils .useCachesIfNecessary (con );
161
- if (con instanceof HttpURLConnection ) {
162
- ((HttpURLConnection ) con ).setRequestMethod ("HEAD" );
163
- }
159
+ customizeConnection (con );
164
160
return con .getContentLength ();
165
161
}
166
162
}
@@ -175,15 +171,40 @@ public long lastModified() throws IOException {
175
171
else {
176
172
// Try a URL connection last-modified header...
177
173
URLConnection con = url .openConnection ();
178
- ResourceUtils .useCachesIfNecessary (con );
179
- if (con instanceof HttpURLConnection ) {
180
- ((HttpURLConnection ) con ).setRequestMethod ("HEAD" );
181
- }
174
+ customizeConnection (con );
182
175
return con .getLastModified ();
183
176
}
184
177
}
185
178
186
179
180
+ /**
181
+ * Customize the given {@link URLConnection}, obtained in the course of an
182
+ * {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
183
+ * <p>Calls {@link ResourceUtils#useCachesIfNecessary(URLConnection)} and
184
+ * delegates to {@link #customizeConnection(HttpURLConnection)} if possible.
185
+ * Can be overridden in subclasses.
186
+ * @param con the URLConnection to customize
187
+ * @throws IOException if thrown from URLConnection methods
188
+ */
189
+ protected void customizeConnection (URLConnection con ) throws IOException {
190
+ ResourceUtils .useCachesIfNecessary (con );
191
+ if (con instanceof HttpURLConnection ) {
192
+ customizeConnection ((HttpURLConnection ) con );
193
+ }
194
+ }
195
+
196
+ /**
197
+ * Customize the given {@link HttpURLConnection}, obtained in the course of an
198
+ * {@link #exists()}, {@link #contentLength()} or {@link #lastModified()} call.
199
+ * <p>Sets request method "HEAD" by default. Can be overridden in subclasses.
200
+ * @param con the HttpURLConnection to customize
201
+ * @throws IOException if thrown from HttpURLConnection methods
202
+ */
203
+ protected void customizeConnection (HttpURLConnection con ) throws IOException {
204
+ con .setRequestMethod ("HEAD" );
205
+ }
206
+
207
+
187
208
/**
188
209
* Inner delegate class, avoiding a hard JBoss VFS API dependency at runtime.
189
210
*/
0 commit comments