From 828ffa35841a585ddeeac9d934e3a4951e1702c0 Mon Sep 17 00:00:00 2001 From: ulrich Date: Mon, 28 Dec 2020 09:53:39 +0000 Subject: [PATCH] Von DefaultServlet auf HttpServlet umgestellt --- src/de/uhilger/wbx/web/AdocServlet.java | 46 +++++++++++++++++++++++++++++++++++----------- 1 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/de/uhilger/wbx/web/AdocServlet.java b/src/de/uhilger/wbx/web/AdocServlet.java index 7bb93b4..a80ba5c 100644 --- a/src/de/uhilger/wbx/web/AdocServlet.java +++ b/src/de/uhilger/wbx/web/AdocServlet.java @@ -22,6 +22,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; @@ -30,11 +31,13 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Map; +import javax.servlet.ServletOutputStream; import static org.asciidoctor.Asciidoctor.Factory.create; import org.asciidoctor.Asciidoctor; import static org.asciidoctor.AttributesBuilder.attributes; import static org.asciidoctor.OptionsBuilder.options; +import org.asciidoctor.SafeMode; @@ -89,23 +92,26 @@ wenn die Quelle sich geandert hat oder die PDF-Datei noch nicht existiert */ + String pdf = request.getParameter(PDF); if(null != pdf && pdf.equalsIgnoreCase(Boolean.TRUE.toString())) { File pdffile = new File(adocfile.getParentFile(), fname + DOT + PDF); outfile = pdffile; // PDF soll zurueckgegeben werden if(!pdffile.exists() || adocfile.lastModified() > pdffile.lastModified()) { + response.setContentType("application/pdf"); transform(absname, PDF); } - } - - try (PrintWriter out = response.getWriter()) { - // abhaengig vom Parameter pdf HTML- oder PDF-Datei ausgeben - FileInputStream in = new FileInputStream(outfile); - BufferedReader reader = new BufferedReader(new InputStreamReader(in)); - String line; - while ((line = reader.readLine()) != null) { - out.println(line); + ServletOutputStream os = response.getOutputStream(); + InputStream bytes = new FileInputStream(outfile); + int b = bytes.read(); + while(b > -1 ) { + os.write(b); + b = bytes.read(); } + } else { + PrintWriter out = response.getWriter(); + InputStreamReader in = new InputStreamReader(new FileInputStream(outfile), "UTF-8"); + in.transferTo(out); } } @@ -125,19 +131,37 @@ * nach PDF transformiert werden soll */ private void transform(String fileName, String backend) { - Map<String, Object> attributes = attributes().sourceHighlighter("highlightjs") - .asMap(); + Map<String, Object> attributes; + File outFile = new File(fileName); + String thisDirName = outFile.getParent(); + File pdfStyles = new File(outFile.getParentFile(), "custom-theme.yml"); + if(pdfStyles.exists()) { + attributes = attributes() + .attribute("pdf-themesdir", thisDirName) + .attribute("pdf-theme","custom") + .attribute("pdf-fontsdir", thisDirName + "/fonts") + .attribute("allow-uri-read") + .sourceHighlighter("highlightjs") + .asMap(); + } else { + attributes = attributes() + .sourceHighlighter("highlightjs") + .asMap(); + } Map<String, Object> options; if(null != backend) { options = options().inPlace(false) + .safe(SafeMode.SERVER) .backend(backend).attributes(attributes).asMap(); } else { options = options().inPlace(false) + .safe(SafeMode.SERVER) .attributes(attributes).asMap(); } Asciidoctor asciidoctor = create(); + asciidoctor.requireLibrary("asciidoctor-diagram"); asciidoctor.convertFile(new File(fileName), options); } -- Gitblit v1.9.3