示例#1
0
  /** Returns a JarDiff for the given request */
  public synchronized DownloadResponse getJarDiffEntry(
      ResourceCatalog catalog, DownloadRequest dreq, JnlpResource res) {
    if (dreq.getCurrentVersionId() == null) return null;

    // check whether the request is from javaws 1.0/1.0.1
    // do not generate minimal jardiff if it is from 1.0/1.0.1
    boolean doJarDiffWorkAround = isJavawsVersion(dreq, "1.0*");

    // First do a lookup to find a match
    JarDiffKey key =
        new JarDiffKey(
            res.getName(),
            dreq.getCurrentVersionId(),
            res.getReturnVersionId(),
            !doJarDiffWorkAround);

    JarDiffEntry entry = (JarDiffEntry) _jarDiffEntries.get(key);
    // If entry is not found, then the querty has not been made.
    if (entry == null) {
      if (_log.isInformationalLevel()) {
        _log.addInformational(
            "servlet.log.info.jardiff.gen",
            res.getName(),
            dreq.getCurrentVersionId(),
            res.getReturnVersionId());
      }
      File f = generateJarDiff(catalog, dreq, res, doJarDiffWorkAround);
      if (f == null) {
        _log.addWarning(
            "servlet.log.warning.jardiff.failed",
            res.getName(),
            dreq.getCurrentVersionId(),
            res.getReturnVersionId());
      }
      // Store entry in table
      entry = new JarDiffEntry(f);
      _jarDiffEntries.put(key, entry);
    }

    // Check for no JarDiff to return
    if (entry.getJarDiffFile() == null) {
      return null;
    } else {
      return DownloadResponse.getFileDownloadResponse(
          entry.getJarDiffFile(),
          _jarDiffMimeType,
          entry.getJarDiffFile().lastModified(),
          res.getReturnVersionId());
    }
  }
示例#2
0
  public static boolean isJavawsVersion(DownloadRequest dreq, String version) {
    String javawsAgent = "javaws";
    String jwsVer = dreq.getHttpRequest().getHeader("User-Agent");

    // check the request is coming from javaws
    if (!jwsVer.startsWith("javaws-")) {
      // this is the new style User-Agent string
      // User-Agent: JNLP/1.0.1 javaws/1.4.2 (b28) J2SE/1.4.2
      StringTokenizer st = new StringTokenizer(jwsVer);
      while (st.hasMoreTokens()) {
        String verString = st.nextToken();
        int index = verString.indexOf(javawsAgent);
        if (index != -1) {
          verString = verString.substring(index + javawsAgent.length() + 1);
          return VersionString.contains(version, verString);
        }
      }
      return false;
    }

    // extract the version id from the download request
    int startIndex = jwsVer.indexOf("-");

    if (startIndex == -1) {
      return false;
    }

    int endIndex = jwsVer.indexOf("/");

    if (endIndex == -1 || endIndex < startIndex) {
      return false;
    }

    String verId = jwsVer.substring(startIndex + 1, endIndex);

    // check whether the versionString contains the versionId
    return VersionString.contains(version, verId);
  }
示例#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;
    }
  }