You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2020/03/28 19:59:54 UTC
[logging-log4j2] 01/02: LOG4J2-2761: For absolute URIs don't fail
on wrongly formatted file URIs
This is an automated email from the ASF dual-hosted git repository.
rgoers pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 0c0fa898198f531adfacda5f88fbc2b2cba31639
Author: Uwe Schindler <uw...@thetaphi.de>
AuthorDate: Thu Mar 26 09:54:17 2020 +0100
LOG4J2-2761: For absolute URIs don't fail on wrongly formatted file URIs
---
.../apache/logging/log4j/core/util/FileUtils.java | 46 ++++++++++------------
1 file changed, 20 insertions(+), 26 deletions(-)
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java
index 6fd73c2..27fa8ef 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/FileUtils.java
@@ -18,12 +18,9 @@ package org.apache.logging.log4j.core.util;
import java.io.File;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
-import java.net.MalformedURLException;
import java.net.URI;
+import java.net.URISyntaxException;
import java.net.URL;
-import java.net.URLDecoder;
-import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -60,40 +57,37 @@ public final class FileUtils {
* @return the resulting file object
*/
public static File fileFromUri(URI uri) {
- // There MUST be a better way to do this. TODO Search other ASL projects...
- if (uri == null || (uri.getScheme() != null
- && (!PROTOCOL_FILE.equals(uri.getScheme()) && !JBOSS_FILE.equals(uri.getScheme())))) {
+ if (uri == null) {
return null;
}
- if (uri.getScheme() == null) {
- File file = new File(uri.toString());
- if (file.exists()) {
- return file;
+ if (uri.isAbsolute()) {
+ if (JBOSS_FILE.equals(uri.getScheme())) try {
+ // patch the scheme
+ uri = new URI(PROTOCOL_FILE, uri.getSchemeSpecificPart(), uri.getFragment());
+ } catch (URISyntaxException use) {
+ // should not happen, ignore
+ }
+ try {
+ if (PROTOCOL_FILE.equals(uri.getScheme())) {
+ return new File(uri);
+ }
+ } catch (final Exception ex) {
+ LOGGER.warn("Invalid URI {}", uri);
}
+ } else {
+ File file = new File(uri.toString());
try {
+ if (file.exists()) {
+ return file;
+ }
final String path = uri.getPath();
file = new File(path);
if (file.exists()) {
return file;
}
- uri = new File(path).toURI();
} catch (final Exception ex) {
LOGGER.warn("Invalid URI {}", uri);
- return null;
- }
- }
- final String charsetName = StandardCharsets.UTF_8.name();
- try {
- String fileName = uri.toURL().getFile();
- if (new File(fileName).exists()) { // LOG4J2-466
- return new File(fileName); // allow files with '+' char in name
}
- fileName = URLDecoder.decode(fileName, charsetName);
- return new File(fileName);
- } catch (final MalformedURLException ex) {
- LOGGER.warn("Invalid URL {}", uri, ex);
- } catch (final UnsupportedEncodingException uee) {
- LOGGER.warn("Invalid encoding: {}", charsetName, uee);
}
return null;
}