forked from j62/ctbrec
1
0
Fork 0

Fix NoSuchFileException caused by URL encoding

The Jar URL has to get URL-decoded before it can be used as a path
This commit is contained in:
0xboobface 2020-01-03 19:20:34 +01:00
parent 4d6e74562c
commit b2138ca99b
1 changed files with 7 additions and 5 deletions

View File

@ -6,7 +6,10 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
@ -50,10 +53,10 @@ public abstract class AbstractDocServlet extends HttpServlet {
List<String> pages = new ArrayList<>();
URL resource = getClass().getResource("/html/docs");
if(Objects.equals(resource.getProtocol(), "file")) {
LOG.debug("FILE {}", resource.toString());
LOG.debug("FILE {}", resource);
indexDirectory(resource, pages);
} else if(Objects.equals(resource.getProtocol(), "jar")) {
LOG.debug("JAR {}", resource.toString());
LOG.debug("JAR {}", resource);
indexJar(resource, pages);
}
pages.add("index.md");
@ -63,6 +66,7 @@ public abstract class AbstractDocServlet extends HttpServlet {
private void indexJar(URL resource, List<String> pages) throws IOException {
String fileUrl = resource.getFile();
fileUrl = URLDecoder.decode(fileUrl, StandardCharsets.UTF_8);
int colon = fileUrl.indexOf(':');
int exclamation = fileUrl.indexOf('!');
String jar = fileUrl.substring(colon + 1, exclamation);
@ -82,9 +86,7 @@ public abstract class AbstractDocServlet extends HttpServlet {
private void indexDirectory(URL resource, List<String> pages) {
File docs = new File(resource.getFile());
String[] files = docs.list((dir, name) -> name.toLowerCase().endsWith(".md"));
for (String file : files) {
pages.add(file);
}
pages.addAll(Arrays.asList(files));
}
String loadMarkdown(String path) throws IOException {