sascha@2502: package de.intevation.flys.client.server; sascha@2502: sascha@2502: import java.io.File; sascha@2502: import java.io.IOException; sascha@2502: import java.io.InputStream; sascha@2502: import java.io.FileOutputStream; sascha@2502: import java.io.FileInputStream; sascha@2502: import java.io.BufferedInputStream; sascha@2502: import java.io.BufferedOutputStream; sascha@2502: import java.io.OutputStream; sascha@2502: sascha@2502: import javax.servlet.ServletException; sascha@2502: sascha@2502: import javax.servlet.http.HttpServlet; sascha@2502: import javax.servlet.http.HttpServletRequest; sascha@2502: import javax.servlet.http.HttpServletResponse; sascha@2502: sascha@2502: import org.apache.log4j.Logger; sascha@2502: sascha@2502: import org.mapfish.print.MapPrinter; sascha@2502: sascha@2502: import org.mapfish.print.output.OutputFactory; sascha@2502: import org.mapfish.print.output.OutputFormat; sascha@2502: sascha@2502: import org.mapfish.print.utils.PJsonObject; sascha@2502: sascha@2502: public class MapPrintServiceImpl sascha@2502: extends HttpServlet sascha@2502: { sascha@2502: private static final Logger log = sascha@2502: Logger.getLogger(MapPrintServiceImpl.class); sascha@2502: sascha@2502: private static final String DEMO_JSON = sascha@2502: "{" + sascha@2502: "\"layout\": \"A4 portrait\"," + sascha@2502: "\"title\": \"A simple example\"," + sascha@2502: "\"srs\": \"EPSG:4326\"," + sascha@2502: "\"dpi\": 254," + sascha@2502: "\"units\": \"degrees\"," + sascha@2502: "\"outputFormat\": \"pdf\"," + sascha@2502: "\"layers\": [{" + sascha@2502: " \"type\": \"WMS\"," + sascha@2502: " \"layers\": [\"basic\"]," + sascha@2502: " \"baseURL\": \"http://labs.metacarta.com/wms/vmap0\"," + sascha@2502: " \"format\": \"image/jpeg\"" + sascha@2502: "}," + sascha@2502: "{" + sascha@2502: " \"type\": \"WMS\"," + sascha@2502: " \"layers\": [\"routes\"]," + sascha@2502: " \"baseURL\": \"http://www.camptocamp.org/cgi-bin/mapserv_c2corg\"," + sascha@2502: " \"format\": \"image/png\"" + sascha@2502: "}, {" + sascha@2502: "\"opacity\": 0.5," + sascha@2502: "\"customParams\": {}," + sascha@2502: "\"type\": \"Vector\"," + sascha@2502: "\"styles\": {" + sascha@2502: " \"1\": {" + sascha@2502: " \"fillColor\": \"red\"," + sascha@2502: " \"strokeColor\": \"red\"," + sascha@2502: " \"fillOpacity\": 0.4000000000000001," + sascha@2502: " \"hoverFillColor\": \"white\"," + sascha@2502: " \"hoverFillOpacity\": 0.8000000000000002," + sascha@2502: " \"strokeOpacity\": 1," + sascha@2502: " \"strokeWidth\": 0.5," + sascha@2502: " \"strokeLinecap\": \"round\"," + sascha@2502: " \"strokeDashstyle\": \"solid\"," + sascha@2502: " \"hoverStrokeColor\": \"red\"," + sascha@2502: " \"hoverStrokeOpacity\": 1.0," + sascha@2502: " \"hoverStrokeWidth\": 0.2," + sascha@2502: " \"pointRadius\": 6," + sascha@2502: " \"hoverPointRadius\": 1," + sascha@2502: " \"hoverPointUnit\": \"%\"," + sascha@2502: " \"pointerEvents\": \"visiblePainted\"," + sascha@2502: " \"cursor\": \"inherit\"" + sascha@2502: " }" + sascha@2502: "}," + sascha@2502: "\"styleProperty\": \"_style\"," + sascha@2502: "\"geoJson\": {" + sascha@2502: " \"type\": \"FeatureCollection\"," + sascha@2502: " \"features\": [{" + sascha@2502: " \"type\": \"Feature\"," + sascha@2502: " \"id\": \"OpenLayers.Feature.Vector_243\"," + sascha@2502: " \"properties\": {" + sascha@2502: " \"_style\": 1," + sascha@2502: " \"name\": \"\"," + sascha@2502: " \"description\": \"\"," + sascha@2502: " \"ext-comp-1048\": \"default\"" + sascha@2502: " }," + sascha@2502: " \"geometry\": {" + sascha@2502: " \"type\": \"Polygon\"," + sascha@2502: " \"coordinates\": [[[5, 45], [5, 47], [7, 47], [7, 45], [5, 45]]]" + sascha@2502: " }" + sascha@2502: " }]" + sascha@2502: "}," + sascha@2502: "\"name\": \"Cosmetic\"" + sascha@2502: " }" + sascha@2502: "]," + sascha@2502: "\"pages\": [" + sascha@2502: " {" + sascha@2502: " \"center\": [6, 45.5]," + sascha@2502: " \"scale\": 4000000," + sascha@2502: " \"mapTitle\": \"First map\"," + sascha@2502: " \"comment\": \"This is the first page selected by the user.\"," + sascha@2502: " \"rotation\": 0," + sascha@2502: " \"data\": {" + sascha@2502: " \"data\": [" + sascha@2502: " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," + sascha@2502: " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" + sascha@2502: " ]," + sascha@2502: " \"columns\": [\"id\", \"name\", \"icon\"]" + sascha@2502: " }" + sascha@2502: " },{" + sascha@2502: " \"center\": [6, 45.5]," + sascha@2502: " \"scale\": 4000000," + sascha@2502: " \"mapTitle\": \"First map\"," + sascha@2502: " \"comment\": \"This is the first page selected by the user.\"," + sascha@2502: " \"rotation\": 0," + sascha@2502: " \"data\": {" + sascha@2502: " \"data\": [" + sascha@2502: " {\"id\":1, \"name\": \"blah\", \"icon\": \"icon_pan\", \"nameBackgroundColor\": \"red\", \"nameBorderColor\": \"blue\"}," + sascha@2502: " {\"id\":2, \"name\": \"blip\", \"icon\": \"icon_zoomin\", \"nameBackgroundColor\": \"yellow\", \"nameBorderColor\": \"green\"}" + sascha@2502: " ]," + sascha@2502: " \"columns\": [\"id\", \"name\", \"icon\"]" + sascha@2502: " }" + sascha@2502: " }" + sascha@2502: "]" + sascha@2502: "}"; sascha@2502: sascha@2502: sascha@2502: private MapPrinter getMapPrinter() throws ServletException, IOException { sascha@2502: String configPath = getInitParameter("config"); sascha@2502: if (configPath == null) { sascha@2502: throw new ServletException("Missing configuration in web.xml"); sascha@2502: } sascha@2502: sascha@2502: File configFile = new File(configPath); sascha@2502: if (!configFile.isAbsolute()) { sascha@2502: configFile = new File(getServletContext().getRealPath(configPath)); sascha@2502: } sascha@2502: sascha@2502: if (!configFile.isFile() || !configFile.canRead()) { sascha@2502: throw new ServletException("Cannot read '" + configFile + "'"); sascha@2502: } sascha@2502: sascha@2502: return new MapPrinter(configFile); sascha@2502: } sascha@2502: sascha@2502: @Override sascha@2502: public void doGet(HttpServletRequest req, HttpServletResponse resp) sascha@2502: throws ServletException, IOException sascha@2502: { sascha@2502: log.info("MapPrintServiceImpl.doGet"); sascha@2502: sascha@2502: PJsonObject jsonSpec = MapPrinter.parseSpec(DEMO_JSON); sascha@2502: sascha@2502: MapPrinter printer = getMapPrinter(); sascha@2502: sascha@2502: OutputFormat outputFormat = OutputFactory.create( sascha@2502: printer.getConfig(), jsonSpec); sascha@2502: sascha@2502: resp.setHeader("Content-Disposition", "attachment;filename=print.pdf"); sascha@2502: resp.setHeader("Content-Type", "application/pdf"); sascha@2502: sascha@2502: // XXX: Streaming the generated PDF directly sascha@2502: // to the request out does not work. :-/ sascha@2502: File tmpFile = File.createTempFile("map-printing", null); sascha@2502: sascha@2502: try { sascha@2502: OutputStream out = sascha@2502: new BufferedOutputStream( sascha@2502: new FileOutputStream(tmpFile)); sascha@2502: try { sascha@2502: outputFormat.print(printer, jsonSpec, out, ""); sascha@2502: out.flush(); sascha@2502: } sascha@2502: catch (Exception e) { sascha@2502: log.error(e); sascha@2502: throw new ServletException(e); sascha@2502: } sascha@2502: finally { sascha@2502: printer.stop(); sascha@2502: out.close(); sascha@2502: } sascha@2502: InputStream in = sascha@2502: new BufferedInputStream( sascha@2502: new FileInputStream(tmpFile)); sascha@2502: out = resp.getOutputStream(); sascha@2502: try { sascha@2502: byte [] buf = new byte[4096]; sascha@2502: int r; sascha@2502: while ((r = in.read(buf)) >= 0) { sascha@2502: out.write(buf, 0, r); sascha@2502: } sascha@2502: out.flush(); sascha@2502: } sascha@2502: finally { sascha@2502: in.close(); sascha@2502: out.close(); sascha@2502: } sascha@2502: } sascha@2502: finally { sascha@2502: if (tmpFile.exists()) { sascha@2502: tmpFile.delete(); sascha@2502: } sascha@2502: } sascha@2502: } sascha@2502: } sascha@2502: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :