/*
|
Helix - Dateiverwaltung
|
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.httpserver.template;
|
|
import com.github.mustachejava.Mustache;
|
import com.github.mustachejava.MustacheFactory;
|
import com.sun.net.httpserver.HttpExchange;
|
import de.uhilger.httpserver.base.HttpHelper;
|
import de.uhilger.httpserver.base.HttpResponder;
|
import de.uhilger.httpserver.base.handler.FileHandler;
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.io.StringWriter;
|
import java.util.Map;
|
import java.util.logging.Logger;
|
|
/**
|
* Der TemplateActor rendert den Inhalt einer Map in
|
* ein Mustache-Template und gibt das Ergebnis zurueck.
|
*
|
* @author Ulrich Hilger
|
* @version 1, 04.07.2021
|
*/
|
public class TemplateActor {
|
|
private static final Logger logger = Logger.getLogger(TemplateActor.class.getName());
|
|
public static final String STR_DOT = ".";
|
public static final String STR_EMPTY = "";
|
public static final String STR_SLASH = "/";
|
|
public static final String ATTR_TEMPLATE = "template";
|
|
//public static final String WELCOME_FILE = "index.htmi";
|
|
/**
|
* Eine HTTP-Anfrage bearbeiten
|
*
|
* Erfordert den Namen des Templates im Attribut TemplateActor.ATTR_TEMPLATE
|
*
|
* Wenn im HttpContext das Attribut FileHandler.ATTR_FILE_BASE vorhanden ist,
|
* wird dessen Inhalt als Basis-Ordner für das zu verwendende Template genutzt.
|
* Anderenfalls wird das Template im Classpath gesucht.
|
*
|
* @param exchange die Anfrage und -Antwort-Infos der HTTP-Anfrage
|
* @param data die Map mit den variablen Inhalten des Templates
|
* @return das ausgefuellte Template
|
* @throws IOException
|
*/
|
public String render(HttpExchange exchange, Map data) throws IOException {
|
MustacheFactory mf;
|
Mustache m;
|
HttpHelper helper = new HttpHelper();
|
Map attributes = exchange.getHttpContext().getAttributes();
|
String fileBase = helper.getAttrStr(attributes, FileHandler.ATTR_FILE_BASE, STR_EMPTY);
|
File fileRoot = new File(fileBase);
|
String template = helper.getAttrStr(attributes, ATTR_TEMPLATE, STR_EMPTY);
|
File templateFile = new File(fileRoot, template);
|
if(templateFile.exists()) {
|
mf = new NeonMustacheFactory(fileRoot);
|
m = mf.compile(template);
|
} else {
|
mf = new NeonMustacheFactory();
|
m = mf.compile(template);
|
}
|
StringWriter writer = new StringWriter();
|
m.execute(writer, data).flush();
|
return writer.toString();
|
}
|
|
}
|