Skip to content

Commit

Permalink
Restoring cluster-support being removed during latest upgrade (#67)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniele Romagnoli <daniele.romagnoli@geosolutionsgroup.com>
  • Loading branch information
tdipisa and dromagnoli authored May 13, 2024
1 parent 28a544a commit 0d24e8a
Showing 1 changed file with 95 additions and 17 deletions.
112 changes: 95 additions & 17 deletions src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -170,7 +185,7 @@ protected void createAndGetPDF(HttpServletRequest httpServletRequest, HttpServle
} catch (Throwable e) {
error(httpServletResponse, e);
} finally {
deleteFile(tempFile);
deleteTempFile(tempFile);
}
}

Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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")));
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -550,7 +628,7 @@ protected void purgeOldTemporaryFiles() {
while (it.hasNext()) {
Map.Entry<String, TempFile> entry = it.next();
if (entry.getValue().creationTime < minTime) {
deleteFile(entry.getValue());
deleteTempFile(entry.getValue());
it.remove();
}
}
Expand Down

0 comments on commit 0d24e8a

Please sign in to comment.