commit | author | age
|
aed034
|
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.google.gson.Gson; |
|
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.Writer; |
|
26 |
import java.io.File; |
|
27 |
import java.io.IOException; |
|
28 |
import java.util.logging.Level; |
|
29 |
import java.util.logging.Logger; |
|
30 |
|
|
31 |
/** |
|
32 |
* Abstrakte Basisklasse mit gemeinsamen Methoden fuer Dateioperationen |
|
33 |
* |
|
34 |
* @author Ulrich Hilger |
|
35 |
* @version 0.1, 07.11.2024 |
|
36 |
*/ |
|
37 |
public abstract class AbstractFileActor { |
|
38 |
|
|
39 |
protected File file; |
|
40 |
protected String fileName; |
|
41 |
protected HttpHelper h; |
|
42 |
protected String base; |
|
43 |
|
b3d917
|
44 |
/** |
U |
45 |
* Hilfsmittel bereitstellen, die beim Ausfuehren eines Actors fuer Dateioperationen |
|
46 |
* immer benoetigt werden. |
|
47 |
* |
|
48 |
* Von dieser Klasse abgeleitete Actos sollten mit super.run(exchange) stets diese |
|
49 |
* Methode ausfuehren. |
|
50 |
* |
|
51 |
* @param exchange Infos zu HTTP Request, -Response, Kontext usw. |
|
52 |
*/ |
|
53 |
protected void init(HttpExchange exchange) { |
aed034
|
54 |
base = exchange.getHttpContext().getAttributes().get(FileServer.ATTR_FILE_BASE).toString(); |
U |
55 |
h = new HttpHelper(); |
|
56 |
fileName = h.getFileName(exchange); |
|
57 |
file = new File(base, fileName); |
|
58 |
} |
|
59 |
|
b3d917
|
60 |
/** |
U |
61 |
* Eine Dateiliste als String Array aus einer Dateiliste im JSON-Format |
|
62 |
* erzeugen, die aus dem HTTP-Request-Body gelesen wurde |
|
63 |
* |
|
64 |
* @param exchange Infos zu HTTP Request, -Response, Kontext usw. |
|
65 |
* @return die Dateiliste als String Array |
|
66 |
* @throws IOException wenn etwas schief geht |
|
67 |
*/ |
aed034
|
68 |
protected String[] dateiliste(HttpExchange exchange) throws IOException { |
U |
69 |
String body = h.bodyLesen(exchange); |
|
70 |
//logger.fine("dateien: " + body); |
|
71 |
Gson gson = new Gson(); |
|
72 |
return gson.fromJson(body, String[].class); |
|
73 |
} |
|
74 |
|
b3d917
|
75 |
/** |
U |
76 |
* Den Body eines HTTP Request in eine Datei schreiben |
|
77 |
* |
|
78 |
* TODO das evtl. noch in java.nio.file ueberfuehren.. |
|
79 |
* |
|
80 |
* @param e Infos zu HTTP Request, -Response, Kontext usw. |
|
81 |
* @throws IOException wenn etwas schief geht |
|
82 |
*/ |
aed034
|
83 |
protected void speichern(HttpExchange e) throws IOException { |
U |
84 |
String body = h.bodyLesen(e); |
|
85 |
if (new Writer().speichern(file, body) == 0) { |
|
86 |
antwort(e, HttpResponder.SC_OK, file.getAbsolutePath()); |
|
87 |
} else { |
|
88 |
antwort(e, HttpResponder.SC_INTERNAL_SERVER_ERROR, "speichern fehlgeschlagen"); |
|
89 |
} |
|
90 |
} |
|
91 |
|
b3d917
|
92 |
/** |
U |
93 |
* Eine Fehler-Antwort als HTTP Status Code 401 not found senden |
|
94 |
* |
|
95 |
* @param exchange Infos zu HTTP Request, -Response, Kontext usw. |
|
96 |
* @param ex der aufgetretene Fehler |
|
97 |
*/ |
aed034
|
98 |
protected void fehlerAntwort(HttpExchange exchange, Exception ex) { |
U |
99 |
try { |
|
100 |
Logger.getLogger(AbstractFileActor.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); |
|
101 |
antwort(exchange, HttpResponder.SC_NOT_FOUND, ex.getMessage()); |
|
102 |
} catch (IOException ex1) { |
|
103 |
Logger.getLogger(AbstractFileActor.class.getName()).log(Level.SEVERE, ex.getMessage(), ex1); |
|
104 |
} |
|
105 |
} |
|
106 |
|
b3d917
|
107 |
/** |
U |
108 |
* Eine Antwort als HTTP Response senden |
|
109 |
* |
|
110 |
* @param exchange Infos zu HTTP Request, -Response, Kontext usw. |
|
111 |
* @param code HTTP Status code fuer die Antwort |
|
112 |
* @param text Inhalt der Antwort |
|
113 |
* @throws IOException wenn etwas schif geht |
|
114 |
*/ |
aed034
|
115 |
protected void antwort(HttpExchange exchange, int code, String text) throws IOException { |
U |
116 |
new HttpResponder().antwortSenden(exchange, code, text); |
|
117 |
} |
|
118 |
|
b3d917
|
119 |
/** |
U |
120 |
* Die Zeiger auf die von einer Instanz dieser abstrakten Basisklasse |
|
121 |
* erzeugten Objekte wieder frei geben |
|
122 |
*/ |
aed034
|
123 |
protected void free() { |
U |
124 |
file = null; |
|
125 |
fileName = null; |
|
126 |
h = null; |
|
127 |
base = null; |
|
128 |
} |
|
129 |
} |