Package io.lumify.web.routes

Source Code of io.lumify.web.routes.Index

package io.lumify.web.routes;

import com.github.jknack.handlebars.Handlebars;
import com.github.jknack.handlebars.Template;
import com.github.jknack.handlebars.io.ServletContextTemplateLoader;
import com.github.jknack.handlebars.io.TemplateLoader;
import io.lumify.miniweb.Handler;
import io.lumify.miniweb.HandlerChain;
import io.lumify.web.WebApp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Index implements Handler {
    public static final String PLUGIN_JS_RESOURCES_PARAM = "pluginJsResources";
    public static final String PLUGIN_CSS_RESOURCES_PARAM = "pluginCssResources";
    private String indexHtml;

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, HandlerChain chain) throws Exception {
        response.setContentType("text/html");
        response.getWriter().write(getIndexHtml(request));
    }

    private String getIndexHtml(HttpServletRequest request) throws IOException {
        if (indexHtml == null) {
            WebApp app = (WebApp) WebApp.getApp(request);
            Map<String, List<String>> context = new HashMap<String, List<String>>();
            context.put(PLUGIN_JS_RESOURCES_PARAM, app.getPluginsJsResources());
            context.put(PLUGIN_CSS_RESOURCES_PARAM, app.getPluginsCssResources());
            TemplateLoader templateLoader = new ServletContextTemplateLoader(request.getServletContext(), "/", ".hbs");
            Handlebars handlebars = new Handlebars(templateLoader);
            Template template = handlebars.compile("index");
            indexHtml = template.apply(context);
        }
        return indexHtml;
    }
}
TOP

Related Classes of io.lumify.web.routes.Index

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.