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 |
|
|
44 |
public void run(HttpExchange exchange) { |
|
45 |
base = exchange.getHttpContext().getAttributes().get(FileServer.ATTR_FILE_BASE).toString(); |
|
46 |
h = new HttpHelper(); |
|
47 |
fileName = h.getFileName(exchange); |
|
48 |
file = new File(base, fileName); |
|
49 |
} |
|
50 |
|
|
51 |
protected String[] dateiliste(HttpExchange exchange) throws IOException { |
|
52 |
String body = h.bodyLesen(exchange); |
|
53 |
//logger.fine("dateien: " + body); |
|
54 |
Gson gson = new Gson(); |
|
55 |
return gson.fromJson(body, String[].class); |
|
56 |
} |
|
57 |
|
|
58 |
protected void speichern(HttpExchange e) throws IOException { |
|
59 |
String body = h.bodyLesen(e); |
|
60 |
if (new Writer().speichern(file, body) == 0) { |
|
61 |
antwort(e, HttpResponder.SC_OK, file.getAbsolutePath()); |
|
62 |
} else { |
|
63 |
antwort(e, HttpResponder.SC_INTERNAL_SERVER_ERROR, "speichern fehlgeschlagen"); |
|
64 |
} |
|
65 |
} |
|
66 |
|
|
67 |
protected void fehlerAntwort(HttpExchange exchange, Exception ex) { |
|
68 |
try { |
|
69 |
Logger.getLogger(AbstractFileActor.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); |
|
70 |
antwort(exchange, HttpResponder.SC_NOT_FOUND, ex.getMessage()); |
|
71 |
} catch (IOException ex1) { |
|
72 |
Logger.getLogger(AbstractFileActor.class.getName()).log(Level.SEVERE, ex.getMessage(), ex1); |
|
73 |
} |
|
74 |
} |
|
75 |
|
|
76 |
protected void antwort(HttpExchange exchange, int code, String text) throws IOException { |
|
77 |
new HttpResponder().antwortSenden(exchange, code, text); |
|
78 |
} |
|
79 |
|
|
80 |
protected void free() { |
|
81 |
file = null; |
|
82 |
fileName = null; |
|
83 |
h = null; |
|
84 |
base = null; |
|
85 |
} |
|
86 |
} |