| | |
| | | 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 |
| | |
| | | */ |
| | | 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 ATTR_TEMPLATE = "template"; |
| | | |
| | | //public static final String WELCOME_FILE = "index.htmi"; |
| | | |
| | |
| | | * @return das ausgefuellte Template |
| | | * @throws IOException |
| | | */ |
| | | public String render(HttpExchange exchange, Map data) throws IOException { |
| | | public String render(HttpExchange exchange, Object data, String template) 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); |
| | | //logger.fine("fileRoot: " + fileRoot.getAbsolutePath()); |
| | | //String template = helper.getAttrStr(attributes, ATTR_TEMPLATE, STR_EMPTY); |
| | | File templateFile = new File(fileRoot, template); |
| | | if(templateFile.exists()) { |
| | | //logger.fine("using template " + templateFile.getAbsolutePath()); |
| | | mf = new NeonMustacheFactory(fileRoot); |
| | | m = mf.compile(template); |
| | | } else { |
| | | //logger.fine("template " + templateFile.getAbsolutePath() + " not found"); |
| | | mf = new NeonMustacheFactory(); |
| | | m = mf.compile(template); |
| | | } |