From 13b8b922734e22c8f1d7aa59f7b918ce4754d4ba Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Mon, 10 Sep 2018 13:44:42 +0200 Subject: [PATCH] fixed web resource path loading on windows --- .../sonia/scm/plugin/WebResourceLoader.java | 4 +- .../scm/plugin/PathWebResourceLoader.java | 85 +++---------------- 2 files changed, 14 insertions(+), 75 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/plugin/WebResourceLoader.java b/scm-core/src/main/java/sonia/scm/plugin/WebResourceLoader.java index 94b31ac844..61466c0e2c 100644 --- a/scm-core/src/main/java/sonia/scm/plugin/WebResourceLoader.java +++ b/scm-core/src/main/java/sonia/scm/plugin/WebResourceLoader.java @@ -53,9 +53,11 @@ public interface WebResourceLoader * Returns a {@link URL} for the given path. The method will return null if no * resources could be found for the given path. * + * Note: The path is a web path and uses "/" as path separator + * * @param path resource path * * @return url object for the given path or null */ - public URL getResource(String path); + URL getResource(String path); } diff --git a/scm-webapp/src/main/java/sonia/scm/plugin/PathWebResourceLoader.java b/scm-webapp/src/main/java/sonia/scm/plugin/PathWebResourceLoader.java index 230cfa6c7a..4b9e502c4d 100644 --- a/scm-webapp/src/main/java/sonia/scm/plugin/PathWebResourceLoader.java +++ b/scm-webapp/src/main/java/sonia/scm/plugin/PathWebResourceLoader.java @@ -31,18 +31,11 @@ package sonia.scm.plugin; -//~--- non-JDK imports -------------------------------------------------------- - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -//~--- JDK imports ------------------------------------------------------------ - -import java.io.File; - import java.net.MalformedURLException; import java.net.URL; - import java.nio.file.Files; import java.nio.file.Path; @@ -55,47 +48,27 @@ import java.nio.file.Path; public class PathWebResourceLoader implements WebResourceLoader { - /** Field description */ - private static final String DEFAULT_SEPARATOR = "/"; + private static final String SEPARATOR = "/"; /** * the logger for PathWebResourceLoader */ - private static final Logger logger = + private static final Logger LOG = LoggerFactory.getLogger(PathWebResourceLoader.class); - //~--- constructors --------------------------------------------------------- - - /** - * Constructs ... - * - * - * @param directory - */ public PathWebResourceLoader(Path directory) { this.directory = directory; } - //~--- get methods ---------------------------------------------------------- - - /** - * Method description - * - * - * @param path - * - * @return - */ @Override - public URL getResource(String path) - { + public URL getResource(String path) { URL resource = null; Path file = directory.resolve(filePath(path)); if (Files.exists(file) && ! Files.isDirectory(file)) { - logger.trace("found path {} at {}", path, file); + LOG.trace("found path {} at {}", path, file); try { @@ -103,56 +76,20 @@ public class PathWebResourceLoader implements WebResourceLoader } catch (MalformedURLException ex) { - logger.error("could not transform path to url", ex); + LOG.error("could not transform path to url", ex); } + } else { + LOG.trace("could not find file {}", file); } return resource; } - //~--- methods -------------------------------------------------------------- - - /** - * Method description - * - * - * @param path - * - * @return - */ - private String filePath(String path) - { - - // TODO handle illegal path parts, such as .. - String filePath = filePath(DEFAULT_SEPARATOR, path); - - if (!DEFAULT_SEPARATOR.equals(File.separator)) - { - filePath = filePath(File.separator, path); + private String filePath(String path) { + if (path.startsWith(SEPARATOR)) { + return path.substring(1); } - - return filePath; - } - - /** - * Method description - * - * - * @param separator - * @param path - * - * @return - */ - private String filePath(String separator, String path) - { - String filePath = path; - - if (filePath.startsWith(separator)) - { - filePath = filePath.substring(separator.length()); - } - - return filePath; + return path; } //~--- fields ---------------------------------------------------------------