Skip to content

Commit 384cfd2

Browse files
committed
Preserve ref and query when creating URL in loader's handler
Closes gh-14011
1 parent 3a2c962 commit 384cfd2

File tree

2 files changed

+26
-2
lines changed
  • spring-boot-tools/spring-boot-loader/src

2 files changed

+26
-2
lines changed

spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,15 @@ private String trimToJarRoot(String file) {
211211
}
212212

213213
private void setFile(URL context, String file) {
214-
setURL(context, JAR_PROTOCOL, null, -1, null, null, normalize(file), null, null);
214+
String path = normalize(file);
215+
String query = null;
216+
int queryIndex = path.lastIndexOf('?');
217+
if (queryIndex != -1) {
218+
query = path.substring(queryIndex + 1);
219+
path = path.substring(0, queryIndex);
220+
}
221+
setURL(context, JAR_PROTOCOL, null, -1, null, null, path, query,
222+
context.getRef());
215223
}
216224

217225
private String normalize(String file) {

spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 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.
@@ -158,12 +158,28 @@ public void urlWithSpecReferencingCurrentDirectory() throws MalformedURLExceptio
158158
"./folderB/./b.xsd");
159159
}
160160

161+
@Test
162+
public void urlWithRef() throws MalformedURLException {
163+
assertStandardAndCustomHandlerUrlsAreEqual("file:/test.jar!/BOOT-INF/classes",
164+
"!/foo.txt#alpha");
165+
}
166+
167+
@Test
168+
public void urlWithQuery() throws MalformedURLException {
169+
assertStandardAndCustomHandlerUrlsAreEqual("file:/test.jar!/BOOT-INF/classes",
170+
"!/foo.txt?alpha");
171+
}
172+
161173
private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec)
162174
throws MalformedURLException {
163175
URL standardUrl = new URL(new URL("jar:" + context), spec);
164176
URL customHandlerUrl = new URL(new URL("jar", null, -1, context, this.handler),
165177
spec);
166178
assertThat(customHandlerUrl.toString()).isEqualTo(standardUrl.toString());
179+
assertThat(customHandlerUrl.getFile()).isEqualTo(standardUrl.getFile());
180+
assertThat(customHandlerUrl.getPath()).isEqualTo(standardUrl.getPath());
181+
assertThat(customHandlerUrl.getQuery()).isEqualTo(standardUrl.getQuery());
182+
assertThat(customHandlerUrl.getRef()).isEqualTo(standardUrl.getRef());
167183
}
168184

169185
private URL createUrl(String file) throws MalformedURLException {

0 commit comments

Comments
 (0)