Persoenliche Mediazentrale
undisclosed
2023-01-29 7f44143e08ee4ed5d9c89efe5e0592d2c5b39dd8
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
/*
  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;
 
import com.sun.net.httpserver.HttpServer;
import de.uhilger.tango.api.FileHandler;
import de.uhilger.tango.api.GeraetSteuerung;
import de.uhilger.tango.api.ListFileHandler;
import de.uhilger.tango.api.ListHandler;
import de.uhilger.tango.api.MediaSteuerung;
import de.uhilger.tango.api.StopServerHandler;
import de.uhilger.tango.api.StorageHandler;
import de.uhilger.tango.api.StreamHandler;
import de.uhilger.tango.store.FileStorage;
import de.uhilger.tango.entity.Ablageort;
import java.io.File;
import java.io.IOException;
import java.util.logging.Logger;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import de.uhilger.tango.entity.Entity;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
 
/**
 * Die Klasse Server stellt Methoden zur Ausf&uuml;hrung eines HTTP-Servers
 * bereit
 *
 * @author Ulrich Hilger
 * @version 0.1, 25.03.2021
 */
public class Server {
 
  private static final Logger logger = Logger.getLogger(Server.class.getName());
 
  public static final String RB_SERVER_START_MSG = "msgServerStart";
  public static final String RB_WEBROOT = "webroot";
  public static final String RB_STORE = "store";
  public static final String RB_STRG = "strg";
  public static final String RB_GSTRG = "gstrg";
  public static final String RB_ALIST = "alist";
  public static final String RB_STRM = "strm";
  //public static final String RB_UI_ROOT = "uiroot";
  public static final String RB_STOP_SERVER = "stopServer";
  //public static final String RB_ABLAGE_TEST = "testAblage";
  //public static final String RB_STORE_TEST = "testStore";
  public static final String SLASH = "/";
  public static final String NEWLINE = "\n";
 
  private int port;
 
  private String ctx;
  
  //private HttpServer server;
 
  /**
   * Ein neues Objekt der Kalsse Server erzeugen
   *
   * @param port der Port, &uuml;ber den dieser Server erreichbar sein soll
   */
  public Server(int port) {
    this.port = port;
  }  
 
  /**
   * Den Port angeben, unter dem der Server erreichbar sein soll
   *
   * @param port der Port, unter dem der Server erreichbar sein soll
   */
  public void setPort(int port) {
    this.port = port;
  }
 
  /**
   * Den Namen des Kontexts angeben, &uuml;ber den dieser Server erreichbar sein
   * soll
   *
   * @param ctxName Name des Kontexts, unter dem der Server aufrufbar sein soll
   */
  public void setContextName(String ctxName) {
    if (!ctxName.startsWith(SLASH)) {
      this.ctx = SLASH + ctxName;
    } else {
      this.ctx = ctxName;
    }
  }
 
  /**
   * Die Endpunkte einrichten, unter denen die Dienste dieses
   * Servers erreichbar sein sollen und den Server starten
   *
   * @throws IOException wenn etwas schief geht, finden sich Angaben in diesem
   * Objekt
   * @throws java.lang.ClassNotFoundException
   */
  public void start(String wwwData, String conf) throws IOException, ClassNotFoundException {
    ResourceBundle rb = ResourceBundle.getBundle(App.RB_NAME);
    logger.log(Level.INFO, rb.getString(RB_SERVER_START_MSG), Integer.toString(port));
 
    //String wwwData = App.getInitParameter(rb.getString(App.RB_AP_WWW_DATA));
    File wwwDir = new File(wwwData);
 
    HttpServer server = HttpServer.create(new InetSocketAddress(port), 0);
    server.createContext(ctx + rb.getString(RB_WEBROOT), new FileHandler(wwwDir.getAbsolutePath()));
    ablageorteEinklinken(server, rb, conf);
    server.createContext(ctx + rb.getString(RB_STORE), new StorageHandler(conf));
    MediaSteuerung ms = new MediaSteuerung(conf);
    server.createContext(ctx + rb.getString(RB_STRG), ms);
    server.createContext(ctx + rb.getString(RB_GSTRG), new GeraetSteuerung(conf));
    ListHandler lh = new ListHandler(conf);
    lh.addPlaylistListener(ms);
    server.createContext(ctx + rb.getString(RB_ALIST), lh);
    server.createContext(ctx + rb.getString(RB_STRM), new StreamHandler(conf));
    server.createContext(ctx + rb.getString(RB_STOP_SERVER), new StopServerHandler());
    //server.setExecutor(Executors.newFixedThreadPool(20));
    server.setExecutor(Executors.newFixedThreadPool(5));
    server.start();
  }
  
  /*
  public void ablageortEntfernen(String url) {
    server.removeContext(ctx + url);
  }
 
  public void ablageortHinzufuegen(Ablageort ort, String conf) {
    server.createContext(ctx + ort.getUrl(),  
          new ListFileHandler(new File(ort.getOrt()).getAbsolutePath(), conf));
  }
  */
  
  private void ablageorteEinklinken(HttpServer server, ResourceBundle rb, String conf) 
              throws ClassNotFoundException, IOException {
    String typ = Ablageort.class.getSimpleName();
    FileStorage store = new FileStorage(conf);
    List<String> orte = store.list(typ);
    Iterator<String> i = orte.iterator();
    while(i.hasNext()) {
      String ortName = i.next();
      Entity e = store.read(typ, ortName);
      if(e instanceof Ablageort) {
        Ablageort ablageort = (Ablageort) e;
        logger.log(Level.FINE, "{0}{1}", new Object[]{ctx, ablageort.getUrl()});
        logger.fine(ablageort.getOrt());
        server.createContext(ctx + ablageort.getUrl(),  
          new ListFileHandler(new File(ablageort.getOrt()).getAbsolutePath(), conf));
      }
    }
  }
}