Ultrakompakter HTTP Server
ulrich
16 hours ago e52bef21012dce5fb38e28f3cd7ef45c81813606
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
/*
  neon - Embeddable HTTP Server based on jdk.httpserver
  Copyright (C) 2024  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.neon.entity;
 
import com.grack.nanojson.JsonArray;
import com.grack.nanojson.JsonObject;
import com.grack.nanojson.JsonParser;
import com.grack.nanojson.JsonParserException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import java.util.stream.Collectors;
 
/**
 * Objekte der Klasse NeonDescriptor beschreiben einen Neon-Server
 * 
 * @author Ulrich Hilger
 */
public class NeonDescriptor {
  
  public String contentType;
  public String contentId;
  public String contentDescription;
  public String instanceName;
  public String instanceDescription;
  public List<String> actorPackages;
  public AuthenticatorDescriptor authenticator;
  public List<ServerDescriptor> server; 
  public List<WsServerDescriptor> wsserver;
 
  /**
   * Den NeonDescriptor aus einer in JSON abgefassten Serverbeschreibungsdatei lesen.
   * 
   * vgl. https://uhilger.de/data/pg/neon/anleitung.adoc#srv-desc
   * 
   * @param fileName Name und ggf. Pfad zur Serverbeschreibung
   */
  public void read(String fileName) {
    try {
      File f = new File(fileName);
      JsonObject serverDesc = JsonParser.object().from(new FileReader(f));
 
      //NeonDescriptor d = new NeonDescriptor();
      this.instanceName = serverDesc.getString("instanceName");
      this.contentType = serverDesc.getString("contentType");
      this.contentId = serverDesc.getString("contentId");
      this.contentDescription = serverDesc.getString("contentDescription");
      this.instanceDescription = serverDesc.getString("instanceDescription");
      this.actorPackages = getArray(serverDesc, "actorPackages");
 
      JsonObject authObj = serverDesc.getObject("authenticator");
      if (null != authObj) {
        this.authenticator = new AuthenticatorDescriptor();
        this.authenticator.className = authObj.getString("className");
        this.authenticator.attributes = getAttrMap(authObj, "attributes");
      }
      JsonArray serverArray = serverDesc.getArray("server");
      this.server = new ArrayList<>();
      Iterator serverIterator = serverArray.iterator();
      while (serverIterator.hasNext()) {
        Object iteratorObject = serverIterator.next();
        if (iteratorObject instanceof JsonObject) {
          JsonObject serverObject = (JsonObject) iteratorObject;
          ServerDescriptor sd = new ServerDescriptor();
          sd.name = serverObject.getString("name");
          sd.port = serverObject.getInt("port", 0);
          sd.contexts = new ArrayList<>();
          JsonArray contextsArray = serverObject.getArray("contexts");
          Iterator contextIterator = contextsArray.iterator();
          while (contextIterator.hasNext()) {
            Object contextObj = contextIterator.next();
            if (contextObj instanceof JsonObject) {
              JsonObject context = (JsonObject) contextObj;
              ContextDescriptor cd = new ContextDescriptor();
              cd.className = context.getString("className");
              cd.sharedHandler = context.getBoolean("sharedHandler");
              cd.contextPath = context.getString("contextPath");
              if (context.containsKey("filter")) {
                cd.filter = getArray(context, "filter");
              }
              cd.authenticator = context.getString("authenticator");
              cd.attributes = getAttrMap(context, "attributes");
              sd.contexts.add(cd);
            }
          }
          this.server.add(sd);
        }
      }
      //return d;
    } catch (JsonParserException | FileNotFoundException ex) {
      Logger.getLogger(NeonDescriptor.class.getName()).severe(ex.getMessage());
      //return null;
    }
  }
 
  /**
   * Die Eintraege eines einfachen JSON-Array in eine String-Liste uebertragen
   * @param src das Objekt, aus dem die Inhalte des JSON-Arrays gelesen werden
   * @param name der Name des zu lesenden JSON-Arrays
   * @return der Inhalt des JSON-Arrays als String-Liste
   */
  private List<String> getArray(JsonObject src, String name) {
    return src.getArray(name)
            .stream()
            .map((o) -> Objects.toString(o, null))
            .collect(Collectors.toList());
  }
 
  /**
   * Eine Liste von Attributen (Schluessel/Wert-Paare) aus einem JSON-Objekt 
   * in eine Map uebertragen.
   * 
   * @param src das JSON-Objekt, aus dem die Attribute gelesen werden sollen
   * @param name der Name des JSON-Elements, das die Attribute enthaelt
   * @return eine Map mit den gelesenen Attributen
   */
  private HashMap<String, String> getAttrMap(JsonObject src, String name) {
    HashMap<String, String> map = new HashMap<>();
    JsonObject attrObj = src.getObject(name);
    Iterator attributeIterator = attrObj.keySet().iterator();
    while (attributeIterator.hasNext()) {
      Object keyObj = attributeIterator.next();
      if (keyObj instanceof String) {
        String key = (String) keyObj;
        map.put(key, attrObj.get(key).toString());
      }
    }
    return map;
  }
}