Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit 00ccc23

Browse files
committed
#89 Handling spaces in asset directory paths
1 parent 2bf7f7d commit 00ccc23

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/main/java/com/marklogic/client/ext/modulesloader/impl/BaseModulesFinder.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.io.File;
1414
import java.io.FilenameFilter;
1515
import java.io.IOException;
16-
import java.nio.file.Paths;
1716
import java.util.ArrayList;
1817
import java.util.Arrays;
1918
import java.util.List;
@@ -62,7 +61,13 @@ protected void addAssetDirectories(Modules modules, String baseDir) {
6261
// classpath needs the trailing / to find child dirs
6362
findResources("asset module directories", baseDir, "*", "*/").stream().forEach(resource -> {
6463
try {
65-
File f = new File(resource.getURL().getFile());
64+
String resourceFile = resource.getURL().getFile();
65+
if (logger.isDebugEnabled()) {
66+
logger.debug("Checking resource to see if it's a valid asset directory: " + resourceFile);
67+
}
68+
69+
resourceFile = decodeAssetDirectoryResource(resourceFile);
70+
File f = new File(resourceFile);
6671
String uri = resource.getURI().toString();
6772
boolean isRecognized = recognizedPaths.contains(f.getName());
6873
// when the modules are in a jar inside a war
@@ -81,6 +86,29 @@ protected void addAssetDirectories(Modules modules, String baseDir) {
8186
modules.setAssetDirectories(dirs);
8287
}
8388

89+
/**
90+
* There may be other characters that need decoding, but for now, only %20 is being converted back into a space.
91+
*
92+
* The reason %20 exists is because a Resource that represents a potential asset directory is accessed as a URL in
93+
* order to support jar and war files. Accessing the directory as a URL results in spaces being converted to %20.
94+
* In order to construct a File, these must be converted back into spaces.
95+
*
96+
* It may be that performing a full URL decoding on the resourceFile is the correct solution, just don't have enough
97+
* test cases to know that this is safe for sure.
98+
*
99+
* @param resourceFile
100+
* @return
101+
*/
102+
protected String decodeAssetDirectoryResource(String resourceFile) {
103+
if (resourceFile.contains("%20")) {
104+
resourceFile = resourceFile.replaceAll("%20", " ");
105+
if (logger.isDebugEnabled()) {
106+
logger.debug("Replaced occurrences of %20 with a space in potential asset directory: " + resourceFile);
107+
}
108+
}
109+
return resourceFile;
110+
}
111+
84112
protected List<String> getRecognizedPaths() {
85113
return Arrays.asList(optionsPath, servicesPath, transformsPath, namespacesPath, schemasPath);
86114
}

src/test/java/com/marklogic/client/ext/modulesloader/impl/LoadModulesTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.marklogic.client.DatabaseClient;
44
import com.marklogic.client.ext.AbstractIntegrationTest;
5+
import com.marklogic.client.ext.modulesloader.Modules;
6+
import com.marklogic.client.ext.modulesloader.ModulesFinder;
57
import com.marklogic.client.ext.tokenreplacer.DefaultTokenReplacer;
68
import com.marklogic.client.io.BytesHandle;
79
import com.marklogic.client.io.StringHandle;
@@ -10,6 +12,7 @@
1012
import org.springframework.core.io.Resource;
1113

1214
import java.nio.file.Paths;
15+
import java.util.List;
1316
import java.util.Properties;
1417
import java.util.Set;
1518
import java.util.regex.Pattern;
@@ -39,6 +42,20 @@ public void setup() {
3942
modulesLoader.setModulesManager(null);
4043
}
4144

45+
@Test
46+
public void pathWithSpaces() {
47+
String dir = Paths.get("src", "test", "resources", "path with spaces").toString();
48+
ModulesFinder finder = new DefaultModulesFinder();
49+
Modules modules = finder.findModules(dir);
50+
List<Resource> assetDirectories = modules.getAssetDirectories();
51+
assertEquals("Expecting one directory, the 'root' directory", 1, assetDirectories.size());
52+
53+
// Now load the modules for real
54+
modulesLoader.loadModules(dir, finder, client);
55+
String moduleXml = modulesClient.newXMLDocumentManager().read("/example/example.xqy", new StringHandle()).get();
56+
assertEquals("<example/>", moduleXml.trim());
57+
}
58+
4259
/**
4360
* This test is a little brittle because it assumes the URI of options/services/transforms that are loaded
4461
* into the Modules database.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<example/>

0 commit comments

Comments
 (0)