/* neon-fm - Dateiverwaltung fuer neon Copyright (C) 2024 Ulrich Hilger This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.uhilger.neon.fm; import com.sun.net.httpserver.HttpContext; import com.sun.net.httpserver.HttpExchange; import de.uhilger.neon.FileServer; import de.uhilger.neon.HttpHelper; import de.uhilger.neon.HttpResponder; import de.uhilger.fm.Catalog; import java.io.IOException; /** * * @author Ulrich Hilger * @version 0.1, 05.11.2024 */ public class FileList extends AbstractFileActor { public void run(HttpExchange exchange) { try { super.run(exchange); HttpContext ctx = exchange.getHttpContext(); String base = ctx.getAttributes().getOrDefault(FileServer.ATTR_FILE_BASE, "").toString(); if(base.length() == 0) { new HttpResponder().sendNotFound(exchange, exchange.getRequestURI().toString()); // not found } else { String fName = new HttpHelper().getFileName(exchange); if (fName.endsWith(FileServer.STR_SLASH)) { // Ordnerliste erzeugen String json = new Catalog().list(fName, ctx.getPath(), base); if (null != json) { new HttpResponder().antwortSenden(exchange, HttpResponder.SC_OK, json); } else { new HttpResponder().antwortSenden(exchange, HttpResponder.SC_OK, "{}"); // leere Liste } } else { new FileServer().serveFile(exchange); // Datei ausliefern } } } catch (IOException | IllegalArgumentException ex) { fehlerAntwort(exchange, ex); } finally { free(); } } }