Merge branch 'BF-3913' of /home/git/repositories/brainfood/ofbiz-directcontrolservlet
Showing
2 changed files
with
71 additions
and
0 deletions
... | @@ -209,6 +209,14 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -209,6 +209,14 @@ public class DirectControlServlet extends HttpServlet { |
209 | } | 209 | } |
210 | } | 210 | } |
211 | 211 | ||
212 | // Check if there is an output handler | ||
213 | String outputHandler = "JSON"; | ||
214 | if (serviceName.indexOf("|") != -1) { | ||
215 | String[] parts = serviceName.split("\\|"); | ||
216 | serviceName = parts[0]; | ||
217 | outputHandler = parts[1]; | ||
218 | } | ||
219 | |||
212 | Debug.logInfo("Service name" +serviceName + " mapped for " + pathInfo + "#" + method, module); | 220 | Debug.logInfo("Service name" +serviceName + " mapped for " + pathInfo + "#" + method, module); |
213 | 221 | ||
214 | // If the sessionId parameter is set, attempt to look up the corresponding | 222 | // If the sessionId parameter is set, attempt to look up the corresponding |
... | @@ -266,6 +274,7 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -266,6 +274,7 @@ public class DirectControlServlet extends HttpServlet { |
266 | response.setStatus(400); | 274 | response.setStatus(400); |
267 | } | 275 | } |
268 | 276 | ||
277 | if ("JSON".equals(outputHandler)) { | ||
269 | response.setContentType("application/x-json"); | 278 | response.setContentType("application/x-json"); |
270 | 279 | ||
271 | PrintWriter writer = response.getWriter(); | 280 | PrintWriter writer = response.getWriter(); |
... | @@ -279,6 +288,11 @@ public class DirectControlServlet extends HttpServlet { | ... | @@ -279,6 +288,11 @@ public class DirectControlServlet extends HttpServlet { |
279 | writer.write(jsonStr); | 288 | writer.write(jsonStr); |
280 | writer.flush(); | 289 | writer.flush(); |
281 | writer.close(); | 290 | writer.close(); |
291 | } | ||
292 | |||
293 | if ("PDF".equals(outputHandler)) { | ||
294 | LibreOfficeRenderer.service(request, response, result); | ||
295 | } | ||
282 | } catch (Throwable t) { | 296 | } catch (Throwable t) { |
283 | response.setStatus(500); | 297 | response.setStatus(500); |
284 | PrintWriter writer = response.getWriter(); | 298 | 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 | import org.ofbiz.base.component.ComponentConfig; | ||
18 | import org.ofbiz.base.component.ComponentException; | ||
19 | |||
20 | import groovy.text.SimpleTemplateEngine; | ||
21 | import groovy.text.Template; | ||
22 | import groovy.lang.Writable; | ||
23 | |||
24 | public class LibreOfficeRenderer { | ||
25 | public static void service(HttpServletRequest request, HttpServletResponse response, Map<String, Object> context) | ||
26 | throws InterruptedException, IOException, ClassNotFoundException, ComponentException { | ||
27 | Object templateFile = context.get("_template"); | ||
28 | |||
29 | if (templateFile != null) { | ||
30 | String fullPath = ComponentConfig.getRootLocation("driverup") + "/pdf-templates/" + templateFile.toString(); | ||
31 | |||
32 | String template = UtilIO.readString(new FileInputStream(fullPath)); | ||
33 | |||
34 | // FIXME: This is a hack. The Libreoffice file contains a $ declaration and this fixes it. | ||
35 | context.put("Linux_X86_64", "$Linux_X86_64"); | ||
36 | context.put("Build", "$Build"); | ||
37 | |||
38 | SimpleTemplateEngine engine = new SimpleTemplateEngine(); | ||
39 | Writable stTemplate = engine.createTemplate(template).make(context); | ||
40 | String output = stTemplate.toString(); | ||
41 | |||
42 | File outputFile = File.createTempFile("bfdcs-", "-tmpl"); | ||
43 | UtilIO.writeString(outputFile, output); | ||
44 | |||
45 | List<String> args = Arrays.asList(new String[]{"libreoffice", "--headless", "--convert-to", "pdf", outputFile.getPath(), "--outdir", "/tmp"}); | ||
46 | Process process = new ProcessBuilder(args).start(); | ||
47 | InputStream is = process.getInputStream(); | ||
48 | process.waitFor(); | ||
49 | |||
50 | response.setContentType("application/pdf"); | ||
51 | UtilIO.copy(new FileInputStream(outputFile.getPath() + ".pdf"), true, response.getOutputStream(), true); | ||
52 | |||
53 | outputFile.delete(); | ||
54 | new File(outputFile.getPath() + ".pdf").delete(); | ||
55 | } | ||
56 | } | ||
57 | } |
-
Please register or sign in to post a comment