From fc78315d9ffca3efa94c56c4a542083bd5656e53 Mon Sep 17 00:00:00 2001 From: "A.Lepe" Date: Wed, 28 Jun 2023 15:52:10 +0900 Subject: [PATCH] Implemented issue #568 : Support multiple calls to staticFileLocation --- src/main/java/spark/Service.java | 13 +----- .../staticfiles/StaticFilesConfiguration.java | 44 ++++++++----------- .../spark/staticfiles/StaticFilesTest.java | 41 ++++++++++++++++- src/test/resources/private/secret.text | 1 + 4 files changed, 62 insertions(+), 37 deletions(-) create mode 100644 src/test/resources/private/secret.text diff --git a/src/main/java/spark/Service.java b/src/main/java/spark/Service.java index 7e2f2b579c..af0a1d3f7c 100644 --- a/src/main/java/spark/Service.java +++ b/src/main/java/spark/Service.java @@ -426,12 +426,7 @@ public synchronized Service staticFileLocation(String folder) { if (initialized && !isRunningFromServlet()) { throwBeforeRouteMappingException(); } - - if (!staticFilesConfiguration.isStaticResourcesSet()) { - staticFilesConfiguration.configure(folder); - } else { - LOG.warn("Static file location has already been set"); - } + staticFilesConfiguration.configure(folder); return this; } @@ -447,11 +442,7 @@ public synchronized Service externalStaticFileLocation(String externalFolder) { throwBeforeRouteMappingException(); } - if (!staticFilesConfiguration.isExternalStaticResourcesSet()) { - staticFilesConfiguration.configureExternal(externalFolder); - } else { - LOG.warn("External static file location has already been set"); - } + staticFilesConfiguration.configureExternal(externalFolder); return this; } diff --git a/src/main/java/spark/staticfiles/StaticFilesConfiguration.java b/src/main/java/spark/staticfiles/StaticFilesConfiguration.java index 73e8fb8866..f99d93eced 100644 --- a/src/main/java/spark/staticfiles/StaticFilesConfiguration.java +++ b/src/main/java/spark/staticfiles/StaticFilesConfiguration.java @@ -139,16 +139,13 @@ public boolean isExternalStaticResourcesSet() { public synchronized void configure(String folder) { Assert.notNull(folder, "'folder' must not be null"); - if (!staticResourcesSet) { - - if (staticResourceHandlers == null) { - staticResourceHandlers = new ArrayList<>(); - } - - staticResourceHandlers.add(new ClassPathResourceHandler(folder, "index.html")); - LOG.info("StaticResourceHandler configured with folder = " + folder); - staticResourcesSet = true; + if (staticResourceHandlers == null) { + staticResourceHandlers = new ArrayList<>(); } + + staticResourceHandlers.add(new ClassPathResourceHandler(folder, "index.html")); + LOG.info("StaticResourceHandler configured with folder = " + folder); + staticResourcesSet = true; } /** @@ -159,25 +156,22 @@ public synchronized void configure(String folder) { public synchronized void configureExternal(String folder) { Assert.notNull(folder, "'folder' must not be null"); - if (!externalStaticResourcesSet) { - try { - ExternalResource resource = new ExternalResource(folder); - if (!resource.getFile().isDirectory()) { - LOG.error("External Static resource location must be a folder"); - return; - } - - if (staticResourceHandlers == null) { - staticResourceHandlers = new ArrayList<>(); - } - staticResourceHandlers.add(new ExternalResourceHandler(folder, "index.html")); - LOG.info("External StaticResourceHandler configured with folder = " + folder); - } catch (IOException e) { - LOG.error("Error when creating external StaticResourceHandler", e); + try { + ExternalResource resource = new ExternalResource(folder); + if (!resource.getFile().isDirectory()) { + LOG.error("External Static resource location must be a folder"); + return; } - externalStaticResourcesSet = true; + if (staticResourceHandlers == null) { + staticResourceHandlers = new ArrayList<>(); + } + staticResourceHandlers.add(new ExternalResourceHandler(folder, "index.html")); + LOG.info("External StaticResourceHandler configured with folder = " + folder); + } catch (IOException e) { + LOG.error("Error when creating external StaticResourceHandler", e); } + externalStaticResourcesSet = true; } public static StaticFilesConfiguration create() { diff --git a/src/test/java/spark/staticfiles/StaticFilesTest.java b/src/test/java/spark/staticfiles/StaticFilesTest.java index d338d2f348..466c4d47dc 100644 --- a/src/test/java/spark/staticfiles/StaticFilesTest.java +++ b/src/test/java/spark/staticfiles/StaticFilesTest.java @@ -20,6 +20,7 @@ import java.io.FileWriter; import java.io.IOException; import java.net.URLEncoder; +import java.nio.file.Files; import org.junit.AfterClass; import org.junit.Assert; @@ -31,6 +32,7 @@ import spark.Spark; import spark.examples.exception.NotFoundException; import spark.util.SparkTestUtil; +import spark.utils.IOUtils; import static spark.Spark.exception; import static spark.Spark.get; @@ -53,14 +55,25 @@ public class StaticFilesTest { private static SparkTestUtil testUtil; private static File tmpExternalFile; + private static File tmpExternalDir2; @AfterClass public static void tearDown() { Spark.stop(); if (tmpExternalFile != null) { - LOGGER.debug("tearDown().deleting: " + tmpExternalFile); + LOGGER.debug("tearDown().deleting: " + tmpExternalFile.getAbsolutePath()); tmpExternalFile.delete(); } + if(tmpExternalDir2 != null) { + LOGGER.debug("tearDown().deleting directory: " + tmpExternalDir2.getAbsolutePath()); + File[] files = tmpExternalDir2.listFiles(); + if (files != null) { + for (File f : files) { + f.delete(); + } + } + tmpExternalDir2.delete(); + } } @BeforeClass @@ -74,8 +87,17 @@ public static void setup() throws IOException { writer.flush(); writer.close(); + // Second external location + tmpExternalDir2 = Files.createTempDirectory("spark").toFile(); + writer = new FileWriter(new File(tmpExternalDir2, "test.txt").getAbsoluteFile()); + writer.write(CONTENT_OF_EXTERNAL_FILE); + writer.flush(); + writer.close(); + staticFiles.location("/public"); + staticFiles.location("/private"); staticFiles.externalLocation(System.getProperty("java.io.tmpdir")); + staticFiles.externalLocation(tmpExternalDir2.getAbsolutePath()); get("/hello", (q, a) -> FO_SHIZZY); @@ -137,6 +159,15 @@ public void testStaticFilePageHtml() throws Exception { testGet(); } + @Test //This test will check if multiple static directories can be set + public void testStaticFileGetPrivate() throws Exception { + SparkTestUtil.UrlResponse response = doGet("/secret.text"); + Assert.assertEquals(200, response.status); + Assert.assertTrue(response.body.contains("7AKNTJqvKhLAoTybTdFid9RyX9oVTszEfgVvtALvyimaNrpf")); + + testGet(); + } + @Test public void testDirectoryTraversalProtectionLocal() throws Exception { String path = "/" + URLEncoder.encode("..\\spark\\", "UTF-8") + "Spark.class"; @@ -156,6 +187,14 @@ public void testExternalStaticFile() throws Exception { testGet(); } + @Test + public void testExternalStaticFileAlt() throws Exception { + SparkTestUtil.UrlResponse response = doGet("/test.txt"); + Assert.assertEquals(200, response.status); + Assert.assertEquals(CONTENT_OF_EXTERNAL_FILE, response.body); + + testGet(); + } /** * Used to verify that "normal" functionality works after static files mapping */ diff --git a/src/test/resources/private/secret.text b/src/test/resources/private/secret.text new file mode 100644 index 0000000000..e19d38f6bd --- /dev/null +++ b/src/test/resources/private/secret.text @@ -0,0 +1 @@ +7AKNTJqvKhLAoTybTdFid9RyX9oVTszEfgVvtALvyimaNrpf