Skip to content

Commit

Permalink
allow formatting of date in outputFilename
Browse files Browse the repository at this point in the history
  • Loading branch information
Jesse Eichar committed Dec 13, 2011
1 parent 6e3d4c3 commit d788b84
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 26 deletions.
8 changes: 7 additions & 1 deletion docs/configuration.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,13 @@ New versions of tilecache added the support for merging multiple layers in a sin

"connectionTimeout" and "socketTimeout" (only since MapFish v1.2) can be used to tune the timeouts for reading tiles from map servers.

If the 'outputFilename' parameter is defined in the main body then that name will be used by the MapPrintServlet when sending the pdf to the client. It will be the name of the file that the client downloads. If the 'outputFilename' parameter is defined in a layout then that value will override the default name. In both cases the .pdf is optional; if not present the server will append .pdf to the name. In all cases the json request can override the filename defined in the configuration file by posting a 'outputFilename' attribute in the posted JSON. If the outputFilename has ${date}, ${time} or ${dateTime} in it, it will be replaced with the current date using the related DateFormat.get*Instance().format() method.
If the 'outputFilename' parameter is defined in the main body then that name will be used by the MapPrintServlet when sending the pdf to the client. It will be the name of the file that the client downloads. If the 'outputFilename' parameter is defined in a layout then that value will override the default name. In both cases the .pdf is optional; if not present the server will append .pdf to the name. In all cases the json request can override the filename defined in the configuration file by posting a 'outputFilename' attribute in the posted JSON. If the outputFilename has ${date}, ${time} or ${dateTime} in it, it will be replaced with the current date using the related DateFormat.get*Instance().format() method. If a pattern is provided it will be passed to SimpleDataFormat for processing. A few examples follow:

* outputFilename: "host-${yyyyMMdd}.pdf" # results in host-20111213.pdf
* outputFilename: "host-${date}" # results in host-Dec_13_2011.pdf (actual output depends on local of server)
* outputFilename: "host-${dateTime}" # results in host-Dec_13_2011_1:10:50_PM.pdf (actual output depends on local of server)
* outputFilename: "host-${time}.pdf" # results in host-1:11:14_PM.pdf (actual output depends on local of server)
* outputFilename: "host-${yyMMdd-hhmmss}"# results in host-111213-011154.pdf (actual output depends on local of server)

Security
--------
Expand Down
3 changes: 2 additions & 1 deletion docs/protocol.txt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ The print module will use the nearest scale and will make sure the aspect ratio

The geodetic parameter can be set to true so the scale of geodetic layers can correctly be calculated. Certain projections (Google and Latlong for example) are based on a spheroid and therefore require **geodetic: true** in order to correctly calculate the scale. If the geodetic parameter is not present it will be assumed to be false.

The outputFilename parameter is optional and if omitted the values used in the server's configuration will be used instead. If it is present it will be the name of the downloaded file. The suffix will be added if not left off in the parameter.
The outputFilename parameter is optional and if omitted the values used in the server's configuration will be used instead. If it is present it will be the name of the downloaded file. The suffix will be added if not left off in the parameter. The date can be substituted into the filename as well if desired. See configuration's outputFilename for more information and examples

The outputFormat parameter is optional and if omitted the value 'pdf' will be used. Only the formats returned in the info are permitted.

Expand All @@ -124,6 +124,7 @@ For the format of the **layers** section, please look at the implementations poi

This command returns the PDF file directly.


create.json
-----------

