From 4db3d0d9c7625b6d1b521158e7b9e900c667f286 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Thu, 15 Sep 2016 00:13:35 +0300 Subject: rendering help using pebble --- juick-www/src/main/java/com/juick/www/Help.java | 49 +++++++++++-------------- juick-www/src/main/java/com/juick/www/Main.java | 11 +++++- 2 files changed, 32 insertions(+), 28 deletions(-) (limited to 'juick-www/src/main/java/com/juick') diff --git a/juick-www/src/main/java/com/juick/www/Help.java b/juick-www/src/main/java/com/juick/www/Help.java index a8bfaf3f..301d2edc 100644 --- a/juick-www/src/main/java/com/juick/www/Help.java +++ b/juick-www/src/main/java/com/juick/www/Help.java @@ -17,12 +17,22 @@ */ package com.juick.www; +import com.mitchellbosecke.pebble.error.PebbleException; +import com.mitchellbosecke.pebble.template.PebbleTemplate; import org.springframework.jdbc.core.JdbcTemplate; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import java.io.*; +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; /** * @@ -34,7 +44,7 @@ public class Help { Utils.sendTemporaryRedirect(response, "/help/ru/"); } - protected void doGetHelp(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + protected void doGetHelp(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, PebbleException, URISyntaxException { com.juick.User visitor = Utils.getVisitorUser(sql, request, response); String path[] = request.getRequestURI().split("/"); @@ -54,37 +64,22 @@ public class Help { page = "index"; } - File f = new File("/var/www/juick.com/help/" + path[2] + "/" + page); - if (!f.isFile()) { + URL f = getClass().getResource("/help/" + path[2] + "/" + page); + if (f == null) { Errors.doGet404(sql, request, response); return; } response.setContentType("text/html; charset=UTF-8"); try (PrintWriter out = response.getWriter()) { - PageTemplates.pageHead(out, visitor, "Помощь", null); - PageTemplates.pageNavigation(out, visitor, null); - - out.println(""); - - out.println("
"); - out.println("
"); - printFile(out, f); - out.println("
"); - out.println("
"); - - PageTemplates.pageFooter(request, out, visitor, false); - PageTemplates.pageEnd(out); - } - } - - private void printFile(PrintWriter out, File f) throws IOException { - BufferedReader br = new BufferedReader(new FileReader(f)); - String str; - while ((str = br.readLine()) != null) { - out.println(str); + PebbleTemplate template = Utils.getEngine().getTemplate("views/help.html"); + Map context = new HashMap<>(); + context.put("title", "Помощь"); + context.put("visitor", visitor); + context.put("content", new String(Files.readAllBytes(Paths.get(f.toURI())))); + context.put("navigation", new String(Files.readAllBytes( + new File(getClass().getResource("/help/" + path[2] + "/navigation").toURI()).toPath()))); + template.evaluate(out, context); } } } diff --git a/juick-www/src/main/java/com/juick/www/Main.java b/juick-www/src/main/java/com/juick/www/Main.java index fa016edd..57704f36 100644 --- a/juick-www/src/main/java/com/juick/www/Main.java +++ b/juick-www/src/main/java/com/juick/www/Main.java @@ -38,6 +38,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.Socket; +import java.net.URISyntaxException; import java.net.URLEncoder; import java.util.Objects; import java.util.Properties; @@ -233,7 +234,15 @@ public class Main extends HttpServlet implements Stream.StreamListener { } else if (uri.equals("/help") || uri.equals("/help/")) { help.doRedirectToHelpIndex(response); } else if (uri.startsWith("/help/")) { - help.doGetHelp(sql, request, response); + try { + help.doGetHelp(sql, request, response); + } catch (PebbleException e) { + log("pebble exception", e); + response.sendError(500); + } catch (URISyntaxException e) { + log("help exception", e); + response.sendError(500); + } } else if (uri.startsWith("/tag/")) { discover.doGet(sql, sqlSearch, request, response); } else if (uri.matches("^/\\d+$")) { -- cgit v1.2.3