| | |
| | | package de.uhilger.tango.api; |
| | | |
| | | import com.google.gson.Gson; |
| | | import com.sun.net.httpserver.HttpContext; |
| | | import com.sun.net.httpserver.HttpExchange; |
| | | import de.uhilger.tango.App; |
| | | import static de.uhilger.tango.App.RB_EP_LISTE; |
| | |
| | | import static de.uhilger.tango.store.FileStorage.ST_ABLAGEORT; |
| | | import static de.uhilger.tango.store.FileStorage.ST_GERAET; |
| | | import de.uhilger.tango.store.Storage; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.net.URI; |
| | | import java.util.Iterator; |
| | |
| | | public class StorageHandler extends AbstractHandler { |
| | | |
| | | private static final Logger logger = Logger.getLogger(StorageHandler.class.getName()); |
| | | |
| | | private String conf; |
| | | |
| | | public StorageHandler(String conf) { |
| | | this.conf = conf; |
| | | } |
| | | |
| | | @Override |
| | | protected String put(HttpExchange e) throws IOException { |
| | |
| | | String[] elems = path.split(Server.SLASH); |
| | | String type = elems[elems.length - 2]; |
| | | String elemName = elems[elems.length - 1]; // alter Name, wenn Aenderung |
| | | if(!elemName.equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
| | | FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
| | | if(!elemName.equalsIgnoreCase(getResString(RB_EP_LISTE))) { |
| | | FileStorage fs = new FileStorage(conf); |
| | | Gson gson = new Gson(); |
| | | logger.log(Level.FINE, "type: {0}, token: {1}", new Object[]{type, fs.typeFromName(type).getType().getTypeName()}); |
| | | Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType()); |
| | |
| | | Entity aoe = fs.read(type, elemName); |
| | | if(aoe instanceof Ablageort) { |
| | | Ablageort ablageort = (Ablageort) aoe; |
| | | App.getServer().ablageortEntfernen(ablageort.getUrl()); |
| | | //App.getServer().ablageortEntfernen(ablageort.getUrl()); |
| | | e.getHttpContext().getServer().removeContext(ablageort.getUrl()); |
| | | } |
| | | } |
| | | fs.write(entity, true); |
| | |
| | | if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) { |
| | | if(entity instanceof Ablageort) { |
| | | Ablageort ablageort = (Ablageort) entity; |
| | | App.getServer().ablageortHinzufuegen(ablageort); |
| | | //App.getServer().ablageortHinzufuegen(ablageort); |
| | | ablageortHinzufuegen(e, ablageort); |
| | | } |
| | | } |
| | | } else { // Neu |
| | |
| | | if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) { |
| | | if(entity instanceof Ablageort) { |
| | | Ablageort ablageort = (Ablageort) entity; |
| | | App.getServer().ablageortHinzufuegen(ablageort); |
| | | //App.getServer().ablageortHinzufuegen(ablageort); |
| | | ablageortHinzufuegen(e, ablageort); |
| | | } |
| | | } |
| | | } |
| | |
| | | return "Ungueltiges Objekt im Body."; |
| | | } |
| | | } else { |
| | | return "Ungueltiger Elementname: " + App.getRs(RB_EP_LISTE); |
| | | return "Ungueltiger Elementname: " + getResString(RB_EP_LISTE); |
| | | } |
| | | } |
| | | |
| | | private void ablageortHinzufuegen(HttpExchange e, Ablageort ort) { |
| | | HttpContext c = e.getHttpContext(); |
| | | String ctx = c.getPath(); |
| | | c.getServer().createContext(ctx + ort.getUrl(), |
| | | new ListFileHandler(new File(ort.getOrt()).getAbsolutePath(), conf)); |
| | | } |
| | | |
| | | private boolean loeschen(HttpExchange e) { |
| | |
| | | String[] elems = path.split(Server.SLASH); |
| | | String type = elems[elems.length - 2]; |
| | | String elemName = elems[elems.length - 1]; |
| | | FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
| | | FileStorage fs = new FileStorage(conf); |
| | | if(type.equalsIgnoreCase(FileStorage.ST_ABLAGEORT)) { |
| | | // im laufenden Server den Context entfernen |
| | | Entity entity = fs.read(type, elemName); |
| | | if(entity instanceof Ablageort) { |
| | | Ablageort ablageort = (Ablageort) entity; |
| | | App.getServer().ablageortEntfernen(ablageort.getUrl()); |
| | | //App.getServer().ablageortEntfernen(ablageort.getUrl()); |
| | | e.getHttpContext().getServer().removeContext(ablageort.getUrl()); |
| | | } |
| | | } |
| | | return fs.delete(type, elemName); |
| | |
| | | private String lesen(HttpExchange e) throws IOException, InterruptedException { |
| | | String path = e.getRequestURI().toString(); |
| | | String[] elems = path.split(Server.SLASH); |
| | | FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); |
| | | FileStorage fs = new FileStorage(conf); |
| | | if(path.endsWith(Server.SLASH)) { |
| | | List list = null; |
| | | if(elems[elems.length - 1].equalsIgnoreCase(App.getRs(RB_EP_LISTE_ALLES))) { |
| | | if(elems[elems.length - 1].equalsIgnoreCase(getResString(RB_EP_LISTE_ALLES))) { |
| | | String type = elems[elems.length - 2]; |
| | | logger.fine(type); |
| | | if(type.equalsIgnoreCase(ST_GERAET)) { |
| | |
| | | Object o = gson.fromJson(bodyLesen(e), fs.typeFromName(type).getType()); |
| | | return gson.toJson(list); |
| | | } |
| | | } else if(elems[elems.length - 1].equalsIgnoreCase(App.getRs(RB_EP_LISTE))) { |
| | | } else if(elems[elems.length - 1].equalsIgnoreCase(getResString(RB_EP_LISTE))) { |
| | | String type = elems[elems.length - 2]; |
| | | logger.fine(type); |
| | | list = fs.list(type); |