Skip to content

Commit

Permalink
Implemented issue perwendel#568 : Support multiple calls to staticFil…
Browse files Browse the repository at this point in the history
…eLocation
  • Loading branch information
A.Lepe committed Jun 28, 2023
1 parent 8c626c1 commit fc78315
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 37 deletions.
13 changes: 2 additions & 11 deletions src/main/java/spark/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down
44 changes: 19 additions & 25 deletions src/main/java/spark/staticfiles/StaticFilesConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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() {
Expand Down
41 changes: 40 additions & 1 deletion src/test/java/spark/staticfiles/StaticFilesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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);

Expand Down Expand Up @@ -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";
Expand All @@ -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
*/
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/private/secret.text
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
7AKNTJqvKhLAoTybTdFid9RyX9oVTszEfgVvtALvyimaNrpf

0 comments on commit fc78315

Please sign in to comment.