55e32ab2 by Ean Schuessler

Merge branch 'BF-3913' of /home/git/repositories/brainfood/ofbiz-directcontrolservlet

2 parents cd8ca01e 7646b262
......@@ -209,6 +209,14 @@ public class DirectControlServlet extends HttpServlet {
}
}
// Check if there is an output handler
String outputHandler = "JSON";
if (serviceName.indexOf("|") != -1) {
String[] parts = serviceName.split("\\|");
serviceName = parts[0];
outputHandler = parts[1];
}
Debug.logInfo("Service name" +serviceName + " mapped for " + pathInfo + "#" + method, module);
// If the sessionId parameter is set, attempt to look up the corresponding
......@@ -266,19 +274,25 @@ public class DirectControlServlet extends HttpServlet {
response.setStatus(400);
}
response.setContentType("application/x-json");
if ("JSON".equals(outputHandler)) {
response.setContentType("application/x-json");
PrintWriter writer = response.getWriter();
PrintWriter writer = response.getWriter();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new ISODateValueProcessor());
jsonConfig.registerJsonValueProcessor(Timestamp.class, new ISODateValueProcessor());
JSONObject json = JSONObject.fromObject(result, jsonConfig);
String jsonStr = json.toString();
response.setContentLength(jsonStr.getBytes("UTF8").length);
writer.write(jsonStr);
writer.flush();
writer.close();
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.registerJsonValueProcessor(Date.class, new ISODateValueProcessor());
jsonConfig.registerJsonValueProcessor(Timestamp.class, new ISODateValueProcessor());
JSONObject json = JSONObject.fromObject(result, jsonConfig);
String jsonStr = json.toString();
response.setContentLength(jsonStr.getBytes("UTF8").length);
writer.write(jsonStr);
writer.flush();
writer.close();
}
if ("PDF".equals(outputHandler)) {
LibreOfficeRenderer.service(request, response, result);
}
} catch (Throwable t) {
response.setStatus(500);
PrintWriter writer = response.getWriter();
......
package com.brainfood.ofbiz;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import org.ofbiz.base.util.UtilIO;
import org.ofbiz.base.component.ComponentConfig;
import org.ofbiz.base.component.ComponentException;
import groovy.text.SimpleTemplateEngine;
import groovy.text.Template;
import groovy.lang.Writable;
public class LibreOfficeRenderer {
public static void service(HttpServletRequest request, HttpServletResponse response, Map<String, Object> context)
throws InterruptedException, IOException, ClassNotFoundException, ComponentException {
Object templateFile = context.get("_template");
if (templateFile != null) {
String fullPath = ComponentConfig.getRootLocation("driverup") + "/pdf-templates/" + templateFile.toString();
String template = UtilIO.readString(new FileInputStream(fullPath));
// FIXME: This is a hack. The Libreoffice file contains a $ declaration and this fixes it.
context.put("Linux_X86_64", "$Linux_X86_64");
context.put("Build", "$Build");
SimpleTemplateEngine engine = new SimpleTemplateEngine();
Writable stTemplate = engine.createTemplate(template).make(context);
String output = stTemplate.toString();
File outputFile = File.createTempFile("bfdcs-", "-tmpl");
UtilIO.writeString(outputFile, output);
List<String> args = Arrays.asList(new String[]{"libreoffice", "--headless", "--convert-to", "pdf", outputFile.getPath(), "--outdir", "/tmp"});
Process process = new ProcessBuilder(args).start();
InputStream is = process.getInputStream();
process.waitFor();
response.setContentType("application/pdf");
UtilIO.copy(new FileInputStream(outputFile.getPath() + ".pdf"), true, response.getOutputStream(), true);
outputFile.delete();
new File(outputFile.getPath() + ".pdf").delete();
}
}
}