Пример #1
0
  // fix for 4720897
  // if the jar file resides in a war file, download it to a temp dir
  // so it can be used to generate jardiff
  private String getRealPath(String path) throws IOException {

    URL fileURL = _servletContext.getResource(path);

    File tempDir = (File) _servletContext.getAttribute("javax.servlet.context.tempdir");

    // download file into temp dir
    if (fileURL != null) {
      File newFile = File.createTempFile("temp", ".jar", tempDir);
      if (download(fileURL, newFile)) {
        String filePath = newFile.getPath();
        return filePath;
      }
    }
    return null;
  }
Пример #2
0
  /**
   * This method handles PUT requests from the client. PUT requests will come from the applet
   * portion of this application and are the way that images and other files can be posted to the
   * server.
   *
   * @param request the HTTP request object
   * @param response the HTTP response object
   * @exception ServletException
   * @exception IOException
   */
  public void doPut(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    HttpSession session = request.getSession();
    /*
     * The Scan applet will zip all the files together to create a
     * faster upload and to use just one server connection.
     */
    ZipInputStream in = new ZipInputStream(request.getInputStream());

    /*
     * This will write all the files to a directory on the server.
     */
    try {
      try {

        File file = new File("scan");
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(file));
        document.open();
        String fileSize = null;
        while (true) {
          ZipEntry entry = in.getNextEntry();
          if (entry == null) {
            break;
          }
          File f = File.createTempFile("translate", entry.getName());
          FileOutputStream out = new FileOutputStream(f);
          FileInputStream inStream = null;
          try {
            int read;
            byte[] buf = new byte[2024];

            while ((read = in.read(buf)) > 0) {
              out.write(buf, 0, read);
            }
            out.close();
            inStream = new FileInputStream(f);
            System.out.println(entry.getSize());
            byte[] b = new byte[inStream.available()];
            inStream.read(b);
            System.out.println(b.length);
            com.itextpdf.text.Image image1 = com.itextpdf.text.Image.getInstance(b);
            image1.scalePercent(30);
            image1.setCompressionLevel(9);
            document.add(image1);
          } finally {
          }
        }
        document.close();
        //                fileSize = CommonUtils.getFileSize(file);
        //                DocumentStoreLogDAO documentStoreLogDAO = new DocumentStoreLogDAO();
        //                DocumentStoreLog documentStoreLog = null;
        //                DocumentStoreLog documentStore =
        // (DocumentStoreLog)session.getAttribute(CommonConstants.DOCUMENT_STORE_LOG);
        //                if(null != documentStore) {
        //                    documentStore.setFileSize(fileSize);
        //                }
        //                Transaction tx = documentStoreLogDAO.getSession().getTransaction();
        //                tx.begin();
        //                documentStoreLogDAO.save(documentStore);
        //                tx.commit();
      } catch (ZipException ze) {
        /*
         * We want to catch each sip exception separately because
         * there is a possibility that we can read more files from
         * the archive even if one of them is corrupted.
         */
        ze.printStackTrace();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      in.close();
    }

    /*
     * Now that we have finished uploading the files
     * we will send a reponse to the server indicating
     * our success.
     */

    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html><head><title>ImageSrv</title></head></html>");
    out.flush();
    out.close();
    response.setStatus(HttpServletResponse.SC_OK);
  }
Пример #3
0
  private File generateJarDiff(
      ResourceCatalog catalog,
      DownloadRequest dreq,
      JnlpResource res,
      boolean doJarDiffWorkAround) {
    boolean del_old = false;
    boolean del_new = false;

    // Lookup up file for request version
    DownloadRequest fromDreq = dreq.getFromDownloadRequest();
    try {
      JnlpResource fromRes = catalog.lookupResource(fromDreq);

      /* Get file locations */
      String newFilePath = _servletContext.getRealPath(res.getPath());
      String oldFilePath = _servletContext.getRealPath(fromRes.getPath());

      // fix for 4720897
      if (newFilePath == null) {
        newFilePath = getRealPath(res.getPath());
        if (newFilePath != null) del_new = true;
      }

      if (oldFilePath == null) {
        oldFilePath = getRealPath(fromRes.getPath());
        if (oldFilePath != null) del_old = true;
      }

      if (newFilePath == null || oldFilePath == null) {
        return null;
      }

      // Create temp. file to store JarDiff file in
      File tempDir = (File) _servletContext.getAttribute("javax.servlet.context.tempdir");

      // fix for 4653036: JarDiffHandler() should use javax.servlet.context.tempdir to store the
      // jardiff
      File outputFile = File.createTempFile("jnlp", ".jardiff", tempDir);

      _log.addDebug(
          "Generating Jardiff between "
              + oldFilePath
              + " and "
              + newFilePath
              + " Store in "
              + outputFile);

      // Generate JarDiff
      OutputStream os = new FileOutputStream(outputFile);

      JarDiff.createPatch(oldFilePath, newFilePath, os, !doJarDiffWorkAround);
      os.close();

      try {

        // Check that Jardiff is smaller, or return null
        if (outputFile.length() >= (new File(newFilePath).length())) {
          _log.addDebug("JarDiff discarded - since it is bigger");
          return null;
        }

        // Check that Jardiff is smaller than the packed version of
        // the new file, if the file exists at all
        File newFilePacked = new File(newFilePath + ".pack.gz");
        if (newFilePacked.exists()) {
          _log.addDebug("generated jardiff size: " + outputFile.length());
          _log.addDebug("packed requesting file size: " + newFilePacked.length());
          if (outputFile.length() >= newFilePacked.length()) {
            _log.addDebug("JarDiff discarded - packed version of requesting file is smaller");
            return null;
          }
        }

        _log.addDebug("JarDiff generation succeeded");
        return outputFile;

      } finally {
        // delete the temporarily downloaded file
        if (del_new) {
          new File(newFilePath).delete();
        }

        if (del_old) {
          new File(oldFilePath).delete();
        }
      }
    } catch (IOException ioe) {
      _log.addDebug("Failed to genereate jardiff", ioe);
      return null;
    } catch (ErrorResponseException ere) {
      _log.addDebug("Failed to genereate jardiff", ere);
      return null;
    }
  }