LibreOffice render helper #3913
Showing
2 changed files
with
68 additions
and
4 deletions
... | @@ -141,9 +141,7 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -141,9 +141,7 @@ public class DirectControlServlet extends HttpServlet { |
141 | } | 141 | } |
142 | } else { | 142 | } else { |
143 | // Check if the request is a backbone style "emulateJSON" request | 143 | // Check if the request is a backbone style "emulateJSON" request |
144 | if (contentType != null && | 144 | if (contentType != null && contentType.indexOf("x-www-form-urlencoded") != -1 && request.getParameter("model") != null) { |
145 | contentType.indexOf("x-www-form-urlencoded") != -1 && | ||
146 | request.getParameter("model") != null) { | ||
147 | Debug.logInfo("MODEL: " + request.getParameter("model"), module); | 145 | Debug.logInfo("MODEL: " + request.getParameter("model"), module); |
148 | JSON json = new JSON(new StringReader(request.getParameter("model"))); | 146 | JSON json = new JSON(new StringReader(request.getParameter("model"))); |
149 | Map<String,Object> items = json.JSONObject(); | 147 | Map<String,Object> items = json.JSONObject(); |
... | @@ -186,6 +184,14 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -186,6 +184,14 @@ public class DirectControlServlet extends HttpServlet { |
186 | } | 184 | } |
187 | } | 185 | } |
188 | 186 | ||
187 | // Check if there is an output handler | ||
188 | String outputHandler = "JSON"; | ||
189 | if (serviceName.indexOf("|") != -1) { | ||
190 | String[] parts = serviceName.split("\\|"); | ||
191 | serviceName = parts[0]; | ||
192 | outputHandler = parts[1]; | ||
193 | } | ||
194 | |||
189 | Debug.logInfo("Service name" +serviceName + " mapped for " + pathInfo + "#" + method, module); | 195 | Debug.logInfo("Service name" +serviceName + " mapped for " + pathInfo + "#" + method, module); |
190 | 196 | ||
191 | // If the sessionId parameter is set, attempt to look up the corresponding | 197 | // If the sessionId parameter is set, attempt to look up the corresponding |
... | @@ -243,6 +249,7 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -243,6 +249,7 @@ public class DirectControlServlet extends HttpServlet { |
243 | response.setStatus(400); | 249 | response.setStatus(400); |
244 | } | 250 | } |
245 | 251 | ||
252 | if ("JSON".equals(outputHandler)) { | ||
246 | response.setContentType("application/x-json"); | 253 | response.setContentType("application/x-json"); |
247 | 254 | ||
248 | PrintWriter writer = response.getWriter(); | 255 | PrintWriter writer = response.getWriter(); |
... | @@ -254,9 +261,13 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -254,9 +261,13 @@ public class DirectControlServlet extends HttpServlet { |
254 | String jsonStr = json.toString(); | 261 | String jsonStr = json.toString(); |
255 | response.setContentLength(jsonStr.getBytes("UTF8").length); | 262 | response.setContentLength(jsonStr.getBytes("UTF8").length); |
256 | writer.write(jsonStr); | 263 | writer.write(jsonStr); |
257 | |||
258 | writer.flush(); | 264 | writer.flush(); |
259 | writer.close(); | 265 | writer.close(); |
266 | } | ||
267 | |||
268 | if ("PDF".equals(outputHandler)) { | ||
269 | LibreOfficeRenderer.service(request, response, result); | ||
270 | } | ||
260 | } catch (Throwable t) { | 271 | } catch (Throwable t) { |
261 | response.setStatus(500); | 272 | response.setStatus(500); |
262 | PrintWriter writer = response.getWriter(); | 273 | PrintWriter writer = response.getWriter(); | ... | ... |
1 | package com.brainfood.ofbiz; | ||
2 | |||
3 | import javax.servlet.http.HttpServletRequest; | ||
4 | import javax.servlet.http.HttpServletResponse; | ||
5 | |||
6 | import java.util.Map; | ||
7 | import java.util.List; | ||
8 | import java.util.ArrayList; | ||
9 | import java.util.Arrays; | ||
10 | |||
11 | import java.io.File; | ||
12 | import java.io.InputStream; | ||
13 | import java.io.FileInputStream; | ||
14 | import java.io.IOException; | ||
15 | |||
16 | import org.ofbiz.base.util.UtilIO; | ||
17 | |||
18 | import groovy.text.SimpleTemplateEngine; | ||
19 | import groovy.text.Template; | ||
20 | import groovy.lang.Writable; | ||
21 | |||
22 | public class LibreOfficeRenderer { | ||
23 | public static void service(HttpServletRequest request, HttpServletResponse response, Map<String, Object> context) | ||
24 | throws InterruptedException, IOException, ClassNotFoundException { | ||
25 | Object templateFile = context.get("_template"); | ||
26 | |||
27 | if (templateFile != null) { | ||
28 | String template = UtilIO.readString(new FileInputStream(templateFile.toString())); | ||
29 | |||
30 | // FIXME: This is a hack. The Libreoffice file contains a $ declaration and this fixes it. | ||
31 | context.put("Linux_X86_64", "$Linux_X86_64"); | ||
32 | context.put("Build", "$Build"); | ||
33 | |||
34 | SimpleTemplateEngine engine = new SimpleTemplateEngine(); | ||
35 | Writable stTemplate = engine.createTemplate(template).make(context); | ||
36 | String output = stTemplate.toString(); | ||
37 | |||
38 | File outputFile = File.createTempFile("bfdcs-", "-tmpl"); | ||
39 | UtilIO.writeString(outputFile, output); | ||
40 | |||
41 | List<String> args = Arrays.asList(new String[]{"libreoffice", "--headless", "--convert-to", "pdf", outputFile.getPath(), "--outdir", "/tmp"}); | ||
42 | Process process = new ProcessBuilder(args).start(); | ||
43 | InputStream is = process.getInputStream(); | ||
44 | process.waitFor(); | ||
45 | |||
46 | response.setContentType("application/pdf"); | ||
47 | UtilIO.copy(new FileInputStream(outputFile.getPath() + ".pdf"), true, response.getOutputStream(), true); | ||
48 | |||
49 | outputFile.delete(); | ||
50 | new File(outputFile.getPath() + ".pdf").delete(); | ||
51 | } | ||
52 | } | ||
53 | } |
-
Please register or sign in to post a comment