Expand Down
54 changes: 43 additions & 11 deletions src/main/java/org/mapfish/print/servlet/MapPrinterServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Main print servlet.
Expand Down Expand Up @@ -467,26 +470,55 @@ public TempFile(File tempFile, PJsonObject jsonSpec, OutputFormat format) {

public String getOutputFileName(MapPrinter mapPrinter) {
if(outputFileName != null) {
return addSuffix(outputFileName);
return formatFileName(suffix, outputFileName, new Date());
} else {
return addSuffix(mapPrinter.getOutputFilename(printedLayoutName, getName()));
return formatFileName(suffix, mapPrinter.getOutputFilename(printedLayoutName, getName()), new Date());
}
}

private String addSuffix(String startingName) {
startingName = formatFileName(startingName, new Date());

if(!startingName.toLowerCase().endsWith("."+suffix.toLowerCase())) {
return startingName+"."+suffix;
public static String formatFileName(String suffix, String startingName, Date date) {
Matcher matcher = Pattern.compile("\\$\\{(.+?)\\}").matcher(startingName);
HashMap<String,String> replacements = new HashMap<String,String>();
while(matcher.find()) {
String pattern = matcher.group(1);
String key = "${"+pattern+"}";
replacements.put(key, findReplacement(pattern, date));
}
String result = startingName;
for(Map.Entry<String,String> entry: replacements.entrySet()) {
result = result.replace(entry.getKey(), entry.getValue());
}

while(suffix.startsWith(".")) {
suffix = suffix.substring(1);
}
if(suffix.isEmpty() || result.toLowerCase().endsWith("."+suffix.toLowerCase())) {
return result;
} else {
return startingName;
return result+"."+suffix;
}
}

public static String formatFileName(String startingName, Date date) {
return startingName.replace("${date}", DateFormat.getDateInstance().format(date)).
replace("${dateTime}", DateFormat.getDateTimeInstance().format(date)).
replace("${time}", DateFormat.getTimeInstance().format(date));
public static String cleanUpName(String original) {
return original.replace(",","").replaceAll("\\s+", "_");
}

private static String findReplacement(String pattern, Date date) {
if (pattern.toLowerCase().equals("date")) {
return cleanUpName(DateFormat.getDateInstance().format(date));
} else if (pattern.toLowerCase().equals("datetime")) {
return cleanUpName(DateFormat.getDateTimeInstance().format(date));
} else if (pattern.toLowerCase().equals("time")) {
return cleanUpName(DateFormat.getTimeInstance().format(date));
} else {
try {
return new SimpleDateFormat(pattern).format(date);
} catch (Exception e) {
LOGGER.error("Unable to format timestamp according to pattern: "+pattern, e);
return "${"+pattern+"}";
}
}
}

public String contentType() {
Expand Down
45 changes: 32 additions & 13 deletions src/test/java/org/mapfish/print/servlet/MapPrinterServletTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package org.mapfish.print.servlet;

import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.mapfish.print.servlet.MapPrinterServlet.TempFile.cleanUpName;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.junit.Test;
Expand All @@ -13,21 +15,38 @@ public class MapPrinterServletTest {
public void TempFileFormatFileNameTest() {
Date date = new Date();

String dateString = DateFormat.getDateInstance().format(date);
String dateTimeString = DateFormat.getDateTimeInstance().format(date);
String timeString = DateFormat.getTimeInstance().format(date);
String dateString = cleanUpName(DateFormat.getDateInstance().format(date));
String dateTimeString = cleanUpName(DateFormat.getDateTimeInstance().format(date));
String timeString = cleanUpName(DateFormat.getTimeInstance().format(date));
String customPattern = "yy-MM-dd";
String customPattern2 = "yy:MM:dd:mm:ss";
String custom = new SimpleDateFormat(customPattern).format(date);
String custom2 = new SimpleDateFormat(customPattern2).format(date);

assertExpectedFormat(date, "|${else}|", "|${else}|");
assertExpectedFormat(date, "|"+dateString+"|", "|${date}|");
assertExpectedFormat(date, "|"+dateTimeString+"|", "|${dateTime}|");
assertExpectedFormat(date, "|"+timeString+"|", "|${time}|");
assertExpectedFormat(date, "|"+timeString+"|"+dateString, "|${time}|${date}");
assertExpectedFormat(date, "|"+timeString+"|"+dateTimeString, "|${time}|${dateTime}");
assertExpectedFormat(date, "|"+timeString+"|"+dateTimeString+"|"+timeString, "|${time}|${dateTime}|${time}");
assertExpectedFormat(date, "|${else}|", "|${else}|", "");
assertExpectedFormat(date, "|"+dateString+"|", "|${date}|", "");
assertExpectedFormat(date, "|"+dateTimeString+"|", "|${dateTime}|", "");
assertExpectedFormat(date, "|"+timeString+"|", "|${time}|", "");
assertExpectedFormat(date, "|"+custom+"|", "|${"+customPattern+"}|", "");
assertExpectedFormat(date, "|"+custom2+"|", "|${"+customPattern2+"}|", "");
assertExpectedFormat(date, "|"+timeString+"|"+dateString, "|${time}|${date}", "");
assertExpectedFormat(date, "|"+timeString+"|"+dateTimeString, "|${time}|${dateTime}", "");
assertExpectedFormat(date, "|"+timeString+"|"+dateTimeString+"|"+timeString, "|${time}|${dateTime}|${time}", "");
assertExpectedFormat(date, "|"+custom+"|"+custom2+"|"+timeString, "|${"+customPattern+"}|${"+customPattern2+"}|${time}", "");
}

private void assertExpectedFormat(Date date, String expected, String fileName) {
assertEquals(expected, MapPrinterServlet.TempFile.formatFileName(fileName,date));
@Test
public void addSuffixTest() {
Date date = new Date();

assertExpectedFormat(date, "filename.pdf", "filename", "pdf");
assertExpectedFormat(date, "filename.pdf", "filename", ".pdf");
assertExpectedFormat(date, "filename.pdf", "filename.pdf", ".pdf");
assertExpectedFormat(date, "filename.tif.pdf", "filename.tif", ".pdf");
}

private void assertExpectedFormat(Date date, String expected, String fileName, String suffix) {
assertEquals(expected, MapPrinterServlet.TempFile.formatFileName(suffix, fileName, date));
}

}

0 comments on commit d788b84

Please sign in to comment.