Skip to content

Commit

Permalink
Merge branch '6.2.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jan 20, 2025
2 parents 6240556 + ee60eb7 commit 001ccca
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,6 +66,20 @@ public boolean exists() {
else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
return false;
}
else if (code == HttpURLConnection.HTTP_BAD_METHOD) {
con = url.openConnection();
customizeConnection(con);
if (con instanceof HttpURLConnection newHttpCon) {
code = newHttpCon.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) {
return true;
}
else if (code == HttpURLConnection.HTTP_NOT_FOUND) {
return false;
}
httpCon = newHttpCon;
}
}
}
if (con.getContentLengthLong() > 0) {
return true;
Expand Down Expand Up @@ -111,6 +125,15 @@ boolean checkReadable(URL url) {
if (con instanceof HttpURLConnection httpCon) {
httpCon.setRequestMethod("HEAD");
int code = httpCon.getResponseCode();
if (code == HttpURLConnection.HTTP_BAD_METHOD) {
con = url.openConnection();
customizeConnection(con);
if (!(con instanceof HttpURLConnection newHttpCon)) {
return false;
}
code = newHttpCon.getResponseCode();
httpCon = newHttpCon;
}
if (code != HttpURLConnection.HTTP_OK) {
httpCon.disconnect();
return false;
Expand Down Expand Up @@ -259,7 +282,14 @@ public long contentLength() throws IOException {
if (con instanceof HttpURLConnection httpCon) {
httpCon.setRequestMethod("HEAD");
}
return con.getContentLengthLong();
long length = con.getContentLengthLong();
if (length <= 0 && con instanceof HttpURLConnection httpCon &&
httpCon.getResponseCode() == HttpURLConnection.HTTP_BAD_METHOD) {
con = url.openConnection();
customizeConnection(con);
length = con.getContentLengthLong();
}
return length;
}
}

Expand Down Expand Up @@ -288,9 +318,17 @@ public long lastModified() throws IOException {
httpCon.setRequestMethod("HEAD");
}
long lastModified = con.getLastModified();
if (fileCheck && lastModified == 0 && con.getContentLengthLong() <= 0) {
throw new FileNotFoundException(getDescription() +
" cannot be resolved in the file system for checking its last-modified timestamp");
if (lastModified == 0) {
if (con instanceof HttpURLConnection httpCon &&
httpCon.getResponseCode() == HttpURLConnection.HTTP_BAD_METHOD) {
con = url.openConnection();
customizeConnection(con);
lastModified = con.getLastModified();
}
if (fileCheck && con.getContentLengthLong() <= 0) {
throw new FileNotFoundException(getDescription() +
" cannot be resolved in the file system for checking its last-modified timestamp");
}
}
return lastModified;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2024 the original author or authors.
* Copyright 2002-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -306,7 +306,9 @@ private void assertUrlAndUriBehavior(Resource resource) throws IOException {
@Nested
class UrlResourceTests {

private MockWebServer server = new MockWebServer();
private static final String LAST_MODIFIED = "Wed, 09 Apr 2014 09:57:42 GMT";

private final MockWebServer server = new MockWebServer();

@Test
void sameResourceWithRelativePathIsEqual() throws Exception {
Expand Down Expand Up @@ -385,22 +387,44 @@ void unusualRelativeResourcesAreEqual() throws Exception {

@Test
void missingRemoteResourceDoesNotExist() throws Exception {
String baseUrl = startServer();
String baseUrl = startServer(true);
UrlResource resource = new UrlResource(baseUrl + "/missing");
assertThat(resource.exists()).isFalse();
}

@Test
void remoteResourceExists() throws Exception {
String baseUrl = startServer();
String baseUrl = startServer(true);
UrlResource resource = new UrlResource(baseUrl + "/resource");
assertThat(resource.exists()).isTrue();
assertThat(resource.isReadable()).isTrue();
assertThat(resource.contentLength()).isEqualTo(6);
assertThat(resource.lastModified()).isGreaterThan(0);
}

@Test
void remoteResourceExistsFallback() throws Exception {
String baseUrl = startServer(false);
UrlResource resource = new UrlResource(baseUrl + "/resource");
assertThat(resource.exists()).isTrue();
assertThat(resource.isReadable()).isTrue();
assertThat(resource.contentLength()).isEqualTo(6);
assertThat(resource.lastModified()).isGreaterThan(0);
}

@Test
void canCustomizeHttpUrlConnectionForExists() throws Exception {
String baseUrl = startServer();
String baseUrl = startServer(true);
CustomResource resource = new CustomResource(baseUrl + "/resource");
assertThat(resource.exists()).isTrue();
RecordedRequest request = this.server.takeRequest();
assertThat(request.getMethod()).isEqualTo("HEAD");
assertThat(request.getHeader("Framework-Name")).isEqualTo("Spring");
}

@Test
void canCustomizeHttpUrlConnectionForExistsFallback() throws Exception {
String baseUrl = startServer(false);
CustomResource resource = new CustomResource(baseUrl + "/resource");
assertThat(resource.exists()).isTrue();
RecordedRequest request = this.server.takeRequest();
Expand All @@ -410,7 +434,7 @@ void canCustomizeHttpUrlConnectionForExists() throws Exception {

@Test
void canCustomizeHttpUrlConnectionForRead() throws Exception {
String baseUrl = startServer();
String baseUrl = startServer(true);
CustomResource resource = new CustomResource(baseUrl + "/resource");
assertThat(resource.getInputStream()).hasContent("Spring");
RecordedRequest request = this.server.takeRequest();
Expand All @@ -420,7 +444,7 @@ void canCustomizeHttpUrlConnectionForRead() throws Exception {

@Test
void useUserInfoToSetBasicAuth() throws Exception {
startServer();
startServer(true);
UrlResource resource = new UrlResource(
"http://alice:secret@localhost:" + this.server.getPort() + "/resource");
assertThat(resource.getInputStream()).hasContent("Spring");
Expand All @@ -436,8 +460,8 @@ void shutdown() throws Exception {
this.server.shutdown();
}

private String startServer() throws Exception {
this.server.setDispatcher(new ResourceDispatcher());
private String startServer(boolean withHeadSupport) throws Exception {
this.server.setDispatcher(new ResourceDispatcher(withHeadSupport));
this.server.start();
return "http://localhost:" + this.server.getPort();
}
Expand All @@ -456,15 +480,26 @@ protected void customizeConnection(HttpURLConnection con) {

class ResourceDispatcher extends Dispatcher {

boolean withHeadSupport;

public ResourceDispatcher(boolean withHeadSupport) {
this.withHeadSupport = withHeadSupport;
}

@Override
public MockResponse dispatch(RecordedRequest request) {
if (request.getPath().equals("/resource")) {
return switch (request.getMethod()) {
case "HEAD" -> new MockResponse()
.addHeader("Content-Length", "6");
case "HEAD" -> (this.withHeadSupport ?
new MockResponse()
.addHeader("Content-Type", "text/plain")
.addHeader("Content-Length", "6")
.addHeader("Last-Modified", LAST_MODIFIED) :
new MockResponse().setResponseCode(405));
case "GET" -> new MockResponse()
.addHeader("Content-Length", "6")
.addHeader("Content-Type", "text/plain")
.addHeader("Content-Length", "6")
.addHeader("Last-Modified", LAST_MODIFIED)
.setBody("Spring");
default -> new MockResponse().setResponseCode(404);
};
Expand Down

0 comments on commit 001ccca

Please sign in to comment.