From ca4941c93db6a5fa1deca6d38ac3edfe4fb005b4 Mon Sep 17 00:00:00 2001 From: ritika-t-thakur-alb Date: Thu, 3 Oct 2024 09:48:32 +0100 Subject: [PATCH 1/3] Adding Support for reading the tempfile from the disk --- pom.xml | 2 +- .../print/servlet/MapPrinterServlet.java | 252 ++++++++++-------- .../print/servlet/MapPrinterServletTest.java | 4 +- 3 files changed, 145 insertions(+), 113 deletions(-) diff --git a/pom.xml b/pom.xml index deaa63a..e365b44 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.mapfish.print print-lib - 2.3.1 + 2.3.100-SNAPSHOT MapFish Print Version 2 has reached end-of-life and is no longer under active development. diff --git a/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java b/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java index 68712fb..50e2982 100644 --- a/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java +++ b/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java @@ -19,20 +19,12 @@ package org.mapfish.print.servlet; +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.io.CharStreams; import com.google.common.io.Closer; -import org.apache.logging.log4j.Level; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.LogManager; -import org.pvalsecc.misc.FileUtilities; -import org.mapfish.print.utils.PJsonObject; -import org.mapfish.print.output.OutputFormat; -import org.mapfish.print.MapPrinter; -import org.mapfish.print.Constants; -import org.json.JSONWriter; - -import org.json.JSONException; - +import com.lowagie.text.DocumentException; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; @@ -43,6 +35,10 @@ import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; +import java.nio.file.attribute.FileTime; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; @@ -50,44 +46,53 @@ import java.util.Iterator; import java.util.Map; import java.util.TreeSet; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; import java.util.regex.Pattern; - import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; - -import com.lowagie.text.DocumentException; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.json.JSONException; +import org.json.JSONWriter; +import org.mapfish.print.Constants; +import org.mapfish.print.MapPrinter; +import org.mapfish.print.output.OutputFormat; +import org.mapfish.print.utils.PJsonObject; +import org.pvalsecc.misc.FileUtilities; /** * Main print servlet. */ public class MapPrinterServlet extends BaseMapServlet { public static final Logger SPEC_LOGGER = LogManager.getLogger(BaseMapServlet.class.getPackage().toString() + ".spec"); + protected static final String TEMP_FILE_PREFIX = "mapfish-print"; + protected static final String TEMP_FILE_METADATA_PREFIX = "mapfish-print-metadata"; private static final long serialVersionUID = -4706371598927161642L; private static final String CONTEXT_TEMPDIR = "javax.servlet.context.tempdir"; - private static final String INFO_URL = "/info.json"; private static final String PRINT_URL = "/print.pdf"; private static final String CREATE_URL = "/create.json"; - protected static final String TEMP_FILE_PREFIX = "mapfish-print"; private static final String TEMP_FILE_SUFFIX = ".printout"; - - private String app = null; - private static final int TEMP_FILE_PURGE_SECONDS = 10 * 60; - + /** + * Map of temporary files. + */ + private final ObjectMapper objectMapper = new ObjectMapper(); + /** + * Map of temporary files. + */ + private final Map tempFilesMetadataCache = new ConcurrentHashMap(); + private String app = null; private File tempDir = null; private String encoding = null; /** * Tells if a thread is alread purging the old temporary files or not. */ private AtomicBoolean purging = new AtomicBoolean(false); - /** - * Map of temporary files. - */ - private final Map tempFiles = new HashMap(); protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException { //do the routing in function of the actual URL @@ -123,17 +128,24 @@ public void init() throws ServletException { File dir = getTempDir(); File[] files = dir.listFiles(); for (File file : files) { - deleteFile(file); + try { + if (shouldFileBeDelete(file)) { + deleteFile(file); + } + } catch (IOException e) { + LOGGER.debug("Unable to handle file :: ", e); + } } } + public boolean shouldFileBeDelete(File file) throws IOException { + BasicFileAttributes attr = Files.readAttributes(Path.of(file.getPath()), BasicFileAttributes.class); + FileTime creationTime = attr.creationTime(); + final long minTime = System.currentTimeMillis() - TEMP_FILE_PURGE_SECONDS * 1000L; + return creationTime.toMillis() < minTime && (file.getName().startsWith(TEMP_FILE_PREFIX) || file.getName().startsWith(TEMP_FILE_METADATA_PREFIX)) && file.isFile(); + } + public void destroy() { - synchronized (tempFiles) { - for (File file : tempFiles.values()) { - deleteFile(file); - } - tempFiles.clear(); - } super.destroy(); } @@ -143,7 +155,7 @@ public void destroy() { */ protected void createAndGetPDF(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { //get the spec from the query - TempFile tempFile = null; + TempFileMetadata tempFileMetadata = null; String spec = null; try { httpServletRequest.setCharacterEncoding("UTF-8"); @@ -166,12 +178,12 @@ protected void createAndGetPDF(HttpServletRequest httpServletRequest, HttpServle } try { - tempFile = doCreatePDFFile(spec, httpServletRequest); - sendPdfFile(httpServletResponse, tempFile, Boolean.parseBoolean(httpServletRequest.getParameter("inline"))); + tempFileMetadata = doCreatePDFFile(spec, httpServletRequest); + sendPdfFile(httpServletResponse, tempFileMetadata, Boolean.parseBoolean(httpServletRequest.getParameter("inline"))); } catch (Throwable e) { error(httpServletResponse, e); } finally { - deleteFile(tempFile); + deleteFile(tempFileMetadata.tempFile); } } @@ -179,23 +191,23 @@ protected void createAndGetPDF(HttpServletRequest httpServletRequest, HttpServle * Create the PDF and returns to the client (in JSON) the URL to get the PDF. */ protected void createPDF(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, String basePath) throws ServletException { - TempFile tempFile = null; + TempFileMetadata tempFileMetadata = null; try { purgeOldTemporaryFiles(); String spec = getSpecFromPostBody(httpServletRequest); - tempFile = doCreatePDFFile(spec, httpServletRequest); - if (tempFile == null) { + tempFileMetadata = doCreatePDFFile(spec, httpServletRequest); + if (tempFileMetadata == null) { error(httpServletResponse, "Missing 'spec' parameter", 500); return; } } catch (Throwable e) { - deleteFile(tempFile); + deleteFile(tempFileMetadata.tempFile); error(httpServletResponse, e); return; } - final String id = generateId(tempFile); + final String id = generateId(tempFileMetadata.tempFile); httpServletResponse.setContentType("application/json; charset=utf-8"); PrintWriter writer = null; try { @@ -207,27 +219,31 @@ protected void createPDF(HttpServletRequest httpServletRequest, HttpServletRespo } json.endObject(); } catch (JSONException e) { - deleteFile(tempFile); + deleteFile(tempFileMetadata.tempFile); throw new ServletException(e); } catch (IOException e) { - deleteFile(tempFile); + deleteFile(tempFileMetadata.tempFile); throw new ServletException(e); } finally { - if(writer != null) { + if (writer != null) { writer.close(); } } - addTempFile(tempFile, id); + addTempFileMetaData(tempFileMetadata, id); } - protected void addTempFile(TempFile tempFile, String id) { - synchronized (tempFiles) { - tempFiles.put(id, tempFile); + protected void addTempFileMetaData(TempFileMetadata tempFileMetadata, String id) { + try { + objectMapper.writeValue(File.createTempFile(TEMP_FILE_METADATA_PREFIX + id + "_", + ".json", getTempDir()), tempFileMetadata); + tempFilesMetadataCache.put(id, tempFileMetadata); + } catch (IOException e) { + LOGGER.debug("Unable to persist tempFileMetadata", e); } } protected String getSpecFromPostBody(HttpServletRequest httpServletRequest) throws IOException { - if(httpServletRequest.getParameter("spec") != null) { + if (httpServletRequest.getParameter("spec") != null) { return httpServletRequest.getParameter("spec"); } BufferedReader data = new BufferedReader(new InputStreamReader(httpServletRequest.getInputStream(), StandardCharsets.UTF_8)); @@ -242,14 +258,14 @@ protected String getSpecFromPostBody(HttpServletRequest httpServletRequest) thro closer.close(); } } - + /** * Get and cache the used Encoding. */ protected String getEncoding() { if (encoding == null) { - encoding = getInitParameter("encoding"); - LOGGER.debug("Using '" + encoding + "' to encode Inputcontent."); + encoding = getInitParameter("encoding"); + LOGGER.debug("Using '" + encoding + "' to encode Inputcontent."); } if (encoding == null) { return "UTF-8"; @@ -262,15 +278,24 @@ protected String getEncoding() { * To get the PDF created previously. */ protected void getFile(HttpServletRequest req, HttpServletResponse httpServletResponse, String id) throws IOException, ServletException { - final TempFile file; - synchronized (tempFiles) { - file = tempFiles.get(id); - } - if (file == null) { + TempFileMetadata tempFileMetadata = getTempFileMetadata(id); + if (tempFileMetadata == null) { error(httpServletResponse, "File with id=" + id + " unknown", 404); return; } - sendPdfFile(httpServletResponse, file, Boolean.parseBoolean(req.getParameter("inline"))); + sendPdfFile(httpServletResponse, tempFileMetadata, Boolean.parseBoolean(req.getParameter("inline"))); + } + + private TempFileMetadata getTempFileMetadata(String id) throws IOException { + TempFileMetadata tempFileMetadata = tempFilesMetadataCache.get(id); + if (tempFileMetadata == null) { + File[] files = getTempDir().listFiles(file -> file.getName().startsWith(TEMP_FILE_METADATA_PREFIX + id + "_")); + if (0 != files.length) { + tempFileMetadata = objectMapper.readValue(files[0], TempFileMetadata.class); + tempFilesMetadataCache.put(id, tempFileMetadata); + } + } + return tempFileMetadata; } /** @@ -325,12 +350,11 @@ protected void getInfo(HttpServletRequest req, HttpServletResponse resp, String } } - /** * Do the actual work of creating the PDF temporary file. * @throws InterruptedException */ - protected TempFile doCreatePDFFile(String spec, HttpServletRequest httpServletRequest) throws IOException, DocumentException, ServletException, InterruptedException { + protected TempFileMetadata doCreatePDFFile(String spec, HttpServletRequest httpServletRequest) throws IOException, DocumentException, ServletException, InterruptedException { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Generating PDF for spec=" + spec); } @@ -355,7 +379,7 @@ protected TempFile doCreatePDFFile(String spec, HttpServletRequest httpServletRe configHeaders.add("Referer"); configHeaders.add("Cookie"); } - for (Iterator header_iter = configHeaders.iterator(); header_iter.hasNext();) { + for (Iterator header_iter = configHeaders.iterator(); header_iter.hasNext(); ) { String header = header_iter.next(); if (httpServletRequest.getHeader(header) != null) { headers.put(header, httpServletRequest.getHeader(header)); @@ -366,18 +390,18 @@ protected TempFile doCreatePDFFile(String spec, HttpServletRequest httpServletRe // create a temporary file that will contain the PDF final File tempJavaFile = File.createTempFile(TEMP_FILE_PREFIX, "." + outputFormat.getFileSuffix() + TEMP_FILE_SUFFIX, getTempDir()); - TempFile tempFile = new TempFile(tempJavaFile, specJson, outputFormat); + TempFileMetadata tempFileMetadata = new TempFileMetadata(tempJavaFile, specJson, outputFormat); FileOutputStream out = null; try { - out = new FileOutputStream(tempFile); - if(mapPrinter.getConfig().isAddForwardedFor()) { - String ipAddress = httpServletRequest.getHeader("X-FORWARDED-FOR"); + out = new FileOutputStream(tempFileMetadata.tempFile); + if (mapPrinter.getConfig().isAddForwardedFor()) { + String ipAddress = httpServletRequest.getHeader("X-FORWARDED-FOR"); if (ipAddress != null) { String[] ips = ipAddress.split(", "); ipAddress = ips[0]; } else { - ipAddress = httpServletRequest.getRemoteAddr(); + ipAddress = httpServletRequest.getRemoteAddr(); } if (LOGGER.isDebugEnabled()) { LOGGER.debug("Forwarded for: " + ipAddress); @@ -386,15 +410,15 @@ protected TempFile doCreatePDFFile(String spec, HttpServletRequest httpServletRe } mapPrinter.print(specJson, out, headers); - return tempFile; + return tempFileMetadata; } catch (IOException e) { - deleteFile(tempFile); + deleteFile(tempFileMetadata.tempFile); throw e; } catch (DocumentException e) { - deleteFile(tempFile); + deleteFile(tempFileMetadata.tempFile); throw e; } catch (InterruptedException e) { - deleteFile(tempFile); + deleteFile(tempFileMetadata.tempFile); throw e; } finally { if (out != null) { @@ -411,14 +435,14 @@ protected TempFile doCreatePDFFile(String spec, HttpServletRequest httpServletRe /** * copy the PDF into the output stream */ - protected void sendPdfFile(HttpServletResponse httpServletResponse, TempFile tempFile, boolean inline) throws IOException, ServletException { - FileInputStream pdf = new FileInputStream(tempFile); + protected void sendPdfFile(HttpServletResponse httpServletResponse, TempFileMetadata tempFileMetadata, boolean inline) throws IOException, ServletException { + FileInputStream pdf = new FileInputStream(tempFileMetadata.tempFile); final OutputStream response = httpServletResponse.getOutputStream(); MapPrinter mapPrinter = getMapPrinter(app); try { - httpServletResponse.setContentType(tempFile.contentType()); + httpServletResponse.setContentType(tempFileMetadata.contentType()); if (!inline) { - final String fileName = tempFile.getOutputFileName(mapPrinter); + final String fileName = tempFileMetadata.getOutputFileName(mapPrinter); httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + fileName); } FileUtilities.copyStream(pdf, response); @@ -485,7 +509,7 @@ protected File getTempDir() { if (tempDir == null) { String tempDirPath = getInitParameter("tempdir"); if (tempDirPath == null) { - tempDirPath = System.getProperty("MAPFISH_PDF_FOLDER"); + tempDirPath = System.getProperty("MAPFISH_PDF_FOLDER"); } if (tempDirPath != null && !"".equals(tempDirPath.trim())) { tempDir = new File(tempDirPath); @@ -532,7 +556,7 @@ protected String getBaseUrl(HttpServletRequest httpServletRequest) { return fullUrl.replaceFirst(additionalPath + "$", ""); } else { String customUrl = System.getProperty("PRINT_BASE_URL"); - if(customUrl != null && !"".equals(customUrl.trim())) { + if (customUrl != null && !"".equals(customUrl.trim())) { return customUrl.replaceFirst(additionalPath + "$", ""); } else { return httpServletRequest.getRequestURL().toString().replaceFirst(additionalPath + "$", ""); @@ -545,73 +569,73 @@ protected String getBaseUrl(HttpServletRequest httpServletRequest) { */ protected void purgeOldTemporaryFiles() { if (!purging.getAndSet(true)) { - final long minTime = System.currentTimeMillis() - TEMP_FILE_PURGE_SECONDS * 1000L; - synchronized (tempFiles) { - Iterator> it = tempFiles.entrySet().iterator(); - while (it.hasNext()) { - Map.Entry entry = it.next(); - if (entry.getValue().creationTime < minTime) { - deleteFile(entry.getValue()); - it.remove(); + File[] tempDirFiles = getTempDir().listFiles(); + for (File file : tempDirFiles) { + try { + if (shouldFileBeDelete(file)) { + String fileName = file.getName(); + tempFilesMetadataCache.remove(fileName.substring(TEMP_FILE_METADATA_PREFIX.length(), fileName.indexOf("_"))); + deleteFile(file); } + } catch (IOException e) { + LOGGER.warn("Unable to delete file :: ", e); } } - purging.set(false); } + purging.set(false); } - static class TempFile extends File { + static class TempFileMetadata { private static final long serialVersionUID = 455104129549002361L; - private final long creationTime; public final String printedLayoutName; public final String outputFileName; - private final String contentType; - private String suffix; + public final String contentType; + private final File tempFile; + public String suffix; - public TempFile(File tempFile, PJsonObject jsonSpec, OutputFormat format) { - super(tempFile.getAbsolutePath()); - creationTime = System.currentTimeMillis(); + public TempFileMetadata(File tempFile, PJsonObject jsonSpec, OutputFormat format) { this.outputFileName = jsonSpec.optString(Constants.OUTPUT_FILENAME_KEY); this.printedLayoutName = jsonSpec.optString(Constants.JSON_LAYOUT_KEY, null); - + this.tempFile = tempFile; this.suffix = format.getFileSuffix(); this.contentType = format.getContentType(); } - public String getOutputFileName(MapPrinter mapPrinter) { - if(outputFileName != null) { - return formatFileName(suffix, outputFileName, new Date()); - } else { - return formatFileName(suffix, mapPrinter.getOutputFilename(printedLayoutName, getName()), new Date()); - } + @JsonCreator + public TempFileMetadata(@JsonProperty("tempFile") File tempFile, @JsonProperty("outputFileName") String outputFileName, @JsonProperty("printedLayoutName") String printedLayoutName, + @JsonProperty("suffix") String suffix, @JsonProperty("contentType") String contentType) { + this.outputFileName = outputFileName; + this.printedLayoutName = printedLayoutName; + this.tempFile = tempFile; + this.suffix = suffix; + this.contentType = contentType; } - public static String formatFileName(String suffix, String startingName, Date date) { Matcher matcher = Pattern.compile("\\$\\{(.+?)\\}").matcher(startingName); - HashMap replacements = new HashMap(); - while(matcher.find()) { + HashMap replacements = new HashMap(); + while (matcher.find()) { String pattern = matcher.group(1); - String key = "${"+pattern+"}"; + String key = "${" + pattern + "}"; replacements.put(key, findReplacement(pattern, date)); } String result = startingName; - for(Map.Entry entry: replacements.entrySet()) { + for (Map.Entry entry : replacements.entrySet()) { result = result.replace(entry.getKey(), entry.getValue()); } - while(suffix.startsWith(".")) { + while (suffix.startsWith(".")) { suffix = suffix.substring(1); } - if(suffix.isEmpty() || result.toLowerCase().endsWith("."+suffix.toLowerCase())) { + if (suffix.isEmpty() || result.toLowerCase().endsWith("." + suffix.toLowerCase())) { return result; } else { - return result+"."+suffix; + return result + "." + suffix; } } public static String cleanUpName(String original) { - return original.replace(",","").replaceAll("\\s+", "_"); + return original.replace(",", "").replaceAll("\\s+", "_"); } private static String findReplacement(String pattern, Date date) { @@ -625,12 +649,20 @@ private static String findReplacement(String pattern, Date date) { try { return new SimpleDateFormat(pattern).format(date); } catch (Exception e) { - LOGGER.log(Level.WARN,String.format("Unable to format timestamp according to pattern: ${%s}",pattern), e); - return "${"+pattern+"}"; + LOGGER.log(Level.WARN, String.format("Unable to format timestamp according to pattern: ${%s}", pattern), e); + return "${" + pattern + "}"; } } } + public String getOutputFileName(MapPrinter mapPrinter) { + if (outputFileName != null) { + return formatFileName(suffix, outputFileName, new Date()); + } else { + return formatFileName(suffix, mapPrinter.getOutputFilename(printedLayoutName, tempFile.getName()), new Date()); + } + } + public String contentType() { return contentType; } diff --git a/src/test/java/org/mapfish/print/servlet/MapPrinterServletTest.java b/src/test/java/org/mapfish/print/servlet/MapPrinterServletTest.java index e70976b..69851ad 100644 --- a/src/test/java/org/mapfish/print/servlet/MapPrinterServletTest.java +++ b/src/test/java/org/mapfish/print/servlet/MapPrinterServletTest.java @@ -1,7 +1,7 @@ package org.mapfish.print.servlet; import static org.junit.Assert.assertEquals; -import static org.mapfish.print.servlet.MapPrinterServlet.TempFile.cleanUpName; +import static org.mapfish.print.servlet.MapPrinterServlet.TempFileMetadata.cleanUpName; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -46,7 +46,7 @@ public void addSuffixTest() { } private void assertExpectedFormat(Date date, String expected, String fileName, String suffix) { - assertEquals(expected, MapPrinterServlet.TempFile.formatFileName(suffix, fileName, date)); + assertEquals(expected, MapPrinterServlet.TempFileMetadata.formatFileName(suffix, fileName, date)); } } From fc737830401ff7f970096e30bc412e6b8003732c Mon Sep 17 00:00:00 2001 From: ritika-t-thakur-alb Date: Fri, 4 Oct 2024 10:55:29 +0100 Subject: [PATCH 2/3] Adding Support for reading the tempfile from the disk Removing the caching mechanism --- .../print/servlet/MapPrinterServlet.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java b/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java index 50e2982..a194a68 100644 --- a/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java +++ b/src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java @@ -85,7 +85,6 @@ public class MapPrinterServlet extends BaseMapServlet { /** * Map of temporary files. */ - private final Map tempFilesMetadataCache = new ConcurrentHashMap(); private String app = null; private File tempDir = null; private String encoding = null; @@ -232,11 +231,15 @@ protected void createPDF(HttpServletRequest httpServletRequest, HttpServletRespo addTempFileMetaData(tempFileMetadata, id); } + /** + * Creates a json file to hold the meta-data for the temp file. + * @param tempFileMetadata Object holding the information regarding the file to be printed + * @param id identifier for the temp file and is being used to identify the meta data file + */ protected void addTempFileMetaData(TempFileMetadata tempFileMetadata, String id) { try { objectMapper.writeValue(File.createTempFile(TEMP_FILE_METADATA_PREFIX + id + "_", ".json", getTempDir()), tempFileMetadata); - tempFilesMetadataCache.put(id, tempFileMetadata); } catch (IOException e) { LOGGER.debug("Unable to persist tempFileMetadata", e); } @@ -286,15 +289,24 @@ protected void getFile(HttpServletRequest req, HttpServletResponse httpServletRe sendPdfFile(httpServletResponse, tempFileMetadata, Boolean.parseBoolean(req.getParameter("inline"))); } - private TempFileMetadata getTempFileMetadata(String id) throws IOException { - TempFileMetadata tempFileMetadata = tempFilesMetadataCache.get(id); - if (tempFileMetadata == null) { + /** + * + * @param id identifier to fetch the meta-data json file from the disk. + * @return + */ + private TempFileMetadata getTempFileMetadata(String id) { + TempFileMetadata tempFileMetadata = null; + // get from disk File[] files = getTempDir().listFiles(file -> file.getName().startsWith(TEMP_FILE_METADATA_PREFIX + id + "_")); if (0 != files.length) { - tempFileMetadata = objectMapper.readValue(files[0], TempFileMetadata.class); - tempFilesMetadataCache.put(id, tempFileMetadata); + try { + tempFileMetadata = objectMapper.readValue(files[0], TempFileMetadata.class); + } catch (IOException e) { + // could be deleted while the reading has not started + LOGGER.info("File requested for the id ::" + id + " has been deleted"); + tempFileMetadata = null; + } } - } return tempFileMetadata; } @@ -573,8 +585,6 @@ protected void purgeOldTemporaryFiles() { for (File file : tempDirFiles) { try { if (shouldFileBeDelete(file)) { - String fileName = file.getName(); - tempFilesMetadataCache.remove(fileName.substring(TEMP_FILE_METADATA_PREFIX.length(), fileName.indexOf("_"))); deleteFile(file); } } catch (IOException e) { @@ -590,7 +600,7 @@ static class TempFileMetadata { public final String printedLayoutName; public final String outputFileName; public final String contentType; - private final File tempFile; + public final File tempFile; public String suffix; public TempFileMetadata(File tempFile, PJsonObject jsonSpec, OutputFormat format) { From d7cfbb41ed95ac3d51653557c30a0146d5996868 Mon Sep 17 00:00:00 2001 From: ritika-t-thakur-alb Date: Fri, 4 Oct 2024 12:20:09 +0100 Subject: [PATCH 3/3] Adding Support for reading the tempfile from the disk Fixing the mistake of version changes --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index e365b44..61b36d6 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.mapfish.print print-lib - 2.3.100-SNAPSHOT + 2.3-SNAPSHOT MapFish Print Version 2 has reached end-of-life and is no longer under active development.