/*
|
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 <https://www.gnu.org/licenses/>.
|
*/
|
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;
|
|
/**
|
* Erzeugung einer Liste von Dateien und Ordnern
|
*
|
* @author Ulrich Hilger
|
* @version 0.1, 05.11.2024
|
*/
|
public class FileCatalog extends AbstractFileActor {
|
|
/**
|
* <p>Diese Methode ist als Reaktion auf einen HTTP GET Aufruf an
|
* folgenden URL gedacht und liefert eine Liste des Ordnerinhalts
|
* im JSON-Format in der HTTP-Antwort aus</p>
|
*
|
* <pre>
|
* Ordnerinhalt auflisten:
|
* http://localhost:[port]/[kontext]/pfad/zum/ordner/
|
* </pre>
|
*
|
* Die Methode liefert den Dateiinhalt in der HTTP-Antwort aus,
|
* wenn der URL eine Datei bezeichnet
|
*
|
* @param exchange das Objekt mit Infos zu HTTP-Request, -Response usw.
|
*/
|
public void list(HttpExchange exchange) {
|
try {
|
init(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();
|
}
|
}
|
}
|