edea003b by Adam Heath

Allow for a "templateLocation" to be used instead of just "template";

this is the first step towards removing the driverup hard-coded values.
1 parent f55c2104
...@@ -3,6 +3,7 @@ package com.brainfood.ofbiz; ...@@ -3,6 +3,7 @@ package com.brainfood.ofbiz;
3 import javax.servlet.http.HttpServletRequest; 3 import javax.servlet.http.HttpServletRequest;
4 import javax.servlet.http.HttpServletResponse; 4 import javax.servlet.http.HttpServletResponse;
5 5
6 import java.util.HashMap;
6 import java.util.Map; 7 import java.util.Map;
7 import java.util.List; 8 import java.util.List;
8 import java.util.ArrayList; 9 import java.util.ArrayList;
...@@ -16,6 +17,7 @@ import java.io.IOException; ...@@ -16,6 +17,7 @@ import java.io.IOException;
16 import org.ofbiz.base.util.UtilIO; 17 import org.ofbiz.base.util.UtilIO;
17 import org.ofbiz.base.component.ComponentConfig; 18 import org.ofbiz.base.component.ComponentConfig;
18 import org.ofbiz.base.component.ComponentException; 19 import org.ofbiz.base.component.ComponentException;
20 import org.ofbiz.base.location.FlexibleLocation;
19 21
20 import groovy.text.SimpleTemplateEngine; 22 import groovy.text.SimpleTemplateEngine;
21 import groovy.text.Template; 23 import groovy.text.Template;
...@@ -24,13 +26,25 @@ import groovy.lang.Writable; ...@@ -24,13 +26,25 @@ import groovy.lang.Writable;
24 public class LibreOfficeRenderer { 26 public class LibreOfficeRenderer {
25 public static void service(HttpServletRequest request, HttpServletResponse response, Map<String, Object> context) 27 public static void service(HttpServletRequest request, HttpServletResponse response, Map<String, Object> context)
26 throws InterruptedException, IOException, ClassNotFoundException, ComponentException { 28 throws InterruptedException, IOException, ClassNotFoundException, ComponentException {
29 String templateLocation = (String) context.get("_templateLocation");
27 Object templateFile = context.get("_template"); 30 Object templateFile = context.get("_template");
28 31
32 Map<String, String> envVars = new HashMap<String, String>();
33
34 String template;
29 if (templateFile != null) { 35 if (templateFile != null) {
30 String fullPath = ComponentConfig.getRootLocation("driverup") + "/pdf-templates/" + templateFile.toString(); 36 String fullPath = ComponentConfig.getRootLocation("driverup") + "/pdf-templates/" + templateFile.toString();
31 37 template = UtilIO.readString(new FileInputStream(fullPath));
32 String template = UtilIO.readString(new FileInputStream(fullPath)); 38 envVars.put("HOME", "/home/driverup");
33 39 envVars.put("USERNAME", "driverup");
40 } else if (templateLocation != null) {
41 template = UtilIO.readString(FlexibleLocation.resolveLocation(templateLocation).openConnection().getInputStream());
42 envVars.put("HOME", "/home/ofbiz");
43 envVars.put("USERNAME", "ofbiz");
44 } else {
45 template = null;
46 }
47 if (template != null) {
34 // FIXME: This is a hack. The Libreoffice file contains a $ declaration and this fixes it. 48 // FIXME: This is a hack. The Libreoffice file contains a $ declaration and this fixes it.
35 context.put("Linux_X86_64", "$Linux_X86_64"); 49 context.put("Linux_X86_64", "$Linux_X86_64");
36 context.put("Build", "$Build"); 50 context.put("Build", "$Build");
...@@ -53,8 +67,7 @@ public class LibreOfficeRenderer { ...@@ -53,8 +67,7 @@ public class LibreOfficeRenderer {
53 67
54 ProcessBuilder pb = new ProcessBuilder("libreoffice", "--headless", "--convert-to", "pdf", outputFile.getPath(), "--outdir", outputFile.getParentFile().getPath()); 68 ProcessBuilder pb = new ProcessBuilder("libreoffice", "--headless", "--convert-to", "pdf", outputFile.getPath(), "--outdir", outputFile.getParentFile().getPath());
55 Map<String, String> env = pb.environment(); 69 Map<String, String> env = pb.environment();
56 env.put("HOME", "/home/driverup"); 70 env.putAll(envVars);
57 env.put("USERNAME", "driverup");
58 Process process = pb.start(); 71 Process process = pb.start();
59 InputStream is = process.getInputStream(); 72 InputStream is = process.getInputStream();
60 process.waitFor(); 73 process.waitFor();
......