/*
|
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.google.gson.Gson;
|
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.Writer;
|
import java.io.File;
|
import java.io.IOException;
|
import java.util.logging.Level;
|
import java.util.logging.Logger;
|
|
/**
|
* Abstrakte Basisklasse mit gemeinsamen Methoden fuer Dateioperationen
|
*
|
* @author Ulrich Hilger
|
* @version 0.1, 07.11.2024
|
*/
|
public abstract class AbstractFileActor {
|
|
protected File file;
|
protected String fileName;
|
protected HttpHelper h;
|
protected String base;
|
|
public void run(HttpExchange exchange) {
|
base = exchange.getHttpContext().getAttributes().get(FileServer.ATTR_FILE_BASE).toString();
|
h = new HttpHelper();
|
fileName = h.getFileName(exchange);
|
file = new File(base, fileName);
|
}
|
|
protected String[] dateiliste(HttpExchange exchange) throws IOException {
|
String body = h.bodyLesen(exchange);
|
//logger.fine("dateien: " + body);
|
Gson gson = new Gson();
|
return gson.fromJson(body, String[].class);
|
}
|
|
protected void speichern(HttpExchange e) throws IOException {
|
String body = h.bodyLesen(e);
|
if (new Writer().speichern(file, body) == 0) {
|
antwort(e, HttpResponder.SC_OK, file.getAbsolutePath());
|
} else {
|
antwort(e, HttpResponder.SC_INTERNAL_SERVER_ERROR, "speichern fehlgeschlagen");
|
}
|
}
|
|
protected void fehlerAntwort(HttpExchange exchange, Exception ex) {
|
try {
|
Logger.getLogger(AbstractFileActor.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
|
antwort(exchange, HttpResponder.SC_NOT_FOUND, ex.getMessage());
|
} catch (IOException ex1) {
|
Logger.getLogger(AbstractFileActor.class.getName()).log(Level.SEVERE, ex.getMessage(), ex1);
|
}
|
}
|
|
protected void antwort(HttpExchange exchange, int code, String text) throws IOException {
|
new HttpResponder().antwortSenden(exchange, code, text);
|
}
|
|
protected void free() {
|
file = null;
|
fileName = null;
|
h = null;
|
base = null;
|
}
|
}
|