This is an automated email from the ASF dual-hosted git repository. vy pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push: new 6debe00 #335 Fix reading of Windows paths in Uris. 6debe00 is described below commit 6debe0038255db12c5438cea54075c845b7d5ae1 Author: Volkan Yazıcı <volkan.yaz...@gmail.com> AuthorDate: Tue Jun 9 11:30:11 2020 +0200 #335 Fix reading of Windows paths in Uris. --- .../log4j/layout/json/template/util/Uris.java | 34 +++++++++++++++------- .../log4j/layout/json/template/util/UrisTest.java | 3 +- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/log4j-layout-json-template/src/main/java/org/apache/logging/log4j/layout/json/template/util/Uris.java b/log4j-layout-json-template/src/main/java/org/apache/logging/log4j/layout/json/template/util/Uris.java index 4c03843..65cd863 100644 --- a/log4j-layout-json-template/src/main/java/org/apache/logging/log4j/layout/json/template/util/Uris.java +++ b/log4j-layout-json-template/src/main/java/org/apache/logging/log4j/layout/json/template/util/Uris.java @@ -39,7 +39,7 @@ public enum Uris {; private static final Logger LOGGER = StatusLogger.getLogger(); /** - * Reads {@link URI}s of scheme <tt>classpath</tt> and <tt>file</tt>. + * Reads {@link URI} specs of scheme <tt>classpath</tt> and <tt>file</tt>. * * @param spec the {@link URI} spec, e.g., <tt>file:/holy/cow.txt</tt> or * <tt>classpath:/holy/cat.txt</tt> @@ -49,20 +49,34 @@ public enum Uris {; Objects.requireNonNull(spec, "spec"); Objects.requireNonNull(charset, "charset"); try { - return unsafeReadUri(spec, charset); + final URI uri = new URI(spec); + return unsafeReadUri(uri, charset); } catch (final Exception error) { - final String message = String.format( - "failed reading URI (spec=%s, charset=%s)", - spec, charset); - throw new RuntimeException(message, error); + throw new RuntimeException("failed reading URI: " + spec, error); + } + } + + /** + * Reads {@link URI}s of scheme <tt>classpath</tt> and <tt>file</tt>. + * + * @param uri the {@link URI}, e.g., <tt>file:/holy/cow.txt</tt> or + * <tt>classpath:/holy/cat.txt</tt> + * @param charset used {@link Charset} for decoding the file + */ + public static String readUri(final URI uri, final Charset charset) { + Objects.requireNonNull(uri, "uri"); + Objects.requireNonNull(charset, "charset"); + try { + return unsafeReadUri(uri, charset); + } catch (final Exception error) { + throw new RuntimeException("failed reading URI: " + uri, error); } } private static String unsafeReadUri( - final String spec, + final URI uri, final Charset charset) throws Exception { - final URI uri = new URI(spec); final String uriScheme = uri.getScheme().toLowerCase(); switch (uriScheme) { case "classpath": @@ -70,11 +84,9 @@ public enum Uris {; case "file": return readFileUri(uri, charset); default: { - final String message = String.format("unknown URI scheme (spec=%s)", spec); - throw new IllegalArgumentException(message); + throw new IllegalArgumentException("unknown scheme in URI: " + uri); } } - } private static String readFileUri( diff --git a/log4j-layout-json-template/src/test/java/org/apache/logging/log4j/layout/json/template/util/UrisTest.java b/log4j-layout-json-template/src/test/java/org/apache/logging/log4j/layout/json/template/util/UrisTest.java index a70bbeb..05e679b 100644 --- a/log4j-layout-json-template/src/test/java/org/apache/logging/log4j/layout/json/template/util/UrisTest.java +++ b/log4j-layout-json-template/src/test/java/org/apache/logging/log4j/layout/json/template/util/UrisTest.java @@ -25,6 +25,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -50,7 +51,7 @@ public class UrisTest { try (final OutputStream outputStream = new FileOutputStream(file)) { outputStream.write(nonAsciiUtfText.getBytes(StandardCharsets.UTF_8)); } - final String uri = String.format("file:%s", file.getAbsoluteFile()); + final URI uri = file.toURI(); final String content = Uris.readUri(uri, StandardCharsets.UTF_8); Assert.assertEquals(nonAsciiUtfText, content); } finally {