diff --git a/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java b/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java index adedd02674..e6b650bd5e 100644 --- a/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java +++ b/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java @@ -26,16 +26,19 @@ import org.pvalsecc.misc.FileUtilities; import org.mapfish.print.utils.PJsonObject; import org.mapfish.print.output.OutputFormat; +import org.mapfish.print.output.PdfOutputFactory; import org.mapfish.print.MapPrinter; import org.mapfish.print.Constants; import org.json.JSONWriter; - +import org.json.JSONObject; import org.json.JSONException; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; @@ -78,6 +81,8 @@ public class MapPrinterServlet extends BaseMapServlet { private static final int TEMP_FILE_PURGE_SECONDS = 10 * 60; private File tempDir = null; + private Boolean cluster = null; + private String encoding = null; /** * Tells if a thread is alread purging the old temporary files or not. @@ -122,14 +127,24 @@ public void init() throws ServletException { File dir = getTempDir(); File[] files = dir.listFiles(); for (File file : files) { - deleteFile(file); + deleteTempFile(file); + } + } + + protected void deleteTempFile(File tempFile) { + deleteFile(tempFile); + if(isCluster() && tempFile != null) { + File specFile = new File(tempFile.getAbsolutePath()+".json"); + if(specFile.exists()) { + deleteFile(specFile); + } } } public void destroy() { synchronized (tempFiles) { for (File file : tempFiles.values()) { - deleteFile(file); + deleteTempFile(file); } tempFiles.clear(); } @@ -170,7 +185,7 @@ protected void createAndGetPDF(HttpServletRequest httpServletRequest, HttpServle } catch (Throwable e) { error(httpServletResponse, e); } finally { - deleteFile(tempFile); + deleteTempFile(tempFile); } } @@ -188,8 +203,11 @@ protected void createPDF(HttpServletRequest httpServletRequest, HttpServletRespo error(httpServletResponse, "Missing 'spec' parameter", 500); return; } + if(isCluster()) { + doCreateSharedSpec(spec, tempFile); + } } catch (Throwable e) { - deleteFile(tempFile); + deleteTempFile(tempFile); error(httpServletResponse, e); return; } @@ -206,10 +224,10 @@ protected void createPDF(HttpServletRequest httpServletRequest, HttpServletRespo } json.endObject(); } catch (JSONException e) { - deleteFile(tempFile); + deleteTempFile(tempFile); throw new ServletException(e); } catch (IOException e) { - deleteFile(tempFile); + deleteTempFile(tempFile); throw new ServletException(e); } finally { if(writer != null) { @@ -219,6 +237,21 @@ protected void createPDF(HttpServletRequest httpServletRequest, HttpServletRespo addTempFile(tempFile, id); } + protected void doCreateSharedSpec(String spec, TempFile tempFile) throws IOException { + BufferedWriter out = null; + try { + File specFile = new File(tempFile.getAbsolutePath() + ".json"); + out = new BufferedWriter(new FileWriter(specFile)); + out.write(spec); + } catch (IOException e) { + deleteTempFile(tempFile); + throw e; + } finally { + if (out != null) + out.close(); + } + } + protected void addTempFile(TempFile tempFile, String id) { synchronized (tempFiles) { tempFiles.put(id, tempFile); @@ -262,12 +295,41 @@ protected String getEncoding() { */ protected void getFile(HttpServletRequest req, HttpServletResponse httpServletResponse, String id) throws IOException, ServletException { final TempFile file; - synchronized (tempFiles) { - file = tempFiles.get(id); - } - if (file == null) { - error(httpServletResponse, "File with id=" + id + " unknown", 404); - return; + if(isCluster()) { + File tempFileCandidate = new File(getTempDir() + File.separator + TEMP_FILE_PREFIX + id + TEMP_FILE_SUFFIX); + File specFile = new File(tempFileCandidate.getAbsolutePath() + ".json"); + if(tempFileCandidate.exists()) { + PJsonObject jsonSpec = null; + OutputFormat outputFormat = null; + if(specFile.exists()) { + //jsonSpec = getSpecJson(); + jsonSpec = MapPrinter.parseSpec(FileUtilities.readWholeTextFile(specFile)); + if (jsonSpec.has("app")) { + app = jsonSpec.getString("app"); + } else { + app = null; + } + MapPrinter mapPrinter = getMapPrinter(app); + outputFormat = mapPrinter.getOutputFormat(jsonSpec); + } else { + jsonSpec = new PJsonObject(new JSONObject(), "tempFile"); + outputFormat = new PdfOutputFactory(); + } + + file = new TempFile(tempFileCandidate, jsonSpec, outputFormat); + } else { + error(httpServletResponse, "File with id=" + id + " unknown", 404); + return; + } + + } else { + synchronized (tempFiles) { + file = tempFiles.get(id); + } + if (file == null) { + error(httpServletResponse, "File with id=" + id + " unknown", 404); + return; + } } sendPdfFile(httpServletResponse, file, Boolean.parseBoolean(req.getParameter("inline"))); } @@ -387,13 +449,13 @@ protected TempFile doCreatePDFFile(String spec, HttpServletRequest httpServletRe return tempFile; } catch (IOException e) { - deleteFile(tempFile); + deleteTempFile(tempFile); throw e; } catch (DocumentException e) { - deleteFile(tempFile); + deleteTempFile(tempFile); throw e; } catch (InterruptedException e) { - deleteFile(tempFile); + deleteTempFile(tempFile); throw e; } finally { if (out != null) { @@ -500,6 +562,22 @@ protected File getTempDir() { return tempDir; } + /** + * Get and cache the temporary directory to use for saving the generated PDF files. + */ + protected boolean isCluster() { + if (cluster == null) { + String clusterValue = getInitParameter("cluster"); + if (clusterValue != null) { + cluster = Boolean.valueOf(clusterValue); + } else { + cluster = false; + } + } + LOGGER.debug("Clustering " + (cluster ? "enabled" : "disabled")); + return cluster; + } + /** * If the file is defined, delete it. */ @@ -550,7 +628,7 @@ protected void purgeOldTemporaryFiles() { while (it.hasNext()) { Map.Entry entry = it.next(); if (entry.getValue().creationTime < minTime) { - deleteFile(entry.getValue()); + deleteTempFile(entry.getValue()); it.remove(); } }