protected void handleDirectoryRequest(RouteContext routeContext, File dir) throws MalformedURLException { File index = getIndexFile(dir); if (index != null) { routeContext.getResponse().contentLength(index.length()); URL url = index.toURI().toURL(); streamResource(url, routeContext); return; } sendDirectoryListing(routeContext, dir); }
public DirectoryHandler(String urlPath, File directory) { this.urlPath = urlPath; String normalizedPath = getNormalizedPath(urlPath); if (normalizedPath.length() > 0) { this.uriPattern = String.format("/%s/?{%s: .*}", getNormalizedPath(urlPath), PATH_PARAMETER); } else { this.uriPattern = String.format("/{%s: .*}", PATH_PARAMETER); } this.directory = directory.getAbsoluteFile(); comparator = new DirectoryBeforeComparator(); }
@Override public final void handle(RouteContext routeContext) { String resourcePath = getResourcePath(routeContext); log.trace("Request resource '{}'", resourcePath); handle(routeContext, resourcePath); routeContext.next(); }
log.warn("Request for '{}' which is not located in '{}'", requestedPath, directory); } else if (StringUtils.isNullOrEmpty(resourcePath) || "/".equals(resourcePath)) { handleDirectoryRequest(routeContext, directory); } else { switch (routeContext.getRequestMethod()) { case HttpConstants.Method.HEAD: setResponseHeaders(url, routeContext); routeContext.getResponse().commit(); break; case HttpConstants.Method.GET: streamResource(url, routeContext); break; default: handleDirectoryRequest(routeContext, file);
private void sendDirectoryListing(RouteContext routeContext, File dir) { String absoluteDirUri = getUrlPath() + StringUtils.addStart(directory.toPath().relativize(dir.toPath()).toString(), "/"); if (StringUtils.isNullOrEmpty(directoryTemplate)) { // Generate primitive, default directory listing String page = generateDefaultDirectoryListing(routeContext, dir, absoluteDirUri); routeContext.html().send(page); } else { // Render directory listing template int numFiles = 0; int numDirs = 0; long diskUsage = 0; List<DirEntry> dirEntries = getDirEntries(routeContext, dir, absoluteDirUri); for (DirEntry dirEntry : dirEntries) { if (dirEntry.isFile()) { numFiles++; diskUsage += dirEntry.getSize(); } else if (dirEntry.isDirectory() && !dirEntry.getName().contains("..")) { numDirs++; } } routeContext.setLocal("dirUrl", absoluteDirUri); routeContext.setLocal("dirPath", absoluteDirUri.substring(getUrlPath().length())); routeContext.setLocal("dirEntries", dirEntries); routeContext.setLocal("numDirs", numDirs); routeContext.setLocal("numFiles", numFiles); routeContext.setLocal("diskUsage", diskUsage); routeContext.render(directoryTemplate); } }
protected String generateDefaultDirectoryListing(RouteContext routeContext, File dir, String absoluteDirUri) { StringBuilder sb = new StringBuilder(); sb.append("<html><body><table>"); SimpleDateFormat df = new SimpleDateFormat(timestampPattern); NumberFormat nf = new DecimalFormat(fileSizePattern); for (DirEntry dirEntry : getDirEntries(routeContext, dir, absoluteDirUri)) { sb.append(StringUtils.format("<tr><td><a href=\"{}\">{}</a></td><td>{}</td><td>{}</td></tr>\n", dirEntry.getUrl(), dirEntry.getName(), dirEntry.isFile() ? nf.format(dirEntry.getSize()) : "", df.format(dirEntry.getLastModified()))); } sb.append("</table></body></html>"); return sb.toString(); }
protected List<DirEntry> getDirEntries(RouteContext routeContext, File dir, String absoluteDirUri) { List<DirEntry> list = new ArrayList<>(); for (File file : getFiles(dir)) { String fileUrl = routeContext.getRequest().getApplicationPath() + StringUtils.removeEnd(StringUtils.addStart(absoluteDirUri, "/"), "/") + StringUtils.addStart(file.getName(), "/"); list.add(new DirEntry(fileUrl, file)); } if (comparator != null) { list.sort(comparator); } if (!directory.equals(dir)) { File upDir = new File(dir, "../"); list.add(0, new DirEntry(routeContext.getRequest().getApplicationPath() + StringUtils.removeEnd(StringUtils.addStart(absoluteDirUri, "/"), "/") + StringUtils.addStart(upDir.getName(), "/"), upDir)); } return list; }
protected String getResourcePath(RouteContext routeContext) { return getNormalizedPath(routeContext.getParameter(PATH_PARAMETER).toString()); }