List of usage examples for io.vertx.core.file FileSystem readDir
Future<List<String>> readDir(String path, String filter);
From source file:com.klwork.spring.vertx.render.MyStaticHandlerImpl.java
License:Open Source License
private void sendDirectoryListing(String dir, RoutingContext context) { FileSystem fileSystem = new WindowsFileSystem((VertxInternal) context.vertx()); HttpServerRequest request = context.request(); fileSystem.readDir(dir, asyncResult -> { if (asyncResult.failed()) { context.fail(asyncResult.cause()); } else {//w w w .j ava 2 s. c o m String accept = request.headers().get("accept"); if (accept == null) { accept = "text/plain"; } if (accept.contains("html")) { String normalizedDir = context.normalisedPath(); if (!normalizedDir.endsWith("/")) { normalizedDir += "/"; } String file; StringBuilder files = new StringBuilder("<ul id=\"files\">"); List<String> list = asyncResult.result(); Collections.sort(list); for (String s : list) { file = s.substring(s.lastIndexOf(File.separatorChar) + 1); // skip dot files if (!includeHidden && file.charAt(0) == '.') { continue; } files.append("<li><a href=\""); files.append(normalizedDir); files.append(file); files.append("\" title=\""); files.append(file); files.append("\">"); files.append(file); files.append("</a></li>"); } files.append("</ul>"); // link to parent dir int slashPos = 0; for (int i = normalizedDir.length() - 2; i > 0; i--) { if (normalizedDir.charAt(i) == '/') { slashPos = i; break; } } String parent = "<a href=\"" + normalizedDir.substring(0, slashPos + 1) + "\">..</a>"; request.response().putHeader("content-type", "text/html"); request.response().end(directoryTemplate(context.vertx()).replace("{directory}", normalizedDir) .replace("{parent}", parent).replace("{files}", files.toString())); } else if (accept.contains("json")) { String file; JsonArray json = new JsonArray(); for (String s : asyncResult.result()) { file = s.substring(s.lastIndexOf(File.separatorChar) + 1); // skip dot files if (!includeHidden && file.charAt(0) == '.') { continue; } json.add(file); } request.response().putHeader("content-type", "application/json"); request.response().end(json.encode()); } else { String file; StringBuilder buffer = new StringBuilder(); for (String s : asyncResult.result()) { file = s.substring(s.lastIndexOf(File.separatorChar) + 1); // skip dot files if (!includeHidden && file.charAt(0) == '.') { continue; } buffer.append(file); buffer.append('\n'); } request.response().putHeader("content-type", "text/plain"); request.response().end(buffer.toString()); } } }); }
From source file:org.entcore.directory.pojo.ImportInfos.java
License:Open Source License
public void validate(final boolean isAdmc, final Vertx vertx, final Handler<AsyncResult<String>> handler) { if (!isAdmc && isEmpty(structureId)) { handler.handle(new DefaultAsyncResult<>("invalid.structure.id")); } else if (isEmpty(structureName)) { handler.handle(new DefaultAsyncResult<>("invalid.structure.name")); } else if (ImportType.CSV == type) { final FileSystem fs = vertx.fileSystem(); fs.readDir(path, new Handler<AsyncResult<List<String>>>() { @Override/*from ww w .j ava 2 s . c o m*/ public void handle(AsyncResult<List<String>> list) { if (list.succeeded()) { if (list.result().size() > 0) { moveFiles(list.result(), fs, handler); } else { handler.handle(new DefaultAsyncResult<>("missing.csv.files")); } } else { handler.handle(new DefaultAsyncResult<String>(list.cause())); } } }); } else { handler.handle(new DefaultAsyncResult<>("invalid.import.type")); } }