commit | author | age
|
5a290d
|
1 |
/* |
U |
2 |
Helix - Dateiverwaltung |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.httpserver.template; |
|
19 |
|
|
20 |
import com.github.mustachejava.Mustache; |
|
21 |
import com.github.mustachejava.MustacheFactory; |
|
22 |
import com.sun.net.httpserver.HttpExchange; |
|
23 |
import de.uhilger.httpserver.base.HttpHelper; |
|
24 |
import de.uhilger.httpserver.base.handler.FileHandler; |
|
25 |
import java.io.File; |
|
26 |
import java.io.IOException; |
|
27 |
import java.io.StringWriter; |
|
28 |
import java.util.Map; |
|
29 |
|
|
30 |
/** |
|
31 |
* Der TemplateActor rendert den Inhalt einer Map in |
|
32 |
* ein Mustache-Template und gibt das Ergebnis zurueck. |
|
33 |
* |
|
34 |
* @author Ulrich Hilger |
|
35 |
* @version 1, 04.07.2021 |
|
36 |
*/ |
|
37 |
public class TemplateActor { |
|
38 |
|
|
39 |
public static final String STR_DOT = "."; |
|
40 |
public static final String STR_EMPTY = ""; |
|
41 |
public static final String STR_SLASH = "/"; |
|
42 |
|
73eed0
|
43 |
//public static final String ATTR_TEMPLATE = "template"; |
5a290d
|
44 |
|
U |
45 |
//public static final String WELCOME_FILE = "index.htmi"; |
|
46 |
|
|
47 |
/** |
|
48 |
* Eine HTTP-Anfrage bearbeiten |
|
49 |
* |
|
50 |
* Erfordert den Namen des Templates im Attribut TemplateActor.ATTR_TEMPLATE |
|
51 |
* |
|
52 |
* Wenn im HttpContext das Attribut FileHandler.ATTR_FILE_BASE vorhanden ist, |
|
53 |
* wird dessen Inhalt als Basis-Ordner für das zu verwendende Template genutzt. |
|
54 |
* Anderenfalls wird das Template im Classpath gesucht. |
|
55 |
* |
|
56 |
* @param exchange die Anfrage und -Antwort-Infos der HTTP-Anfrage |
|
57 |
* @param data die Map mit den variablen Inhalten des Templates |
|
58 |
* @return das ausgefuellte Template |
|
59 |
* @throws IOException |
|
60 |
*/ |
47bd6b
|
61 |
public String render(HttpExchange exchange, Object data, String template) throws IOException { |
5a290d
|
62 |
MustacheFactory mf; |
U |
63 |
Mustache m; |
|
64 |
HttpHelper helper = new HttpHelper(); |
|
65 |
Map attributes = exchange.getHttpContext().getAttributes(); |
|
66 |
String fileBase = helper.getAttrStr(attributes, FileHandler.ATTR_FILE_BASE, STR_EMPTY); |
|
67 |
File fileRoot = new File(fileBase); |
31d9df
|
68 |
//logger.fine("fileRoot: " + fileRoot.getAbsolutePath()); |
73eed0
|
69 |
//String template = helper.getAttrStr(attributes, ATTR_TEMPLATE, STR_EMPTY); |
5a290d
|
70 |
File templateFile = new File(fileRoot, template); |
U |
71 |
if(templateFile.exists()) { |
31d9df
|
72 |
//logger.fine("using template " + templateFile.getAbsolutePath()); |
5a290d
|
73 |
mf = new NeonMustacheFactory(fileRoot); |
U |
74 |
m = mf.compile(template); |
|
75 |
} else { |
31d9df
|
76 |
//logger.fine("template " + templateFile.getAbsolutePath() + " not found"); |
5a290d
|
77 |
mf = new NeonMustacheFactory(); |
U |
78 |
m = mf.compile(template); |
|
79 |
} |
|
80 |
StringWriter writer = new StringWriter(); |
|
81 |
m.execute(writer, data).flush(); |
|
82 |
return writer.toString(); |
|
83 |
} |
|
84 |
|
|
85 |
} |