/* Tango - Personal Media Center Copyright (C) 2021 Ulrich Hilger This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.uhilger.tango.api; import com.google.gson.Gson; import com.sun.net.httpserver.HttpExchange; import de.uhilger.tango.App; import de.uhilger.tango.Server; import de.uhilger.tango.entity.Abspielliste; import de.uhilger.tango.entity.Entity; import de.uhilger.tango.entity.Titel; import de.uhilger.tango.store.FileStorage; import java.io.IOException; import java.util.logging.Logger; /** * Der ListHandler bearbeitet HTTP-Anfragen zu Abspiellisten * * GET /mz/api/alist/[pl-name] die Titel-Objekte der Liste [pl-name] liefern * PUT /mz/api/alist/[pl-name] den Titel im Body anfuegen an die Liste [pl-name] * PUT /mz/api/alist/[pl-name]/[nr] an der Position nr der Liste [pl-name] den Titel im Body einfuegen * DELETE /mz/api/alist/[pl-name]/[nr] den Titel an der Position [nr] aus der Liste [pl-name] entfernen * DELETE /mz/api/alist/[pl-name]/alle alle Titel aus der Liste [pl-name] entfernen * * @author Ulrich Hilger * @version 1, 8.4.2021 */ public class ListHandler extends AbstractHandler { private static final Logger logger = Logger.getLogger(ListHandler.class.getName()); public static final String ALLE_TITEL = "alle"; @Override protected String get(HttpExchange e) { String path = e.getRequestURI().toString(); String[] elems = path.split(Server.SLASH); String plname = elems[elems.length - 1]; FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname); return embedInCustomType(json, FileStorage.ST_ABSPIELLISTE); } @Override protected String put(HttpExchange e) throws IOException { String path = e.getRequestURI().toString(); String[] elems = path.split(Server.SLASH); String response = "ListHandler.put: ungueltiger URL"; switch(elems.length) { case 5: // ohne nr am Ende response = addTitel(e, elems[4]); break; case 6: response = "Einfuegen noch nicht fertig."; break; } return response; } private String addTitel(HttpExchange e, String plname) throws IOException { FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname); String response = "Titel konnte nicht hinzugefuegt werden."; if(entity instanceof Abspielliste) { Abspielliste aliste = (Abspielliste) entity; String titelJson = bodyLesen(e); Gson gson = new Gson(); Object o = gson.fromJson(titelJson, fs.typeFromName(Titel.class.getSimpleName()).getType()); if(o instanceof Titel) { Titel titel = (Titel) o; aliste.addTitel(titel); fs.write(aliste, true); response = "Titel " + titel.getName() + " der Liste " + aliste.getName() + " hinzugefuegt."; } } return response; } @Override protected String post(HttpExchange e) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } // DELETE /mz/api/alist/[pl-name]/[nr] den Titel an der Position [nr] aus der Liste [pl-name] entfernen @Override protected boolean delete(HttpExchange e) { String path = e.getRequestURI().toString(); String[] elems = path.split(Server.SLASH); String listName = elems[elems.length - 2]; FileStorage fs = new FileStorage(App.getInitParameter(App.getRs(App.RB_AP_CONF))); Entity entity = fs.read(Abspielliste.class.getSimpleName(), listName); if(entity instanceof Abspielliste) { Abspielliste liste = (Abspielliste) entity; String titelStr = elems[elems.length-1]; if(titelStr.equalsIgnoreCase(ALLE_TITEL)) { liste.getTitel().clear(); } else { liste.getTitel().remove(Integer.parseInt(elems[elems.length-1])); } fs.write(liste, true); return true; } else { return false; } } }