commit | author | age
|
031c6b
|
1 |
/* |
U |
2 |
neon-fm - Dateiverwaltung fuer neon |
|
3 |
Copyright (C) 2024 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.neon.fm; |
|
19 |
|
|
20 |
import com.sun.net.httpserver.HttpContext; |
|
21 |
import com.sun.net.httpserver.HttpExchange; |
|
22 |
import de.uhilger.neon.FileServer; |
|
23 |
import de.uhilger.neon.HttpHelper; |
|
24 |
import de.uhilger.neon.HttpResponder; |
|
25 |
import de.uhilger.fm.Catalog; |
|
26 |
import java.io.IOException; |
|
27 |
|
|
28 |
/** |
bb2648
|
29 |
* Erzeugung einer Liste von Dateien und Ordnern |
031c6b
|
30 |
* |
U |
31 |
* @author Ulrich Hilger |
|
32 |
* @version 0.1, 05.11.2024 |
|
33 |
*/ |
|
34 |
public class FileCatalog extends AbstractFileActor { |
|
35 |
|
|
36 |
/** |
|
37 |
* <p>Diese Methode ist als Reaktion auf einen HTTP GET Aufruf an |
|
38 |
* folgenden URL gedacht und liefert eine Liste des Ordnerinhalts |
a9ce27
|
39 |
* im JSON-Format in der HTTP-Antwort aus</p> |
031c6b
|
40 |
* |
U |
41 |
* <pre> |
|
42 |
* Ordnerinhalt auflisten: |
|
43 |
* http://localhost:[port]/[kontext]/pfad/zum/ordner/ |
|
44 |
* </pre> |
|
45 |
* |
a9ce27
|
46 |
* Die Methode liefert den Dateiinhalt in der HTTP-Antwort aus, |
U |
47 |
* wenn der URL eine Datei bezeichnet |
|
48 |
* |
031c6b
|
49 |
* @param exchange das Objekt mit Infos zu HTTP-Request, -Response usw. |
U |
50 |
*/ |
|
51 |
public void list(HttpExchange exchange) { |
|
52 |
try { |
|
53 |
init(exchange); |
|
54 |
HttpContext ctx = exchange.getHttpContext(); |
|
55 |
//String base = ctx.getAttributes().getOrDefault(FileServer.ATTR_FILE_BASE, "").toString(); |
|
56 |
if(base.length() == 0) { |
|
57 |
new HttpResponder().sendNotFound(exchange, exchange.getRequestURI().toString()); // not found |
|
58 |
} else { |
|
59 |
String fName = new HttpHelper().getFileName(exchange); |
|
60 |
if (fName.endsWith(FileServer.STR_SLASH)) { // Ordnerliste erzeugen |
|
61 |
String json = new Catalog().list(fName, ctx.getPath(), base); |
|
62 |
if (null != json) { |
|
63 |
new HttpResponder().antwortSenden(exchange, HttpResponder.SC_OK, json); |
|
64 |
} else { |
|
65 |
new HttpResponder().antwortSenden(exchange, HttpResponder.SC_OK, "{}"); // leere Liste |
|
66 |
} |
|
67 |
} else { |
|
68 |
new FileServer().serveFile(exchange); // Datei ausliefern |
|
69 |
} |
|
70 |
} |
|
71 |
} catch (IOException | IllegalArgumentException ex) { |
|
72 |
fehlerAntwort(exchange, ex); |
|
73 |
} finally { |
|
74 |
free(); |
|
75 |
} |
|
76 |
} |
|
77 |
} |