コード例 #1
0
    public TempFile(File tempFile, PJsonObject jsonSpec, OutputFormat format) {
      super(tempFile.getAbsolutePath());
      creationTime = System.currentTimeMillis();
      this.outputFileName = jsonSpec.optString(Constants.OUTPUT_FILENAME_KEY);
      this.printedLayoutName = jsonSpec.optString(Constants.JSON_LAYOUT_KEY, null);

      this.suffix = format.getFileSuffix();
      this.contentType = format.getContentType();
    }
コード例 #2
0
  /**
   * 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();
      }
    }
  }