public String getOutputFileName(MapPrinter mapPrinter) { if (outputFileName != null) { return formatFileName(suffix, outputFileName, new Date()); } else { return formatFileName( suffix, mapPrinter.getOutputFilename(printedLayoutName, getName()), new Date()); } }
/** To get (in JSON) the information about the available formats and CO. */ protected void getInfo(HttpServletRequest req, HttpServletResponse resp, String basePath) throws ServletException, IOException { app = req.getParameter("app"); // System.out.println("app = "+app); MapPrinter printer = getMapPrinter(app); resp.setContentType("application/json; charset=utf-8"); final PrintWriter writer = resp.getWriter(); try { final String var = req.getParameter("var"); if (var != null) { writer.print(var + "="); } JSONWriter json = new JSONWriter(writer); try { json.object(); { printer.printClientConfig(json); String urlToUseInSpec = basePath; String proxyUrl = printer.getConfig().getProxyBaseUrl(); if (proxyUrl != null) { urlToUseInSpec = proxyUrl; } json.key("printURL").value(urlToUseInSpec + PRINT_URL); json.key("createURL").value(urlToUseInSpec + CREATE_URL); if (app != null) { json.key("app").value(app); } } json.endObject(); } catch (JSONException e) { throw new ServletException(e); } if (var != null) { writer.print(";"); } } finally { writer.close(); if (app == null && printer != null) { printer.stop(); } } }
/** copy the PDF into the output stream */ protected void sendPdfFile( HttpServletResponse httpServletResponse, TempFile tempFile, boolean inline) throws IOException, ServletException { FileInputStream pdf = new FileInputStream(tempFile); final OutputStream response = httpServletResponse.getOutputStream(); MapPrinter mapPrinter = getMapPrinter(app); try { httpServletResponse.setContentType(tempFile.contentType()); if (!inline) { final String fileName = tempFile.getOutputFileName(mapPrinter); httpServletResponse.setHeader("Content-disposition", "attachment; filename=" + fileName); } FileUtilities.copyStream(pdf, response); } finally { try { pdf.close(); } finally { response.close(); } if (app == null && mapPrinter != null) { mapPrinter.stop(); } } }
/** * Do the actual work of creating the PDF temporary file. * * @throws InterruptedException */ protected TempFile doCreatePDFFile(String spec, HttpServletRequest httpServletRequest) throws IOException, DocumentException, ServletException, InterruptedException { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Generating PDF for spec=" + spec); } if (SPEC_LOGGER.isInfoEnabled()) { SPEC_LOGGER.info(spec); } PJsonObject specJson = MapPrinter.parseSpec(spec); if (specJson.has("app")) { app = specJson.getString("app"); } else { app = null; } MapPrinter mapPrinter = getMapPrinter(app); Map<String, String> headers = new HashMap<String, String>(); TreeSet<String> configHeaders = mapPrinter.getConfig().getHeaders(); if (configHeaders == null) { configHeaders = new TreeSet<String>(); configHeaders.add("Referer"); configHeaders.add("Cookie"); } for (Iterator<String> header_iter = configHeaders.iterator(); header_iter.hasNext(); ) { String header = header_iter.next(); if (httpServletRequest.getHeader(header) != null) { headers.put(header, httpServletRequest.getHeader(header)); } } final OutputFormat outputFormat = mapPrinter.getOutputFormat(specJson); // 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); FileOutputStream out = null; try { out = new FileOutputStream(tempFile); mapPrinter.print(specJson, out, headers); return tempFile; } catch (IOException e) { deleteFile(tempFile); throw e; } catch (DocumentException e) { deleteFile(tempFile); throw e; } catch (InterruptedException e) { deleteFile(tempFile); throw e; } finally { if (out != null) { out.close(); } } }