de.perdian.apps.dashboard.mvc.portal.PortalView.java Source code

Java tutorial

Introduction

Here is the source code for de.perdian.apps.dashboard.mvc.portal.PortalView.java

Source

/*
 * Dashboard
 * Copyright 2014 Christian Robert
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.perdian.apps.dashboard.mvc.portal;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.web.servlet.View;

class PortalView implements View {

    private Resource jsResource = null;
    private List<PortalModule> modules = null;

    @Override
    public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");

        PrintWriter printWriter = response.getWriter();
        printWriter.append("<!DOCTYPE html>\n");
        printWriter.append("<html>\n");
        printWriter.append("<head>\n");
        this.appendDashboardHead(model, request, response);
        printWriter.append("</head>\n");
        printWriter.append("<body>\n");
        this.appendDashboardBody(model, request, response);
        printWriter.append("</body>\n");
        printWriter.append("</html>\n");

    }

    private void appendDashboardHead(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        for (PortalModule module : this.getModules()) {
            for (String jsResource : module.getJsResources()) {

                StringBuilder jsResourceLocation = new StringBuilder();
                jsResourceLocation.append(request.getContextPath()).append("/resources/modules/");
                jsResourceLocation.append(module.getName()).append("/").append(jsResource);

                response.getWriter().append("<script type=\"text/javascript\" src=\"");
                response.getWriter().append(jsResourceLocation).append("\"></script>\n");

            }
            for (String cssResource : module.getCssResources()) {

                StringBuilder cssResourceLocation = new StringBuilder();
                cssResourceLocation.append(request.getContextPath()).append("/resources/modules/");
                cssResourceLocation.append(module.getName()).append("/").append(cssResource);

                response.getWriter().append("<link rel=\"stylesheet\" type=\"text/css\" href=\"");
                response.getWriter().append(cssResourceLocation).append("\" />\n");

            }
        }
    }

    private void appendDashboardBody(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.getWriter().append("<script type=\"text/javascript\">\n");
        this.appendDashboardJsContent(model, request, response);
        response.getWriter().append("</script>\n");
    }

    private void appendDashboardJsContent(Map<String, ?> model, HttpServletRequest request,
            HttpServletResponse response) throws IOException {

        // Make sure to make environment information available before the
        // actual JavaScript is included in the page
        response.getWriter().append("dashboard.contextPath = \"").append(request.getContextPath())
                .append("\";\n\n");

        // Now include the JavaScript file into the resulting document which
        // will render the actual dashboard
        try (Reader resourceReader = new InputStreamReader(
                new BufferedInputStream(this.getJsResource().getInputStream()), "UTF-8")) {
            IOUtils.copy(resourceReader, response.getWriter());
        }

        // Make sure page is reloaded once every three hours, so that changes will
        // propagate automatically and to reset memory
        response.getWriter()
                .append("\nwindow.setTimeout(function() { location.reload(); }, (1000 * 60 * 60 * 3));\n");

    }

    @Override
    public String getContentType() {
        return "text/html";
    }

    // -------------------------------------------------------------------------
    // --- Property access methods ---------------------------------------------
    // -------------------------------------------------------------------------

    Resource getJsResource() {
        return this.jsResource;
    }

    void setJsResource(Resource jsResource) {
        this.jsResource = jsResource;
    }

    List<PortalModule> getModules() {
        return this.modules;
    }

    void setModules(List<PortalModule> modules) {
        this.modules = modules;
    }

}