commit | author | age
|
b379f5
|
1 |
/* |
5f70da
|
2 |
Mediazentrale - Personal Media Center |
U |
3 |
Copyright (C) 2021 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/>. |
b379f5
|
17 |
*/ |
U |
18 |
package de.uhilger.mediaz.api; |
|
19 |
|
|
20 |
import com.google.gson.Gson; |
|
21 |
import com.sun.net.httpserver.HttpExchange; |
|
22 |
import de.uhilger.mediaz.App; |
f45e20
|
23 |
import static de.uhilger.mediaz.App.RB_EP_LISTE; |
b379f5
|
24 |
import de.uhilger.mediaz.Server; |
081606
|
25 |
import de.uhilger.mediaz.store.FileStorage; |
b1bf96
|
26 |
import de.uhilger.mediaz.entity.Entity; |
d027b5
|
27 |
import static de.uhilger.mediaz.store.FileStorage.ST_ABLAGEORT; |
b379f5
|
28 |
import java.io.IOException; |
2b5c60
|
29 |
import java.util.List; |
081606
|
30 |
import java.util.logging.Level; |
b379f5
|
31 |
import java.util.logging.Logger; |
U |
32 |
|
|
33 |
/** |
2597cd
|
34 |
* HttpHandler fuer die Verwaltung von Entitaeten der Mediazentrale |
5f70da
|
35 |
* |
U |
36 |
* @author Ulrich Hilger |
|
37 |
* @version 1, 5.4.2021 |
b379f5
|
38 |
*/ |
8d7d35
|
39 |
public class StorageHandler extends AbstractHandler { |
b379f5
|
40 |
|
081606
|
41 |
private static final Logger logger = Logger.getLogger(StorageHandler.class.getName()); |
b379f5
|
42 |
|
8d7d35
|
43 |
@Override |
U |
44 |
protected String put(HttpExchange e) throws IOException { |
dfb7d3
|
45 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
46 |
String[] elems = path.split(Server.SLASH); |
dfb7d3
|
47 |
String type = elems[elems.length - 2]; |
U |
48 |
String elemName = elems[elems.length - 1]; // alter Name, wenn Aenderung |
|
49 |
if(!elemName.equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
|
50 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
|
51 |
Gson gson = new Gson(); |
|
52 |
logger.log(Level.FINE, "type: {0}, token: {1}", new Object[]{type, fs.typeFromName(type).getType().getTypeName()}); |
|
53 |
Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType()); |
|
54 |
if(o instanceof Entity) { |
|
55 |
Entity entity = (Entity) o; |
|
56 |
if(fs.exists(type, elemName)) { |
|
57 |
fs.delete(type, elemName); |
|
58 |
fs.write(entity, true); |
|
59 |
} else { |
|
60 |
fs.write(entity, false); |
|
61 |
} |
0e9cd3
|
62 |
return type + Server.SLASH + entity.getName(); |
dfb7d3
|
63 |
} else { |
U |
64 |
return "Ungueltiges Objekt im Body."; |
|
65 |
} |
|
66 |
} else { |
|
67 |
return "Ungueltiger Elementname: " + App.getRs(RB_EP_LISTE); |
|
68 |
} |
|
69 |
} |
8d7d35
|
70 |
|
2b5c60
|
71 |
private boolean loeschen(HttpExchange e) { |
5f70da
|
72 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
73 |
String[] elems = path.split(Server.SLASH); |
5f70da
|
74 |
String type = elems[elems.length - 2]; |
U |
75 |
String elemName = elems[elems.length - 1]; |
|
76 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
2b5c60
|
77 |
return fs.delete(type, elemName); |
U |
78 |
} |
|
79 |
|
|
80 |
private String lesen(HttpExchange e) { |
|
81 |
String path = e.getRequestURI().toString(); |
0e9cd3
|
82 |
String[] elems = path.split(Server.SLASH); |
2b5c60
|
83 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
0e9cd3
|
84 |
if(path.endsWith(Server.SLASH)) { |
f45e20
|
85 |
List list = null; |
dfb7d3
|
86 |
if(elems[elems.length - 1].equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
f45e20
|
87 |
String type = elems[elems.length - 2]; |
U |
88 |
logger.fine(type); |
|
89 |
list = fs.list(type); |
d027b5
|
90 |
if(type.equalsIgnoreCase(ST_ABLAGEORT)) { |
U |
91 |
list.add("Livestreams"); |
|
92 |
} |
f45e20
|
93 |
} else { |
U |
94 |
String type = elems[elems.length - 1]; |
|
95 |
logger.fine(type); |
|
96 |
list = fs.listObjects(type); |
|
97 |
} |
a43e1a
|
98 |
return jsonWithEnclosingType(list); |
2b5c60
|
99 |
} else { |
U |
100 |
String type = elems[elems.length - 2]; |
|
101 |
String elemName = elems[elems.length - 1]; |
|
102 |
return fs.readJson(type, elemName); |
|
103 |
} |
a43e1a
|
104 |
} |
b379f5
|
105 |
|
8d7d35
|
106 |
@Override |
U |
107 |
public String get(HttpExchange e) { |
|
108 |
return lesen(e); |
|
109 |
} |
|
110 |
|
|
111 |
@Override |
|
112 |
public String post(HttpExchange e) { |
|
113 |
return "nicht unterstuetzt"; |
|
114 |
} |
|
115 |
|
|
116 |
@Override |
|
117 |
public boolean delete(HttpExchange e) { |
|
118 |
return loeschen(e); |
b379f5
|
119 |
} |
U |
120 |
} |