Skip to content

Commit 001ccca

Browse files
committed
Merge branch '6.2.x'
2 parents 6240556 + ee60eb7 commit 001ccca

File tree

2 files changed

+90
-17
lines changed

2 files changed

+90
-17
lines changed

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

+43-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,6 +66,20 @@ public boolean exists() {
6666
else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
6767
return false;
6868
}
69+
else if (code == HttpURLConnection.HTTP_BAD_METHOD) {
70+
con = url.openConnection();
71+
customizeConnection(con);
72+
if (con instanceof HttpURLConnection newHttpCon) {
73+
code = newHttpCon.getResponseCode();
74+
if (code == HttpURLConnection.HTTP_OK) {
75+
return true;
76+
}
77+
else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
78+
return false;
79+
}
80+
httpCon = newHttpCon;
81+
}
82+
}
6983
}
7084
if (con.getContentLengthLong() > 0) {
7185
return true;
@@ -111,6 +125,15 @@ boolean checkReadable(URL url) {
111125
if (con instanceof HttpURLConnection httpCon) {
112126
httpCon.setRequestMethod("HEAD");
113127
int code = httpCon.getResponseCode();
128+
if (code == HttpURLConnection.HTTP_BAD_METHOD) {
129+
con = url.openConnection();
130+
customizeConnection(con);
131+
if (!(con instanceof HttpURLConnection newHttpCon)) {
132+
return false;
133+
}
134+
code = newHttpCon.getResponseCode();
135+
httpCon = newHttpCon;
136+
}
114137
if (code != HttpURLConnection.HTTP_OK) {
115138
httpCon.disconnect();
116139
return false;
@@ -259,7 +282,14 @@ public long contentLength() throws IOException {
259282
if (con instanceof HttpURLConnection httpCon) {
260283
httpCon.setRequestMethod("HEAD");
261284
}
262-
return con.getContentLengthLong();
285+
long length = con.getContentLengthLong();
286+
if (length <= 0 && con instanceof HttpURLConnection httpCon &&
287+
httpCon.getResponseCode() == HttpURLConnection.HTTP_BAD_METHOD) {
288+
con = url.openConnection();
289+
customizeConnection(con);
290+
length = con.getContentLengthLong();
291+
}
292+
return length;
263293
}
264294
}
265295

@@ -288,9 +318,17 @@ public long lastModified() throws IOException {
288318
httpCon.setRequestMethod("HEAD");
289319
}
290320
long lastModified = con.getLastModified();
291-
if (fileCheck && lastModified == 0 && con.getContentLengthLong() <= 0) {
292-
throw new FileNotFoundException(getDescription() +
293-
" cannot be resolved in the file system for checking its last-modified timestamp");
321+
if (lastModified == 0) {
322+
if (con instanceof HttpURLConnection httpCon &&
323+
httpCon.getResponseCode() == HttpURLConnection.HTTP_BAD_METHOD) {
324+
con = url.openConnection();
325+
customizeConnection(con);
326+
lastModified = con.getLastModified();
327+
}
328+
if (fileCheck && con.getContentLengthLong() <= 0) {
329+
throw new FileNotFoundException(getDescription() +
330+
" cannot be resolved in the file system for checking its last-modified timestamp");
331+
}
294332
}
295333
return lastModified;
296334
}

spring-core/src/test/java/org/springframework/core/io/ResourceTests.java

+47-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -306,7 +306,9 @@ private void assertUrlAndUriBehavior(Resource resource) throws IOException {
306306
@Nested
307307
class UrlResourceTests {
308308

309-
private MockWebServer server = new MockWebServer();
309+
private static final String LAST_MODIFIED = "Wed, 09 Apr 2014 09:57:42 GMT";
310+
311+
private final MockWebServer server = new MockWebServer();
310312

311313
@Test
312314
void sameResourceWithRelativePathIsEqual() throws Exception {
@@ -385,22 +387,44 @@ void unusualRelativeResourcesAreEqual() throws Exception {
385387

386388
@Test
387389
void missingRemoteResourceDoesNotExist() throws Exception {
388-
String baseUrl = startServer();
390+
String baseUrl = startServer(true);
389391
UrlResource resource = new UrlResource(baseUrl + "/missing");
390392
assertThat(resource.exists()).isFalse();
391393
}
392394

393395
@Test
394396
void remoteResourceExists() throws Exception {
395-
String baseUrl = startServer();
397+
String baseUrl = startServer(true);
398+
UrlResource resource = new UrlResource(baseUrl + "/resource");
399+
assertThat(resource.exists()).isTrue();
400+
assertThat(resource.isReadable()).isTrue();
401+
assertThat(resource.contentLength()).isEqualTo(6);
402+
assertThat(resource.lastModified()).isGreaterThan(0);
403+
}
404+
405+
@Test
406+
void remoteResourceExistsFallback() throws Exception {
407+
String baseUrl = startServer(false);
396408
UrlResource resource = new UrlResource(baseUrl + "/resource");
397409
assertThat(resource.exists()).isTrue();
410+
assertThat(resource.isReadable()).isTrue();
398411
assertThat(resource.contentLength()).isEqualTo(6);
412+
assertThat(resource.lastModified()).isGreaterThan(0);
399413
}
400414

401415
@Test
402416
void canCustomizeHttpUrlConnectionForExists() throws Exception {
403-
String baseUrl = startServer();
417+
String baseUrl = startServer(true);
418+
CustomResource resource = new CustomResource(baseUrl + "/resource");
419+
assertThat(resource.exists()).isTrue();
420+
RecordedRequest request = this.server.takeRequest();
421+
assertThat(request.getMethod()).isEqualTo("HEAD");
422+
assertThat(request.getHeader("Framework-Name")).isEqualTo("Spring");
423+
}
424+
425+
@Test
426+
void canCustomizeHttpUrlConnectionForExistsFallback() throws Exception {
427+
String baseUrl = startServer(false);
404428
CustomResource resource = new CustomResource(baseUrl + "/resource");
405429
assertThat(resource.exists()).isTrue();
406430
RecordedRequest request = this.server.takeRequest();
@@ -410,7 +434,7 @@ void canCustomizeHttpUrlConnectionForExists() throws Exception {
410434

411435
@Test
412436
void canCustomizeHttpUrlConnectionForRead() throws Exception {
413-
String baseUrl = startServer();
437+
String baseUrl = startServer(true);
414438
CustomResource resource = new CustomResource(baseUrl + "/resource");
415439
assertThat(resource.getInputStream()).hasContent("Spring");
416440
RecordedRequest request = this.server.takeRequest();
@@ -420,7 +444,7 @@ void canCustomizeHttpUrlConnectionForRead() throws Exception {
420444

421445
@Test
422446
void useUserInfoToSetBasicAuth() throws Exception {
423-
startServer();
447+
startServer(true);
424448
UrlResource resource = new UrlResource(
425449
"http://alice:secret@localhost:" + this.server.getPort() + "/resource");
426450
assertThat(resource.getInputStream()).hasContent("Spring");
@@ -436,8 +460,8 @@ void shutdown() throws Exception {
436460
this.server.shutdown();
437461
}
438462

439-
private String startServer() throws Exception {
440-
this.server.setDispatcher(new ResourceDispatcher());
463+
private String startServer(boolean withHeadSupport) throws Exception {
464+
this.server.setDispatcher(new ResourceDispatcher(withHeadSupport));
441465
this.server.start();
442466
return "http://localhost:" + this.server.getPort();
443467
}
@@ -456,15 +480,26 @@ protected void customizeConnection(HttpURLConnection con) {
456480

457481
class ResourceDispatcher extends Dispatcher {
458482

483+
boolean withHeadSupport;
484+
485+
public ResourceDispatcher(boolean withHeadSupport) {
486+
this.withHeadSupport = withHeadSupport;
487+
}
488+
459489
@Override
460490
public MockResponse dispatch(RecordedRequest request) {
461491
if (request.getPath().equals("/resource")) {
462492
return switch (request.getMethod()) {
463-
case "HEAD" -> new MockResponse()
464-
.addHeader("Content-Length", "6");
493+
case "HEAD" -> (this.withHeadSupport ?
494+
new MockResponse()
495+
.addHeader("Content-Type", "text/plain")
496+
.addHeader("Content-Length", "6")
497+
.addHeader("Last-Modified", LAST_MODIFIED) :
498+
new MockResponse().setResponseCode(405));
465499
case "GET" -> new MockResponse()
466-
.addHeader("Content-Length", "6")
467500
.addHeader("Content-Type", "text/plain")
501+
.addHeader("Content-Length", "6")
502+
.addHeader("Last-Modified", LAST_MODIFIED)
468503
.setBody("Spring");
469504
default -> new MockResponse().setResponseCode(404);
470505
};

0 commit comments

Comments
 (0)