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; |
2b5c60
|
25 |
import static de.uhilger.mediaz.Server.RB_SLASH; |
081606
|
26 |
import de.uhilger.mediaz.store.FileStorage; |
b1bf96
|
27 |
import de.uhilger.mediaz.entity.Entity; |
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(); |
U |
46 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
47 |
String type = elems[elems.length - 2]; |
|
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 |
} |
|
62 |
return type + App.getRs(Server.RB_SLASH) + entity.getName(); |
|
63 |
} else { |
|
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(); |
U |
73 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
74 |
String type = elems[elems.length - 2]; |
|
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(); |
|
82 |
String[] elems = path.split(App.getRs(Server.RB_SLASH)); |
|
83 |
FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
|
84 |
if(path.endsWith(App.getRs(RB_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); |
|
90 |
} else { |
|
91 |
String type = elems[elems.length - 1]; |
|
92 |
logger.fine(type); |
|
93 |
list = fs.listObjects(type); |
|
94 |
} |
a43e1a
|
95 |
return jsonWithEnclosingType(list); |
2b5c60
|
96 |
} else { |
U |
97 |
String type = elems[elems.length - 2]; |
|
98 |
String elemName = elems[elems.length - 1]; |
|
99 |
return fs.readJson(type, elemName); |
|
100 |
} |
a43e1a
|
101 |
} |
b379f5
|
102 |
|
8d7d35
|
103 |
@Override |
U |
104 |
public String get(HttpExchange e) { |
|
105 |
return lesen(e); |
|
106 |
} |
|
107 |
|
|
108 |
@Override |
|
109 |
public String post(HttpExchange e) { |
|
110 |
return "nicht unterstuetzt"; |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public boolean delete(HttpExchange e) { |
|
115 |
return loeschen(e); |
b379f5
|
116 |
} |
U |
117 |
} |