/*
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 .
*/
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 actorPackages;
public AuthenticatorDescriptor authenticator;
public List server;
public List 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 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 getAttrMap(JsonObject src, String name) {
HashMap 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;
}
}