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