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.sun.net.httpserver.HttpExchange; |
|
21 |
import de.uhilger.neon.FileServer; |
|
22 |
import de.uhilger.neon.HttpResponder; |
|
23 |
import de.uhilger.fm.FileHelper; |
|
24 |
import java.io.IOException; |
|
25 |
import java.util.logging.Level; |
|
26 |
import java.util.logging.Logger; |
|
27 |
|
|
28 |
/** |
b3d917
|
29 |
* Eine Klasse mit Methoden zur Erzeugung von Ordnern und Dateien |
aed034
|
30 |
* |
U |
31 |
* @author Ulrich Hilger |
|
32 |
* @version 0.1, 08.11.2024 |
|
33 |
*/ |
|
34 |
public class FileCreator extends AbstractFileActor { |
|
35 |
|
b3d917
|
36 |
/** |
U |
37 |
* Die methode run legt Ordner und Dateien neu an, sofern im hier |
|
38 |
* uebergebenen HttpExchange Objekt die folgenden Angaben zu finden |
|
39 |
* sind: |
|
40 |
* |
|
41 |
* <pre> |
|
42 |
* Datei neu anlegen (ohne Ueberschreiben): |
|
43 |
* http://localhost:[port]/[kontext]/pfad/zur/datei.txt |
|
44 |
* Body: Dateiinhalt |
|
45 |
* Erzeugt eine neue Datei mit einer laufenden Nummer, |
|
46 |
* falls die per URL angegebene Datei schon existiert |
|
47 |
* |
|
48 |
* Ordner anlegen: |
|
49 |
* http://localhost:[port]/[kontext]/pfad/zum/ordner/ |
|
50 |
* erzeugt einen HTTP-Fehler 422, wenn der Ordner schon existiert |
|
51 |
* </pre> |
|
52 |
* |
|
53 |
* @param exchange Infos zu HTTP Request, -Response, Kontext usw. |
|
54 |
*/ |
|
55 |
public void create(HttpExchange exchange) { |
aed034
|
56 |
try { |
b3d917
|
57 |
init(exchange); |
aed034
|
58 |
if (fileName.endsWith(FileServer.STR_SLASH)) { // es ist ein Ordner |
U |
59 |
if (!file.exists()) { |
|
60 |
file.mkdir(); |
|
61 |
antwort(exchange, HttpResponder.SC_OK, file.getAbsolutePath()); |
|
62 |
} else { |
|
63 |
String antw = "Ordner existiert bereits."; |
|
64 |
antwort(exchange, HttpResponder.SC_UNPROCESSABLE_ENTITY, antw); |
|
65 |
} |
|
66 |
} else { // es ist eine Datei |
|
67 |
if (file.exists()) { |
|
68 |
FileHelper trans = new FileHelper(); |
|
69 |
file = trans.getNewFileName(file); |
|
70 |
} |
|
71 |
speichern(exchange); |
b3d917
|
72 |
} |
aed034
|
73 |
} catch (IOException ex) { |
ca6de6
|
74 |
Logger.getLogger(FileCreator.class.getName()).log(Level.SEVERE, ex.getMessage(), ex); |
aed034
|
75 |
} finally { |
U |
76 |
free(); |
|
77 |
} |
|
78 |
} |
|
79 |
} |