Search in sources :

Example 1 with TemplateRenderer

use of de.zalando.ep.zalenium.servlet.renderer.TemplateRenderer in project zalenium by zalando.

the class LivePreviewServlet method process.

@SuppressWarnings("WeakerAccess")
protected void process(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String refresh = "1200";
    String testBuild = "";
    try {
        refresh = Optional.ofNullable(request.getParameter("refresh")).orElse(refresh);
        testBuild = Optional.ofNullable(request.getParameter("build")).orElse(testBuild);
    } catch (Exception e) {
        LOGGER.debug(e.toString(), e);
    }
    List<String> nodes = new ArrayList<>();
    for (RemoteProxy proxy : getRegistry().getAllProxies()) {
        if (proxy instanceof DockerSeleniumRemoteProxy) {
            DockerSeleniumRemoteProxy dockerSeleniumRemoteProxy = (DockerSeleniumRemoteProxy) proxy;
            HtmlRenderer renderer = new LiveNodeHtmlRenderer(dockerSeleniumRemoteProxy);
            // Render the nodes that are part of an specified test build
            if (testBuild.isEmpty() || testBuild.equalsIgnoreCase(dockerSeleniumRemoteProxy.getTestBuild())) {
                nodes.add(renderer.renderSummary());
            }
        }
    }
    int size = nodes.size();
    int rightColumnSize = size / 2;
    int leftColumnSize = size - rightColumnSize;
    StringBuilder leftColumnNodes = new StringBuilder();
    for (int i = 0; i < leftColumnSize; i++) {
        leftColumnNodes.append(nodes.get(i));
    }
    StringBuilder rightColumnNodes = new StringBuilder();
    for (int i = leftColumnSize; i < nodes.size(); i++) {
        rightColumnNodes.append(nodes.get(i));
    }
    Map<String, String> livePreviewValues = new HashMap<>();
    livePreviewValues.put("{{refreshInterval}}", refresh);
    livePreviewValues.put("{{leftColumnNodes}}", leftColumnNodes.toString());
    livePreviewValues.put("{{rightColumnNodes}}", rightColumnNodes.toString());
    String templateFile = "html_templates/live_preview_servlet.html";
    TemplateRenderer templateRenderer = new TemplateRenderer(templateFile);
    String renderTemplate = templateRenderer.renderTemplate(livePreviewValues);
    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    response.setStatus(200);
    try (InputStream in = new ByteArrayInputStream(renderTemplate.getBytes("UTF-8"))) {
        ByteStreams.copy(in, response.getOutputStream());
    } finally {
        response.getOutputStream().close();
    }
}
Also used : DockerSeleniumRemoteProxy(de.zalando.ep.zalenium.proxy.DockerSeleniumRemoteProxy) RemoteProxy(org.openqa.grid.internal.RemoteProxy) DockerSeleniumRemoteProxy(de.zalando.ep.zalenium.proxy.DockerSeleniumRemoteProxy) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) LiveNodeHtmlRenderer(de.zalando.ep.zalenium.servlet.renderer.LiveNodeHtmlRenderer) IOException(java.io.IOException) ByteArrayInputStream(java.io.ByteArrayInputStream) TemplateRenderer(de.zalando.ep.zalenium.servlet.renderer.TemplateRenderer) LiveNodeHtmlRenderer(de.zalando.ep.zalenium.servlet.renderer.LiveNodeHtmlRenderer) HtmlRenderer(org.openqa.grid.internal.utils.HtmlRenderer)

Aggregations

DockerSeleniumRemoteProxy (de.zalando.ep.zalenium.proxy.DockerSeleniumRemoteProxy)1 LiveNodeHtmlRenderer (de.zalando.ep.zalenium.servlet.renderer.LiveNodeHtmlRenderer)1 TemplateRenderer (de.zalando.ep.zalenium.servlet.renderer.TemplateRenderer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 RemoteProxy (org.openqa.grid.internal.RemoteProxy)1 HtmlRenderer (org.openqa.grid.internal.utils.HtmlRenderer)1