Example #1
0
  /** Download resource to the given file */
  private boolean download(URL target, File file) {

    _log.addDebug("JarDiffHandler:  Doing download");

    boolean ret = true;
    boolean delete = false;
    // use bufferedstream for better performance
    BufferedInputStream in = null;
    BufferedOutputStream out = null;
    try {
      in = new BufferedInputStream(target.openStream());
      out = new BufferedOutputStream(new FileOutputStream(file));
      int read = 0;
      int totalRead = 0;
      byte[] buf = new byte[BUF_SIZE];
      while ((read = in.read(buf)) != -1) {
        out.write(buf, 0, read);
        totalRead += read;
      }

      _log.addDebug("total read: " + totalRead);
      _log.addDebug("Wrote URL " + target.toString() + " to file " + file);

    } catch (IOException ioe) {

      _log.addDebug("Got exception while downloading resource: " + ioe);

      ret = false;

      if (file != null) delete = true;

    } finally {

      try {
        in.close();
        in = null;
      } catch (IOException ioe) {
        _log.addDebug("Got exception while downloading resource: " + ioe);
      }

      try {
        out.close();
        out = null;
      } catch (IOException ioe) {
        _log.addDebug("Got exception while downloading resource: " + ioe);
      }

      if (delete) {
        file.delete();
      }
    }
    return ret;
  }
Example #2
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;
    }
  }