Persoenliche Mediazentrale
undisclosed
2023-01-21 43d323fa088153082a33966b19d81e9169cc7bf2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*
  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 <https://www.gnu.org/licenses/>.
 */
package de.uhilger.tango.api;
 
import com.google.gson.Gson;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
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.File;
import java.io.IOException;
import java.util.List;
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
 * PUT /mz/api/alist/[pl-name]/[nrVon]/[nrNach]   den Titel von seiner aktuellen Position an eine 
 *                                                 andere Position der Liste [pl-name] verschieben
 * GET /mz/api/alist/[pl-name]/m3u
 * 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  
 * 
 * TODO (2.1.2023):
 * - Liste ab Titel spielen
 * - Ganzes Album der Liste hinzufuegen
 *
 * @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";
  
  private String conf;
  
  public ListHandler(String conf) {
    this.conf = conf;
  }
 
  @Override
  protected String get(HttpExchange e) {
    String path = e.getRequestURI().toString();
    String[] elems = path.split(Server.SLASH);
    if(elems.length > 5) {
      if(elems[5].endsWith("m3u")) {
        Headers headers = e.getResponseHeaders();
        headers.add("Content-Type", "application/m3u");
        return getM3u(e, elems[4]);
      } else {
        return "ungueltig";
      }
    } else {
      String plname = elems[elems.length - 1];
      FileStorage fs = new FileStorage(conf);
      String json = fs.readJson(FileStorage.ST_ABSPIELLISTE, plname);
      return embedInCustomType(json, FileStorage.ST_ABSPIELLISTE);
    }
  }
  
  private String getM3u(HttpExchange e, String plname) {
    StringBuilder sb = new StringBuilder();
    FileStorage fs = new FileStorage(conf);    
    Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
    if (entity instanceof Abspielliste) {
      Abspielliste liste = (Abspielliste) entity;
      List<Titel> titelListe = liste.getTitel();
      
      for(Titel titel : titelListe) {
        
        sb.append("http://hsrv:9090/tango");
        sb.append(titel.getKatalogUrl());
        sb.append(titel.getPfad());
        sb.append(titel.getName());
        sb.append("\n");
      }
    }
    return sb.toString();
  }
 
  @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 = insertTitel(e, elems[4], Integer.parseInt(elems[5]));
        break;
        
      case 7:
        response = moveTitel(e, elems[4], Integer.parseInt(elems[5]), Integer.parseInt(elems[6]));
        break;
    }
    return response;
  }
  
  /**
   * Den Titel im Body von seiner aktuellen Position an die angegebene 
   * Position setzen. Der Titel an der angegebenen Position rueckt nach 
   * unten.
   * 
   * Annahme: Die Abspielliste enthaelt keine Titel mehrfach.
   * 
   * @param e
   * @param plname
   * @param zielPos
   * @return
   * @throws IOException 
   */
  private String moveTitel(HttpExchange e, String plname, int pos, int zielPos) throws IOException {
    FileStorage fs = new FileStorage(conf);
    Entity entity = fs.read(FileStorage.ST_ABSPIELLISTE, plname);
    String response = "Titel konnte nicht verschoben werden.";
    if(entity instanceof Abspielliste) {
      if(pos < zielPos) {
        --zielPos;
      }
      Abspielliste aliste = (Abspielliste) entity;
      List<Titel> liste = aliste.getTitel();
      Titel t = liste.get(pos);
      liste.remove(pos);
      liste.add(zielPos, t);
      fs.write(aliste, true);
      response = "Titel " + t.getName() + " der Liste " + aliste.getName() + 
              " an Position " + zielPos + " verschoben.";
    }
    return response;
  }
  
  /**
   * Den Titel im Body an Position anPos einfuegen. Der Titel an anPos 
   * rueckt nach unten.
   * 
   * @param e
   * @param plname
   * @param anPos
   * @return
   * @throws IOException 
   */
  private String insertTitel(HttpExchange e, String plname, int anPos) throws IOException {
    FileStorage fs = new FileStorage(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.putTitel(titel, anPos);
        fs.write(aliste, true);
        response = "Titel " + titel.getName() + " der Liste " + aliste.getName() + 
                " an Position " + anPos + " hinzugefuegt.";
      }
    }
    return response;
  }
  
  private String addTitel(HttpExchange e, String plname) throws IOException {
    FileStorage fs = new FileStorage(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;
  }
 
  // 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(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;
    }
  }
  
}