Skip to content

Commit 246a0e6

Browse files
committed
Merge branch '3.1.x'
Closes gh-38878
2 parents 5354ad1 + 94df3c9 commit 246a0e6

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.io.File;
2020
import java.io.IOException;
21+
import java.net.URLDecoder;
22+
import java.net.URLEncoder;
2123
import java.nio.file.Files;
2224
import java.nio.file.StandardOpenOption;
2325
import java.util.List;
@@ -60,6 +62,7 @@
6062
* {@link Task} that checks for architecture problems.
6163
*
6264
* @author Andy Wilkinson
65+
* @author Yanming Zhou
6366
*/
6467
public abstract class ArchitectureCheck extends DefaultTask {
6568

@@ -71,7 +74,8 @@ public ArchitectureCheck() {
7174
allBeanPostProcessorBeanMethodsShouldBeStaticAndHaveParametersThatWillNotCausePrematureInitialization(),
7275
allBeanFactoryPostProcessorBeanMethodsShouldBeStaticAndHaveNoParameters(),
7376
noClassesShouldCallStepVerifierStepVerifyComplete(),
74-
noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList());
77+
noClassesShouldConfigureDefaultStepVerifierTimeout(), noClassesShouldCallCollectorsToList(),
78+
noClassesShouldCallURLEncoderWithStringEncoding(), noClassesShouldCallURLDecoderWithStringEncoding());
7579
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
7680
}
7781

@@ -190,6 +194,20 @@ private ArchRule noClassesShouldCallCollectorsToList() {
190194
.because("java.util.stream.Stream.toList() should be used instead");
191195
}
192196

197+
private ArchRule noClassesShouldCallURLEncoderWithStringEncoding() {
198+
return ArchRuleDefinition.noClasses()
199+
.should()
200+
.callMethod(URLEncoder.class, "encode", String.class, String.class)
201+
.because("java.net.URLEncoder.encode(String s, Charset charset) should be used instead");
202+
}
203+
204+
private ArchRule noClassesShouldCallURLDecoderWithStringEncoding() {
205+
return ArchRuleDefinition.noClasses()
206+
.should()
207+
.callMethod(URLDecoder.class, "decode", String.class, String.class)
208+
.because("java.net.URLDecoder.decode(String s, Charset charset) should be used instead");
209+
}
210+
193211
public void setClasses(FileCollection classes) {
194212
this.classes = classes;
195213
}

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.URL;
2424
import java.net.URLClassLoader;
2525
import java.net.URLDecoder;
26+
import java.nio.charset.StandardCharsets;
2627
import java.util.ArrayList;
2728
import java.util.Collection;
2829
import java.util.Collections;
@@ -159,7 +160,7 @@ private static List<URL> getUrlsFromManifestClassPathAttribute(URL jarUrl, JarFi
159160
urls.add(referenced);
160161
}
161162
else {
162-
referenced = new URL(jarUrl, URLDecoder.decode(entry, "UTF-8"));
163+
referenced = new URL(jarUrl, URLDecoder.decode(entry, StandardCharsets.UTF_8));
163164
if (new File(referenced.getFile()).exists()) {
164165
urls.add(referenced);
165166
}

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/web/SpringBootMockServletContextTests.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 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.
@@ -20,6 +20,7 @@
2020
import java.net.MalformedURLException;
2121
import java.net.URL;
2222
import java.net.URLDecoder;
23+
import java.nio.charset.StandardCharsets;
2324

2425
import jakarta.servlet.ServletContext;
2526
import org.junit.jupiter.api.Test;
@@ -80,7 +81,7 @@ protected String getResourceLocation(String path) {
8081
};
8182
URL resource = context.getResource("/");
8283
assertThat(resource).isNotNull();
83-
File file = new File(URLDecoder.decode(resource.getPath(), "UTF-8"));
84+
File file = new File(URLDecoder.decode(resource.getPath(), StandardCharsets.UTF_8));
8485
assertThat(file).exists().isDirectory();
8586
String[] contents = file.list((dir, name) -> !(".".equals(name) || "..".equals(name)));
8687
assertThat(contents).isNotNull();

spring-boot-project/spring-boot-tools/spring-boot-loader-classic/src/main/java/org/springframework/boot/loader/PropertiesLauncher.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import java.io.FileInputStream;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23-
import java.io.UnsupportedEncodingException;
2423
import java.lang.reflect.Constructor;
2524
import java.net.HttpURLConnection;
2625
import java.net.URL;
2726
import java.net.URLConnection;
2827
import java.net.URLDecoder;
28+
import java.nio.charset.StandardCharsets;
2929
import java.util.ArrayList;
3030
import java.util.Collections;
3131
import java.util.Iterator;
@@ -225,9 +225,9 @@ private InputStream getResource(String config) throws Exception {
225225
return getFileResource(config);
226226
}
227227

228-
private String handleUrl(String path) throws UnsupportedEncodingException {
228+
private String handleUrl(String path) {
229229
if (path.startsWith("jar:file:") || path.startsWith("file:")) {
230-
path = URLDecoder.decode(path, "UTF-8");
230+
path = URLDecoder.decode(path, StandardCharsets.UTF_8);
231231
if (path.startsWith("file:")) {
232232
path = path.substring("file:".length());
233233
if (path.startsWith("//")) {

spring-boot-project/spring-boot-tools/spring-boot-loader-classic/src/main/java/org/springframework/boot/loader/jar/JarURLConnection.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
import java.io.FileNotFoundException;
2121
import java.io.IOException;
2222
import java.io.InputStream;
23-
import java.io.UnsupportedEncodingException;
2423
import java.net.MalformedURLException;
2524
import java.net.URL;
2625
import java.net.URLConnection;
2726
import java.net.URLEncoder;
2827
import java.net.URLStreamHandler;
28+
import java.nio.charset.StandardCharsets;
2929
import java.security.Permission;
3030

3131
/**
@@ -318,13 +318,8 @@ private void write(String source, ByteArrayOutputStream outputStream) {
318318
for (int i = 0; i < length; i++) {
319319
int c = source.charAt(i);
320320
if (c > 127) {
321-
try {
322-
String encoded = URLEncoder.encode(String.valueOf((char) c), "UTF-8");
323-
write(encoded, outputStream);
324-
}
325-
catch (UnsupportedEncodingException ex) {
326-
throw new IllegalStateException(ex);
327-
}
321+
String encoded = URLEncoder.encode(String.valueOf((char) c), StandardCharsets.UTF_8);
322+
write(encoded, outputStream);
328323
}
329324
else {
330325
if (c == '%') {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/embedded/undertow/UndertowServletWebServerFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.IOException;
2121
import java.net.URL;
2222
import java.net.URLEncoder;
23+
import java.nio.charset.StandardCharsets;
2324
import java.time.Duration;
2425
import java.util.ArrayList;
2526
import java.util.Arrays;
@@ -553,7 +554,7 @@ public void removeResourceChangeListener(ResourceChangeListener listener) {
553554

554555
private URLResource getMetaInfResource(URL resourceJar, String path) {
555556
try {
556-
String urlPath = URLEncoder.encode(ENCODED_SLASH.matcher(path).replaceAll("/"), "UTF-8");
557+
String urlPath = URLEncoder.encode(ENCODED_SLASH.matcher(path).replaceAll("/"), StandardCharsets.UTF_8);
557558
URL resourceUrl = new URL(resourceJar + "META-INF/resources" + urlPath);
558559
URLResource resource = new URLResource(resourceUrl, path);
559560
if (resource.getContentLength() < 0) {

0 commit comments

Comments
 (0)