From 384cfd2ad86b19b6c8897fa1a754b2cce28c0f2c Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 13 Aug 2018 11:30:15 +0100 Subject: [PATCH] Preserve ref and query when creating URL in loader's handler Closes gh-14011 --- .../boot/loader/jar/Handler.java | 10 +++++++++- .../boot/loader/jar/HandlerTests.java | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java index 2137b371fc..77e74fca09 100644 --- a/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java +++ b/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/Handler.java @@ -211,7 +211,15 @@ public class Handler extends URLStreamHandler { } private void setFile(URL context, String file) { - setURL(context, JAR_PROTOCOL, null, -1, null, null, normalize(file), null, null); + String path = normalize(file); + String query = null; + int queryIndex = path.lastIndexOf('?'); + if (queryIndex != -1) { + query = path.substring(queryIndex + 1); + path = path.substring(0, queryIndex); + } + setURL(context, JAR_PROTOCOL, null, -1, null, null, path, query, + context.getRef()); } private String normalize(String file) { diff --git a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java index 4744e94199..e31edfc139 100644 --- a/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java +++ b/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/HandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2018 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. @@ -158,12 +158,28 @@ public class HandlerTests { "./folderB/./b.xsd"); } + @Test + public void urlWithRef() throws MalformedURLException { + assertStandardAndCustomHandlerUrlsAreEqual("file:/test.jar!/BOOT-INF/classes", + "!/foo.txt#alpha"); + } + + @Test + public void urlWithQuery() throws MalformedURLException { + assertStandardAndCustomHandlerUrlsAreEqual("file:/test.jar!/BOOT-INF/classes", + "!/foo.txt?alpha"); + } + private void assertStandardAndCustomHandlerUrlsAreEqual(String context, String spec) throws MalformedURLException { URL standardUrl = new URL(new URL("jar:" + context), spec); URL customHandlerUrl = new URL(new URL("jar", null, -1, context, this.handler), spec); assertThat(customHandlerUrl.toString()).isEqualTo(standardUrl.toString()); + assertThat(customHandlerUrl.getFile()).isEqualTo(standardUrl.getFile()); + assertThat(customHandlerUrl.getPath()).isEqualTo(standardUrl.getPath()); + assertThat(customHandlerUrl.getQuery()).isEqualTo(standardUrl.getQuery()); + assertThat(customHandlerUrl.getRef()).isEqualTo(standardUrl.getRef()); } private URL createUrl(String file) throws MalformedURLException {